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