blob: a41c690d5541e07cba91accd97376b5c3f70ec01 [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
Dianne Hackborndc8a7f62010-05-10 11:29:34 -070019import com.android.internal.view.BaseSurfaceHolder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import com.android.internal.view.IInputMethodCallback;
21import com.android.internal.view.IInputMethodSession;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -070022import com.android.internal.view.RootViewSurfaceTaker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24import android.graphics.Canvas;
25import android.graphics.PixelFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.PorterDuff;
27import android.graphics.Rect;
28import android.graphics.Region;
29import android.os.*;
30import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.util.AndroidRuntimeException;
32import android.util.Config;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070033import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.util.Log;
35import android.util.EventLog;
Christopher Tatefa9e7c02010-05-06 12:07:10 -070036import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070039import android.view.accessibility.AccessibilityEvent;
40import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.view.inputmethod.InputConnection;
42import android.view.inputmethod.InputMethodManager;
43import android.widget.Scroller;
44import android.content.pm.PackageManager;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070045import android.content.res.CompatibilityInfo;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080046import android.content.res.Configuration;
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -070047import android.content.res.Resources;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080048import android.content.ComponentCallbacks;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.content.Context;
50import android.app.ActivityManagerNative;
51import android.Manifest;
52import android.media.AudioManager;
53
54import java.lang.ref.WeakReference;
Christopher Tatefa9e7c02010-05-06 12:07:10 -070055import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import java.io.IOException;
57import java.io.OutputStream;
58import java.util.ArrayList;
59
60import javax.microedition.khronos.egl.*;
61import javax.microedition.khronos.opengles.*;
62import static javax.microedition.khronos.opengles.GL10.*;
63
64/**
65 * The top of a view hierarchy, implementing the needed protocol between View
66 * and the WindowManager. This is for the most part an internal implementation
67 * detail of {@link WindowManagerImpl}.
68 *
69 * {@hide}
70 */
71@SuppressWarnings({"EmptyCatchBlock"})
72public final class ViewRoot extends Handler implements ViewParent,
73 View.AttachInfo.Callbacks {
74 private static final String TAG = "ViewRoot";
75 private static final boolean DBG = false;
Mike Reedfd716532009-10-12 14:42:56 -040076 private static final boolean SHOW_FPS = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 @SuppressWarnings({"ConstantConditionalExpression"})
78 private static final boolean LOCAL_LOGV = false ? Config.LOGD : Config.LOGV;
79 /** @noinspection PointlessBooleanExpression*/
80 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
81 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
Christopher Tatefa9e7c02010-05-06 12:07:10 -070082 private static final boolean DEBUG_INPUT = true || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
84 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
85 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
86 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
Dianne Hackborn694f79b2010-03-17 19:44:59 -070087 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 private static final boolean WATCH_POINTER = false;
89
Michael Chan53071d62009-05-13 17:29:48 -070090 private static final boolean MEASURE_LATENCY = false;
91 private static LatencyTimer lt;
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 /**
94 * Maximum time we allow the user to roll the trackball enough to generate
95 * a key event, before resetting the counters.
96 */
97 static final int MAX_TRACKBALL_DELAY = 250;
98
99 static long sInstanceCount = 0;
100
101 static IWindowSession sWindowSession;
102
103 static final Object mStaticInit = new Object();
104 static boolean mInitialized = false;
105
106 static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>();
107
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800108 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList<Runnable>();
109 static boolean sFirstDrawComplete = false;
110
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800111 static final ArrayList<ComponentCallbacks> sConfigCallbacks
112 = new ArrayList<ComponentCallbacks>();
113
Romain Guy8506ab42009-06-11 17:35:47 -0700114 private static int sDrawTime;
Romain Guy13922e02009-05-12 17:56:14 -0700115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 long mLastTrackballTime = 0;
117 final TrackballAxis mTrackballAxisX = new TrackballAxis();
118 final TrackballAxis mTrackballAxisY = new TrackballAxis();
119
120 final int[] mTmpLocation = new int[2];
Romain Guy8506ab42009-06-11 17:35:47 -0700121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 final InputMethodCallback mInputMethodCallback;
123 final SparseArray<Object> mPendingEvents = new SparseArray<Object>();
124 int mPendingEventSeq = 0;
Romain Guy8506ab42009-06-11 17:35:47 -0700125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final Thread mThread;
127
128 final WindowLeaked mLocation;
129
130 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
131
132 final W mWindow;
133
134 View mView;
135 View mFocusedView;
136 View mRealFocusedView; // this is not set to null in touch mode
137 int mViewVisibility;
138 boolean mAppVisible = true;
139
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700140 SurfaceHolder.Callback mSurfaceHolderCallback;
141 BaseSurfaceHolder mSurfaceHolder;
142 boolean mIsCreating;
143 boolean mDrawingAllowed;
144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 final Region mTransparentRegion;
146 final Region mPreviousTransparentRegion;
147
148 int mWidth;
149 int mHeight;
150 Rect mDirty; // will be a graphics.Region soon
Romain Guybb93d552009-03-24 21:04:15 -0700151 boolean mIsAnimating;
Romain Guy8506ab42009-06-11 17:35:47 -0700152
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700153 CompatibilityInfo.Translator mTranslator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154
155 final View.AttachInfo mAttachInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700156 InputChannel mInputChannel;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157
158 final Rect mTempRect; // used in the transaction to not thrash the heap.
159 final Rect mVisRect; // used to retrieve visible rect of focused view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160
161 boolean mTraversalScheduled;
162 boolean mWillDrawSoon;
163 boolean mLayoutRequested;
164 boolean mFirst;
165 boolean mReportNextDraw;
166 boolean mFullRedrawNeeded;
167 boolean mNewSurfaceNeeded;
168 boolean mHasHadWindowFocus;
169 boolean mLastWasImTarget;
170
171 boolean mWindowAttributesChanged = false;
172
173 // These can be accessed by any thread, must be protected with a lock.
Mathias Agopian5583dc62009-07-09 16:28:11 -0700174 // Surface can never be reassigned or cleared (use Surface.clear()).
175 private final Surface mSurface = new Surface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
177 boolean mAdded;
178 boolean mAddedTouchMode;
179
180 /*package*/ int mAddNesting;
181
182 // These are accessed by multiple threads.
183 final Rect mWinFrame; // frame given by window manager.
184
185 final Rect mPendingVisibleInsets = new Rect();
186 final Rect mPendingContentInsets = new Rect();
187 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
188 = new ViewTreeObserver.InternalInsetsInfo();
189
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700190 final Configuration mLastConfiguration = new Configuration();
191 final Configuration mPendingConfiguration = new Configuration();
192
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800193 class ResizedInfo {
194 Rect coveredInsets;
195 Rect visibleInsets;
196 Configuration newConfig;
197 }
198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 boolean mScrollMayChange;
200 int mSoftInputMode;
201 View mLastScrolledFocus;
202 int mScrollY;
203 int mCurScrollY;
204 Scroller mScroller;
Romain Guy8506ab42009-06-11 17:35:47 -0700205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 EGL10 mEgl;
207 EGLDisplay mEglDisplay;
208 EGLContext mEglContext;
209 EGLSurface mEglSurface;
210 GL11 mGL;
211 Canvas mGlCanvas;
212 boolean mUseGL;
213 boolean mGlWanted;
214
Romain Guy8506ab42009-06-11 17:35:47 -0700215 final ViewConfiguration mViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
217 /**
218 * see {@link #playSoundEffect(int)}
219 */
220 AudioManager mAudioManager;
221
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700222 private final int mDensity;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700223
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700224 public static IWindowSession getWindowSession(Looper mainLooper) {
225 synchronized (mStaticInit) {
226 if (!mInitialized) {
227 try {
228 InputMethodManager imm = InputMethodManager.getInstance(mainLooper);
229 sWindowSession = IWindowManager.Stub.asInterface(
230 ServiceManager.getService("window"))
231 .openSession(imm.getClient(), imm.getInputContext());
232 mInitialized = true;
233 } catch (RemoteException e) {
234 }
235 }
236 return sWindowSession;
237 }
238 }
239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 public ViewRoot(Context context) {
241 super();
242
Michael Chan53071d62009-05-13 17:29:48 -0700243 if (MEASURE_LATENCY && lt == null) {
244 lt = new LatencyTimer(100, 1000);
245 }
246
Carl Shapiro82fe5642010-02-24 00:14:23 -0800247 // For debug only
248 //++sInstanceCount;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
250 // Initialize the statics when this class is first instantiated. This is
251 // done here instead of in the static block because Zygote does not
252 // allow the spawning of threads.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700253 getWindowSession(context.getMainLooper());
254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 mThread = Thread.currentThread();
256 mLocation = new WindowLeaked(null);
257 mLocation.fillInStackTrace();
258 mWidth = -1;
259 mHeight = -1;
260 mDirty = new Rect();
261 mTempRect = new Rect();
262 mVisRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 mWinFrame = new Rect();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -0700264 mWindow = new W(this, context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mInputMethodCallback = new InputMethodCallback(this);
266 mViewVisibility = View.GONE;
267 mTransparentRegion = new Region();
268 mPreviousTransparentRegion = new Region();
269 mFirst = true; // true for the first time the view is added
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 mAdded = false;
271 mAttachInfo = new View.AttachInfo(sWindowSession, mWindow, this, this);
272 mViewConfiguration = ViewConfiguration.get(context);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700273 mDensity = context.getResources().getDisplayMetrics().densityDpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
Carl Shapiro82fe5642010-02-24 00:14:23 -0800276 // For debug only
277 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 @Override
279 protected void finalize() throws Throwable {
280 super.finalize();
281 --sInstanceCount;
282 }
Carl Shapiro82fe5642010-02-24 00:14:23 -0800283 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284
285 public static long getInstanceCount() {
286 return sInstanceCount;
287 }
288
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800289 public static void addFirstDrawHandler(Runnable callback) {
290 synchronized (sFirstDrawHandlers) {
291 if (!sFirstDrawComplete) {
292 sFirstDrawHandlers.add(callback);
293 }
294 }
295 }
296
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800297 public static void addConfigCallback(ComponentCallbacks callback) {
298 synchronized (sConfigCallbacks) {
299 sConfigCallbacks.add(callback);
300 }
301 }
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 // FIXME for perf testing only
304 private boolean mProfile = false;
305
306 /**
307 * Call this to profile the next traversal call.
308 * FIXME for perf testing only. Remove eventually
309 */
310 public void profile() {
311 mProfile = true;
312 }
313
314 /**
315 * Indicates whether we are in touch mode. Calling this method triggers an IPC
316 * call and should be avoided whenever possible.
317 *
318 * @return True, if the device is in touch mode, false otherwise.
319 *
320 * @hide
321 */
322 static boolean isInTouchMode() {
323 if (mInitialized) {
324 try {
325 return sWindowSession.getInTouchMode();
326 } catch (RemoteException e) {
327 }
328 }
329 return false;
330 }
331
332 private void initializeGL() {
333 initializeGLInner();
334 int err = mEgl.eglGetError();
335 if (err != EGL10.EGL_SUCCESS) {
336 // give-up on using GL
337 destroyGL();
338 mGlWanted = false;
339 }
340 }
341
342 private void initializeGLInner() {
343 final EGL10 egl = (EGL10) EGLContext.getEGL();
344 mEgl = egl;
345
346 /*
347 * Get to the default display.
348 */
349 final EGLDisplay eglDisplay = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
350 mEglDisplay = eglDisplay;
351
352 /*
353 * We can now initialize EGL for that display
354 */
355 int[] version = new int[2];
356 egl.eglInitialize(eglDisplay, version);
357
358 /*
359 * Specify a configuration for our opengl session
360 * and grab the first configuration that matches is
361 */
362 final int[] configSpec = {
363 EGL10.EGL_RED_SIZE, 5,
364 EGL10.EGL_GREEN_SIZE, 6,
365 EGL10.EGL_BLUE_SIZE, 5,
366 EGL10.EGL_DEPTH_SIZE, 0,
367 EGL10.EGL_NONE
368 };
369 final EGLConfig[] configs = new EGLConfig[1];
370 final int[] num_config = new int[1];
371 egl.eglChooseConfig(eglDisplay, configSpec, configs, 1, num_config);
372 final EGLConfig config = configs[0];
373
374 /*
375 * Create an OpenGL ES context. This must be done only once, an
376 * OpenGL context is a somewhat heavy object.
377 */
378 final EGLContext context = egl.eglCreateContext(eglDisplay, config,
379 EGL10.EGL_NO_CONTEXT, null);
380 mEglContext = context;
381
382 /*
383 * Create an EGL surface we can render into.
384 */
385 final EGLSurface surface = egl.eglCreateWindowSurface(eglDisplay, config, mHolder, null);
386 mEglSurface = surface;
387
388 /*
389 * Before we can issue GL commands, we need to make sure
390 * the context is current and bound to a surface.
391 */
392 egl.eglMakeCurrent(eglDisplay, surface, surface, context);
393
394 /*
395 * Get to the appropriate GL interface.
396 * This is simply done by casting the GL context to either
397 * GL10 or GL11.
398 */
399 final GL11 gl = (GL11) context.getGL();
400 mGL = gl;
401 mGlCanvas = new Canvas(gl);
402 mUseGL = true;
403 }
404
405 private void destroyGL() {
406 // inform skia that the context is gone
407 nativeAbandonGlCaches();
408
409 mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
410 EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
411 mEgl.eglDestroyContext(mEglDisplay, mEglContext);
412 mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
413 mEgl.eglTerminate(mEglDisplay);
414 mEglContext = null;
415 mEglSurface = null;
416 mEglDisplay = null;
417 mEgl = null;
418 mGlCanvas = null;
419 mGL = null;
420 mUseGL = false;
421 }
422
423 private void checkEglErrors() {
424 if (mUseGL) {
425 int err = mEgl.eglGetError();
426 if (err != EGL10.EGL_SUCCESS) {
427 // something bad has happened revert to
428 // normal rendering.
429 destroyGL();
430 if (err != EGL11.EGL_CONTEXT_LOST) {
431 // we'll try again if it was context lost
432 mGlWanted = false;
433 }
434 }
435 }
436 }
437
438 /**
439 * We have one child
440 */
441 public void setView(View view, WindowManager.LayoutParams attrs,
442 View panelParentView) {
443 synchronized (this) {
444 if (mView == null) {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700445 mView = view;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700446 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700447 attrs = mWindowAttributes;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700448 if (view instanceof RootViewSurfaceTaker) {
449 mSurfaceHolderCallback =
450 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
451 if (mSurfaceHolderCallback != null) {
452 mSurfaceHolder = new TakenSurfaceHolder();
453 }
454 }
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700455 Resources resources = mView.getContext().getResources();
456 CompatibilityInfo compatibilityInfo = resources.getCompatibilityInfo();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700457 mTranslator = compatibilityInfo.getTranslator();
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700458
459 if (mTranslator != null || !compatibilityInfo.supportsScreen()) {
Mitsuru Oshima240f8a72009-07-22 20:39:14 -0700460 mSurface.setCompatibleDisplayMetrics(resources.getDisplayMetrics(),
461 mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700462 }
463
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700464 boolean restore = false;
Romain Guy35b38ce2009-10-07 13:38:55 -0700465 if (mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700466 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700467 attrs.backup();
468 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700469 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700470 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs);
471
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700472 if (!compatibilityInfo.supportsScreen()) {
473 attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
474 }
475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 mSoftInputMode = attrs.softInputMode;
477 mWindowAttributesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 mAttachInfo.mRootView = view;
Romain Guy35b38ce2009-10-07 13:38:55 -0700479 mAttachInfo.mScalingRequired = mTranslator != null;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700480 mAttachInfo.mApplicationScale =
481 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 if (panelParentView != null) {
483 mAttachInfo.mPanelParentWindowToken
484 = panelParentView.getApplicationWindowToken();
485 }
486 mAdded = true;
487 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 // Schedule the first layout -before- adding to the window
490 // manager, to make sure we do the relayout before receiving
491 // any other events from the system.
492 requestLayout();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700493 mInputChannel = new InputChannel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 try {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700495 res = sWindowSession.add(mWindow, mWindowAttributes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700496 getHostVisibility(), mAttachInfo.mContentInsets,
497 mInputChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 } catch (RemoteException e) {
499 mAdded = false;
500 mView = null;
501 mAttachInfo.mRootView = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700502 mInputChannel = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 unscheduleTraversals();
504 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700505 } finally {
506 if (restore) {
507 attrs.restore();
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700510
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700511 if (mTranslator != null) {
512 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 mPendingContentInsets.set(mAttachInfo.mContentInsets);
515 mPendingVisibleInsets.set(0, 0, 0, 0);
516 if (Config.LOGV) Log.v("ViewRoot", "Added window " + mWindow);
517 if (res < WindowManagerImpl.ADD_OKAY) {
518 mView = null;
519 mAttachInfo.mRootView = null;
520 mAdded = false;
521 unscheduleTraversals();
522 switch (res) {
523 case WindowManagerImpl.ADD_BAD_APP_TOKEN:
524 case WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN:
525 throw new WindowManagerImpl.BadTokenException(
526 "Unable to add window -- token " + attrs.token
527 + " is not valid; is your activity running?");
528 case WindowManagerImpl.ADD_NOT_APP_TOKEN:
529 throw new WindowManagerImpl.BadTokenException(
530 "Unable to add window -- token " + attrs.token
531 + " is not for an application");
532 case WindowManagerImpl.ADD_APP_EXITING:
533 throw new WindowManagerImpl.BadTokenException(
534 "Unable to add window -- app for token " + attrs.token
535 + " is exiting");
536 case WindowManagerImpl.ADD_DUPLICATE_ADD:
537 throw new WindowManagerImpl.BadTokenException(
538 "Unable to add window -- window " + mWindow
539 + " has already been added");
540 case WindowManagerImpl.ADD_STARTING_NOT_NEEDED:
541 // Silently ignore -- we would have just removed it
542 // right away, anyway.
543 return;
544 case WindowManagerImpl.ADD_MULTIPLE_SINGLETON:
545 throw new WindowManagerImpl.BadTokenException(
546 "Unable to add window " + mWindow +
547 " -- another window of this type already exists");
548 case WindowManagerImpl.ADD_PERMISSION_DENIED:
549 throw new WindowManagerImpl.BadTokenException(
550 "Unable to add window " + mWindow +
551 " -- permission denied for this window type");
552 }
553 throw new RuntimeException(
554 "Unable to add window -- unknown error code " + res);
555 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700556
557 if (WindowManagerPolicy.ENABLE_NATIVE_INPUT_DISPATCH) {
558 InputQueue.registerInputChannel(mInputChannel, mInputHandler,
559 Looper.myQueue());
560 }
561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 view.assignParent(this);
563 mAddedTouchMode = (res&WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE) != 0;
564 mAppVisible = (res&WindowManagerImpl.ADD_FLAG_APP_VISIBLE) != 0;
565 }
566 }
567 }
568
569 public View getView() {
570 return mView;
571 }
572
573 final WindowLeaked getLocation() {
574 return mLocation;
575 }
576
577 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
578 synchronized (this) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700579 int oldSoftInputMode = mWindowAttributes.softInputMode;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700580 // preserve compatible window flag if exists.
581 int compatibleWindowFlag =
582 mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700584 mWindowAttributes.flags |= compatibleWindowFlag;
585
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 if (newView) {
587 mSoftInputMode = attrs.softInputMode;
588 requestLayout();
589 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700590 // Don't lose the mode we last auto-computed.
591 if ((attrs.softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
592 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
593 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
594 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
595 | (oldSoftInputMode
596 & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 mWindowAttributesChanged = true;
599 scheduleTraversals();
600 }
601 }
602
603 void handleAppVisibility(boolean visible) {
604 if (mAppVisible != visible) {
605 mAppVisible = visible;
606 scheduleTraversals();
607 }
608 }
609
610 void handleGetNewSurface() {
611 mNewSurfaceNeeded = true;
612 mFullRedrawNeeded = true;
613 scheduleTraversals();
614 }
615
616 /**
617 * {@inheritDoc}
618 */
619 public void requestLayout() {
620 checkThread();
621 mLayoutRequested = true;
622 scheduleTraversals();
623 }
624
625 /**
626 * {@inheritDoc}
627 */
628 public boolean isLayoutRequested() {
629 return mLayoutRequested;
630 }
631
632 public void invalidateChild(View child, Rect dirty) {
633 checkThread();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700634 if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
635 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -0700637 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700638 if (mCurScrollY != 0) {
Romain Guy1e095972009-07-07 11:22:45 -0700639 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700640 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700641 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -0700642 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700643 }
Romain Guy1e095972009-07-07 11:22:45 -0700644 if (mAttachInfo.mScalingRequired) {
645 dirty.inset(-1, -1);
646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647 }
648 mDirty.union(dirty);
649 if (!mWillDrawSoon) {
650 scheduleTraversals();
651 }
652 }
653
654 public ViewParent getParent() {
655 return null;
656 }
657
658 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
659 invalidateChild(null, dirty);
660 return null;
661 }
662
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700663 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 if (child != mView) {
665 throw new RuntimeException("child is not mine, honest!");
666 }
667 // Note: don't apply scroll offset, because we want to know its
668 // visibility in the virtual canvas being given to the view hierarchy.
669 return r.intersect(0, 0, mWidth, mHeight);
670 }
671
672 public void bringChildToFront(View child) {
673 }
674
675 public void scheduleTraversals() {
676 if (!mTraversalScheduled) {
677 mTraversalScheduled = true;
678 sendEmptyMessage(DO_TRAVERSAL);
679 }
680 }
681
682 public void unscheduleTraversals() {
683 if (mTraversalScheduled) {
684 mTraversalScheduled = false;
685 removeMessages(DO_TRAVERSAL);
686 }
687 }
688
689 int getHostVisibility() {
690 return mAppVisible ? mView.getVisibility() : View.GONE;
691 }
Romain Guy8506ab42009-06-11 17:35:47 -0700692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 private void performTraversals() {
694 // cache mView since it is used so much below...
695 final View host = mView;
696
697 if (DBG) {
698 System.out.println("======================================");
699 System.out.println("performTraversals");
700 host.debug();
701 }
702
703 if (host == null || !mAdded)
704 return;
705
706 mTraversalScheduled = false;
707 mWillDrawSoon = true;
708 boolean windowResizesToFitContent = false;
709 boolean fullRedrawNeeded = mFullRedrawNeeded;
710 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700711 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 WindowManager.LayoutParams lp = mWindowAttributes;
713
714 int desiredWindowWidth;
715 int desiredWindowHeight;
716 int childWidthMeasureSpec;
717 int childHeightMeasureSpec;
718
719 final View.AttachInfo attachInfo = mAttachInfo;
720
721 final int viewVisibility = getHostVisibility();
722 boolean viewVisibilityChanged = mViewVisibility != viewVisibility
723 || mNewSurfaceNeeded;
724
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700725 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 WindowManager.LayoutParams params = null;
728 if (mWindowAttributesChanged) {
729 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700730 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800731 params = lp;
732 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700733 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 if (mFirst) {
735 fullRedrawNeeded = true;
736 mLayoutRequested = true;
737
Romain Guy8506ab42009-06-11 17:35:47 -0700738 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700739 mView.getContext().getResources().getDisplayMetrics();
740 desiredWindowWidth = packageMetrics.widthPixels;
741 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742
743 // For the very first time, tell the view hierarchy that it
744 // is attached to the window. Note that at this point the surface
745 // object is not initialized to its backing store, but soon it
746 // will be (assuming the window is visible).
747 attachInfo.mSurface = mSurface;
Romain Guy35b38ce2009-10-07 13:38:55 -0700748 attachInfo.mTranslucentWindow = lp.format != PixelFormat.OPAQUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 attachInfo.mHasWindowFocus = false;
750 attachInfo.mWindowVisibility = viewVisibility;
751 attachInfo.mRecomputeGlobalAttributes = false;
752 attachInfo.mKeepScreenOn = false;
753 viewVisibilityChanged = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700754 mLastConfiguration.setTo(host.getResources().getConfiguration());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 host.dispatchAttachedToWindow(attachInfo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -0700757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700759 desiredWindowWidth = frame.width();
760 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
762 if (DEBUG_ORIENTATION) Log.v("ViewRoot",
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700763 "View " + host + " resized to: " + frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 fullRedrawNeeded = true;
765 mLayoutRequested = true;
766 windowResizesToFitContent = true;
767 }
768 }
769
770 if (viewVisibilityChanged) {
771 attachInfo.mWindowVisibility = viewVisibility;
772 host.dispatchWindowVisibilityChanged(viewVisibility);
773 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
774 if (mUseGL) {
775 destroyGL();
776 }
777 }
778 if (viewVisibility == View.GONE) {
779 // After making a window gone, we will count it as being
780 // shown for the first time the next time it gets focus.
781 mHasHadWindowFocus = false;
782 }
783 }
784
785 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -0700786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 if (mLayoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -0700788 // Execute enqueued actions on every layout in case a view that was detached
789 // enqueued an action after being detached
790 getRunQueue().executeActions(attachInfo.mHandler);
791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800792 if (mFirst) {
793 host.fitSystemWindows(mAttachInfo.mContentInsets);
794 // make sure touch mode code executes by setting cached value
795 // to opposite of the added touch mode.
796 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -0700797 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 } else {
799 if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) {
800 mAttachInfo.mContentInsets.set(mPendingContentInsets);
801 host.fitSystemWindows(mAttachInfo.mContentInsets);
802 insetsChanged = true;
803 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
804 + mAttachInfo.mContentInsets);
805 }
806 if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) {
807 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
808 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
809 + mAttachInfo.mVisibleInsets);
810 }
811 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
812 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
813 windowResizesToFitContent = true;
814
Romain Guy8506ab42009-06-11 17:35:47 -0700815 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700816 mView.getContext().getResources().getDisplayMetrics();
817 desiredWindowWidth = packageMetrics.widthPixels;
818 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 }
820 }
821
822 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
823 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
824
825 // Ask host how big it wants to be
826 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v("ViewRoot",
827 "Measuring " + host + " in display " + desiredWindowWidth
828 + "x" + desiredWindowHeight + "...");
829 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
830
831 if (DBG) {
832 System.out.println("======================================");
833 System.out.println("performTraversals -- after measure");
834 host.debug();
835 }
836 }
837
838 if (attachInfo.mRecomputeGlobalAttributes) {
839 //Log.i(TAG, "Computing screen on!");
840 attachInfo.mRecomputeGlobalAttributes = false;
841 boolean oldVal = attachInfo.mKeepScreenOn;
842 attachInfo.mKeepScreenOn = false;
843 host.dispatchCollectViewAttributes(0);
844 if (attachInfo.mKeepScreenOn != oldVal) {
845 params = lp;
846 //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn);
847 }
848 }
849
850 if (mFirst || attachInfo.mViewVisibilityChanged) {
851 attachInfo.mViewVisibilityChanged = false;
852 int resizeMode = mSoftInputMode &
853 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
854 // If we are in auto resize mode, then we need to determine
855 // what mode to use now.
856 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
857 final int N = attachInfo.mScrollContainers.size();
858 for (int i=0; i<N; i++) {
859 if (attachInfo.mScrollContainers.get(i).isShown()) {
860 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
861 }
862 }
863 if (resizeMode == 0) {
864 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
865 }
866 if ((lp.softInputMode &
867 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
868 lp.softInputMode = (lp.softInputMode &
869 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
870 resizeMode;
871 params = lp;
872 }
873 }
874 }
Romain Guy8506ab42009-06-11 17:35:47 -0700875
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
877 if (!PixelFormat.formatHasAlpha(params.format)) {
878 params.format = PixelFormat.TRANSLUCENT;
879 }
880 }
881
882 boolean windowShouldResize = mLayoutRequested && windowResizesToFitContent
Romain Guy2e4f4262010-04-06 11:07:52 -0700883 && ((mWidth != host.mMeasuredWidth || mHeight != host.mMeasuredHeight)
884 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
885 frame.width() < desiredWindowWidth && frame.width() != mWidth)
886 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
887 frame.height() < desiredWindowHeight && frame.height() != mHeight));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888
889 final boolean computesInternalInsets =
890 attachInfo.mTreeObserver.hasComputeInternalInsetsListeners();
891 boolean insetsPending = false;
892 int relayoutResult = 0;
893 if (mFirst || windowShouldResize || insetsChanged
894 || viewVisibilityChanged || params != null) {
895
896 if (viewVisibility == View.VISIBLE) {
897 // If this window is giving internal insets to the window
898 // manager, and it is being added or changing its visibility,
899 // then we want to first give the window manager "fake"
900 // insets to cause it to effectively ignore the content of
901 // the window during layout. This avoids it briefly causing
902 // other windows to resize/move based on the raw frame of the
903 // window, waiting until we can finish laying out this window
904 // and get back to the window manager with the ultimately
905 // computed insets.
906 insetsPending = computesInternalInsets
907 && (mFirst || viewVisibilityChanged);
Romain Guy8506ab42009-06-11 17:35:47 -0700908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 if (mWindowAttributes.memoryType == WindowManager.LayoutParams.MEMORY_TYPE_GPU) {
910 if (params == null) {
911 params = mWindowAttributes;
912 }
913 mGlWanted = true;
914 }
915 }
916
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700917 if (mSurfaceHolder != null) {
918 mSurfaceHolder.mSurfaceLock.lock();
919 mDrawingAllowed = true;
920 lp.format = mSurfaceHolder.getRequestedFormat();
921 lp.type = mSurfaceHolder.getRequestedType();
922 }
923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 boolean initialized = false;
925 boolean contentInsetsChanged = false;
Romain Guy13922e02009-05-12 17:56:14 -0700926 boolean visibleInsetsChanged;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700927 boolean hadSurface = mSurface.isValid();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 int fl = 0;
930 if (params != null) {
931 fl = params.flags;
932 if (attachInfo.mKeepScreenOn) {
933 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
934 }
935 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700936 if (DEBUG_LAYOUT) {
937 Log.i(TAG, "host=w:" + host.mMeasuredWidth + ", h:" +
938 host.mMeasuredHeight + ", params=" + params);
939 }
940 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 if (params != null) {
943 params.flags = fl;
944 }
945
946 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString()
947 + " content=" + mPendingContentInsets.toShortString()
948 + " visible=" + mPendingVisibleInsets.toShortString()
949 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -0700950
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700951 if (mPendingConfiguration.seq != 0) {
952 if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: "
953 + mPendingConfiguration);
954 updateConfiguration(mPendingConfiguration, !mFirst);
955 mPendingConfiguration.seq = 0;
956 }
957
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 contentInsetsChanged = !mPendingContentInsets.equals(
959 mAttachInfo.mContentInsets);
960 visibleInsetsChanged = !mPendingVisibleInsets.equals(
961 mAttachInfo.mVisibleInsets);
962 if (contentInsetsChanged) {
963 mAttachInfo.mContentInsets.set(mPendingContentInsets);
964 host.fitSystemWindows(mAttachInfo.mContentInsets);
965 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
966 + mAttachInfo.mContentInsets);
967 }
968 if (visibleInsetsChanged) {
969 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
970 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
971 + mAttachInfo.mVisibleInsets);
972 }
973
974 if (!hadSurface) {
975 if (mSurface.isValid()) {
976 // If we are creating a new surface, then we need to
977 // completely redraw it. Also, when we get to the
978 // point of drawing it we will hold off and schedule
979 // a new traversal instead. This is so we can tell the
980 // window manager about all of the windows being displayed
981 // before actually drawing them, so it can display then
982 // all at once.
983 newSurface = true;
984 fullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -0700985 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -0700986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800987 if (mGlWanted && !mUseGL) {
988 initializeGL();
989 initialized = mGlCanvas != null;
990 }
991 }
992 } else if (!mSurface.isValid()) {
993 // If the surface has been removed, then reset the scroll
994 // positions.
995 mLastScrolledFocus = null;
996 mScrollY = mCurScrollY = 0;
997 if (mScroller != null) {
998 mScroller.abortAnimation();
999 }
1000 }
1001 } catch (RemoteException e) {
1002 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 if (DEBUG_ORIENTATION) Log.v(
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001005 "ViewRoot", "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006
1007 attachInfo.mWindowLeft = frame.left;
1008 attachInfo.mWindowTop = frame.top;
1009
1010 // !!FIXME!! This next section handles the case where we did not get the
1011 // window size we asked for. We should avoid this by getting a maximum size from
1012 // the window session beforehand.
1013 mWidth = frame.width();
1014 mHeight = frame.height();
1015
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001016 if (mSurfaceHolder != null) {
1017 // The app owns the surface; tell it about what is going on.
1018 if (mSurface.isValid()) {
1019 // XXX .copyFrom() doesn't work!
1020 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1021 mSurfaceHolder.mSurface = mSurface;
1022 }
1023 mSurfaceHolder.mSurfaceLock.unlock();
1024 if (mSurface.isValid()) {
1025 if (!hadSurface) {
1026 mSurfaceHolder.ungetCallbacks();
1027
1028 mIsCreating = true;
1029 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
1030 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1031 if (callbacks != null) {
1032 for (SurfaceHolder.Callback c : callbacks) {
1033 c.surfaceCreated(mSurfaceHolder);
1034 }
1035 }
1036 surfaceChanged = true;
1037 }
1038 if (surfaceChanged) {
1039 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
1040 lp.format, mWidth, mHeight);
1041 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1042 if (callbacks != null) {
1043 for (SurfaceHolder.Callback c : callbacks) {
1044 c.surfaceChanged(mSurfaceHolder, lp.format,
1045 mWidth, mHeight);
1046 }
1047 }
1048 }
1049 mIsCreating = false;
1050 } else if (hadSurface) {
1051 mSurfaceHolder.ungetCallbacks();
1052 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1053 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
1054 if (callbacks != null) {
1055 for (SurfaceHolder.Callback c : callbacks) {
1056 c.surfaceDestroyed(mSurfaceHolder);
1057 }
1058 }
1059 mSurfaceHolder.mSurfaceLock.lock();
1060 // Make surface invalid.
1061 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1062 mSurfaceHolder.mSurface = new Surface();
1063 mSurfaceHolder.mSurfaceLock.unlock();
1064 }
1065 }
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 if (initialized) {
Mitsuru Oshima61324e52009-07-21 15:40:36 -07001068 mGlCanvas.setViewport((int) (mWidth * appScale + 0.5f),
1069 (int) (mHeight * appScale + 0.5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071
1072 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Romain Guy2d4cff62010-04-09 15:39:00 -07001073 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 if (focusChangedDueToTouchMode || mWidth != host.mMeasuredWidth
1075 || mHeight != host.mMeasuredHeight || contentInsetsChanged) {
1076 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
1077 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
1078
1079 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
1080 + mWidth + " measuredWidth=" + host.mMeasuredWidth
1081 + " mHeight=" + mHeight
1082 + " measuredHeight" + host.mMeasuredHeight
1083 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -07001084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001085 // Ask host how big it wants to be
1086 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1087
1088 // Implementation of weights from WindowManager.LayoutParams
1089 // We just grow the dimensions as needed and re-measure if
1090 // needs be
1091 int width = host.mMeasuredWidth;
1092 int height = host.mMeasuredHeight;
1093 boolean measureAgain = false;
1094
1095 if (lp.horizontalWeight > 0.0f) {
1096 width += (int) ((mWidth - width) * lp.horizontalWeight);
1097 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1098 MeasureSpec.EXACTLY);
1099 measureAgain = true;
1100 }
1101 if (lp.verticalWeight > 0.0f) {
1102 height += (int) ((mHeight - height) * lp.verticalWeight);
1103 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1104 MeasureSpec.EXACTLY);
1105 measureAgain = true;
1106 }
1107
1108 if (measureAgain) {
1109 if (DEBUG_LAYOUT) Log.v(TAG,
1110 "And hey let's measure once more: width=" + width
1111 + " height=" + height);
1112 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1113 }
1114
1115 mLayoutRequested = true;
1116 }
1117 }
1118
1119 final boolean didLayout = mLayoutRequested;
1120 boolean triggerGlobalLayoutListener = didLayout
1121 || attachInfo.mRecomputeGlobalAttributes;
1122 if (didLayout) {
1123 mLayoutRequested = false;
1124 mScrollMayChange = true;
1125 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
1126 "ViewRoot", "Laying out " + host + " to (" +
1127 host.mMeasuredWidth + ", " + host.mMeasuredHeight + ")");
Romain Guy13922e02009-05-12 17:56:14 -07001128 long startTime = 0L;
1129 if (Config.DEBUG && ViewDebug.profileLayout) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 startTime = SystemClock.elapsedRealtime();
1131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
1133
Romain Guy13922e02009-05-12 17:56:14 -07001134 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1135 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
1136 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
1137 + "please refer to the logs with the tag "
1138 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1139 }
1140 }
1141
1142 if (Config.DEBUG && ViewDebug.profileLayout) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1144 }
1145
1146 // By this point all views have been sized and positionned
1147 // We can compute the transparent area
1148
1149 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1150 // start out transparent
1151 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1152 host.getLocationInWindow(mTmpLocation);
1153 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1154 mTmpLocation[0] + host.mRight - host.mLeft,
1155 mTmpLocation[1] + host.mBottom - host.mTop);
1156
1157 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001158 if (mTranslator != null) {
1159 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1160 }
1161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1163 mPreviousTransparentRegion.set(mTransparentRegion);
1164 // reconfigure window manager
1165 try {
1166 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1167 } catch (RemoteException e) {
1168 }
1169 }
1170 }
1171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 if (DBG) {
1173 System.out.println("======================================");
1174 System.out.println("performTraversals -- after setFrame");
1175 host.debug();
1176 }
1177 }
1178
1179 if (triggerGlobalLayoutListener) {
1180 attachInfo.mRecomputeGlobalAttributes = false;
1181 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1182 }
1183
1184 if (computesInternalInsets) {
1185 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1186 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1187 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1188 givenContent.left = givenContent.top = givenContent.right
1189 = givenContent.bottom = givenVisible.left = givenVisible.top
1190 = givenVisible.right = givenVisible.bottom = 0;
1191 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001192 Rect contentInsets = insets.contentInsets;
1193 Rect visibleInsets = insets.visibleInsets;
1194 if (mTranslator != null) {
1195 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1196 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1199 mLastGivenInsets.set(insets);
1200 try {
1201 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001202 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 } catch (RemoteException e) {
1204 }
1205 }
1206 }
Romain Guy8506ab42009-06-11 17:35:47 -07001207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 if (mFirst) {
1209 // handle first focus request
1210 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1211 + mView.hasFocus());
1212 if (mView != null) {
1213 if (!mView.hasFocus()) {
1214 mView.requestFocus(View.FOCUS_FORWARD);
1215 mFocusedView = mRealFocusedView = mView.findFocus();
1216 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1217 + mFocusedView);
1218 } else {
1219 mRealFocusedView = mView.findFocus();
1220 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1221 + mRealFocusedView);
1222 }
1223 }
1224 }
1225
1226 mFirst = false;
1227 mWillDrawSoon = false;
1228 mNewSurfaceNeeded = false;
1229 mViewVisibility = viewVisibility;
1230
1231 if (mAttachInfo.mHasWindowFocus) {
1232 final boolean imTarget = WindowManager.LayoutParams
1233 .mayUseInputMethod(mWindowAttributes.flags);
1234 if (imTarget != mLastWasImTarget) {
1235 mLastWasImTarget = imTarget;
1236 InputMethodManager imm = InputMethodManager.peekInstance();
1237 if (imm != null && imTarget) {
1238 imm.startGettingWindowFocus(mView);
1239 imm.onWindowFocus(mView, mView.findFocus(),
1240 mWindowAttributes.softInputMode,
1241 !mHasHadWindowFocus, mWindowAttributes.flags);
1242 }
1243 }
1244 }
Romain Guy8506ab42009-06-11 17:35:47 -07001245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1247
1248 if (!cancelDraw && !newSurface) {
1249 mFullRedrawNeeded = false;
1250 draw(fullRedrawNeeded);
1251
1252 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1253 || mReportNextDraw) {
1254 if (LOCAL_LOGV) {
1255 Log.v("ViewRoot", "FINISHED DRAWING: " + mWindowAttributes.getTitle());
1256 }
1257 mReportNextDraw = false;
1258 try {
1259 sWindowSession.finishDrawing(mWindow);
1260 } catch (RemoteException e) {
1261 }
1262 }
1263 } else {
1264 // We were supposed to report when we are done drawing. Since we canceled the
1265 // draw, remember it here.
1266 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1267 mReportNextDraw = true;
1268 }
1269 if (fullRedrawNeeded) {
1270 mFullRedrawNeeded = true;
1271 }
1272 // Try again
1273 scheduleTraversals();
1274 }
1275 }
1276
1277 public void requestTransparentRegion(View child) {
1278 // the test below should not fail unless someone is messing with us
1279 checkThread();
1280 if (mView == child) {
1281 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1282 // Need to make sure we re-evaluate the window attributes next
1283 // time around, to ensure the window has the correct format.
1284 mWindowAttributesChanged = true;
1285 }
1286 }
1287
1288 /**
1289 * Figures out the measure spec for the root view in a window based on it's
1290 * layout params.
1291 *
1292 * @param windowSize
1293 * The available width or height of the window
1294 *
1295 * @param rootDimension
1296 * The layout params for one dimension (width or height) of the
1297 * window.
1298 *
1299 * @return The measure spec to use to measure the root view.
1300 */
1301 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1302 int measureSpec;
1303 switch (rootDimension) {
1304
Romain Guy980a9382010-01-08 15:06:28 -08001305 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 // Window can't resize. Force root view to be windowSize.
1307 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1308 break;
1309 case ViewGroup.LayoutParams.WRAP_CONTENT:
1310 // Window can resize. Set max size for root view.
1311 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1312 break;
1313 default:
1314 // Window wants to be an exact size. Force root view to be that size.
1315 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1316 break;
1317 }
1318 return measureSpec;
1319 }
1320
1321 private void draw(boolean fullRedrawNeeded) {
1322 Surface surface = mSurface;
1323 if (surface == null || !surface.isValid()) {
1324 return;
1325 }
1326
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001327 if (!sFirstDrawComplete) {
1328 synchronized (sFirstDrawHandlers) {
1329 sFirstDrawComplete = true;
1330 for (int i=0; i<sFirstDrawHandlers.size(); i++) {
1331 post(sFirstDrawHandlers.get(i));
1332 }
1333 }
1334 }
1335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 scrollToRectOrFocus(null, false);
1337
1338 if (mAttachInfo.mViewScrollChanged) {
1339 mAttachInfo.mViewScrollChanged = false;
1340 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1341 }
Romain Guy8506ab42009-06-11 17:35:47 -07001342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 int yoff;
Romain Guy5bcdff42009-05-14 21:27:18 -07001344 final boolean scrolling = mScroller != null && mScroller.computeScrollOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 if (scrolling) {
1346 yoff = mScroller.getCurrY();
1347 } else {
1348 yoff = mScrollY;
1349 }
1350 if (mCurScrollY != yoff) {
1351 mCurScrollY = yoff;
1352 fullRedrawNeeded = true;
1353 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001354 float appScale = mAttachInfo.mApplicationScale;
1355 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356
1357 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001358 if (mSurfaceHolder != null) {
1359 // The app owns the surface, we won't draw.
1360 dirty.setEmpty();
1361 return;
1362 }
1363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001364 if (mUseGL) {
1365 if (!dirty.isEmpty()) {
1366 Canvas canvas = mGlCanvas;
Romain Guy5bcdff42009-05-14 21:27:18 -07001367 if (mGL != null && canvas != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 mGL.glDisable(GL_SCISSOR_TEST);
1369 mGL.glClearColor(0, 0, 0, 0);
1370 mGL.glClear(GL_COLOR_BUFFER_BIT);
1371 mGL.glEnable(GL_SCISSOR_TEST);
1372
1373 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
Romain Guy5bcdff42009-05-14 21:27:18 -07001374 mAttachInfo.mIgnoreDirtyState = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 mView.mPrivateFlags |= View.DRAWN;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001376
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001377 int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
1378 try {
1379 canvas.translate(0, -yoff);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001380 if (mTranslator != null) {
1381 mTranslator.translateCanvas(canvas);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001382 }
Dianne Hackborn0d221012009-07-29 15:41:19 -07001383 canvas.setScreenDensity(scalingRequired
1384 ? DisplayMetrics.DENSITY_DEVICE : 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001385 mView.draw(canvas);
Romain Guy13922e02009-05-12 17:56:14 -07001386 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1387 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1388 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001389 } finally {
1390 canvas.restoreToCount(saveCount);
1391 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392
Romain Guy5bcdff42009-05-14 21:27:18 -07001393 mAttachInfo.mIgnoreDirtyState = false;
1394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001395 mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
1396 checkEglErrors();
1397
Mike Reedfd716532009-10-12 14:42:56 -04001398 if (SHOW_FPS || Config.DEBUG && ViewDebug.showFps) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 int now = (int)SystemClock.elapsedRealtime();
1400 if (sDrawTime != 0) {
1401 nativeShowFPS(canvas, now - sDrawTime);
1402 }
1403 sDrawTime = now;
1404 }
1405 }
1406 }
1407 if (scrolling) {
1408 mFullRedrawNeeded = true;
1409 scheduleTraversals();
1410 }
1411 return;
1412 }
1413
Romain Guy5bcdff42009-05-14 21:27:18 -07001414 if (fullRedrawNeeded) {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001415 mAttachInfo.mIgnoreDirtyState = true;
Mitsuru Oshima61324e52009-07-21 15:40:36 -07001416 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
Romain Guy5bcdff42009-05-14 21:27:18 -07001417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418
1419 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
1420 Log.v("ViewRoot", "Draw " + mView + "/"
1421 + mWindowAttributes.getTitle()
1422 + ": dirty={" + dirty.left + "," + dirty.top
1423 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001424 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1425 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 }
1427
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001428 if (!dirty.isEmpty() || mIsAnimating) {
1429 Canvas canvas;
1430 try {
1431 int left = dirty.left;
1432 int top = dirty.top;
1433 int right = dirty.right;
1434 int bottom = dirty.bottom;
1435 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001436
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001437 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1438 bottom != dirty.bottom) {
1439 mAttachInfo.mIgnoreDirtyState = true;
1440 }
1441
1442 // TODO: Do this in native
1443 canvas.setDensity(mDensity);
1444 } catch (Surface.OutOfResourcesException e) {
1445 Log.e("ViewRoot", "OutOfResourcesException locking surface", e);
1446 // TODO: we should ask the window manager to do something!
1447 // for now we just do nothing
1448 return;
1449 } catch (IllegalArgumentException e) {
1450 Log.e("ViewRoot", "IllegalArgumentException locking surface", e);
1451 // TODO: we should ask the window manager to do something!
1452 // for now we just do nothing
1453 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001454 }
1455
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001456 try {
1457 if (!dirty.isEmpty() || mIsAnimating) {
1458 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001460 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
1461 Log.v("ViewRoot", "Surface " + surface + " drawing to bitmap w="
1462 + canvas.getWidth() + ", h=" + canvas.getHeight());
1463 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001466 if (Config.DEBUG && ViewDebug.profileDrawing) {
1467 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001469
1470 // If this bitmap's format includes an alpha channel, we
1471 // need to clear it before drawing so that the child will
1472 // properly re-composite its drawing on a transparent
1473 // background. This automatically respects the clip/dirty region
1474 // or
1475 // If we are applying an offset, we need to clear the area
1476 // where the offset doesn't appear to avoid having garbage
1477 // left in the blank areas.
1478 if (!canvas.isOpaque() || yoff != 0) {
1479 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1480 }
1481
1482 dirty.setEmpty();
1483 mIsAnimating = false;
1484 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1485 mView.mPrivateFlags |= View.DRAWN;
1486
1487 if (DEBUG_DRAW) {
1488 Context cxt = mView.getContext();
1489 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1490 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1491 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1492 }
1493 int saveCount = canvas.save(Canvas.MATRIX_SAVE_FLAG);
1494 try {
1495 canvas.translate(0, -yoff);
1496 if (mTranslator != null) {
1497 mTranslator.translateCanvas(canvas);
1498 }
1499 canvas.setScreenDensity(scalingRequired
1500 ? DisplayMetrics.DENSITY_DEVICE : 0);
1501 mView.draw(canvas);
1502 } finally {
1503 mAttachInfo.mIgnoreDirtyState = false;
1504 canvas.restoreToCount(saveCount);
1505 }
1506
1507 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1508 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1509 }
1510
1511 if (SHOW_FPS || Config.DEBUG && ViewDebug.showFps) {
1512 int now = (int)SystemClock.elapsedRealtime();
1513 if (sDrawTime != 0) {
1514 nativeShowFPS(canvas, now - sDrawTime);
1515 }
1516 sDrawTime = now;
1517 }
1518
1519 if (Config.DEBUG && ViewDebug.profileDrawing) {
1520 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001524 } finally {
1525 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 }
1528
1529 if (LOCAL_LOGV) {
1530 Log.v("ViewRoot", "Surface " + surface + " unlockCanvasAndPost");
1531 }
Romain Guy8506ab42009-06-11 17:35:47 -07001532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 if (scrolling) {
1534 mFullRedrawNeeded = true;
1535 scheduleTraversals();
1536 }
1537 }
1538
1539 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1540 final View.AttachInfo attachInfo = mAttachInfo;
1541 final Rect ci = attachInfo.mContentInsets;
1542 final Rect vi = attachInfo.mVisibleInsets;
1543 int scrollY = 0;
1544 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001546 if (vi.left > ci.left || vi.top > ci.top
1547 || vi.right > ci.right || vi.bottom > ci.bottom) {
1548 // We'll assume that we aren't going to change the scroll
1549 // offset, since we want to avoid that unless it is actually
1550 // going to make the focus visible... otherwise we scroll
1551 // all over the place.
1552 scrollY = mScrollY;
1553 // We can be called for two different situations: during a draw,
1554 // to update the scroll position if the focus has changed (in which
1555 // case 'rectangle' is null), or in response to a
1556 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1557 // is non-null and we just want to scroll to whatever that
1558 // rectangle is).
1559 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001560
1561 // When in touch mode, focus points to the previously focused view,
1562 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001563 // line checks whether the view is still in our hierarchy.
1564 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001565 mRealFocusedView = null;
1566 return false;
1567 }
1568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001569 if (focus != mLastScrolledFocus) {
1570 // If the focus has changed, then ignore any requests to scroll
1571 // to a rectangle; first we want to make sure the entire focus
1572 // view is visible.
1573 rectangle = null;
1574 }
1575 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1576 + " rectangle=" + rectangle + " ci=" + ci
1577 + " vi=" + vi);
1578 if (focus == mLastScrolledFocus && !mScrollMayChange
1579 && rectangle == null) {
1580 // Optimization: if the focus hasn't changed since last
1581 // time, and no layout has happened, then just leave things
1582 // as they are.
1583 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1584 + mScrollY + " vi=" + vi.toShortString());
1585 } else if (focus != null) {
1586 // We need to determine if the currently focused view is
1587 // within the visible part of the window and, if not, apply
1588 // a pan so it can be seen.
1589 mLastScrolledFocus = focus;
1590 mScrollMayChange = false;
1591 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1592 // Try to find the rectangle from the focus view.
1593 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1594 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1595 + mView.getWidth() + " h=" + mView.getHeight()
1596 + " ci=" + ci.toShortString()
1597 + " vi=" + vi.toShortString());
1598 if (rectangle == null) {
1599 focus.getFocusedRect(mTempRect);
1600 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1601 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001602 if (mView instanceof ViewGroup) {
1603 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1604 focus, mTempRect);
1605 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001606 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1607 "Focus in window: focusRect="
1608 + mTempRect.toShortString()
1609 + " visRect=" + mVisRect.toShortString());
1610 } else {
1611 mTempRect.set(rectangle);
1612 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1613 "Request scroll to rect: "
1614 + mTempRect.toShortString()
1615 + " visRect=" + mVisRect.toShortString());
1616 }
1617 if (mTempRect.intersect(mVisRect)) {
1618 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1619 "Focus window visible rect: "
1620 + mTempRect.toShortString());
1621 if (mTempRect.height() >
1622 (mView.getHeight()-vi.top-vi.bottom)) {
1623 // If the focus simply is not going to fit, then
1624 // best is probably just to leave things as-is.
1625 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1626 "Too tall; leaving scrollY=" + scrollY);
1627 } else if ((mTempRect.top-scrollY) < vi.top) {
1628 scrollY -= vi.top - (mTempRect.top-scrollY);
1629 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1630 "Top covered; scrollY=" + scrollY);
1631 } else if ((mTempRect.bottom-scrollY)
1632 > (mView.getHeight()-vi.bottom)) {
1633 scrollY += (mTempRect.bottom-scrollY)
1634 - (mView.getHeight()-vi.bottom);
1635 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1636 "Bottom covered; scrollY=" + scrollY);
1637 }
1638 handled = true;
1639 }
1640 }
1641 }
1642 }
Romain Guy8506ab42009-06-11 17:35:47 -07001643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 if (scrollY != mScrollY) {
1645 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1646 + mScrollY + " , new=" + scrollY);
1647 if (!immediate) {
1648 if (mScroller == null) {
1649 mScroller = new Scroller(mView.getContext());
1650 }
1651 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1652 } else if (mScroller != null) {
1653 mScroller.abortAnimation();
1654 }
1655 mScrollY = scrollY;
1656 }
Romain Guy8506ab42009-06-11 17:35:47 -07001657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 return handled;
1659 }
Romain Guy8506ab42009-06-11 17:35:47 -07001660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 public void requestChildFocus(View child, View focused) {
1662 checkThread();
1663 if (mFocusedView != focused) {
1664 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1665 scheduleTraversals();
1666 }
1667 mFocusedView = mRealFocusedView = focused;
1668 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1669 + mFocusedView);
1670 }
1671
1672 public void clearChildFocus(View child) {
1673 checkThread();
1674
1675 View oldFocus = mFocusedView;
1676
1677 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1678 mFocusedView = mRealFocusedView = null;
1679 if (mView != null && !mView.hasFocus()) {
1680 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1681 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1682 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1683 }
1684 } else if (oldFocus != null) {
1685 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1686 }
1687 }
1688
1689
1690 public void focusableViewAvailable(View v) {
1691 checkThread();
1692
1693 if (mView != null && !mView.hasFocus()) {
1694 v.requestFocus();
1695 } else {
1696 // the one case where will transfer focus away from the current one
1697 // is if the current view is a view group that prefers to give focus
1698 // to its children first AND the view is a descendant of it.
1699 mFocusedView = mView.findFocus();
1700 boolean descendantsHaveDibsOnFocus =
1701 (mFocusedView instanceof ViewGroup) &&
1702 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1703 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1704 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1705 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1706 v.requestFocus();
1707 }
1708 }
1709 }
1710
1711 public void recomputeViewAttributes(View child) {
1712 checkThread();
1713 if (mView == child) {
1714 mAttachInfo.mRecomputeGlobalAttributes = true;
1715 if (!mWillDrawSoon) {
1716 scheduleTraversals();
1717 }
1718 }
1719 }
1720
1721 void dispatchDetachedFromWindow() {
1722 if (Config.LOGV) Log.v("ViewRoot", "Detaching in " + this + " of " + mSurface);
1723
1724 if (mView != null) {
1725 mView.dispatchDetachedFromWindow();
1726 }
1727
1728 mView = null;
1729 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001730 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731
1732 if (mUseGL) {
1733 destroyGL();
1734 }
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001735 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736
Jeff Brown46b9ac02010-04-22 18:58:52 -07001737 if (WindowManagerPolicy.ENABLE_NATIVE_INPUT_DISPATCH) {
1738 if (mInputChannel != null) {
1739 InputQueue.unregisterInputChannel(mInputChannel);
1740 mInputChannel.dispose();
1741 mInputChannel = null;
1742 }
1743 }
1744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 try {
1746 sWindowSession.remove(mWindow);
1747 } catch (RemoteException e) {
1748 }
1749 }
Romain Guy8506ab42009-06-11 17:35:47 -07001750
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001751 void updateConfiguration(Configuration config, boolean force) {
1752 if (DEBUG_CONFIGURATION) Log.v(TAG,
1753 "Applying new config to window "
1754 + mWindowAttributes.getTitle()
1755 + ": " + config);
1756 synchronized (sConfigCallbacks) {
1757 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1758 sConfigCallbacks.get(i).onConfigurationChanged(config);
1759 }
1760 }
1761 if (mView != null) {
1762 // At this point the resources have been updated to
1763 // have the most recent config, whatever that is. Use
1764 // the on in them which may be newer.
1765 if (mView != null) {
1766 config = mView.getResources().getConfiguration();
1767 }
1768 if (force || mLastConfiguration.diff(config) != 0) {
1769 mLastConfiguration.setTo(config);
1770 mView.dispatchConfigurationChanged(config);
1771 }
1772 }
1773 }
1774
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 /**
1776 * Return true if child is an ancestor of parent, (or equal to the parent).
1777 */
1778 private static boolean isViewDescendantOf(View child, View parent) {
1779 if (child == parent) {
1780 return true;
1781 }
1782
1783 final ViewParent theParent = child.getParent();
1784 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1785 }
1786
Romain Guycdb86672010-03-18 18:54:50 -07001787 private static void forceLayout(View view) {
1788 view.forceLayout();
1789 if (view instanceof ViewGroup) {
1790 ViewGroup group = (ViewGroup) view;
1791 final int count = group.getChildCount();
1792 for (int i = 0; i < count; i++) {
1793 forceLayout(group.getChildAt(i));
1794 }
1795 }
1796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797
1798 public final static int DO_TRAVERSAL = 1000;
1799 public final static int DIE = 1001;
1800 public final static int RESIZED = 1002;
1801 public final static int RESIZED_REPORT = 1003;
1802 public final static int WINDOW_FOCUS_CHANGED = 1004;
1803 public final static int DISPATCH_KEY = 1005;
1804 public final static int DISPATCH_POINTER = 1006;
1805 public final static int DISPATCH_TRACKBALL = 1007;
1806 public final static int DISPATCH_APP_VISIBILITY = 1008;
1807 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1808 public final static int FINISHED_EVENT = 1010;
1809 public final static int DISPATCH_KEY_FROM_IME = 1011;
1810 public final static int FINISH_INPUT_CONNECTION = 1012;
1811 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001812 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813
1814 @Override
1815 public void handleMessage(Message msg) {
1816 switch (msg.what) {
1817 case View.AttachInfo.INVALIDATE_MSG:
1818 ((View) msg.obj).invalidate();
1819 break;
1820 case View.AttachInfo.INVALIDATE_RECT_MSG:
1821 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1822 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1823 info.release();
1824 break;
1825 case DO_TRAVERSAL:
1826 if (mProfile) {
1827 Debug.startMethodTracing("ViewRoot");
1828 }
1829
1830 performTraversals();
1831
1832 if (mProfile) {
1833 Debug.stopMethodTracing();
1834 mProfile = false;
1835 }
1836 break;
1837 case FINISHED_EVENT:
1838 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1839 break;
1840 case DISPATCH_KEY:
1841 if (LOCAL_LOGV) Log.v(
1842 "ViewRoot", "Dispatching key "
1843 + msg.obj + " to " + mView);
1844 deliverKeyEvent((KeyEvent)msg.obj, true);
1845 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001846 case DISPATCH_POINTER: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 MotionEvent event = (MotionEvent)msg.obj;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001848 boolean callWhenDone = msg.arg1 != 0;
1849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 if (event == null) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001851 long timeBeforeGettingEvents;
1852 if (MEASURE_LATENCY) {
1853 timeBeforeGettingEvents = System.nanoTime();
1854 }
Michael Chan53071d62009-05-13 17:29:48 -07001855
Jeff Brown46b9ac02010-04-22 18:58:52 -07001856 event = getPendingPointerMotionEvent();
Michael Chan53071d62009-05-13 17:29:48 -07001857
Jeff Brown46b9ac02010-04-22 18:58:52 -07001858 if (MEASURE_LATENCY && event != null) {
1859 lt.sample("9 Client got events ", System.nanoTime() - event.getEventTimeNano());
1860 lt.sample("8 Client getting events ", timeBeforeGettingEvents - event.getEventTimeNano());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001862 callWhenDone = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001864 if (event != null && mTranslator != null) {
1865 mTranslator.translateEventInScreenToAppWindow(event);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 try {
1868 boolean handled;
1869 if (mView != null && mAdded && event != null) {
1870
1871 // enter touch mode on the down
1872 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
1873 if (isDown) {
1874 ensureTouchMode(true);
1875 }
1876 if(Config.LOGV) {
1877 captureMotionLog("captureDispatchPointer", event);
1878 }
Dianne Hackbornddca3ee2009-07-23 19:01:31 -07001879 if (mCurScrollY != 0) {
1880 event.offsetLocation(0, mCurScrollY);
1881 }
Michael Chan53071d62009-05-13 17:29:48 -07001882 if (MEASURE_LATENCY) {
1883 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
1884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 handled = mView.dispatchTouchEvent(event);
Michael Chan53071d62009-05-13 17:29:48 -07001886 if (MEASURE_LATENCY) {
1887 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
1888 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 if (!handled && isDown) {
1890 int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
1891
1892 final int edgeFlags = event.getEdgeFlags();
1893 int direction = View.FOCUS_UP;
1894 int x = (int)event.getX();
1895 int y = (int)event.getY();
1896 final int[] deltas = new int[2];
1897
1898 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
1899 direction = View.FOCUS_DOWN;
1900 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1901 deltas[0] = edgeSlop;
1902 x += edgeSlop;
1903 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1904 deltas[0] = -edgeSlop;
1905 x -= edgeSlop;
1906 }
1907 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
1908 direction = View.FOCUS_UP;
1909 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1910 deltas[0] = edgeSlop;
1911 x += edgeSlop;
1912 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1913 deltas[0] = -edgeSlop;
1914 x -= edgeSlop;
1915 }
1916 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
1917 direction = View.FOCUS_RIGHT;
1918 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
1919 direction = View.FOCUS_LEFT;
1920 }
1921
1922 if (edgeFlags != 0 && mView instanceof ViewGroup) {
1923 View nearest = FocusFinder.getInstance().findNearestTouchable(
1924 ((ViewGroup) mView), x, y, direction, deltas);
1925 if (nearest != null) {
1926 event.offsetLocation(deltas[0], deltas[1]);
1927 event.setEdgeFlags(0);
1928 mView.dispatchTouchEvent(event);
1929 }
1930 }
1931 }
1932 }
1933 } finally {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001934 if (callWhenDone) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07001935 finishMotionEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001937 recycleMotionEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
1939 // Let the exception fall through -- the looper will catch
1940 // it and take care of the bad app for us.
1941 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001942 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001943 case DISPATCH_TRACKBALL:
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07001944 deliverTrackballEvent((MotionEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 break;
1946 case DISPATCH_APP_VISIBILITY:
1947 handleAppVisibility(msg.arg1 != 0);
1948 break;
1949 case DISPATCH_GET_NEW_SURFACE:
1950 handleGetNewSurface();
1951 break;
1952 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001953 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001956 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001957 && mPendingVisibleInsets.equals(ri.visibleInsets)
1958 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001959 break;
1960 }
1961 // fall through...
1962 case RESIZED_REPORT:
1963 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001964 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1965 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001966 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 mWinFrame.left = 0;
1969 mWinFrame.right = msg.arg1;
1970 mWinFrame.top = 0;
1971 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001972 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1973 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 if (msg.what == RESIZED_REPORT) {
1975 mReportNextDraw = true;
1976 }
Romain Guycdb86672010-03-18 18:54:50 -07001977
1978 if (mView != null) {
1979 forceLayout(mView);
1980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 requestLayout();
1982 }
1983 break;
1984 case WINDOW_FOCUS_CHANGED: {
1985 if (mAdded) {
1986 boolean hasWindowFocus = msg.arg1 != 0;
1987 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1988 if (hasWindowFocus) {
1989 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001990 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991
1992 if (mGlWanted) {
1993 checkEglErrors();
1994 // we lost the gl context, so recreate it.
1995 if (mGlWanted && !mUseGL) {
1996 initializeGL();
1997 if (mGlCanvas != null) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001998 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001999 mGlCanvas.setViewport(
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002000 (int) (mWidth * appScale + 0.5f),
2001 (int) (mHeight * appScale + 0.5f));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 }
2003 }
2004 }
2005 }
Romain Guy8506ab42009-06-11 17:35:47 -07002006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 mLastWasImTarget = WindowManager.LayoutParams
2008 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07002009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002010 InputMethodManager imm = InputMethodManager.peekInstance();
2011 if (mView != null) {
2012 if (hasWindowFocus && imm != null && mLastWasImTarget) {
2013 imm.startGettingWindowFocus(mView);
2014 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002015 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 mView.dispatchWindowFocusChanged(hasWindowFocus);
2017 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002018
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 // Note: must be done after the focus change callbacks,
2020 // so all of the view state is set up correctly.
2021 if (hasWindowFocus) {
2022 if (imm != null && mLastWasImTarget) {
2023 imm.onWindowFocus(mView, mView.findFocus(),
2024 mWindowAttributes.softInputMode,
2025 !mHasHadWindowFocus, mWindowAttributes.flags);
2026 }
2027 // Clear the forward bit. We can just do this directly, since
2028 // the window manager doesn't care about it.
2029 mWindowAttributes.softInputMode &=
2030 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
2031 ((WindowManager.LayoutParams)mView.getLayoutParams())
2032 .softInputMode &=
2033 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
2034 mHasHadWindowFocus = true;
2035 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002036
2037 if (hasWindowFocus && mView != null) {
2038 sendAccessibilityEvents();
2039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
2041 } break;
2042 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07002043 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07002045 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 if (LOCAL_LOGV) Log.v(
2047 "ViewRoot", "Dispatching key "
2048 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07002049 KeyEvent event = (KeyEvent)msg.obj;
2050 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
2051 // The IME is trying to say this event is from the
2052 // system! Bad bad bad!
2053 event = KeyEvent.changeFlags(event,
2054 event.getFlags()&~KeyEvent.FLAG_FROM_SYSTEM);
2055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 deliverKeyEventToViewHierarchy((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07002057 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 case FINISH_INPUT_CONNECTION: {
2059 InputMethodManager imm = InputMethodManager.peekInstance();
2060 if (imm != null) {
2061 imm.reportFinishInputConnection((InputConnection)msg.obj);
2062 }
2063 } break;
2064 case CHECK_FOCUS: {
2065 InputMethodManager imm = InputMethodManager.peekInstance();
2066 if (imm != null) {
2067 imm.checkFocus();
2068 }
2069 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07002070 case CLOSE_SYSTEM_DIALOGS: {
2071 if (mView != null) {
2072 mView.onCloseSystemDialogs((String)msg.obj);
2073 }
2074 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 }
2076 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002077
2078 private void finishKeyEvent(KeyEvent event) {
2079 if (WindowManagerPolicy.ENABLE_NATIVE_INPUT_DISPATCH) {
2080 if (mFinishedCallback != null) {
2081 mFinishedCallback.run();
2082 mFinishedCallback = null;
2083 }
2084 } else {
2085 try {
2086 sWindowSession.finishKey(mWindow);
2087 } catch (RemoteException e) {
2088 }
2089 }
2090 }
2091
2092 private void finishMotionEvent() {
2093 if (WindowManagerPolicy.ENABLE_NATIVE_INPUT_DISPATCH) {
2094 throw new IllegalStateException("Should not be reachable with native input dispatch.");
2095 }
2096
2097 try {
2098 sWindowSession.finishKey(mWindow);
2099 } catch (RemoteException e) {
2100 }
2101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102
Jeff Brown46b9ac02010-04-22 18:58:52 -07002103 private void recycleMotionEvent(MotionEvent event) {
2104 if (event != null) {
2105 event.recycle();
2106 }
2107 }
2108
2109 private MotionEvent getPendingPointerMotionEvent() {
2110 if (WindowManagerPolicy.ENABLE_NATIVE_INPUT_DISPATCH) {
2111 throw new IllegalStateException("Should not be reachable with native input dispatch.");
2112 }
2113
2114 try {
2115 return sWindowSession.getPendingPointerMove(mWindow);
2116 } catch (RemoteException e) {
2117 return null;
2118 }
2119 }
2120
2121 private MotionEvent getPendingTrackballMotionEvent() {
2122 if (WindowManagerPolicy.ENABLE_NATIVE_INPUT_DISPATCH) {
2123 throw new IllegalStateException("Should not be reachable with native input dispatch.");
2124 }
2125
2126 try {
2127 return sWindowSession.getPendingTrackballMove(mWindow);
2128 } catch (RemoteException e) {
2129 return null;
2130 }
2131 }
2132
2133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 /**
2135 * Something in the current window tells us we need to change the touch mode. For
2136 * example, we are not in touch mode, and the user touches the screen.
2137 *
2138 * If the touch mode has changed, tell the window manager, and handle it locally.
2139 *
2140 * @param inTouchMode Whether we want to be in touch mode.
2141 * @return True if the touch mode changed and focus changed was changed as a result
2142 */
2143 boolean ensureTouchMode(boolean inTouchMode) {
2144 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
2145 + "touch mode is " + mAttachInfo.mInTouchMode);
2146 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2147
2148 // tell the window manager
2149 try {
2150 sWindowSession.setInTouchMode(inTouchMode);
2151 } catch (RemoteException e) {
2152 throw new RuntimeException(e);
2153 }
2154
2155 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07002156 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157 }
2158
2159 /**
2160 * Ensure that the touch mode for this window is set, and if it is changing,
2161 * take the appropriate action.
2162 * @param inTouchMode Whether we want to be in touch mode.
2163 * @return True if the touch mode changed and focus changed was changed as a result
2164 */
Romain Guy2d4cff62010-04-09 15:39:00 -07002165 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
2167 + "touch mode is " + mAttachInfo.mInTouchMode);
2168
2169 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2170
2171 mAttachInfo.mInTouchMode = inTouchMode;
2172 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
2173
Romain Guy2d4cff62010-04-09 15:39:00 -07002174 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 }
2176
2177 private boolean enterTouchMode() {
2178 if (mView != null) {
2179 if (mView.hasFocus()) {
2180 // note: not relying on mFocusedView here because this could
2181 // be when the window is first being added, and mFocused isn't
2182 // set yet.
2183 final View focused = mView.findFocus();
2184 if (focused != null && !focused.isFocusableInTouchMode()) {
2185
2186 final ViewGroup ancestorToTakeFocus =
2187 findAncestorToTakeFocusInTouchMode(focused);
2188 if (ancestorToTakeFocus != null) {
2189 // there is an ancestor that wants focus after its descendants that
2190 // is focusable in touch mode.. give it focus
2191 return ancestorToTakeFocus.requestFocus();
2192 } else {
2193 // nothing appropriate to have focus in touch mode, clear it out
2194 mView.unFocus();
2195 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
2196 mFocusedView = null;
2197 return true;
2198 }
2199 }
2200 }
2201 }
2202 return false;
2203 }
2204
2205
2206 /**
2207 * Find an ancestor of focused that wants focus after its descendants and is
2208 * focusable in touch mode.
2209 * @param focused The currently focused view.
2210 * @return An appropriate view, or null if no such view exists.
2211 */
2212 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
2213 ViewParent parent = focused.getParent();
2214 while (parent instanceof ViewGroup) {
2215 final ViewGroup vgParent = (ViewGroup) parent;
2216 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
2217 && vgParent.isFocusableInTouchMode()) {
2218 return vgParent;
2219 }
2220 if (vgParent.isRootNamespace()) {
2221 return null;
2222 } else {
2223 parent = vgParent.getParent();
2224 }
2225 }
2226 return null;
2227 }
2228
2229 private boolean leaveTouchMode() {
2230 if (mView != null) {
2231 if (mView.hasFocus()) {
2232 // i learned the hard way to not trust mFocusedView :)
2233 mFocusedView = mView.findFocus();
2234 if (!(mFocusedView instanceof ViewGroup)) {
2235 // some view has focus, let it keep it
2236 return false;
2237 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2238 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2239 // some view group has focus, and doesn't prefer its children
2240 // over itself for focus, so let them keep it.
2241 return false;
2242 }
2243 }
2244
2245 // find the best view to give focus to in this brave new non-touch-mode
2246 // world
2247 final View focused = focusSearch(null, View.FOCUS_DOWN);
2248 if (focused != null) {
2249 return focused.requestFocus(View.FOCUS_DOWN);
2250 }
2251 }
2252 return false;
2253 }
2254
2255
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002256 private void deliverTrackballEvent(MotionEvent event, boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 if (event == null) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002258 event = getPendingTrackballMotionEvent();
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002259 callWhenDone = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 }
2261
2262 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2263
2264 boolean handled = false;
2265 try {
2266 if (event == null) {
2267 handled = true;
2268 } else if (mView != null && mAdded) {
2269 handled = mView.dispatchTrackballEvent(event);
2270 if (!handled) {
2271 // we could do something here, like changing the focus
2272 // or something?
2273 }
2274 }
2275 } finally {
2276 if (handled) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002277 if (callWhenDone) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002278 finishMotionEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002280 recycleMotionEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 // If we reach this, we delivered a trackball event to mView and
2282 // mView consumed it. Because we will not translate the trackball
2283 // event into a key event, touch mode will not exit, so we exit
2284 // touch mode here.
2285 ensureTouchMode(false);
2286 //noinspection ReturnInsideFinallyBlock
2287 return;
2288 }
2289 // Let the exception fall through -- the looper will catch
2290 // it and take care of the bad app for us.
2291 }
2292
2293 final TrackballAxis x = mTrackballAxisX;
2294 final TrackballAxis y = mTrackballAxisY;
2295
2296 long curTime = SystemClock.uptimeMillis();
2297 if ((mLastTrackballTime+MAX_TRACKBALL_DELAY) < curTime) {
2298 // It has been too long since the last movement,
2299 // so restart at the beginning.
2300 x.reset(0);
2301 y.reset(0);
2302 mLastTrackballTime = curTime;
2303 }
2304
2305 try {
2306 final int action = event.getAction();
2307 final int metastate = event.getMetaState();
2308 switch (action) {
2309 case MotionEvent.ACTION_DOWN:
2310 x.reset(2);
2311 y.reset(2);
2312 deliverKeyEvent(new KeyEvent(curTime, curTime,
2313 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER,
2314 0, metastate), false);
2315 break;
2316 case MotionEvent.ACTION_UP:
2317 x.reset(2);
2318 y.reset(2);
2319 deliverKeyEvent(new KeyEvent(curTime, curTime,
2320 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER,
2321 0, metastate), false);
2322 break;
2323 }
2324
2325 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2326 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2327 + " move=" + event.getX()
2328 + " / Y=" + y.position + " step="
2329 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2330 + " move=" + event.getY());
2331 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2332 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
2333
2334 // Generate DPAD events based on the trackball movement.
2335 // We pick the axis that has moved the most as the direction of
2336 // the DPAD. When we generate DPAD events for one axis, then the
2337 // other axis is reset -- we don't want to perform DPAD jumps due
2338 // to slight movements in the trackball when making major movements
2339 // along the other axis.
2340 int keycode = 0;
2341 int movement = 0;
2342 float accel = 1;
2343 if (xOff > yOff) {
2344 movement = x.generate((2/event.getXPrecision()));
2345 if (movement != 0) {
2346 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2347 : KeyEvent.KEYCODE_DPAD_LEFT;
2348 accel = x.acceleration;
2349 y.reset(2);
2350 }
2351 } else if (yOff > 0) {
2352 movement = y.generate((2/event.getYPrecision()));
2353 if (movement != 0) {
2354 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2355 : KeyEvent.KEYCODE_DPAD_UP;
2356 accel = y.acceleration;
2357 x.reset(2);
2358 }
2359 }
2360
2361 if (keycode != 0) {
2362 if (movement < 0) movement = -movement;
2363 int accelMovement = (int)(movement * accel);
2364 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2365 + " accelMovement=" + accelMovement
2366 + " accel=" + accel);
2367 if (accelMovement > movement) {
2368 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2369 + keycode);
2370 movement--;
2371 deliverKeyEvent(new KeyEvent(curTime, curTime,
2372 KeyEvent.ACTION_MULTIPLE, keycode,
2373 accelMovement-movement, metastate), false);
2374 }
2375 while (movement > 0) {
2376 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2377 + keycode);
2378 movement--;
2379 curTime = SystemClock.uptimeMillis();
2380 deliverKeyEvent(new KeyEvent(curTime, curTime,
2381 KeyEvent.ACTION_DOWN, keycode, 0, event.getMetaState()), false);
2382 deliverKeyEvent(new KeyEvent(curTime, curTime,
2383 KeyEvent.ACTION_UP, keycode, 0, metastate), false);
2384 }
2385 mLastTrackballTime = curTime;
2386 }
2387 } finally {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002388 if (callWhenDone) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07002389 finishMotionEvent();
2390 recycleMotionEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002391 }
2392 // Let the exception fall through -- the looper will catch
2393 // it and take care of the bad app for us.
2394 }
2395 }
2396
2397 /**
2398 * @param keyCode The key code
2399 * @return True if the key is directional.
2400 */
2401 static boolean isDirectional(int keyCode) {
2402 switch (keyCode) {
2403 case KeyEvent.KEYCODE_DPAD_LEFT:
2404 case KeyEvent.KEYCODE_DPAD_RIGHT:
2405 case KeyEvent.KEYCODE_DPAD_UP:
2406 case KeyEvent.KEYCODE_DPAD_DOWN:
2407 return true;
2408 }
2409 return false;
2410 }
2411
2412 /**
2413 * Returns true if this key is a keyboard key.
2414 * @param keyEvent The key event.
2415 * @return whether this key is a keyboard key.
2416 */
2417 private static boolean isKeyboardKey(KeyEvent keyEvent) {
2418 final int convertedKey = keyEvent.getUnicodeChar();
2419 return convertedKey > 0;
2420 }
2421
2422
2423
2424 /**
2425 * See if the key event means we should leave touch mode (and leave touch
2426 * mode if so).
2427 * @param event The key event.
2428 * @return Whether this key event should be consumed (meaning the act of
2429 * leaving touch mode alone is considered the event).
2430 */
2431 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Adam Powell51a6bee2010-03-15 14:07:28 -07002432 final int action = event.getAction();
2433 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 return false;
2435 }
2436 if ((event.getFlags()&KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2437 return false;
2438 }
2439
2440 // only relevant if we are in touch mode
2441 if (!mAttachInfo.mInTouchMode) {
2442 return false;
2443 }
2444
2445 // if something like an edit text has focus and the user is typing,
2446 // leave touch mode
2447 //
2448 // note: the condition of not being a keyboard key is kind of a hacky
2449 // approximation of whether we think the focused view will want the
2450 // key; if we knew for sure whether the focused view would consume
2451 // the event, that would be better.
2452 if (isKeyboardKey(event) && mView != null && mView.hasFocus()) {
2453 mFocusedView = mView.findFocus();
2454 if ((mFocusedView instanceof ViewGroup)
2455 && ((ViewGroup) mFocusedView).getDescendantFocusability() ==
2456 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2457 // something has focus, but is holding it weakly as a container
2458 return false;
2459 }
2460 if (ensureTouchMode(false)) {
2461 throw new IllegalStateException("should not have changed focus "
2462 + "when leaving touch mode while a view has focus.");
2463 }
2464 return false;
2465 }
2466
2467 if (isDirectional(event.getKeyCode())) {
2468 // no view has focus, so we leave touch mode (and find something
2469 // to give focus to). the event is consumed if we were able to
2470 // find something to give focus to.
2471 return ensureTouchMode(false);
2472 }
2473 return false;
2474 }
2475
2476 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002477 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 */
2479 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002480 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 if (ev == null ||
2482 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2483 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 }
Romain Guy8506ab42009-06-11 17:35:47 -07002485
2486 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 sb.append(ev.getDownTime()).append(',');
2488 sb.append(ev.getEventTime()).append(',');
2489 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002490 sb.append(ev.getX()).append(',');
2491 sb.append(ev.getY()).append(',');
2492 sb.append(ev.getPressure()).append(',');
2493 sb.append(ev.getSize()).append(',');
2494 sb.append(ev.getMetaState()).append(',');
2495 sb.append(ev.getXPrecision()).append(',');
2496 sb.append(ev.getYPrecision()).append(',');
2497 sb.append(ev.getDeviceId()).append(',');
2498 sb.append(ev.getEdgeFlags());
2499 Log.d(TAG, sb.toString());
2500 }
2501 /**
2502 * log motion events
2503 */
2504 private static void captureKeyLog(String subTag, KeyEvent ev) {
2505 //check dynamic switch
2506 if (ev == null ||
2507 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2508 return;
2509 }
2510 StringBuilder sb = new StringBuilder(subTag + ": ");
2511 sb.append(ev.getDownTime()).append(',');
2512 sb.append(ev.getEventTime()).append(',');
2513 sb.append(ev.getAction()).append(',');
2514 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 sb.append(ev.getRepeatCount()).append(',');
2516 sb.append(ev.getMetaState()).append(',');
2517 sb.append(ev.getDeviceId()).append(',');
2518 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002519 Log.d(TAG, sb.toString());
2520 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521
2522 int enqueuePendingEvent(Object event, boolean sendDone) {
2523 int seq = mPendingEventSeq+1;
2524 if (seq < 0) seq = 0;
2525 mPendingEventSeq = seq;
2526 mPendingEvents.put(seq, event);
2527 return sendDone ? seq : -seq;
2528 }
2529
2530 Object retrievePendingEvent(int seq) {
2531 if (seq < 0) seq = -seq;
2532 Object event = mPendingEvents.get(seq);
2533 if (event != null) {
2534 mPendingEvents.remove(seq);
2535 }
2536 return event;
2537 }
Romain Guy8506ab42009-06-11 17:35:47 -07002538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002539 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
2540 // If mView is null, we just consume the key event because it doesn't
2541 // make sense to do anything else with it.
2542 boolean handled = mView != null
2543 ? mView.dispatchKeyEventPreIme(event) : true;
2544 if (handled) {
2545 if (sendDone) {
2546 if (LOCAL_LOGV) Log.v(
2547 "ViewRoot", "Telling window manager key is finished");
Jeff Brown46b9ac02010-04-22 18:58:52 -07002548 finishKeyEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002549 }
2550 return;
2551 }
2552 // If it is possible for this window to interact with the input
2553 // method window, then we want to first dispatch our key events
2554 // to the input method.
2555 if (mLastWasImTarget) {
2556 InputMethodManager imm = InputMethodManager.peekInstance();
2557 if (imm != null && mView != null) {
2558 int seq = enqueuePendingEvent(event, sendDone);
2559 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2560 + seq + " event=" + event);
2561 imm.dispatchKeyEvent(mView.getContext(), seq, event,
2562 mInputMethodCallback);
2563 return;
2564 }
2565 }
2566 deliverKeyEventToViewHierarchy(event, sendDone);
2567 }
2568
2569 void handleFinishedEvent(int seq, boolean handled) {
2570 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2571 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2572 + " handled=" + handled + " event=" + event);
2573 if (event != null) {
2574 final boolean sendDone = seq >= 0;
2575 if (!handled) {
2576 deliverKeyEventToViewHierarchy(event, sendDone);
2577 return;
2578 } else if (sendDone) {
2579 if (LOCAL_LOGV) Log.v(
2580 "ViewRoot", "Telling window manager key is finished");
Jeff Brown46b9ac02010-04-22 18:58:52 -07002581 finishKeyEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 } else {
2583 Log.w("ViewRoot", "handleFinishedEvent(seq=" + seq
2584 + " handled=" + handled + " ev=" + event
2585 + ") neither delivering nor finishing key");
2586 }
2587 }
2588 }
Romain Guy8506ab42009-06-11 17:35:47 -07002589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002590 private void deliverKeyEventToViewHierarchy(KeyEvent event, boolean sendDone) {
2591 try {
2592 if (mView != null && mAdded) {
2593 final int action = event.getAction();
2594 boolean isDown = (action == KeyEvent.ACTION_DOWN);
2595
2596 if (checkForLeavingTouchModeAndConsume(event)) {
2597 return;
Romain Guy8506ab42009-06-11 17:35:47 -07002598 }
2599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002600 if (Config.LOGV) {
2601 captureKeyLog("captureDispatchKeyEvent", event);
2602 }
2603 boolean keyHandled = mView.dispatchKeyEvent(event);
2604
2605 if (!keyHandled && isDown) {
2606 int direction = 0;
2607 switch (event.getKeyCode()) {
2608 case KeyEvent.KEYCODE_DPAD_LEFT:
2609 direction = View.FOCUS_LEFT;
2610 break;
2611 case KeyEvent.KEYCODE_DPAD_RIGHT:
2612 direction = View.FOCUS_RIGHT;
2613 break;
2614 case KeyEvent.KEYCODE_DPAD_UP:
2615 direction = View.FOCUS_UP;
2616 break;
2617 case KeyEvent.KEYCODE_DPAD_DOWN:
2618 direction = View.FOCUS_DOWN;
2619 break;
2620 }
2621
2622 if (direction != 0) {
2623
2624 View focused = mView != null ? mView.findFocus() : null;
2625 if (focused != null) {
2626 View v = focused.focusSearch(direction);
2627 boolean focusPassed = false;
2628 if (v != null && v != focused) {
2629 // do the math the get the interesting rect
2630 // of previous focused into the coord system of
2631 // newly focused view
2632 focused.getFocusedRect(mTempRect);
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07002633 if (mView instanceof ViewGroup) {
2634 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2635 focused, mTempRect);
2636 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2637 v, mTempRect);
2638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002639 focusPassed = v.requestFocus(direction, mTempRect);
2640 }
2641
2642 if (!focusPassed) {
2643 mView.dispatchUnhandledMove(focused, direction);
2644 } else {
2645 playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
2646 }
2647 }
2648 }
2649 }
2650 }
2651
2652 } finally {
2653 if (sendDone) {
2654 if (LOCAL_LOGV) Log.v(
2655 "ViewRoot", "Telling window manager key is finished");
Jeff Brown46b9ac02010-04-22 18:58:52 -07002656 finishKeyEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002657 }
2658 // Let the exception fall through -- the looper will catch
2659 // it and take care of the bad app for us.
2660 }
2661 }
2662
2663 private AudioManager getAudioManager() {
2664 if (mView == null) {
2665 throw new IllegalStateException("getAudioManager called when there is no mView");
2666 }
2667 if (mAudioManager == null) {
2668 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2669 }
2670 return mAudioManager;
2671 }
2672
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002673 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2674 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002675
2676 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002677 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002678 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002679 restore = true;
2680 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002681 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002682 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002683 if (params != null) {
2684 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002685 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002686 mPendingConfiguration.seq = 0;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002687 int relayoutResult = sWindowSession.relayout(
2688 mWindow, params,
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002689 (int) (mView.mMeasuredWidth * appScale + 0.5f),
2690 (int) (mView.mMeasuredHeight * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002691 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002692 mPendingContentInsets, mPendingVisibleInsets,
2693 mPendingConfiguration, mSurface);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002694 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002695 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002696 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002697
2698 if (mTranslator != null) {
2699 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2700 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2701 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002702 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002703 return relayoutResult;
2704 }
Romain Guy8506ab42009-06-11 17:35:47 -07002705
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002707 * {@inheritDoc}
2708 */
2709 public void playSoundEffect(int effectId) {
2710 checkThread();
2711
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002712 try {
2713 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002715 switch (effectId) {
2716 case SoundEffectConstants.CLICK:
2717 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2718 return;
2719 case SoundEffectConstants.NAVIGATION_DOWN:
2720 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2721 return;
2722 case SoundEffectConstants.NAVIGATION_LEFT:
2723 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2724 return;
2725 case SoundEffectConstants.NAVIGATION_RIGHT:
2726 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2727 return;
2728 case SoundEffectConstants.NAVIGATION_UP:
2729 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2730 return;
2731 default:
2732 throw new IllegalArgumentException("unknown effect id " + effectId +
2733 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2734 }
2735 } catch (IllegalStateException e) {
2736 // Exception thrown by getAudioManager() when mView is null
2737 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2738 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 }
2740 }
2741
2742 /**
2743 * {@inheritDoc}
2744 */
2745 public boolean performHapticFeedback(int effectId, boolean always) {
2746 try {
2747 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2748 } catch (RemoteException e) {
2749 return false;
2750 }
2751 }
2752
2753 /**
2754 * {@inheritDoc}
2755 */
2756 public View focusSearch(View focused, int direction) {
2757 checkThread();
2758 if (!(mView instanceof ViewGroup)) {
2759 return null;
2760 }
2761 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2762 }
2763
2764 public void debug() {
2765 mView.debug();
2766 }
2767
2768 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002769 if (immediate) {
2770 doDie();
2771 } else {
2772 sendEmptyMessage(DIE);
2773 }
2774 }
2775
2776 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 checkThread();
2778 if (Config.LOGV) Log.v("ViewRoot", "DIE in " + this + " of " + mSurface);
2779 synchronized (this) {
2780 if (mAdded && !mFirst) {
2781 int viewVisibility = mView.getVisibility();
2782 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2783 if (mWindowAttributesChanged || viewVisibilityChanged) {
2784 // If layout params have been changed, first give them
2785 // to the window manager to make sure it has the correct
2786 // animation info.
2787 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002788 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2789 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 sWindowSession.finishDrawing(mWindow);
2791 }
2792 } catch (RemoteException e) {
2793 }
2794 }
2795
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002796 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002797 }
2798 if (mAdded) {
2799 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002800 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002801 }
2802 }
2803 }
2804
2805 public void dispatchFinishedEvent(int seq, boolean handled) {
2806 Message msg = obtainMessage(FINISHED_EVENT);
2807 msg.arg1 = seq;
2808 msg.arg2 = handled ? 1 : 0;
2809 sendMessage(msg);
2810 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002812 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002813 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2815 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2816 + " visibleInsets=" + visibleInsets.toShortString()
2817 + " reportDraw=" + reportDraw);
2818 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002819 if (mTranslator != null) {
2820 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2821 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2822 w *= mTranslator.applicationInvertedScale;
2823 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002824 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002825 msg.arg1 = w;
2826 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002827 ResizedInfo ri = new ResizedInfo();
2828 ri.coveredInsets = new Rect(coveredInsets);
2829 ri.visibleInsets = new Rect(visibleInsets);
2830 ri.newConfig = newConfig;
2831 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 sendMessage(msg);
2833 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002834
2835 private Runnable mFinishedCallback;
2836
2837 private final InputHandler mInputHandler = new InputHandler() {
2838 public void handleKey(KeyEvent event, Runnable finishedCallback) {
2839 mFinishedCallback = finishedCallback;
2840
2841 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2842 //noinspection ConstantConditions
2843 if (false && event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
2844 if (Config.LOGD) Log.d("keydisp",
2845 "===================================================");
2846 if (Config.LOGD) Log.d("keydisp", "Focused view Hierarchy is:");
2847 debug();
2848
2849 if (Config.LOGD) Log.d("keydisp",
2850 "===================================================");
2851 }
2852 }
2853
2854 Message msg = obtainMessage(DISPATCH_KEY);
2855 msg.obj = event;
2856
2857 if (LOCAL_LOGV) Log.v(
2858 "ViewRoot", "sending key " + event + " to " + mView);
2859
2860 sendMessageAtTime(msg, event.getEventTime());
2861 }
2862
2863 public void handleTouch(MotionEvent event, Runnable finishedCallback) {
2864 finishedCallback.run();
2865
2866 Message msg = obtainMessage(DISPATCH_POINTER);
2867 msg.obj = event;
2868 msg.arg1 = 0;
2869 sendMessageAtTime(msg, event.getEventTime());
2870 }
2871
2872 public void handleTrackball(MotionEvent event, Runnable finishedCallback) {
2873 finishedCallback.run();
2874
2875 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2876 msg.obj = event;
2877 msg.arg1 = 0;
2878 sendMessageAtTime(msg, event.getEventTime());
2879 }
2880 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002881
2882 public void dispatchKey(KeyEvent event) {
2883 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2884 //noinspection ConstantConditions
2885 if (false && event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
2886 if (Config.LOGD) Log.d("keydisp",
2887 "===================================================");
2888 if (Config.LOGD) Log.d("keydisp", "Focused view Hierarchy is:");
2889 debug();
2890
2891 if (Config.LOGD) Log.d("keydisp",
2892 "===================================================");
2893 }
2894 }
2895
2896 Message msg = obtainMessage(DISPATCH_KEY);
2897 msg.obj = event;
2898
2899 if (LOCAL_LOGV) Log.v(
2900 "ViewRoot", "sending key " + event + " to " + mView);
2901
2902 sendMessageAtTime(msg, event.getEventTime());
2903 }
2904
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002905 public void dispatchPointer(MotionEvent event, long eventTime,
2906 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002907 Message msg = obtainMessage(DISPATCH_POINTER);
2908 msg.obj = event;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002909 msg.arg1 = callWhenDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 sendMessageAtTime(msg, eventTime);
2911 }
2912
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002913 public void dispatchTrackball(MotionEvent event, long eventTime,
2914 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002915 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2916 msg.obj = event;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07002917 msg.arg1 = callWhenDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002918 sendMessageAtTime(msg, eventTime);
2919 }
2920
2921 public void dispatchAppVisibility(boolean visible) {
2922 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
2923 msg.arg1 = visible ? 1 : 0;
2924 sendMessage(msg);
2925 }
2926
2927 public void dispatchGetNewSurface() {
2928 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
2929 sendMessage(msg);
2930 }
2931
2932 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2933 Message msg = Message.obtain();
2934 msg.what = WINDOW_FOCUS_CHANGED;
2935 msg.arg1 = hasFocus ? 1 : 0;
2936 msg.arg2 = inTouchMode ? 1 : 0;
2937 sendMessage(msg);
2938 }
2939
Dianne Hackbornffa42482009-09-23 22:20:11 -07002940 public void dispatchCloseSystemDialogs(String reason) {
2941 Message msg = Message.obtain();
2942 msg.what = CLOSE_SYSTEM_DIALOGS;
2943 msg.obj = reason;
2944 sendMessage(msg);
2945 }
2946
svetoslavganov75986cf2009-05-14 22:28:01 -07002947 /**
2948 * The window is getting focus so if there is anything focused/selected
2949 * send an {@link AccessibilityEvent} to announce that.
2950 */
2951 private void sendAccessibilityEvents() {
2952 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
2953 return;
2954 }
2955 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2956 View focusedView = mView.findFocus();
2957 if (focusedView != null && focusedView != mView) {
2958 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2959 }
2960 }
2961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 public boolean showContextMenuForChild(View originalView) {
2963 return false;
2964 }
2965
2966 public void createContextMenu(ContextMenu menu) {
2967 }
2968
2969 public void childDrawableStateChanged(View child) {
2970 }
2971
2972 protected Rect getWindowFrame() {
2973 return mWinFrame;
2974 }
2975
2976 void checkThread() {
2977 if (mThread != Thread.currentThread()) {
2978 throw new CalledFromWrongThreadException(
2979 "Only the original thread that created a view hierarchy can touch its views.");
2980 }
2981 }
2982
2983 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2984 // ViewRoot never intercepts touch event, so this can be a no-op
2985 }
2986
2987 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
2988 boolean immediate) {
2989 return scrollToRectOrFocus(rectangle, immediate);
2990 }
Romain Guy8506ab42009-06-11 17:35:47 -07002991
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002992 class TakenSurfaceHolder extends BaseSurfaceHolder {
2993 @Override
2994 public boolean onAllowLockCanvas() {
2995 return mDrawingAllowed;
2996 }
2997
2998 @Override
2999 public void onRelayoutContainer() {
3000 // Not currently interesting -- from changing between fixed and layout size.
3001 }
3002
3003 public void setFormat(int format) {
3004 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
3005 }
3006
3007 public void setType(int type) {
3008 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
3009 }
3010
3011 @Override
3012 public void onUpdateSurface() {
3013 // We take care of format and type changes on our own.
3014 throw new IllegalStateException("Shouldn't be here");
3015 }
3016
3017 public boolean isCreating() {
3018 return mIsCreating;
3019 }
3020
3021 @Override
3022 public void setFixedSize(int width, int height) {
3023 throw new UnsupportedOperationException(
3024 "Currently only support sizing from layout");
3025 }
3026
3027 public void setKeepScreenOn(boolean screenOn) {
3028 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
3029 }
3030 }
3031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 static class InputMethodCallback extends IInputMethodCallback.Stub {
3033 private WeakReference<ViewRoot> mViewRoot;
3034
3035 public InputMethodCallback(ViewRoot viewRoot) {
3036 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
3037 }
Romain Guy8506ab42009-06-11 17:35:47 -07003038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 public void finishedEvent(int seq, boolean handled) {
3040 final ViewRoot viewRoot = mViewRoot.get();
3041 if (viewRoot != null) {
3042 viewRoot.dispatchFinishedEvent(seq, handled);
3043 }
3044 }
3045
3046 public void sessionCreated(IInputMethodSession session) throws RemoteException {
3047 // Stub -- not for use in the client.
3048 }
3049 }
Romain Guy8506ab42009-06-11 17:35:47 -07003050
Jeff Brown46b9ac02010-04-22 18:58:52 -07003051 class EventCompletion extends Handler {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003052 final IWindow mWindow;
3053 final KeyEvent mKeyEvent;
3054 final boolean mIsPointer;
3055 final MotionEvent mMotionEvent;
Romain Guy8506ab42009-06-11 17:35:47 -07003056
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003057 EventCompletion(Looper looper, IWindow window, KeyEvent key,
3058 boolean isPointer, MotionEvent motion) {
3059 super(looper);
3060 mWindow = window;
3061 mKeyEvent = key;
3062 mIsPointer = isPointer;
3063 mMotionEvent = motion;
3064 sendEmptyMessage(0);
3065 }
Romain Guy8506ab42009-06-11 17:35:47 -07003066
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003067 @Override
3068 public void handleMessage(Message msg) {
3069 if (mKeyEvent != null) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003070 finishKeyEvent(mKeyEvent);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07003071 } else if (mIsPointer) {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003072 boolean didFinish;
3073 MotionEvent event = mMotionEvent;
3074 if (event == null) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003075 event = getPendingPointerMotionEvent();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003076 didFinish = true;
3077 } else {
3078 didFinish = event.getAction() == MotionEvent.ACTION_OUTSIDE;
3079 }
3080 if (!didFinish) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003081 finishMotionEvent();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003082 }
3083 } else {
3084 MotionEvent event = mMotionEvent;
3085 if (event == null) {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003086 event = getPendingTrackballMotionEvent();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003087 } else {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003088 finishMotionEvent();
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003089 }
3090 }
3091 }
3092 }
Romain Guy8506ab42009-06-11 17:35:47 -07003093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003095 private final WeakReference<ViewRoot> mViewRoot;
3096 private final Looper mMainLooper;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003098 public W(ViewRoot viewRoot, Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003099 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003100 mMainLooper = context.getMainLooper();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003101 }
3102
3103 public void resized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003104 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003105 final ViewRoot viewRoot = mViewRoot.get();
3106 if (viewRoot != null) {
3107 viewRoot.dispatchResized(w, h, coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003108 visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 }
3110 }
3111
3112 public void dispatchKey(KeyEvent event) {
3113 final ViewRoot viewRoot = mViewRoot.get();
3114 if (viewRoot != null) {
3115 viewRoot.dispatchKey(event);
3116 } else {
3117 Log.w("ViewRoot.W", "Key event " + event + " but no ViewRoot available!");
Jeff Brown46b9ac02010-04-22 18:58:52 -07003118 viewRoot.new EventCompletion(mMainLooper, this, event, false, null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 }
3120 }
3121
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07003122 public void dispatchPointer(MotionEvent event, long eventTime,
3123 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 final ViewRoot viewRoot = mViewRoot.get();
Michael Chan53071d62009-05-13 17:29:48 -07003125 if (viewRoot != null) {
3126 if (MEASURE_LATENCY) {
3127 // Note: eventTime is in milliseconds
3128 ViewRoot.lt.sample("* ViewRoot b4 dispatchPtr", System.nanoTime() - eventTime * 1000000);
3129 }
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07003130 viewRoot.dispatchPointer(event, eventTime, callWhenDone);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003131 } else {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003132 viewRoot.new EventCompletion(mMainLooper, this, null, true, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003133 }
3134 }
3135
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07003136 public void dispatchTrackball(MotionEvent event, long eventTime,
3137 boolean callWhenDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 final ViewRoot viewRoot = mViewRoot.get();
3139 if (viewRoot != null) {
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -07003140 viewRoot.dispatchTrackball(event, eventTime, callWhenDone);
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003141 } else {
Jeff Brown46b9ac02010-04-22 18:58:52 -07003142 viewRoot.new EventCompletion(mMainLooper, this, null, false, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003143 }
3144 }
3145
3146 public void dispatchAppVisibility(boolean visible) {
3147 final ViewRoot viewRoot = mViewRoot.get();
3148 if (viewRoot != null) {
3149 viewRoot.dispatchAppVisibility(visible);
3150 }
3151 }
3152
3153 public void dispatchGetNewSurface() {
3154 final ViewRoot viewRoot = mViewRoot.get();
3155 if (viewRoot != null) {
3156 viewRoot.dispatchGetNewSurface();
3157 }
3158 }
3159
3160 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3161 final ViewRoot viewRoot = mViewRoot.get();
3162 if (viewRoot != null) {
3163 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
3164 }
3165 }
3166
3167 private static int checkCallingPermission(String permission) {
3168 if (!Process.supportsProcesses()) {
3169 return PackageManager.PERMISSION_GRANTED;
3170 }
3171
3172 try {
3173 return ActivityManagerNative.getDefault().checkPermission(
3174 permission, Binder.getCallingPid(), Binder.getCallingUid());
3175 } catch (RemoteException e) {
3176 return PackageManager.PERMISSION_DENIED;
3177 }
3178 }
3179
3180 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3181 final ViewRoot viewRoot = mViewRoot.get();
3182 if (viewRoot != null) {
3183 final View view = viewRoot.mView;
3184 if (view != null) {
3185 if (checkCallingPermission(Manifest.permission.DUMP) !=
3186 PackageManager.PERMISSION_GRANTED) {
3187 throw new SecurityException("Insufficient permissions to invoke"
3188 + " executeCommand() from pid=" + Binder.getCallingPid()
3189 + ", uid=" + Binder.getCallingUid());
3190 }
3191
3192 OutputStream clientStream = null;
3193 try {
3194 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3195 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3196 } catch (IOException e) {
3197 e.printStackTrace();
3198 } finally {
3199 if (clientStream != null) {
3200 try {
3201 clientStream.close();
3202 } catch (IOException e) {
3203 e.printStackTrace();
3204 }
3205 }
3206 }
3207 }
3208 }
3209 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003210
Dianne Hackbornffa42482009-09-23 22:20:11 -07003211 public void closeSystemDialogs(String reason) {
3212 final ViewRoot viewRoot = mViewRoot.get();
3213 if (viewRoot != null) {
3214 viewRoot.dispatchCloseSystemDialogs(reason);
3215 }
3216 }
3217
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003218 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3219 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003220 if (sync) {
3221 try {
3222 sWindowSession.wallpaperOffsetsComplete(asBinder());
3223 } catch (RemoteException e) {
3224 }
3225 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003226 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003227
3228 public void dispatchWallpaperCommand(String action, int x, int y,
3229 int z, Bundle extras, boolean sync) {
3230 if (sync) {
3231 try {
3232 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3233 } catch (RemoteException e) {
3234 }
3235 }
3236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 }
3238
3239 /**
3240 * Maintains state information for a single trackball axis, generating
3241 * discrete (DPAD) movements based on raw trackball motion.
3242 */
3243 static final class TrackballAxis {
3244 /**
3245 * The maximum amount of acceleration we will apply.
3246 */
3247 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003249 /**
3250 * The maximum amount of time (in milliseconds) between events in order
3251 * for us to consider the user to be doing fast trackball movements,
3252 * and thus apply an acceleration.
3253 */
3254 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003256 /**
3257 * Scaling factor to the time (in milliseconds) between events to how
3258 * much to multiple/divide the current acceleration. When movement
3259 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3260 * FAST_MOVE_TIME it divides it.
3261 */
3262 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003264 float position;
3265 float absPosition;
3266 float acceleration = 1;
3267 long lastMoveTime = 0;
3268 int step;
3269 int dir;
3270 int nonAccelMovement;
3271
3272 void reset(int _step) {
3273 position = 0;
3274 acceleration = 1;
3275 lastMoveTime = 0;
3276 step = _step;
3277 dir = 0;
3278 }
3279
3280 /**
3281 * Add trackball movement into the state. If the direction of movement
3282 * has been reversed, the state is reset before adding the
3283 * movement (so that you don't have to compensate for any previously
3284 * collected movement before see the result of the movement in the
3285 * new direction).
3286 *
3287 * @return Returns the absolute value of the amount of movement
3288 * collected so far.
3289 */
3290 float collect(float off, long time, String axis) {
3291 long normTime;
3292 if (off > 0) {
3293 normTime = (long)(off * FAST_MOVE_TIME);
3294 if (dir < 0) {
3295 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3296 position = 0;
3297 step = 0;
3298 acceleration = 1;
3299 lastMoveTime = 0;
3300 }
3301 dir = 1;
3302 } else if (off < 0) {
3303 normTime = (long)((-off) * FAST_MOVE_TIME);
3304 if (dir > 0) {
3305 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3306 position = 0;
3307 step = 0;
3308 acceleration = 1;
3309 lastMoveTime = 0;
3310 }
3311 dir = -1;
3312 } else {
3313 normTime = 0;
3314 }
Romain Guy8506ab42009-06-11 17:35:47 -07003315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003316 // The number of milliseconds between each movement that is
3317 // considered "normal" and will not result in any acceleration
3318 // or deceleration, scaled by the offset we have here.
3319 if (normTime > 0) {
3320 long delta = time - lastMoveTime;
3321 lastMoveTime = time;
3322 float acc = acceleration;
3323 if (delta < normTime) {
3324 // The user is scrolling rapidly, so increase acceleration.
3325 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3326 if (scale > 1) acc *= scale;
3327 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3328 + off + " normTime=" + normTime + " delta=" + delta
3329 + " scale=" + scale + " acc=" + acc);
3330 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3331 } else {
3332 // The user is scrolling slowly, so decrease acceleration.
3333 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3334 if (scale > 1) acc /= scale;
3335 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3336 + off + " normTime=" + normTime + " delta=" + delta
3337 + " scale=" + scale + " acc=" + acc);
3338 acceleration = acc > 1 ? acc : 1;
3339 }
3340 }
3341 position += off;
3342 return (absPosition = Math.abs(position));
3343 }
3344
3345 /**
3346 * Generate the number of discrete movement events appropriate for
3347 * the currently collected trackball movement.
3348 *
3349 * @param precision The minimum movement required to generate the
3350 * first discrete movement.
3351 *
3352 * @return Returns the number of discrete movements, either positive
3353 * or negative, or 0 if there is not enough trackball movement yet
3354 * for a discrete movement.
3355 */
3356 int generate(float precision) {
3357 int movement = 0;
3358 nonAccelMovement = 0;
3359 do {
3360 final int dir = position >= 0 ? 1 : -1;
3361 switch (step) {
3362 // If we are going to execute the first step, then we want
3363 // to do this as soon as possible instead of waiting for
3364 // a full movement, in order to make things look responsive.
3365 case 0:
3366 if (absPosition < precision) {
3367 return movement;
3368 }
3369 movement += dir;
3370 nonAccelMovement += dir;
3371 step = 1;
3372 break;
3373 // If we have generated the first movement, then we need
3374 // to wait for the second complete trackball motion before
3375 // generating the second discrete movement.
3376 case 1:
3377 if (absPosition < 2) {
3378 return movement;
3379 }
3380 movement += dir;
3381 nonAccelMovement += dir;
3382 position += dir > 0 ? -2 : 2;
3383 absPosition = Math.abs(position);
3384 step = 2;
3385 break;
3386 // After the first two, we generate discrete movements
3387 // consistently with the trackball, applying an acceleration
3388 // if the trackball is moving quickly. This is a simple
3389 // acceleration on top of what we already compute based
3390 // on how quickly the wheel is being turned, to apply
3391 // a longer increasing acceleration to continuous movement
3392 // in one direction.
3393 default:
3394 if (absPosition < 1) {
3395 return movement;
3396 }
3397 movement += dir;
3398 position += dir >= 0 ? -1 : 1;
3399 absPosition = Math.abs(position);
3400 float acc = acceleration;
3401 acc *= 1.1f;
3402 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3403 break;
3404 }
3405 } while (true);
3406 }
3407 }
3408
3409 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3410 public CalledFromWrongThreadException(String msg) {
3411 super(msg);
3412 }
3413 }
3414
3415 private SurfaceHolder mHolder = new SurfaceHolder() {
3416 // we only need a SurfaceHolder for opengl. it would be nice
3417 // to implement everything else though, especially the callback
3418 // support (opengl doesn't make use of it right now, but eventually
3419 // will).
3420 public Surface getSurface() {
3421 return mSurface;
3422 }
3423
3424 public boolean isCreating() {
3425 return false;
3426 }
3427
3428 public void addCallback(Callback callback) {
3429 }
3430
3431 public void removeCallback(Callback callback) {
3432 }
3433
3434 public void setFixedSize(int width, int height) {
3435 }
3436
3437 public void setSizeFromLayout() {
3438 }
3439
3440 public void setFormat(int format) {
3441 }
3442
3443 public void setType(int type) {
3444 }
3445
3446 public void setKeepScreenOn(boolean screenOn) {
3447 }
3448
3449 public Canvas lockCanvas() {
3450 return null;
3451 }
3452
3453 public Canvas lockCanvas(Rect dirty) {
3454 return null;
3455 }
3456
3457 public void unlockCanvasAndPost(Canvas canvas) {
3458 }
3459 public Rect getSurfaceFrame() {
3460 return null;
3461 }
3462 };
3463
3464 static RunQueue getRunQueue() {
3465 RunQueue rq = sRunQueues.get();
3466 if (rq != null) {
3467 return rq;
3468 }
3469 rq = new RunQueue();
3470 sRunQueues.set(rq);
3471 return rq;
3472 }
Romain Guy8506ab42009-06-11 17:35:47 -07003473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003474 /**
3475 * @hide
3476 */
3477 static final class RunQueue {
3478 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3479
3480 void post(Runnable action) {
3481 postDelayed(action, 0);
3482 }
3483
3484 void postDelayed(Runnable action, long delayMillis) {
3485 HandlerAction handlerAction = new HandlerAction();
3486 handlerAction.action = action;
3487 handlerAction.delay = delayMillis;
3488
3489 synchronized (mActions) {
3490 mActions.add(handlerAction);
3491 }
3492 }
3493
3494 void removeCallbacks(Runnable action) {
3495 final HandlerAction handlerAction = new HandlerAction();
3496 handlerAction.action = action;
3497
3498 synchronized (mActions) {
3499 final ArrayList<HandlerAction> actions = mActions;
3500
3501 while (actions.remove(handlerAction)) {
3502 // Keep going
3503 }
3504 }
3505 }
3506
3507 void executeActions(Handler handler) {
3508 synchronized (mActions) {
3509 final ArrayList<HandlerAction> actions = mActions;
3510 final int count = actions.size();
3511
3512 for (int i = 0; i < count; i++) {
3513 final HandlerAction handlerAction = actions.get(i);
3514 handler.postDelayed(handlerAction.action, handlerAction.delay);
3515 }
3516
Romain Guy15df6702009-08-17 20:17:30 -07003517 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 }
3519 }
3520
3521 private static class HandlerAction {
3522 Runnable action;
3523 long delay;
3524
3525 @Override
3526 public boolean equals(Object o) {
3527 if (this == o) return true;
3528 if (o == null || getClass() != o.getClass()) return false;
3529
3530 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003531 return !(action != null ? !action.equals(that.action) : that.action != null);
3532
3533 }
3534
3535 @Override
3536 public int hashCode() {
3537 int result = action != null ? action.hashCode() : 0;
3538 result = 31 * result + (int) (delay ^ (delay >>> 32));
3539 return result;
3540 }
3541 }
3542 }
3543
3544 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
3545
3546 // inform skia to just abandon its texture cache IDs
3547 // doesn't call glDeleteTextures
3548 private static native void nativeAbandonGlCaches();
3549}