blob: fe3f47f955615e6d3f85f80ec730ff698d11f788 [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;
1295 }
1296
1297 try {
Romain Guybb93d552009-03-24 21:04:15 -07001298 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy13922e02009-05-12 17:56:14 -07001299 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300
1301 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
1302 Log.v("ViewRoot", "Surface " + surface + " drawing to bitmap w="
1303 + canvas.getWidth() + ", h=" + canvas.getHeight());
1304 //canvas.drawARGB(255, 255, 0, 0);
1305 }
1306
Romain Guy13922e02009-05-12 17:56:14 -07001307 if (Config.DEBUG && ViewDebug.profileDrawing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 startTime = SystemClock.elapsedRealtime();
1309 }
1310
1311 // If this bitmap's format includes an alpha channel, we
1312 // need to clear it before drawing so that the child will
1313 // properly re-composite its drawing on a transparent
1314 // background. This automatically respects the clip/dirty region
Romain Guy5bcdff42009-05-14 21:27:18 -07001315 // or
1316 // If we are applying an offset, we need to clear the area
1317 // where the offset doesn't appear to avoid having garbage
1318 // left in the blank areas.
1319 if (!canvas.isOpaque() || yoff != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1321 }
1322
1323 dirty.setEmpty();
Romain Guybb93d552009-03-24 21:04:15 -07001324 mIsAnimating = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001326 mView.mPrivateFlags |= View.DRAWN;
1327
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001328 if (DEBUG_DRAW) {
Romain Guy5bcdff42009-05-14 21:27:18 -07001329 Context cxt = mView.getContext();
1330 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -07001331 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1332 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001333 }
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001334 int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001335 try {
1336 canvas.translate(0, -yoff);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001337 if (mTranslator != null) {
1338 mTranslator.translateCanvas(canvas);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001339 }
Dianne Hackborn0d221012009-07-29 15:41:19 -07001340 canvas.setScreenDensity(scalingRequired
1341 ? DisplayMetrics.DENSITY_DEVICE : 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001342 mView.draw(canvas);
1343 } finally {
Romain Guy5bcdff42009-05-14 21:27:18 -07001344 mAttachInfo.mIgnoreDirtyState = false;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001345 canvas.restoreToCount(saveCount);
1346 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347
Romain Guy5bcdff42009-05-14 21:27:18 -07001348 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1349 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1350 }
1351
Romain Guy13922e02009-05-12 17:56:14 -07001352 if (Config.DEBUG && ViewDebug.showFps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 int now = (int)SystemClock.elapsedRealtime();
1354 if (sDrawTime != 0) {
1355 nativeShowFPS(canvas, now - sDrawTime);
1356 }
1357 sDrawTime = now;
1358 }
1359
Romain Guy13922e02009-05-12 17:56:14 -07001360 if (Config.DEBUG && ViewDebug.profileDrawing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1362 }
1363 }
Romain Guy8506ab42009-06-11 17:35:47 -07001364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 } finally {
1366 surface.unlockCanvasAndPost(canvas);
1367 }
1368
1369 if (LOCAL_LOGV) {
1370 Log.v("ViewRoot", "Surface " + surface + " unlockCanvasAndPost");
1371 }
Romain Guy8506ab42009-06-11 17:35:47 -07001372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 if (scrolling) {
1374 mFullRedrawNeeded = true;
1375 scheduleTraversals();
1376 }
1377 }
1378
1379 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1380 final View.AttachInfo attachInfo = mAttachInfo;
1381 final Rect ci = attachInfo.mContentInsets;
1382 final Rect vi = attachInfo.mVisibleInsets;
1383 int scrollY = 0;
1384 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 if (vi.left > ci.left || vi.top > ci.top
1387 || vi.right > ci.right || vi.bottom > ci.bottom) {
1388 // We'll assume that we aren't going to change the scroll
1389 // offset, since we want to avoid that unless it is actually
1390 // going to make the focus visible... otherwise we scroll
1391 // all over the place.
1392 scrollY = mScrollY;
1393 // We can be called for two different situations: during a draw,
1394 // to update the scroll position if the focus has changed (in which
1395 // case 'rectangle' is null), or in response to a
1396 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1397 // is non-null and we just want to scroll to whatever that
1398 // rectangle is).
1399 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001400
1401 // When in touch mode, focus points to the previously focused view,
1402 // which may have been removed from the view hierarchy. The following
1403 // line checks whether the view is still in the hierarchy
1404 if (focus == null || focus.getParent() == null) {
1405 mRealFocusedView = null;
1406 return false;
1407 }
1408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 if (focus != mLastScrolledFocus) {
1410 // If the focus has changed, then ignore any requests to scroll
1411 // to a rectangle; first we want to make sure the entire focus
1412 // view is visible.
1413 rectangle = null;
1414 }
1415 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1416 + " rectangle=" + rectangle + " ci=" + ci
1417 + " vi=" + vi);
1418 if (focus == mLastScrolledFocus && !mScrollMayChange
1419 && rectangle == null) {
1420 // Optimization: if the focus hasn't changed since last
1421 // time, and no layout has happened, then just leave things
1422 // as they are.
1423 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1424 + mScrollY + " vi=" + vi.toShortString());
1425 } else if (focus != null) {
1426 // We need to determine if the currently focused view is
1427 // within the visible part of the window and, if not, apply
1428 // a pan so it can be seen.
1429 mLastScrolledFocus = focus;
1430 mScrollMayChange = false;
1431 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1432 // Try to find the rectangle from the focus view.
1433 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1434 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1435 + mView.getWidth() + " h=" + mView.getHeight()
1436 + " ci=" + ci.toShortString()
1437 + " vi=" + vi.toShortString());
1438 if (rectangle == null) {
1439 focus.getFocusedRect(mTempRect);
1440 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1441 + ": focusRect=" + mTempRect.toShortString());
1442 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1443 focus, mTempRect);
1444 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1445 "Focus in window: focusRect="
1446 + mTempRect.toShortString()
1447 + " visRect=" + mVisRect.toShortString());
1448 } else {
1449 mTempRect.set(rectangle);
1450 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1451 "Request scroll to rect: "
1452 + mTempRect.toShortString()
1453 + " visRect=" + mVisRect.toShortString());
1454 }
1455 if (mTempRect.intersect(mVisRect)) {
1456 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1457 "Focus window visible rect: "
1458 + mTempRect.toShortString());
1459 if (mTempRect.height() >
1460 (mView.getHeight()-vi.top-vi.bottom)) {
1461 // If the focus simply is not going to fit, then
1462 // best is probably just to leave things as-is.
1463 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1464 "Too tall; leaving scrollY=" + scrollY);
1465 } else if ((mTempRect.top-scrollY) < vi.top) {
1466 scrollY -= vi.top - (mTempRect.top-scrollY);
1467 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1468 "Top covered; scrollY=" + scrollY);
1469 } else if ((mTempRect.bottom-scrollY)
1470 > (mView.getHeight()-vi.bottom)) {
1471 scrollY += (mTempRect.bottom-scrollY)
1472 - (mView.getHeight()-vi.bottom);
1473 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1474 "Bottom covered; scrollY=" + scrollY);
1475 }
1476 handled = true;
1477 }
1478 }
1479 }
1480 }
Romain Guy8506ab42009-06-11 17:35:47 -07001481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (scrollY != mScrollY) {
1483 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1484 + mScrollY + " , new=" + scrollY);
1485 if (!immediate) {
1486 if (mScroller == null) {
1487 mScroller = new Scroller(mView.getContext());
1488 }
1489 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1490 } else if (mScroller != null) {
1491 mScroller.abortAnimation();
1492 }
1493 mScrollY = scrollY;
1494 }
Romain Guy8506ab42009-06-11 17:35:47 -07001495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 return handled;
1497 }
Romain Guy8506ab42009-06-11 17:35:47 -07001498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 public void requestChildFocus(View child, View focused) {
1500 checkThread();
1501 if (mFocusedView != focused) {
1502 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1503 scheduleTraversals();
1504 }
1505 mFocusedView = mRealFocusedView = focused;
1506 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1507 + mFocusedView);
1508 }
1509
1510 public void clearChildFocus(View child) {
1511 checkThread();
1512
1513 View oldFocus = mFocusedView;
1514
1515 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1516 mFocusedView = mRealFocusedView = null;
1517 if (mView != null && !mView.hasFocus()) {
1518 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1519 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1520 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1521 }
1522 } else if (oldFocus != null) {
1523 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1524 }
1525 }
1526
1527
1528 public void focusableViewAvailable(View v) {
1529 checkThread();
1530
1531 if (mView != null && !mView.hasFocus()) {
1532 v.requestFocus();
1533 } else {
1534 // the one case where will transfer focus away from the current one
1535 // is if the current view is a view group that prefers to give focus
1536 // to its children first AND the view is a descendant of it.
1537 mFocusedView = mView.findFocus();
1538 boolean descendantsHaveDibsOnFocus =
1539 (mFocusedView instanceof ViewGroup) &&
1540 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1541 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1542 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1543 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1544 v.requestFocus();
1545 }
1546 }
1547 }
1548
1549 public void recomputeViewAttributes(View child) {
1550 checkThread();
1551 if (mView == child) {
1552 mAttachInfo.mRecomputeGlobalAttributes = true;
1553 if (!mWillDrawSoon) {
1554 scheduleTraversals();
1555 }
1556 }
1557 }
1558
1559 void dispatchDetachedFromWindow() {
1560 if (Config.LOGV) Log.v("ViewRoot", "Detaching in " + this + " of " + mSurface);
1561
1562 if (mView != null) {
1563 mView.dispatchDetachedFromWindow();
1564 }
1565
1566 mView = null;
1567 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001568 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569
1570 if (mUseGL) {
1571 destroyGL();
1572 }
Mathias Agopian5583dc62009-07-09 16:28:11 -07001573 mSurface.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574
1575 try {
1576 sWindowSession.remove(mWindow);
1577 } catch (RemoteException e) {
1578 }
1579 }
Romain Guy8506ab42009-06-11 17:35:47 -07001580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 /**
1582 * Return true if child is an ancestor of parent, (or equal to the parent).
1583 */
1584 private static boolean isViewDescendantOf(View child, View parent) {
1585 if (child == parent) {
1586 return true;
1587 }
1588
1589 final ViewParent theParent = child.getParent();
1590 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1591 }
1592
1593
1594 public final static int DO_TRAVERSAL = 1000;
1595 public final static int DIE = 1001;
1596 public final static int RESIZED = 1002;
1597 public final static int RESIZED_REPORT = 1003;
1598 public final static int WINDOW_FOCUS_CHANGED = 1004;
1599 public final static int DISPATCH_KEY = 1005;
1600 public final static int DISPATCH_POINTER = 1006;
1601 public final static int DISPATCH_TRACKBALL = 1007;
1602 public final static int DISPATCH_APP_VISIBILITY = 1008;
1603 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1604 public final static int FINISHED_EVENT = 1010;
1605 public final static int DISPATCH_KEY_FROM_IME = 1011;
1606 public final static int FINISH_INPUT_CONNECTION = 1012;
1607 public final static int CHECK_FOCUS = 1013;
1608
1609 @Override
1610 public void handleMessage(Message msg) {
1611 switch (msg.what) {
1612 case View.AttachInfo.INVALIDATE_MSG:
1613 ((View) msg.obj).invalidate();
1614 break;
1615 case View.AttachInfo.INVALIDATE_RECT_MSG:
1616 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1617 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1618 info.release();
1619 break;
1620 case DO_TRAVERSAL:
1621 if (mProfile) {
1622 Debug.startMethodTracing("ViewRoot");
1623 }
1624
1625 performTraversals();
1626
1627 if (mProfile) {
1628 Debug.stopMethodTracing();
1629 mProfile = false;
1630 }
1631 break;
1632 case FINISHED_EVENT:
1633 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1634 break;
1635 case DISPATCH_KEY:
1636 if (LOCAL_LOGV) Log.v(
1637 "ViewRoot", "Dispatching key "
1638 + msg.obj + " to " + mView);
1639 deliverKeyEvent((KeyEvent)msg.obj, true);
1640 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001641 case DISPATCH_POINTER: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 MotionEvent event = (MotionEvent)msg.obj;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001643 boolean callWhenDone = msg.arg1 != 0;
1644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 if (event == null) {
1646 try {
Michael Chan53071d62009-05-13 17:29:48 -07001647 long timeBeforeGettingEvents;
1648 if (MEASURE_LATENCY) {
1649 timeBeforeGettingEvents = System.nanoTime();
1650 }
1651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 event = sWindowSession.getPendingPointerMove(mWindow);
Michael Chan53071d62009-05-13 17:29:48 -07001653
1654 if (MEASURE_LATENCY && event != null) {
1655 lt.sample("9 Client got events ", System.nanoTime() - event.getEventTimeNano());
1656 lt.sample("8 Client getting events ", timeBeforeGettingEvents - event.getEventTimeNano());
1657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 } catch (RemoteException e) {
1659 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001660 callWhenDone = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001662 if (event != null && mTranslator != null) {
1663 mTranslator.translateEventInScreenToAppWindow(event);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001664 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 try {
1666 boolean handled;
1667 if (mView != null && mAdded && event != null) {
1668
1669 // enter touch mode on the down
1670 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
1671 if (isDown) {
1672 ensureTouchMode(true);
1673 }
1674 if(Config.LOGV) {
1675 captureMotionLog("captureDispatchPointer", event);
1676 }
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001677 if (mCurScrollY != 0) {
1678 event.offsetLocation(0, mCurScrollY);
1679 }
Michael Chan53071d62009-05-13 17:29:48 -07001680 if (MEASURE_LATENCY) {
1681 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
1682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 handled = mView.dispatchTouchEvent(event);
Michael Chan53071d62009-05-13 17:29:48 -07001684 if (MEASURE_LATENCY) {
1685 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
1686 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 if (!handled && isDown) {
1688 int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
1689
1690 final int edgeFlags = event.getEdgeFlags();
1691 int direction = View.FOCUS_UP;
1692 int x = (int)event.getX();
1693 int y = (int)event.getY();
1694 final int[] deltas = new int[2];
1695
1696 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
1697 direction = View.FOCUS_DOWN;
1698 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1699 deltas[0] = edgeSlop;
1700 x += edgeSlop;
1701 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1702 deltas[0] = -edgeSlop;
1703 x -= edgeSlop;
1704 }
1705 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
1706 direction = View.FOCUS_UP;
1707 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1708 deltas[0] = edgeSlop;
1709 x += edgeSlop;
1710 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1711 deltas[0] = -edgeSlop;
1712 x -= edgeSlop;
1713 }
1714 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1715 direction = View.FOCUS_RIGHT;
1716 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1717 direction = View.FOCUS_LEFT;
1718 }
1719
1720 if (edgeFlags != 0 && mView instanceof ViewGroup) {
1721 View nearest = FocusFinder.getInstance().findNearestTouchable(
1722 ((ViewGroup) mView), x, y, direction, deltas);
1723 if (nearest != null) {
1724 event.offsetLocation(deltas[0], deltas[1]);
1725 event.setEdgeFlags(0);
1726 mView.dispatchTouchEvent(event);
1727 }
1728 }
1729 }
1730 }
1731 } finally {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001732 if (callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 try {
1734 sWindowSession.finishKey(mWindow);
1735 } catch (RemoteException e) {
1736 }
1737 }
1738 if (event != null) {
1739 event.recycle();
1740 }
1741 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
1742 // Let the exception fall through -- the looper will catch
1743 // it and take care of the bad app for us.
1744 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001745 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 case DISPATCH_TRACKBALL:
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001747 deliverTrackballEvent((MotionEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 break;
1749 case DISPATCH_APP_VISIBILITY:
1750 handleAppVisibility(msg.arg1 != 0);
1751 break;
1752 case DISPATCH_GET_NEW_SURFACE:
1753 handleGetNewSurface();
1754 break;
1755 case RESIZED:
1756 Rect coveredInsets = ((Rect[])msg.obj)[0];
1757 Rect visibleInsets = ((Rect[])msg.obj)[1];
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
1760 && mPendingContentInsets.equals(coveredInsets)
1761 && mPendingVisibleInsets.equals(visibleInsets)) {
1762 break;
1763 }
1764 // fall through...
1765 case RESIZED_REPORT:
1766 if (mAdded) {
1767 mWinFrame.left = 0;
1768 mWinFrame.right = msg.arg1;
1769 mWinFrame.top = 0;
1770 mWinFrame.bottom = msg.arg2;
1771 mPendingContentInsets.set(((Rect[])msg.obj)[0]);
1772 mPendingVisibleInsets.set(((Rect[])msg.obj)[1]);
1773 if (msg.what == RESIZED_REPORT) {
1774 mReportNextDraw = true;
1775 }
1776 requestLayout();
1777 }
1778 break;
1779 case WINDOW_FOCUS_CHANGED: {
1780 if (mAdded) {
1781 boolean hasWindowFocus = msg.arg1 != 0;
1782 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1783 if (hasWindowFocus) {
1784 boolean inTouchMode = msg.arg2 != 0;
1785 ensureTouchModeLocally(inTouchMode);
1786
1787 if (mGlWanted) {
1788 checkEglErrors();
1789 // we lost the gl context, so recreate it.
1790 if (mGlWanted && !mUseGL) {
1791 initializeGL();
1792 if (mGlCanvas != null) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001793 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001794 mGlCanvas.setViewport(
Mitsuru Oshima61324e52009-07-21 15:40:36 -07001795 (int) (mWidth * appScale + 0.5f),
1796 (int) (mHeight * appScale + 0.5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 }
1798 }
1799 }
1800 }
Romain Guy8506ab42009-06-11 17:35:47 -07001801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 mLastWasImTarget = WindowManager.LayoutParams
1803 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001804
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 InputMethodManager imm = InputMethodManager.peekInstance();
1806 if (mView != null) {
1807 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1808 imm.startGettingWindowFocus(mView);
1809 }
1810 mView.dispatchWindowFocusChanged(hasWindowFocus);
1811 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 // Note: must be done after the focus change callbacks,
1814 // so all of the view state is set up correctly.
1815 if (hasWindowFocus) {
1816 if (imm != null && mLastWasImTarget) {
1817 imm.onWindowFocus(mView, mView.findFocus(),
1818 mWindowAttributes.softInputMode,
1819 !mHasHadWindowFocus, mWindowAttributes.flags);
1820 }
1821 // Clear the forward bit. We can just do this directly, since
1822 // the window manager doesn't care about it.
1823 mWindowAttributes.softInputMode &=
1824 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1825 ((WindowManager.LayoutParams)mView.getLayoutParams())
1826 .softInputMode &=
1827 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1828 mHasHadWindowFocus = true;
1829 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001830
1831 if (hasWindowFocus && mView != null) {
1832 sendAccessibilityEvents();
1833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 }
1835 } break;
1836 case DIE:
1837 dispatchDetachedFromWindow();
1838 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001839 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 if (LOCAL_LOGV) Log.v(
1841 "ViewRoot", "Dispatching key "
1842 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07001843 KeyEvent event = (KeyEvent)msg.obj;
1844 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
1845 // The IME is trying to say this event is from the
1846 // system! Bad bad bad!
1847 event = KeyEvent.changeFlags(event,
1848 event.getFlags()&~KeyEvent.FLAG_FROM_SYSTEM);
1849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 deliverKeyEventToViewHierarchy((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07001851 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 case FINISH_INPUT_CONNECTION: {
1853 InputMethodManager imm = InputMethodManager.peekInstance();
1854 if (imm != null) {
1855 imm.reportFinishInputConnection((InputConnection)msg.obj);
1856 }
1857 } break;
1858 case CHECK_FOCUS: {
1859 InputMethodManager imm = InputMethodManager.peekInstance();
1860 if (imm != null) {
1861 imm.checkFocus();
1862 }
1863 } break;
1864 }
1865 }
1866
1867 /**
1868 * Something in the current window tells us we need to change the touch mode. For
1869 * example, we are not in touch mode, and the user touches the screen.
1870 *
1871 * If the touch mode has changed, tell the window manager, and handle it locally.
1872 *
1873 * @param inTouchMode Whether we want to be in touch mode.
1874 * @return True if the touch mode changed and focus changed was changed as a result
1875 */
1876 boolean ensureTouchMode(boolean inTouchMode) {
1877 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
1878 + "touch mode is " + mAttachInfo.mInTouchMode);
1879 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1880
1881 // tell the window manager
1882 try {
1883 sWindowSession.setInTouchMode(inTouchMode);
1884 } catch (RemoteException e) {
1885 throw new RuntimeException(e);
1886 }
1887
1888 // handle the change
1889 return ensureTouchModeLocally(inTouchMode);
1890 }
1891
1892 /**
1893 * Ensure that the touch mode for this window is set, and if it is changing,
1894 * take the appropriate action.
1895 * @param inTouchMode Whether we want to be in touch mode.
1896 * @return True if the touch mode changed and focus changed was changed as a result
1897 */
1898 private boolean ensureTouchModeLocally(boolean inTouchMode) {
1899 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
1900 + "touch mode is " + mAttachInfo.mInTouchMode);
1901
1902 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1903
1904 mAttachInfo.mInTouchMode = inTouchMode;
1905 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
1906
1907 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
1908 }
1909
1910 private boolean enterTouchMode() {
1911 if (mView != null) {
1912 if (mView.hasFocus()) {
1913 // note: not relying on mFocusedView here because this could
1914 // be when the window is first being added, and mFocused isn't
1915 // set yet.
1916 final View focused = mView.findFocus();
1917 if (focused != null && !focused.isFocusableInTouchMode()) {
1918
1919 final ViewGroup ancestorToTakeFocus =
1920 findAncestorToTakeFocusInTouchMode(focused);
1921 if (ancestorToTakeFocus != null) {
1922 // there is an ancestor that wants focus after its descendants that
1923 // is focusable in touch mode.. give it focus
1924 return ancestorToTakeFocus.requestFocus();
1925 } else {
1926 // nothing appropriate to have focus in touch mode, clear it out
1927 mView.unFocus();
1928 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
1929 mFocusedView = null;
1930 return true;
1931 }
1932 }
1933 }
1934 }
1935 return false;
1936 }
1937
1938
1939 /**
1940 * Find an ancestor of focused that wants focus after its descendants and is
1941 * focusable in touch mode.
1942 * @param focused The currently focused view.
1943 * @return An appropriate view, or null if no such view exists.
1944 */
1945 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
1946 ViewParent parent = focused.getParent();
1947 while (parent instanceof ViewGroup) {
1948 final ViewGroup vgParent = (ViewGroup) parent;
1949 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
1950 && vgParent.isFocusableInTouchMode()) {
1951 return vgParent;
1952 }
1953 if (vgParent.isRootNamespace()) {
1954 return null;
1955 } else {
1956 parent = vgParent.getParent();
1957 }
1958 }
1959 return null;
1960 }
1961
1962 private boolean leaveTouchMode() {
1963 if (mView != null) {
1964 if (mView.hasFocus()) {
1965 // i learned the hard way to not trust mFocusedView :)
1966 mFocusedView = mView.findFocus();
1967 if (!(mFocusedView instanceof ViewGroup)) {
1968 // some view has focus, let it keep it
1969 return false;
1970 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
1971 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
1972 // some view group has focus, and doesn't prefer its children
1973 // over itself for focus, so let them keep it.
1974 return false;
1975 }
1976 }
1977
1978 // find the best view to give focus to in this brave new non-touch-mode
1979 // world
1980 final View focused = focusSearch(null, View.FOCUS_DOWN);
1981 if (focused != null) {
1982 return focused.requestFocus(View.FOCUS_DOWN);
1983 }
1984 }
1985 return false;
1986 }
1987
1988
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001989 private void deliverTrackballEvent(MotionEvent event, boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 if (event == null) {
1991 try {
1992 event = sWindowSession.getPendingTrackballMove(mWindow);
1993 } catch (RemoteException e) {
1994 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001995 callWhenDone = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 }
1997
1998 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
1999
2000 boolean handled = false;
2001 try {
2002 if (event == null) {
2003 handled = true;
2004 } else if (mView != null && mAdded) {
2005 handled = mView.dispatchTrackballEvent(event);
2006 if (!handled) {
2007 // we could do something here, like changing the focus
2008 // or something?
2009 }
2010 }
2011 } finally {
2012 if (handled) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002013 if (callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 try {
2015 sWindowSession.finishKey(mWindow);
2016 } catch (RemoteException e) {
2017 }
2018 }
2019 if (event != null) {
2020 event.recycle();
2021 }
2022 // If we reach this, we delivered a trackball event to mView and
2023 // mView consumed it. Because we will not translate the trackball
2024 // event into a key event, touch mode will not exit, so we exit
2025 // touch mode here.
2026 ensureTouchMode(false);
2027 //noinspection ReturnInsideFinallyBlock
2028 return;
2029 }
2030 // Let the exception fall through -- the looper will catch
2031 // it and take care of the bad app for us.
2032 }
2033
2034 final TrackballAxis x = mTrackballAxisX;
2035 final TrackballAxis y = mTrackballAxisY;
2036
2037 long curTime = SystemClock.uptimeMillis();
2038 if ((mLastTrackballTime+MAX_TRACKBALL_DELAY) < curTime) {
2039 // It has been too long since the last movement,
2040 // so restart at the beginning.
2041 x.reset(0);
2042 y.reset(0);
2043 mLastTrackballTime = curTime;
2044 }
2045
2046 try {
2047 final int action = event.getAction();
2048 final int metastate = event.getMetaState();
2049 switch (action) {
2050 case MotionEvent.ACTION_DOWN:
2051 x.reset(2);
2052 y.reset(2);
2053 deliverKeyEvent(new KeyEvent(curTime, curTime,
2054 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER,
2055 0, metastate), false);
2056 break;
2057 case MotionEvent.ACTION_UP:
2058 x.reset(2);
2059 y.reset(2);
2060 deliverKeyEvent(new KeyEvent(curTime, curTime,
2061 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER,
2062 0, metastate), false);
2063 break;
2064 }
2065
2066 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2067 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2068 + " move=" + event.getX()
2069 + " / Y=" + y.position + " step="
2070 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2071 + " move=" + event.getY());
2072 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2073 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
2074
2075 // Generate DPAD events based on the trackball movement.
2076 // We pick the axis that has moved the most as the direction of
2077 // the DPAD. When we generate DPAD events for one axis, then the
2078 // other axis is reset -- we don't want to perform DPAD jumps due
2079 // to slight movements in the trackball when making major movements
2080 // along the other axis.
2081 int keycode = 0;
2082 int movement = 0;
2083 float accel = 1;
2084 if (xOff > yOff) {
2085 movement = x.generate((2/event.getXPrecision()));
2086 if (movement != 0) {
2087 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2088 : KeyEvent.KEYCODE_DPAD_LEFT;
2089 accel = x.acceleration;
2090 y.reset(2);
2091 }
2092 } else if (yOff > 0) {
2093 movement = y.generate((2/event.getYPrecision()));
2094 if (movement != 0) {
2095 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2096 : KeyEvent.KEYCODE_DPAD_UP;
2097 accel = y.acceleration;
2098 x.reset(2);
2099 }
2100 }
2101
2102 if (keycode != 0) {
2103 if (movement < 0) movement = -movement;
2104 int accelMovement = (int)(movement * accel);
2105 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2106 + " accelMovement=" + accelMovement
2107 + " accel=" + accel);
2108 if (accelMovement > movement) {
2109 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2110 + keycode);
2111 movement--;
2112 deliverKeyEvent(new KeyEvent(curTime, curTime,
2113 KeyEvent.ACTION_MULTIPLE, keycode,
2114 accelMovement-movement, metastate), false);
2115 }
2116 while (movement > 0) {
2117 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2118 + keycode);
2119 movement--;
2120 curTime = SystemClock.uptimeMillis();
2121 deliverKeyEvent(new KeyEvent(curTime, curTime,
2122 KeyEvent.ACTION_DOWN, keycode, 0, event.getMetaState()), false);
2123 deliverKeyEvent(new KeyEvent(curTime, curTime,
2124 KeyEvent.ACTION_UP, keycode, 0, metastate), false);
2125 }
2126 mLastTrackballTime = curTime;
2127 }
2128 } finally {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002129 if (callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 try {
2131 sWindowSession.finishKey(mWindow);
2132 } catch (RemoteException e) {
2133 }
2134 if (event != null) {
2135 event.recycle();
2136 }
2137 }
2138 // Let the exception fall through -- the looper will catch
2139 // it and take care of the bad app for us.
2140 }
2141 }
2142
2143 /**
2144 * @param keyCode The key code
2145 * @return True if the key is directional.
2146 */
2147 static boolean isDirectional(int keyCode) {
2148 switch (keyCode) {
2149 case KeyEvent.KEYCODE_DPAD_LEFT:
2150 case KeyEvent.KEYCODE_DPAD_RIGHT:
2151 case KeyEvent.KEYCODE_DPAD_UP:
2152 case KeyEvent.KEYCODE_DPAD_DOWN:
2153 return true;
2154 }
2155 return false;
2156 }
2157
2158 /**
2159 * Returns true if this key is a keyboard key.
2160 * @param keyEvent The key event.
2161 * @return whether this key is a keyboard key.
2162 */
2163 private static boolean isKeyboardKey(KeyEvent keyEvent) {
2164 final int convertedKey = keyEvent.getUnicodeChar();
2165 return convertedKey > 0;
2166 }
2167
2168
2169
2170 /**
2171 * See if the key event means we should leave touch mode (and leave touch
2172 * mode if so).
2173 * @param event The key event.
2174 * @return Whether this key event should be consumed (meaning the act of
2175 * leaving touch mode alone is considered the event).
2176 */
2177 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
2178 if (event.getAction() != KeyEvent.ACTION_DOWN) {
2179 return false;
2180 }
2181 if ((event.getFlags()&KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2182 return false;
2183 }
2184
2185 // only relevant if we are in touch mode
2186 if (!mAttachInfo.mInTouchMode) {
2187 return false;
2188 }
2189
2190 // if something like an edit text has focus and the user is typing,
2191 // leave touch mode
2192 //
2193 // note: the condition of not being a keyboard key is kind of a hacky
2194 // approximation of whether we think the focused view will want the
2195 // key; if we knew for sure whether the focused view would consume
2196 // the event, that would be better.
2197 if (isKeyboardKey(event) && mView != null && mView.hasFocus()) {
2198 mFocusedView = mView.findFocus();
2199 if ((mFocusedView instanceof ViewGroup)
2200 && ((ViewGroup) mFocusedView).getDescendantFocusability() ==
2201 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2202 // something has focus, but is holding it weakly as a container
2203 return false;
2204 }
2205 if (ensureTouchMode(false)) {
2206 throw new IllegalStateException("should not have changed focus "
2207 + "when leaving touch mode while a view has focus.");
2208 }
2209 return false;
2210 }
2211
2212 if (isDirectional(event.getKeyCode())) {
2213 // no view has focus, so we leave touch mode (and find something
2214 // to give focus to). the event is consumed if we were able to
2215 // find something to give focus to.
2216 return ensureTouchMode(false);
2217 }
2218 return false;
2219 }
2220
2221 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002222 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002223 */
2224 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002225 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 if (ev == null ||
2227 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2228 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
Romain Guy8506ab42009-06-11 17:35:47 -07002230
2231 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232 sb.append(ev.getDownTime()).append(',');
2233 sb.append(ev.getEventTime()).append(',');
2234 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002235 sb.append(ev.getX()).append(',');
2236 sb.append(ev.getY()).append(',');
2237 sb.append(ev.getPressure()).append(',');
2238 sb.append(ev.getSize()).append(',');
2239 sb.append(ev.getMetaState()).append(',');
2240 sb.append(ev.getXPrecision()).append(',');
2241 sb.append(ev.getYPrecision()).append(',');
2242 sb.append(ev.getDeviceId()).append(',');
2243 sb.append(ev.getEdgeFlags());
2244 Log.d(TAG, sb.toString());
2245 }
2246 /**
2247 * log motion events
2248 */
2249 private static void captureKeyLog(String subTag, KeyEvent ev) {
2250 //check dynamic switch
2251 if (ev == null ||
2252 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2253 return;
2254 }
2255 StringBuilder sb = new StringBuilder(subTag + ": ");
2256 sb.append(ev.getDownTime()).append(',');
2257 sb.append(ev.getEventTime()).append(',');
2258 sb.append(ev.getAction()).append(',');
2259 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 sb.append(ev.getRepeatCount()).append(',');
2261 sb.append(ev.getMetaState()).append(',');
2262 sb.append(ev.getDeviceId()).append(',');
2263 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002264 Log.d(TAG, sb.toString());
2265 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266
2267 int enqueuePendingEvent(Object event, boolean sendDone) {
2268 int seq = mPendingEventSeq+1;
2269 if (seq < 0) seq = 0;
2270 mPendingEventSeq = seq;
2271 mPendingEvents.put(seq, event);
2272 return sendDone ? seq : -seq;
2273 }
2274
2275 Object retrievePendingEvent(int seq) {
2276 if (seq < 0) seq = -seq;
2277 Object event = mPendingEvents.get(seq);
2278 if (event != null) {
2279 mPendingEvents.remove(seq);
2280 }
2281 return event;
2282 }
Romain Guy8506ab42009-06-11 17:35:47 -07002283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002284 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
2285 // If mView is null, we just consume the key event because it doesn't
2286 // make sense to do anything else with it.
2287 boolean handled = mView != null
2288 ? mView.dispatchKeyEventPreIme(event) : true;
2289 if (handled) {
2290 if (sendDone) {
2291 if (LOCAL_LOGV) Log.v(
2292 "ViewRoot", "Telling window manager key is finished");
2293 try {
2294 sWindowSession.finishKey(mWindow);
2295 } catch (RemoteException e) {
2296 }
2297 }
2298 return;
2299 }
2300 // If it is possible for this window to interact with the input
2301 // method window, then we want to first dispatch our key events
2302 // to the input method.
2303 if (mLastWasImTarget) {
2304 InputMethodManager imm = InputMethodManager.peekInstance();
2305 if (imm != null && mView != null) {
2306 int seq = enqueuePendingEvent(event, sendDone);
2307 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2308 + seq + " event=" + event);
2309 imm.dispatchKeyEvent(mView.getContext(), seq, event,
2310 mInputMethodCallback);
2311 return;
2312 }
2313 }
2314 deliverKeyEventToViewHierarchy(event, sendDone);
2315 }
2316
2317 void handleFinishedEvent(int seq, boolean handled) {
2318 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2319 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2320 + " handled=" + handled + " event=" + event);
2321 if (event != null) {
2322 final boolean sendDone = seq >= 0;
2323 if (!handled) {
2324 deliverKeyEventToViewHierarchy(event, sendDone);
2325 return;
2326 } else if (sendDone) {
2327 if (LOCAL_LOGV) Log.v(
2328 "ViewRoot", "Telling window manager key is finished");
2329 try {
2330 sWindowSession.finishKey(mWindow);
2331 } catch (RemoteException e) {
2332 }
2333 } else {
2334 Log.w("ViewRoot", "handleFinishedEvent(seq=" + seq
2335 + " handled=" + handled + " ev=" + event
2336 + ") neither delivering nor finishing key");
2337 }
2338 }
2339 }
Romain Guy8506ab42009-06-11 17:35:47 -07002340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341 private void deliverKeyEventToViewHierarchy(KeyEvent event, boolean sendDone) {
2342 try {
2343 if (mView != null && mAdded) {
2344 final int action = event.getAction();
2345 boolean isDown = (action == KeyEvent.ACTION_DOWN);
2346
2347 if (checkForLeavingTouchModeAndConsume(event)) {
2348 return;
Romain Guy8506ab42009-06-11 17:35:47 -07002349 }
2350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 if (Config.LOGV) {
2352 captureKeyLog("captureDispatchKeyEvent", event);
2353 }
2354 boolean keyHandled = mView.dispatchKeyEvent(event);
2355
2356 if (!keyHandled && isDown) {
2357 int direction = 0;
2358 switch (event.getKeyCode()) {
2359 case KeyEvent.KEYCODE_DPAD_LEFT:
2360 direction = View.FOCUS_LEFT;
2361 break;
2362 case KeyEvent.KEYCODE_DPAD_RIGHT:
2363 direction = View.FOCUS_RIGHT;
2364 break;
2365 case KeyEvent.KEYCODE_DPAD_UP:
2366 direction = View.FOCUS_UP;
2367 break;
2368 case KeyEvent.KEYCODE_DPAD_DOWN:
2369 direction = View.FOCUS_DOWN;
2370 break;
2371 }
2372
2373 if (direction != 0) {
2374
2375 View focused = mView != null ? mView.findFocus() : null;
2376 if (focused != null) {
2377 View v = focused.focusSearch(direction);
2378 boolean focusPassed = false;
2379 if (v != null && v != focused) {
2380 // do the math the get the interesting rect
2381 // of previous focused into the coord system of
2382 // newly focused view
2383 focused.getFocusedRect(mTempRect);
2384 ((ViewGroup) mView).offsetDescendantRectToMyCoords(focused, mTempRect);
2385 ((ViewGroup) mView).offsetRectIntoDescendantCoords(v, mTempRect);
2386 focusPassed = v.requestFocus(direction, mTempRect);
2387 }
2388
2389 if (!focusPassed) {
2390 mView.dispatchUnhandledMove(focused, direction);
2391 } else {
2392 playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
2393 }
2394 }
2395 }
2396 }
2397 }
2398
2399 } finally {
2400 if (sendDone) {
2401 if (LOCAL_LOGV) Log.v(
2402 "ViewRoot", "Telling window manager key is finished");
2403 try {
2404 sWindowSession.finishKey(mWindow);
2405 } catch (RemoteException e) {
2406 }
2407 }
2408 // Let the exception fall through -- the looper will catch
2409 // it and take care of the bad app for us.
2410 }
2411 }
2412
2413 private AudioManager getAudioManager() {
2414 if (mView == null) {
2415 throw new IllegalStateException("getAudioManager called when there is no mView");
2416 }
2417 if (mAudioManager == null) {
2418 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2419 }
2420 return mAudioManager;
2421 }
2422
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002423 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2424 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002425
2426 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002427 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002428 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002429 restore = true;
2430 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002431 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002432 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002433 if (params != null) {
2434 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002435 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002436 int relayoutResult = sWindowSession.relayout(
2437 mWindow, params,
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002438 (int) (mView.mMeasuredWidth * appScale + 0.5f),
2439 (int) (mView.mMeasuredHeight * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002440 viewVisibility, insetsPending, mWinFrame,
2441 mPendingContentInsets, mPendingVisibleInsets, mSurface);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002442 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002443 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002444 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002445
2446 if (mTranslator != null) {
2447 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2448 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2449 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002450 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002451 return relayoutResult;
2452 }
Romain Guy8506ab42009-06-11 17:35:47 -07002453
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002454 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002455 * {@inheritDoc}
2456 */
2457 public void playSoundEffect(int effectId) {
2458 checkThread();
2459
2460 final AudioManager audioManager = getAudioManager();
2461
2462 switch (effectId) {
2463 case SoundEffectConstants.CLICK:
2464 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2465 return;
2466 case SoundEffectConstants.NAVIGATION_DOWN:
2467 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2468 return;
2469 case SoundEffectConstants.NAVIGATION_LEFT:
2470 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2471 return;
2472 case SoundEffectConstants.NAVIGATION_RIGHT:
2473 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2474 return;
2475 case SoundEffectConstants.NAVIGATION_UP:
2476 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2477 return;
2478 default:
2479 throw new IllegalArgumentException("unknown effect id " + effectId +
2480 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2481 }
2482 }
2483
2484 /**
2485 * {@inheritDoc}
2486 */
2487 public boolean performHapticFeedback(int effectId, boolean always) {
2488 try {
2489 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2490 } catch (RemoteException e) {
2491 return false;
2492 }
2493 }
2494
2495 /**
2496 * {@inheritDoc}
2497 */
2498 public View focusSearch(View focused, int direction) {
2499 checkThread();
2500 if (!(mView instanceof ViewGroup)) {
2501 return null;
2502 }
2503 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2504 }
2505
2506 public void debug() {
2507 mView.debug();
2508 }
2509
2510 public void die(boolean immediate) {
2511 checkThread();
2512 if (Config.LOGV) Log.v("ViewRoot", "DIE in " + this + " of " + mSurface);
2513 synchronized (this) {
2514 if (mAdded && !mFirst) {
2515 int viewVisibility = mView.getVisibility();
2516 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2517 if (mWindowAttributesChanged || viewVisibilityChanged) {
2518 // If layout params have been changed, first give them
2519 // to the window manager to make sure it has the correct
2520 // animation info.
2521 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002522 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2523 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002524 sWindowSession.finishDrawing(mWindow);
2525 }
2526 } catch (RemoteException e) {
2527 }
2528 }
2529
Mathias Agopian5583dc62009-07-09 16:28:11 -07002530 mSurface.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002531 }
2532 if (mAdded) {
2533 mAdded = false;
2534 if (immediate) {
2535 dispatchDetachedFromWindow();
2536 } else if (mView != null) {
2537 sendEmptyMessage(DIE);
2538 }
2539 }
2540 }
2541 }
2542
2543 public void dispatchFinishedEvent(int seq, boolean handled) {
2544 Message msg = obtainMessage(FINISHED_EVENT);
2545 msg.arg1 = seq;
2546 msg.arg2 = handled ? 1 : 0;
2547 sendMessage(msg);
2548 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002550 public void dispatchResized(int w, int h, Rect coveredInsets,
2551 Rect visibleInsets, boolean reportDraw) {
2552 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2553 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2554 + " visibleInsets=" + visibleInsets.toShortString()
2555 + " reportDraw=" + reportDraw);
2556 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002557 if (mTranslator != null) {
2558 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2559 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2560 w *= mTranslator.applicationInvertedScale;
2561 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002562 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002563 msg.arg1 = w;
2564 msg.arg2 = h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002565 msg.obj = new Rect[] { new Rect(coveredInsets), new Rect(visibleInsets) };
2566 sendMessage(msg);
2567 }
2568
2569 public void dispatchKey(KeyEvent event) {
2570 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2571 //noinspection ConstantConditions
2572 if (false && event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
2573 if (Config.LOGD) Log.d("keydisp",
2574 "===================================================");
2575 if (Config.LOGD) Log.d("keydisp", "Focused view Hierarchy is:");
2576 debug();
2577
2578 if (Config.LOGD) Log.d("keydisp",
2579 "===================================================");
2580 }
2581 }
2582
2583 Message msg = obtainMessage(DISPATCH_KEY);
2584 msg.obj = event;
2585
2586 if (LOCAL_LOGV) Log.v(
2587 "ViewRoot", "sending key " + event + " to " + mView);
2588
2589 sendMessageAtTime(msg, event.getEventTime());
2590 }
2591
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002592 public void dispatchPointer(MotionEvent event, long eventTime,
2593 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002594 Message msg = obtainMessage(DISPATCH_POINTER);
2595 msg.obj = event;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002596 msg.arg1 = callWhenDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 sendMessageAtTime(msg, eventTime);
2598 }
2599
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002600 public void dispatchTrackball(MotionEvent event, long eventTime,
2601 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2603 msg.obj = event;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002604 msg.arg1 = callWhenDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 sendMessageAtTime(msg, eventTime);
2606 }
2607
2608 public void dispatchAppVisibility(boolean visible) {
2609 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
2610 msg.arg1 = visible ? 1 : 0;
2611 sendMessage(msg);
2612 }
2613
2614 public void dispatchGetNewSurface() {
2615 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
2616 sendMessage(msg);
2617 }
2618
2619 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2620 Message msg = Message.obtain();
2621 msg.what = WINDOW_FOCUS_CHANGED;
2622 msg.arg1 = hasFocus ? 1 : 0;
2623 msg.arg2 = inTouchMode ? 1 : 0;
2624 sendMessage(msg);
2625 }
2626
svetoslavganov75986cf2009-05-14 22:28:01 -07002627 /**
2628 * The window is getting focus so if there is anything focused/selected
2629 * send an {@link AccessibilityEvent} to announce that.
2630 */
2631 private void sendAccessibilityEvents() {
2632 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
2633 return;
2634 }
2635 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2636 View focusedView = mView.findFocus();
2637 if (focusedView != null && focusedView != mView) {
2638 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2639 }
2640 }
2641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002642 public boolean showContextMenuForChild(View originalView) {
2643 return false;
2644 }
2645
2646 public void createContextMenu(ContextMenu menu) {
2647 }
2648
2649 public void childDrawableStateChanged(View child) {
2650 }
2651
2652 protected Rect getWindowFrame() {
2653 return mWinFrame;
2654 }
2655
2656 void checkThread() {
2657 if (mThread != Thread.currentThread()) {
2658 throw new CalledFromWrongThreadException(
2659 "Only the original thread that created a view hierarchy can touch its views.");
2660 }
2661 }
2662
2663 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2664 // ViewRoot never intercepts touch event, so this can be a no-op
2665 }
2666
2667 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
2668 boolean immediate) {
2669 return scrollToRectOrFocus(rectangle, immediate);
2670 }
Romain Guy8506ab42009-06-11 17:35:47 -07002671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 static class InputMethodCallback extends IInputMethodCallback.Stub {
2673 private WeakReference<ViewRoot> mViewRoot;
2674
2675 public InputMethodCallback(ViewRoot viewRoot) {
2676 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2677 }
Romain Guy8506ab42009-06-11 17:35:47 -07002678
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 public void finishedEvent(int seq, boolean handled) {
2680 final ViewRoot viewRoot = mViewRoot.get();
2681 if (viewRoot != null) {
2682 viewRoot.dispatchFinishedEvent(seq, handled);
2683 }
2684 }
2685
2686 public void sessionCreated(IInputMethodSession session) throws RemoteException {
2687 // Stub -- not for use in the client.
2688 }
2689 }
Romain Guy8506ab42009-06-11 17:35:47 -07002690
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002691 static class EventCompletion extends Handler {
2692 final IWindow mWindow;
2693 final KeyEvent mKeyEvent;
2694 final boolean mIsPointer;
2695 final MotionEvent mMotionEvent;
Romain Guy8506ab42009-06-11 17:35:47 -07002696
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002697 EventCompletion(Looper looper, IWindow window, KeyEvent key,
2698 boolean isPointer, MotionEvent motion) {
2699 super(looper);
2700 mWindow = window;
2701 mKeyEvent = key;
2702 mIsPointer = isPointer;
2703 mMotionEvent = motion;
2704 sendEmptyMessage(0);
2705 }
Romain Guy8506ab42009-06-11 17:35:47 -07002706
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002707 @Override
2708 public void handleMessage(Message msg) {
2709 if (mKeyEvent != null) {
2710 try {
2711 sWindowSession.finishKey(mWindow);
2712 } catch (RemoteException e) {
2713 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002714 } else if (mIsPointer) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002715 boolean didFinish;
2716 MotionEvent event = mMotionEvent;
2717 if (event == null) {
2718 try {
2719 event = sWindowSession.getPendingPointerMove(mWindow);
2720 } catch (RemoteException e) {
2721 }
2722 didFinish = true;
2723 } else {
2724 didFinish = event.getAction() == MotionEvent.ACTION_OUTSIDE;
2725 }
2726 if (!didFinish) {
2727 try {
2728 sWindowSession.finishKey(mWindow);
2729 } catch (RemoteException e) {
2730 }
2731 }
2732 } else {
2733 MotionEvent event = mMotionEvent;
2734 if (event == null) {
2735 try {
2736 event = sWindowSession.getPendingTrackballMove(mWindow);
2737 } catch (RemoteException e) {
2738 }
2739 } else {
2740 try {
2741 sWindowSession.finishKey(mWindow);
2742 } catch (RemoteException e) {
2743 }
2744 }
2745 }
2746 }
2747 }
Romain Guy8506ab42009-06-11 17:35:47 -07002748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002750 private final WeakReference<ViewRoot> mViewRoot;
2751 private final Looper mMainLooper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002752
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002753 public W(ViewRoot viewRoot, Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002755 mMainLooper = context.getMainLooper();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002756 }
2757
2758 public void resized(int w, int h, Rect coveredInsets,
2759 Rect visibleInsets, boolean reportDraw) {
2760 final ViewRoot viewRoot = mViewRoot.get();
2761 if (viewRoot != null) {
2762 viewRoot.dispatchResized(w, h, coveredInsets,
2763 visibleInsets, reportDraw);
2764 }
2765 }
2766
2767 public void dispatchKey(KeyEvent event) {
2768 final ViewRoot viewRoot = mViewRoot.get();
2769 if (viewRoot != null) {
2770 viewRoot.dispatchKey(event);
2771 } else {
2772 Log.w("ViewRoot.W", "Key event " + event + " but no ViewRoot available!");
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002773 new EventCompletion(mMainLooper, this, event, false, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002774 }
2775 }
2776
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002777 public void dispatchPointer(MotionEvent event, long eventTime,
2778 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 final ViewRoot viewRoot = mViewRoot.get();
Michael Chan53071d62009-05-13 17:29:48 -07002780 if (viewRoot != null) {
2781 if (MEASURE_LATENCY) {
2782 // Note: eventTime is in milliseconds
2783 ViewRoot.lt.sample("* ViewRoot b4 dispatchPtr", System.nanoTime() - eventTime * 1000000);
2784 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002785 viewRoot.dispatchPointer(event, eventTime, callWhenDone);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002786 } else {
2787 new EventCompletion(mMainLooper, this, null, true, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002788 }
2789 }
2790
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002791 public void dispatchTrackball(MotionEvent event, long eventTime,
2792 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002793 final ViewRoot viewRoot = mViewRoot.get();
2794 if (viewRoot != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002795 viewRoot.dispatchTrackball(event, eventTime, callWhenDone);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002796 } else {
2797 new EventCompletion(mMainLooper, this, null, false, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 }
2799 }
2800
2801 public void dispatchAppVisibility(boolean visible) {
2802 final ViewRoot viewRoot = mViewRoot.get();
2803 if (viewRoot != null) {
2804 viewRoot.dispatchAppVisibility(visible);
2805 }
2806 }
2807
2808 public void dispatchGetNewSurface() {
2809 final ViewRoot viewRoot = mViewRoot.get();
2810 if (viewRoot != null) {
2811 viewRoot.dispatchGetNewSurface();
2812 }
2813 }
2814
2815 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2816 final ViewRoot viewRoot = mViewRoot.get();
2817 if (viewRoot != null) {
2818 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
2819 }
2820 }
2821
2822 private static int checkCallingPermission(String permission) {
2823 if (!Process.supportsProcesses()) {
2824 return PackageManager.PERMISSION_GRANTED;
2825 }
2826
2827 try {
2828 return ActivityManagerNative.getDefault().checkPermission(
2829 permission, Binder.getCallingPid(), Binder.getCallingUid());
2830 } catch (RemoteException e) {
2831 return PackageManager.PERMISSION_DENIED;
2832 }
2833 }
2834
2835 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
2836 final ViewRoot viewRoot = mViewRoot.get();
2837 if (viewRoot != null) {
2838 final View view = viewRoot.mView;
2839 if (view != null) {
2840 if (checkCallingPermission(Manifest.permission.DUMP) !=
2841 PackageManager.PERMISSION_GRANTED) {
2842 throw new SecurityException("Insufficient permissions to invoke"
2843 + " executeCommand() from pid=" + Binder.getCallingPid()
2844 + ", uid=" + Binder.getCallingUid());
2845 }
2846
2847 OutputStream clientStream = null;
2848 try {
2849 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
2850 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
2851 } catch (IOException e) {
2852 e.printStackTrace();
2853 } finally {
2854 if (clientStream != null) {
2855 try {
2856 clientStream.close();
2857 } catch (IOException e) {
2858 e.printStackTrace();
2859 }
2860 }
2861 }
2862 }
2863 }
2864 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07002865
2866 public void dispatchWallpaperOffsets(float x, float y) {
2867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002868 }
2869
2870 /**
2871 * Maintains state information for a single trackball axis, generating
2872 * discrete (DPAD) movements based on raw trackball motion.
2873 */
2874 static final class TrackballAxis {
2875 /**
2876 * The maximum amount of acceleration we will apply.
2877 */
2878 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07002879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002880 /**
2881 * The maximum amount of time (in milliseconds) between events in order
2882 * for us to consider the user to be doing fast trackball movements,
2883 * and thus apply an acceleration.
2884 */
2885 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07002886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 /**
2888 * Scaling factor to the time (in milliseconds) between events to how
2889 * much to multiple/divide the current acceleration. When movement
2890 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
2891 * FAST_MOVE_TIME it divides it.
2892 */
2893 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07002894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 float position;
2896 float absPosition;
2897 float acceleration = 1;
2898 long lastMoveTime = 0;
2899 int step;
2900 int dir;
2901 int nonAccelMovement;
2902
2903 void reset(int _step) {
2904 position = 0;
2905 acceleration = 1;
2906 lastMoveTime = 0;
2907 step = _step;
2908 dir = 0;
2909 }
2910
2911 /**
2912 * Add trackball movement into the state. If the direction of movement
2913 * has been reversed, the state is reset before adding the
2914 * movement (so that you don't have to compensate for any previously
2915 * collected movement before see the result of the movement in the
2916 * new direction).
2917 *
2918 * @return Returns the absolute value of the amount of movement
2919 * collected so far.
2920 */
2921 float collect(float off, long time, String axis) {
2922 long normTime;
2923 if (off > 0) {
2924 normTime = (long)(off * FAST_MOVE_TIME);
2925 if (dir < 0) {
2926 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
2927 position = 0;
2928 step = 0;
2929 acceleration = 1;
2930 lastMoveTime = 0;
2931 }
2932 dir = 1;
2933 } else if (off < 0) {
2934 normTime = (long)((-off) * FAST_MOVE_TIME);
2935 if (dir > 0) {
2936 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
2937 position = 0;
2938 step = 0;
2939 acceleration = 1;
2940 lastMoveTime = 0;
2941 }
2942 dir = -1;
2943 } else {
2944 normTime = 0;
2945 }
Romain Guy8506ab42009-06-11 17:35:47 -07002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 // The number of milliseconds between each movement that is
2948 // considered "normal" and will not result in any acceleration
2949 // or deceleration, scaled by the offset we have here.
2950 if (normTime > 0) {
2951 long delta = time - lastMoveTime;
2952 lastMoveTime = time;
2953 float acc = acceleration;
2954 if (delta < normTime) {
2955 // The user is scrolling rapidly, so increase acceleration.
2956 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
2957 if (scale > 1) acc *= scale;
2958 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
2959 + off + " normTime=" + normTime + " delta=" + delta
2960 + " scale=" + scale + " acc=" + acc);
2961 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
2962 } else {
2963 // The user is scrolling slowly, so decrease acceleration.
2964 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
2965 if (scale > 1) acc /= scale;
2966 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
2967 + off + " normTime=" + normTime + " delta=" + delta
2968 + " scale=" + scale + " acc=" + acc);
2969 acceleration = acc > 1 ? acc : 1;
2970 }
2971 }
2972 position += off;
2973 return (absPosition = Math.abs(position));
2974 }
2975
2976 /**
2977 * Generate the number of discrete movement events appropriate for
2978 * the currently collected trackball movement.
2979 *
2980 * @param precision The minimum movement required to generate the
2981 * first discrete movement.
2982 *
2983 * @return Returns the number of discrete movements, either positive
2984 * or negative, or 0 if there is not enough trackball movement yet
2985 * for a discrete movement.
2986 */
2987 int generate(float precision) {
2988 int movement = 0;
2989 nonAccelMovement = 0;
2990 do {
2991 final int dir = position >= 0 ? 1 : -1;
2992 switch (step) {
2993 // If we are going to execute the first step, then we want
2994 // to do this as soon as possible instead of waiting for
2995 // a full movement, in order to make things look responsive.
2996 case 0:
2997 if (absPosition < precision) {
2998 return movement;
2999 }
3000 movement += dir;
3001 nonAccelMovement += dir;
3002 step = 1;
3003 break;
3004 // If we have generated the first movement, then we need
3005 // to wait for the second complete trackball motion before
3006 // generating the second discrete movement.
3007 case 1:
3008 if (absPosition < 2) {
3009 return movement;
3010 }
3011 movement += dir;
3012 nonAccelMovement += dir;
3013 position += dir > 0 ? -2 : 2;
3014 absPosition = Math.abs(position);
3015 step = 2;
3016 break;
3017 // After the first two, we generate discrete movements
3018 // consistently with the trackball, applying an acceleration
3019 // if the trackball is moving quickly. This is a simple
3020 // acceleration on top of what we already compute based
3021 // on how quickly the wheel is being turned, to apply
3022 // a longer increasing acceleration to continuous movement
3023 // in one direction.
3024 default:
3025 if (absPosition < 1) {
3026 return movement;
3027 }
3028 movement += dir;
3029 position += dir >= 0 ? -1 : 1;
3030 absPosition = Math.abs(position);
3031 float acc = acceleration;
3032 acc *= 1.1f;
3033 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3034 break;
3035 }
3036 } while (true);
3037 }
3038 }
3039
3040 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3041 public CalledFromWrongThreadException(String msg) {
3042 super(msg);
3043 }
3044 }
3045
3046 private SurfaceHolder mHolder = new SurfaceHolder() {
3047 // we only need a SurfaceHolder for opengl. it would be nice
3048 // to implement everything else though, especially the callback
3049 // support (opengl doesn't make use of it right now, but eventually
3050 // will).
3051 public Surface getSurface() {
3052 return mSurface;
3053 }
3054
3055 public boolean isCreating() {
3056 return false;
3057 }
3058
3059 public void addCallback(Callback callback) {
3060 }
3061
3062 public void removeCallback(Callback callback) {
3063 }
3064
3065 public void setFixedSize(int width, int height) {
3066 }
3067
3068 public void setSizeFromLayout() {
3069 }
3070
3071 public void setFormat(int format) {
3072 }
3073
3074 public void setType(int type) {
3075 }
3076
3077 public void setKeepScreenOn(boolean screenOn) {
3078 }
3079
3080 public Canvas lockCanvas() {
3081 return null;
3082 }
3083
3084 public Canvas lockCanvas(Rect dirty) {
3085 return null;
3086 }
3087
3088 public void unlockCanvasAndPost(Canvas canvas) {
3089 }
3090 public Rect getSurfaceFrame() {
3091 return null;
3092 }
3093 };
3094
3095 static RunQueue getRunQueue() {
3096 RunQueue rq = sRunQueues.get();
3097 if (rq != null) {
3098 return rq;
3099 }
3100 rq = new RunQueue();
3101 sRunQueues.set(rq);
3102 return rq;
3103 }
Romain Guy8506ab42009-06-11 17:35:47 -07003104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 /**
3106 * @hide
3107 */
3108 static final class RunQueue {
3109 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3110
3111 void post(Runnable action) {
3112 postDelayed(action, 0);
3113 }
3114
3115 void postDelayed(Runnable action, long delayMillis) {
3116 HandlerAction handlerAction = new HandlerAction();
3117 handlerAction.action = action;
3118 handlerAction.delay = delayMillis;
3119
3120 synchronized (mActions) {
3121 mActions.add(handlerAction);
3122 }
3123 }
3124
3125 void removeCallbacks(Runnable action) {
3126 final HandlerAction handlerAction = new HandlerAction();
3127 handlerAction.action = action;
3128
3129 synchronized (mActions) {
3130 final ArrayList<HandlerAction> actions = mActions;
3131
3132 while (actions.remove(handlerAction)) {
3133 // Keep going
3134 }
3135 }
3136 }
3137
3138 void executeActions(Handler handler) {
3139 synchronized (mActions) {
3140 final ArrayList<HandlerAction> actions = mActions;
3141 final int count = actions.size();
3142
3143 for (int i = 0; i < count; i++) {
3144 final HandlerAction handlerAction = actions.get(i);
3145 handler.postDelayed(handlerAction.action, handlerAction.delay);
3146 }
3147
Romain Guy15df6702009-08-17 20:17:30 -07003148 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 }
3150 }
3151
3152 private static class HandlerAction {
3153 Runnable action;
3154 long delay;
3155
3156 @Override
3157 public boolean equals(Object o) {
3158 if (this == o) return true;
3159 if (o == null || getClass() != o.getClass()) return false;
3160
3161 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 return !(action != null ? !action.equals(that.action) : that.action != null);
3163
3164 }
3165
3166 @Override
3167 public int hashCode() {
3168 int result = action != null ? action.hashCode() : 0;
3169 result = 31 * result + (int) (delay ^ (delay >>> 32));
3170 return result;
3171 }
3172 }
3173 }
3174
3175 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
3176
3177 // inform skia to just abandon its texture cache IDs
3178 // doesn't call glDeleteTextures
3179 private static native void nativeAbandonGlCaches();
3180}