blob: 7907ad2cb25fb1cb4930d1bae72a076da34d7ed8 [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
Romain Guy6b7bd242010-10-06 19:49:23 -070019import android.Manifest;
20import android.app.ActivityManagerNative;
21import android.content.ClipDescription;
22import android.content.ComponentCallbacks;
23import android.content.Context;
24import android.content.pm.PackageManager;
25import android.content.res.CompatibilityInfo;
26import android.content.res.Configuration;
27import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.Canvas;
29import android.graphics.PixelFormat;
Christopher Tate2c095f32010-10-04 14:13:40 -070030import android.graphics.Point;
Christopher Tatea53146c2010-09-07 11:57:52 -070031import android.graphics.PointF;
Romain Guy6b7bd242010-10-06 19:49:23 -070032import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Rect;
34import android.graphics.Region;
Romain Guy6b7bd242010-10-06 19:49:23 -070035import android.media.AudioManager;
36import android.os.Binder;
37import android.os.Bundle;
38import android.os.Debug;
39import android.os.Handler;
40import android.os.LatencyTimer;
41import android.os.Looper;
42import android.os.Message;
43import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Process;
Romain Guy6b7bd242010-10-06 19:49:23 -070045import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.SystemClock;
48import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AndroidRuntimeException;
50import android.util.Config;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070051import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.EventLog;
Romain Guy6b7bd242010-10-06 19:49:23 -070053import android.util.Log;
Chet Haase949dbf72010-08-11 18:41:06 -070054import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.view.accessibility.AccessibilityEvent;
58import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.inputmethod.InputConnection;
60import android.view.inputmethod.InputMethodManager;
61import android.widget.Scroller;
Romain Guy6b7bd242010-10-06 19:49:23 -070062import com.android.internal.view.BaseSurfaceHolder;
63import com.android.internal.view.IInputMethodCallback;
64import com.android.internal.view.IInputMethodSession;
65import com.android.internal.view.RootViewSurfaceTaker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import java.io.IOException;
68import java.io.OutputStream;
Romain Guy6b7bd242010-10-06 19:49:23 -070069import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import java.util.ArrayList;
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072/**
73 * The top of a view hierarchy, implementing the needed protocol between View
74 * and the WindowManager. This is for the most part an internal implementation
75 * detail of {@link WindowManagerImpl}.
76 *
77 * {@hide}
78 */
Romain Guy812ccbe2010-06-01 14:07:24 -070079@SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"})
80public final class ViewRoot extends Handler implements ViewParent, View.AttachInfo.Callbacks {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 private static final String TAG = "ViewRoot";
82 private static final boolean DBG = false;
Mike Reedfd716532009-10-12 14:42:56 -040083 private static final boolean SHOW_FPS = false;
Romain Guy812ccbe2010-06-01 14:07:24 -070084 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /** @noinspection PointlessBooleanExpression*/
86 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
87 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
Christopher Tatefa9e7c02010-05-06 12:07:10 -070088 private static final boolean DEBUG_INPUT = true || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
90 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
91 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
92 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
Dianne Hackborn694f79b2010-03-17 19:44:59 -070093 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private static final boolean WATCH_POINTER = false;
95
Michael Chan53071d62009-05-13 17:29:48 -070096 private static final boolean MEASURE_LATENCY = false;
97 private static LatencyTimer lt;
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
100 * Maximum time we allow the user to roll the trackball enough to generate
101 * a key event, before resetting the counters.
102 */
103 static final int MAX_TRACKBALL_DELAY = 250;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 static IWindowSession sWindowSession;
106
107 static final Object mStaticInit = new Object();
108 static boolean mInitialized = false;
109
110 static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>();
111
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800112 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList<Runnable>();
113 static boolean sFirstDrawComplete = false;
114
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800115 static final ArrayList<ComponentCallbacks> sConfigCallbacks
116 = new ArrayList<ComponentCallbacks>();
117
Romain Guy8506ab42009-06-11 17:35:47 -0700118 private static int sDrawTime;
Romain Guy13922e02009-05-12 17:56:14 -0700119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 long mLastTrackballTime = 0;
121 final TrackballAxis mTrackballAxisX = new TrackballAxis();
122 final TrackballAxis mTrackballAxisY = new TrackballAxis();
123
124 final int[] mTmpLocation = new int[2];
Romain Guy8506ab42009-06-11 17:35:47 -0700125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final InputMethodCallback mInputMethodCallback;
127 final SparseArray<Object> mPendingEvents = new SparseArray<Object>();
128 int mPendingEventSeq = 0;
Romain Guy8506ab42009-06-11 17:35:47 -0700129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final Thread mThread;
131
132 final WindowLeaked mLocation;
133
134 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
135
136 final W mWindow;
137
138 View mView;
139 View mFocusedView;
140 View mRealFocusedView; // this is not set to null in touch mode
141 int mViewVisibility;
142 boolean mAppVisible = true;
143
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700144 SurfaceHolder.Callback2 mSurfaceHolderCallback;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700145 BaseSurfaceHolder mSurfaceHolder;
146 boolean mIsCreating;
147 boolean mDrawingAllowed;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final Region mTransparentRegion;
150 final Region mPreviousTransparentRegion;
151
152 int mWidth;
153 int mHeight;
154 Rect mDirty; // will be a graphics.Region soon
Romain Guybb93d552009-03-24 21:04:15 -0700155 boolean mIsAnimating;
Romain Guy8506ab42009-06-11 17:35:47 -0700156
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700157 CompatibilityInfo.Translator mTranslator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 final View.AttachInfo mAttachInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700160 InputChannel mInputChannel;
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700161 InputQueue.Callback mInputQueueCallback;
162 InputQueue mInputQueue;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final Rect mTempRect; // used in the transaction to not thrash the heap.
165 final Rect mVisRect; // used to retrieve visible rect of focused view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
167 boolean mTraversalScheduled;
168 boolean mWillDrawSoon;
169 boolean mLayoutRequested;
170 boolean mFirst;
171 boolean mReportNextDraw;
172 boolean mFullRedrawNeeded;
173 boolean mNewSurfaceNeeded;
174 boolean mHasHadWindowFocus;
175 boolean mLastWasImTarget;
176
177 boolean mWindowAttributesChanged = false;
178
179 // These can be accessed by any thread, must be protected with a lock.
Mathias Agopian5583dc62009-07-09 16:28:11 -0700180 // Surface can never be reassigned or cleared (use Surface.clear()).
181 private final Surface mSurface = new Surface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
183 boolean mAdded;
184 boolean mAddedTouchMode;
185
186 /*package*/ int mAddNesting;
187
188 // These are accessed by multiple threads.
189 final Rect mWinFrame; // frame given by window manager.
190
191 final Rect mPendingVisibleInsets = new Rect();
192 final Rect mPendingContentInsets = new Rect();
193 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
194 = new ViewTreeObserver.InternalInsetsInfo();
195
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700196 final Configuration mLastConfiguration = new Configuration();
197 final Configuration mPendingConfiguration = new Configuration();
198
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800199 class ResizedInfo {
200 Rect coveredInsets;
201 Rect visibleInsets;
202 Configuration newConfig;
203 }
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 boolean mScrollMayChange;
206 int mSoftInputMode;
207 View mLastScrolledFocus;
208 int mScrollY;
209 int mCurScrollY;
210 Scroller mScroller;
Romain Guy8506ab42009-06-11 17:35:47 -0700211
Romain Guy8506ab42009-06-11 17:35:47 -0700212 final ViewConfiguration mViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Christopher Tatea53146c2010-09-07 11:57:52 -0700214 /* Drag/drop */
215 ClipDescription mDragDescription;
216 View mCurrentDragView;
217 final PointF mDragPoint = new PointF();
Christopher Tate2c095f32010-10-04 14:13:40 -0700218 final PointF mLastTouchPoint = new PointF();
Christopher Tatea53146c2010-09-07 11:57:52 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
221 * see {@link #playSoundEffect(int)}
222 */
223 AudioManager mAudioManager;
224
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700225 private final int mDensity;
Adam Powellb08013c2010-09-16 16:28:11 -0700226
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700227 public static IWindowSession getWindowSession(Looper mainLooper) {
228 synchronized (mStaticInit) {
229 if (!mInitialized) {
230 try {
231 InputMethodManager imm = InputMethodManager.getInstance(mainLooper);
232 sWindowSession = IWindowManager.Stub.asInterface(
233 ServiceManager.getService("window"))
234 .openSession(imm.getClient(), imm.getInputContext());
235 mInitialized = true;
236 } catch (RemoteException e) {
237 }
238 }
239 return sWindowSession;
240 }
241 }
242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 public ViewRoot(Context context) {
244 super();
245
Romain Guy812ccbe2010-06-01 14:07:24 -0700246 if (MEASURE_LATENCY) {
247 if (lt == null) {
248 lt = new LatencyTimer(100, 1000);
249 }
Michael Chan53071d62009-05-13 17:29:48 -0700250 }
251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 // Initialize the statics when this class is first instantiated. This is
253 // done here instead of in the static block because Zygote does not
254 // allow the spawning of threads.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700255 getWindowSession(context.getMainLooper());
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 mThread = Thread.currentThread();
258 mLocation = new WindowLeaked(null);
259 mLocation.fillInStackTrace();
260 mWidth = -1;
261 mHeight = -1;
262 mDirty = new Rect();
263 mTempRect = new Rect();
264 mVisRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mWinFrame = new Rect();
Romain Guyfb8b7632010-08-23 21:05:08 -0700266 mWindow = new W(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 mInputMethodCallback = new InputMethodCallback(this);
268 mViewVisibility = View.GONE;
269 mTransparentRegion = new Region();
270 mPreviousTransparentRegion = new Region();
271 mFirst = true; // true for the first time the view is added
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mAdded = false;
273 mAttachInfo = new View.AttachInfo(sWindowSession, mWindow, this, this);
274 mViewConfiguration = ViewConfiguration.get(context);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700275 mDensity = context.getResources().getDisplayMetrics().densityDpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800278 public static void addFirstDrawHandler(Runnable callback) {
279 synchronized (sFirstDrawHandlers) {
280 if (!sFirstDrawComplete) {
281 sFirstDrawHandlers.add(callback);
282 }
283 }
284 }
285
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800286 public static void addConfigCallback(ComponentCallbacks callback) {
287 synchronized (sConfigCallbacks) {
288 sConfigCallbacks.add(callback);
289 }
290 }
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 // FIXME for perf testing only
293 private boolean mProfile = false;
294
295 /**
296 * Call this to profile the next traversal call.
297 * FIXME for perf testing only. Remove eventually
298 */
299 public void profile() {
300 mProfile = true;
301 }
302
303 /**
304 * Indicates whether we are in touch mode. Calling this method triggers an IPC
305 * call and should be avoided whenever possible.
306 *
307 * @return True, if the device is in touch mode, false otherwise.
308 *
309 * @hide
310 */
311 static boolean isInTouchMode() {
312 if (mInitialized) {
313 try {
314 return sWindowSession.getInTouchMode();
315 } catch (RemoteException e) {
316 }
317 }
318 return false;
319 }
320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * We have one child
323 */
Romain Guye4d01122010-06-16 18:44:05 -0700324 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 synchronized (this) {
326 if (mView == null) {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700327 mView = view;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700328 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700329 attrs = mWindowAttributes;
Romain Guye4d01122010-06-16 18:44:05 -0700330
Romain Guy529b60a2010-08-03 18:05:47 -0700331 enableHardwareAcceleration(attrs);
Romain Guye4d01122010-06-16 18:44:05 -0700332
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700333 if (view instanceof RootViewSurfaceTaker) {
334 mSurfaceHolderCallback =
335 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
336 if (mSurfaceHolderCallback != null) {
337 mSurfaceHolder = new TakenSurfaceHolder();
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700338 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700339 }
340 }
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700341 Resources resources = mView.getContext().getResources();
342 CompatibilityInfo compatibilityInfo = resources.getCompatibilityInfo();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700343 mTranslator = compatibilityInfo.getTranslator();
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700344
345 if (mTranslator != null || !compatibilityInfo.supportsScreen()) {
Mitsuru Oshima240f8a72009-07-22 20:39:14 -0700346 mSurface.setCompatibleDisplayMetrics(resources.getDisplayMetrics(),
347 mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700348 }
349
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700350 boolean restore = false;
Romain Guy35b38ce2009-10-07 13:38:55 -0700351 if (mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700352 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700353 attrs.backup();
354 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700355 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700356 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs);
357
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700358 if (!compatibilityInfo.supportsScreen()) {
359 attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
360 }
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 mSoftInputMode = attrs.softInputMode;
363 mWindowAttributesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 mAttachInfo.mRootView = view;
Romain Guy35b38ce2009-10-07 13:38:55 -0700365 mAttachInfo.mScalingRequired = mTranslator != null;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700366 mAttachInfo.mApplicationScale =
367 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 if (panelParentView != null) {
369 mAttachInfo.mPanelParentWindowToken
370 = panelParentView.getApplicationWindowToken();
371 }
372 mAdded = true;
373 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 // Schedule the first layout -before- adding to the window
376 // manager, to make sure we do the relayout before receiving
377 // any other events from the system.
378 requestLayout();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700379 mInputChannel = new InputChannel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 try {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700381 res = sWindowSession.add(mWindow, mWindowAttributes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700382 getHostVisibility(), mAttachInfo.mContentInsets,
383 mInputChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 } catch (RemoteException e) {
385 mAdded = false;
386 mView = null;
387 mAttachInfo.mRootView = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700388 mInputChannel = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 unscheduleTraversals();
390 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700391 } finally {
392 if (restore) {
393 attrs.restore();
394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700397 if (mTranslator != null) {
398 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 mPendingContentInsets.set(mAttachInfo.mContentInsets);
401 mPendingVisibleInsets.set(0, 0, 0, 0);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700402 if (Config.LOGV) Log.v(TAG, "Added window " + mWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 if (res < WindowManagerImpl.ADD_OKAY) {
404 mView = null;
405 mAttachInfo.mRootView = null;
406 mAdded = false;
407 unscheduleTraversals();
408 switch (res) {
409 case WindowManagerImpl.ADD_BAD_APP_TOKEN:
410 case WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN:
411 throw new WindowManagerImpl.BadTokenException(
412 "Unable to add window -- token " + attrs.token
413 + " is not valid; is your activity running?");
414 case WindowManagerImpl.ADD_NOT_APP_TOKEN:
415 throw new WindowManagerImpl.BadTokenException(
416 "Unable to add window -- token " + attrs.token
417 + " is not for an application");
418 case WindowManagerImpl.ADD_APP_EXITING:
419 throw new WindowManagerImpl.BadTokenException(
420 "Unable to add window -- app for token " + attrs.token
421 + " is exiting");
422 case WindowManagerImpl.ADD_DUPLICATE_ADD:
423 throw new WindowManagerImpl.BadTokenException(
424 "Unable to add window -- window " + mWindow
425 + " has already been added");
426 case WindowManagerImpl.ADD_STARTING_NOT_NEEDED:
427 // Silently ignore -- we would have just removed it
428 // right away, anyway.
429 return;
430 case WindowManagerImpl.ADD_MULTIPLE_SINGLETON:
431 throw new WindowManagerImpl.BadTokenException(
432 "Unable to add window " + mWindow +
433 " -- another window of this type already exists");
434 case WindowManagerImpl.ADD_PERMISSION_DENIED:
435 throw new WindowManagerImpl.BadTokenException(
436 "Unable to add window " + mWindow +
437 " -- permission denied for this window type");
438 }
439 throw new RuntimeException(
440 "Unable to add window -- unknown error code " + res);
441 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700442
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700443 if (view instanceof RootViewSurfaceTaker) {
444 mInputQueueCallback =
445 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
446 }
447 if (mInputQueueCallback != null) {
448 mInputQueue = new InputQueue(mInputChannel);
449 mInputQueueCallback.onInputQueueCreated(mInputQueue);
450 } else {
451 InputQueue.registerInputChannel(mInputChannel, mInputHandler,
452 Looper.myQueue());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700453 }
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 view.assignParent(this);
456 mAddedTouchMode = (res&WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE) != 0;
457 mAppVisible = (res&WindowManagerImpl.ADD_FLAG_APP_VISIBLE) != 0;
458 }
459 }
460 }
461
Romain Guy529b60a2010-08-03 18:05:47 -0700462 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
Romain Guye4d01122010-06-16 18:44:05 -0700463 // Only enable hardware acceleration if we are not in the system process
464 // The window manager creates ViewRoots to display animated preview windows
465 // of launching apps and we don't want those to be hardware accelerated
Romain Guy52339202010-09-03 16:04:46 -0700466 if (!HardwareRenderer.sRendererDisabled) {
Romain Guye4d01122010-06-16 18:44:05 -0700467 // Try to enable hardware acceleration if requested
Romain Guy529b60a2010-08-03 18:05:47 -0700468 if (attrs != null &&
469 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
Romain Guye4d01122010-06-16 18:44:05 -0700470 final boolean translucent = attrs.format != PixelFormat.OPAQUE;
Romain Guyb051e892010-09-28 19:09:36 -0700471 if (mAttachInfo.mHardwareRenderer != null) {
472 mAttachInfo.mHardwareRenderer.destroy(true);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700473 }
Romain Guyb051e892010-09-28 19:09:36 -0700474 mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent);
Romain Guy2bffd262010-09-12 17:40:02 -0700475 mAttachInfo.mHardwareAccelerated = true;
Romain Guye4d01122010-06-16 18:44:05 -0700476 }
477 }
478 }
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 public View getView() {
481 return mView;
482 }
483
484 final WindowLeaked getLocation() {
485 return mLocation;
486 }
487
488 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
489 synchronized (this) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700490 int oldSoftInputMode = mWindowAttributes.softInputMode;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700491 // preserve compatible window flag if exists.
492 int compatibleWindowFlag =
493 mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700495 mWindowAttributes.flags |= compatibleWindowFlag;
496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 if (newView) {
498 mSoftInputMode = attrs.softInputMode;
499 requestLayout();
500 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700501 // Don't lose the mode we last auto-computed.
502 if ((attrs.softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
503 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
504 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
505 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
506 | (oldSoftInputMode
507 & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 mWindowAttributesChanged = true;
510 scheduleTraversals();
511 }
512 }
513
514 void handleAppVisibility(boolean visible) {
515 if (mAppVisible != visible) {
516 mAppVisible = visible;
517 scheduleTraversals();
518 }
519 }
520
521 void handleGetNewSurface() {
522 mNewSurfaceNeeded = true;
523 mFullRedrawNeeded = true;
524 scheduleTraversals();
525 }
526
527 /**
528 * {@inheritDoc}
529 */
530 public void requestLayout() {
531 checkThread();
532 mLayoutRequested = true;
533 scheduleTraversals();
534 }
535
536 /**
537 * {@inheritDoc}
538 */
539 public boolean isLayoutRequested() {
540 return mLayoutRequested;
541 }
542
543 public void invalidateChild(View child, Rect dirty) {
544 checkThread();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700545 if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
546 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -0700548 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700549 if (mCurScrollY != 0) {
Romain Guy1e095972009-07-07 11:22:45 -0700550 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700551 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700552 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -0700553 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700554 }
Romain Guy1e095972009-07-07 11:22:45 -0700555 if (mAttachInfo.mScalingRequired) {
556 dirty.inset(-1, -1);
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 mDirty.union(dirty);
560 if (!mWillDrawSoon) {
561 scheduleTraversals();
562 }
563 }
564
565 public ViewParent getParent() {
566 return null;
567 }
568
569 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
570 invalidateChild(null, dirty);
571 return null;
572 }
573
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700574 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 if (child != mView) {
576 throw new RuntimeException("child is not mine, honest!");
577 }
578 // Note: don't apply scroll offset, because we want to know its
579 // visibility in the virtual canvas being given to the view hierarchy.
580 return r.intersect(0, 0, mWidth, mHeight);
581 }
582
583 public void bringChildToFront(View child) {
584 }
585
586 public void scheduleTraversals() {
587 if (!mTraversalScheduled) {
588 mTraversalScheduled = true;
589 sendEmptyMessage(DO_TRAVERSAL);
590 }
591 }
592
593 public void unscheduleTraversals() {
594 if (mTraversalScheduled) {
595 mTraversalScheduled = false;
596 removeMessages(DO_TRAVERSAL);
597 }
598 }
599
600 int getHostVisibility() {
601 return mAppVisible ? mView.getVisibility() : View.GONE;
602 }
Romain Guy8506ab42009-06-11 17:35:47 -0700603
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 private void performTraversals() {
605 // cache mView since it is used so much below...
606 final View host = mView;
607
608 if (DBG) {
609 System.out.println("======================================");
610 System.out.println("performTraversals");
611 host.debug();
612 }
613
614 if (host == null || !mAdded)
615 return;
616
617 mTraversalScheduled = false;
618 mWillDrawSoon = true;
619 boolean windowResizesToFitContent = false;
620 boolean fullRedrawNeeded = mFullRedrawNeeded;
621 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700622 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 WindowManager.LayoutParams lp = mWindowAttributes;
624
625 int desiredWindowWidth;
626 int desiredWindowHeight;
627 int childWidthMeasureSpec;
628 int childHeightMeasureSpec;
629
630 final View.AttachInfo attachInfo = mAttachInfo;
631
632 final int viewVisibility = getHostVisibility();
633 boolean viewVisibilityChanged = mViewVisibility != viewVisibility
634 || mNewSurfaceNeeded;
635
636 WindowManager.LayoutParams params = null;
637 if (mWindowAttributesChanged) {
638 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700639 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 params = lp;
641 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700642 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 if (mFirst) {
644 fullRedrawNeeded = true;
645 mLayoutRequested = true;
646
Romain Guy8506ab42009-06-11 17:35:47 -0700647 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700648 mView.getContext().getResources().getDisplayMetrics();
649 desiredWindowWidth = packageMetrics.widthPixels;
650 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800651
652 // For the very first time, tell the view hierarchy that it
653 // is attached to the window. Note that at this point the surface
654 // object is not initialized to its backing store, but soon it
655 // will be (assuming the window is visible).
656 attachInfo.mSurface = mSurface;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700657 attachInfo.mTranslucentWindow = PixelFormat.formatHasAlpha(lp.format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 attachInfo.mHasWindowFocus = false;
659 attachInfo.mWindowVisibility = viewVisibility;
660 attachInfo.mRecomputeGlobalAttributes = false;
661 attachInfo.mKeepScreenOn = false;
662 viewVisibilityChanged = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700663 mLastConfiguration.setTo(host.getResources().getConfiguration());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 host.dispatchAttachedToWindow(attachInfo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -0700666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700668 desiredWindowWidth = frame.width();
669 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700671 if (DEBUG_ORIENTATION) Log.v(TAG,
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700672 "View " + host + " resized to: " + frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 fullRedrawNeeded = true;
674 mLayoutRequested = true;
675 windowResizesToFitContent = true;
676 }
677 }
678
679 if (viewVisibilityChanged) {
680 attachInfo.mWindowVisibility = viewVisibility;
681 host.dispatchWindowVisibilityChanged(viewVisibility);
682 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
Romain Guyb051e892010-09-28 19:09:36 -0700683 if (mAttachInfo.mHardwareRenderer != null) {
684 mAttachInfo.mHardwareRenderer.destroy(false);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700685 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 }
687 if (viewVisibility == View.GONE) {
688 // After making a window gone, we will count it as being
689 // shown for the first time the next time it gets focus.
690 mHasHadWindowFocus = false;
691 }
692 }
693
694 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -0700695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 if (mLayoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -0700697 // Execute enqueued actions on every layout in case a view that was detached
698 // enqueued an action after being detached
699 getRunQueue().executeActions(attachInfo.mHandler);
700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if (mFirst) {
702 host.fitSystemWindows(mAttachInfo.mContentInsets);
703 // make sure touch mode code executes by setting cached value
704 // to opposite of the added touch mode.
705 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -0700706 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 } else {
708 if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) {
709 mAttachInfo.mContentInsets.set(mPendingContentInsets);
710 host.fitSystemWindows(mAttachInfo.mContentInsets);
711 insetsChanged = true;
712 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
713 + mAttachInfo.mContentInsets);
714 }
715 if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) {
716 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
717 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
718 + mAttachInfo.mVisibleInsets);
719 }
720 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
721 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
722 windowResizesToFitContent = true;
723
Romain Guy8506ab42009-06-11 17:35:47 -0700724 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700725 mView.getContext().getResources().getDisplayMetrics();
726 desiredWindowWidth = packageMetrics.widthPixels;
727 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 }
729 }
730
731 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
732 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
733
734 // Ask host how big it wants to be
Jeff Brownc5ed5912010-07-14 18:48:53 -0700735 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 "Measuring " + host + " in display " + desiredWindowWidth
737 + "x" + desiredWindowHeight + "...");
738 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
739
740 if (DBG) {
741 System.out.println("======================================");
742 System.out.println("performTraversals -- after measure");
743 host.debug();
744 }
745 }
746
747 if (attachInfo.mRecomputeGlobalAttributes) {
748 //Log.i(TAG, "Computing screen on!");
749 attachInfo.mRecomputeGlobalAttributes = false;
750 boolean oldVal = attachInfo.mKeepScreenOn;
751 attachInfo.mKeepScreenOn = false;
752 host.dispatchCollectViewAttributes(0);
753 if (attachInfo.mKeepScreenOn != oldVal) {
754 params = lp;
755 //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn);
756 }
757 }
758
759 if (mFirst || attachInfo.mViewVisibilityChanged) {
760 attachInfo.mViewVisibilityChanged = false;
761 int resizeMode = mSoftInputMode &
762 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
763 // If we are in auto resize mode, then we need to determine
764 // what mode to use now.
765 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
766 final int N = attachInfo.mScrollContainers.size();
767 for (int i=0; i<N; i++) {
768 if (attachInfo.mScrollContainers.get(i).isShown()) {
769 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
770 }
771 }
772 if (resizeMode == 0) {
773 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
774 }
775 if ((lp.softInputMode &
776 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
777 lp.softInputMode = (lp.softInputMode &
778 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
779 resizeMode;
780 params = lp;
781 }
782 }
783 }
Romain Guy8506ab42009-06-11 17:35:47 -0700784
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785 if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
786 if (!PixelFormat.formatHasAlpha(params.format)) {
787 params.format = PixelFormat.TRANSLUCENT;
788 }
789 }
790
791 boolean windowShouldResize = mLayoutRequested && windowResizesToFitContent
Romain Guy2e4f4262010-04-06 11:07:52 -0700792 && ((mWidth != host.mMeasuredWidth || mHeight != host.mMeasuredHeight)
793 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
794 frame.width() < desiredWindowWidth && frame.width() != mWidth)
795 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
796 frame.height() < desiredWindowHeight && frame.height() != mHeight));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797
798 final boolean computesInternalInsets =
799 attachInfo.mTreeObserver.hasComputeInternalInsetsListeners();
Romain Guy812ccbe2010-06-01 14:07:24 -0700800
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 boolean insetsPending = false;
802 int relayoutResult = 0;
Romain Guy812ccbe2010-06-01 14:07:24 -0700803
804 if (mFirst || windowShouldResize || insetsChanged ||
805 viewVisibilityChanged || params != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806
807 if (viewVisibility == View.VISIBLE) {
808 // If this window is giving internal insets to the window
809 // manager, and it is being added or changing its visibility,
810 // then we want to first give the window manager "fake"
811 // insets to cause it to effectively ignore the content of
812 // the window during layout. This avoids it briefly causing
813 // other windows to resize/move based on the raw frame of the
814 // window, waiting until we can finish laying out this window
815 // and get back to the window manager with the ultimately
816 // computed insets.
Romain Guy812ccbe2010-06-01 14:07:24 -0700817 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 }
819
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700820 if (mSurfaceHolder != null) {
821 mSurfaceHolder.mSurfaceLock.lock();
822 mDrawingAllowed = true;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700823 }
Romain Guy812ccbe2010-06-01 14:07:24 -0700824
825 boolean hwIntialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 boolean contentInsetsChanged = false;
Romain Guy13922e02009-05-12 17:56:14 -0700827 boolean visibleInsetsChanged;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700828 boolean hadSurface = mSurface.isValid();
Romain Guy812ccbe2010-06-01 14:07:24 -0700829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 int fl = 0;
832 if (params != null) {
833 fl = params.flags;
834 if (attachInfo.mKeepScreenOn) {
835 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
836 }
837 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700838 if (DEBUG_LAYOUT) {
839 Log.i(TAG, "host=w:" + host.mMeasuredWidth + ", h:" +
840 host.mMeasuredHeight + ", params=" + params);
841 }
842 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 if (params != null) {
845 params.flags = fl;
846 }
847
848 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString()
849 + " content=" + mPendingContentInsets.toShortString()
850 + " visible=" + mPendingVisibleInsets.toShortString()
851 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -0700852
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700853 if (mPendingConfiguration.seq != 0) {
854 if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: "
855 + mPendingConfiguration);
856 updateConfiguration(mPendingConfiguration, !mFirst);
857 mPendingConfiguration.seq = 0;
858 }
859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 contentInsetsChanged = !mPendingContentInsets.equals(
861 mAttachInfo.mContentInsets);
862 visibleInsetsChanged = !mPendingVisibleInsets.equals(
863 mAttachInfo.mVisibleInsets);
864 if (contentInsetsChanged) {
865 mAttachInfo.mContentInsets.set(mPendingContentInsets);
866 host.fitSystemWindows(mAttachInfo.mContentInsets);
867 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
868 + mAttachInfo.mContentInsets);
869 }
870 if (visibleInsetsChanged) {
871 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
872 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
873 + mAttachInfo.mVisibleInsets);
874 }
875
876 if (!hadSurface) {
877 if (mSurface.isValid()) {
878 // If we are creating a new surface, then we need to
879 // completely redraw it. Also, when we get to the
880 // point of drawing it we will hold off and schedule
881 // a new traversal instead. This is so we can tell the
882 // window manager about all of the windows being displayed
883 // before actually drawing them, so it can display then
884 // all at once.
885 newSurface = true;
886 fullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -0700887 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -0700888
Romain Guyb051e892010-09-28 19:09:36 -0700889 if (mAttachInfo.mHardwareRenderer != null) {
890 hwIntialized = mAttachInfo.mHardwareRenderer.initialize(mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
892 }
893 } else if (!mSurface.isValid()) {
894 // If the surface has been removed, then reset the scroll
895 // positions.
896 mLastScrolledFocus = null;
897 mScrollY = mCurScrollY = 0;
898 if (mScroller != null) {
899 mScroller.abortAnimation();
900 }
901 }
902 } catch (RemoteException e) {
903 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 if (DEBUG_ORIENTATION) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -0700906 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907
908 attachInfo.mWindowLeft = frame.left;
909 attachInfo.mWindowTop = frame.top;
910
911 // !!FIXME!! This next section handles the case where we did not get the
912 // window size we asked for. We should avoid this by getting a maximum size from
913 // the window session beforehand.
914 mWidth = frame.width();
915 mHeight = frame.height();
916
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700917 if (mSurfaceHolder != null) {
918 // The app owns the surface; tell it about what is going on.
919 if (mSurface.isValid()) {
920 // XXX .copyFrom() doesn't work!
921 //mSurfaceHolder.mSurface.copyFrom(mSurface);
922 mSurfaceHolder.mSurface = mSurface;
923 }
924 mSurfaceHolder.mSurfaceLock.unlock();
925 if (mSurface.isValid()) {
926 if (!hadSurface) {
927 mSurfaceHolder.ungetCallbacks();
928
929 mIsCreating = true;
930 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
931 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
932 if (callbacks != null) {
933 for (SurfaceHolder.Callback c : callbacks) {
934 c.surfaceCreated(mSurfaceHolder);
935 }
936 }
937 surfaceChanged = true;
938 }
939 if (surfaceChanged) {
940 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
941 lp.format, mWidth, mHeight);
942 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
943 if (callbacks != null) {
944 for (SurfaceHolder.Callback c : callbacks) {
945 c.surfaceChanged(mSurfaceHolder, lp.format,
946 mWidth, mHeight);
947 }
948 }
949 }
950 mIsCreating = false;
951 } else if (hadSurface) {
952 mSurfaceHolder.ungetCallbacks();
953 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
954 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
955 if (callbacks != null) {
956 for (SurfaceHolder.Callback c : callbacks) {
957 c.surfaceDestroyed(mSurfaceHolder);
958 }
959 }
960 mSurfaceHolder.mSurfaceLock.lock();
961 // Make surface invalid.
962 //mSurfaceHolder.mSurface.copyFrom(mSurface);
963 mSurfaceHolder.mSurface = new Surface();
964 mSurfaceHolder.mSurfaceLock.unlock();
965 }
966 }
Romain Guy53389bd2010-09-07 17:16:32 -0700967
Romain Guyb051e892010-09-28 19:09:36 -0700968 if (hwIntialized || (windowShouldResize && mAttachInfo.mHardwareRenderer != null)) {
969 mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 }
971
972 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Romain Guy2d4cff62010-04-09 15:39:00 -0700973 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 if (focusChangedDueToTouchMode || mWidth != host.mMeasuredWidth
975 || mHeight != host.mMeasuredHeight || contentInsetsChanged) {
976 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
977 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
978
979 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
980 + mWidth + " measuredWidth=" + host.mMeasuredWidth
981 + " mHeight=" + mHeight
982 + " measuredHeight" + host.mMeasuredHeight
983 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -0700984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 // Ask host how big it wants to be
986 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
987
988 // Implementation of weights from WindowManager.LayoutParams
989 // We just grow the dimensions as needed and re-measure if
990 // needs be
991 int width = host.mMeasuredWidth;
992 int height = host.mMeasuredHeight;
993 boolean measureAgain = false;
994
995 if (lp.horizontalWeight > 0.0f) {
996 width += (int) ((mWidth - width) * lp.horizontalWeight);
997 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
998 MeasureSpec.EXACTLY);
999 measureAgain = true;
1000 }
1001 if (lp.verticalWeight > 0.0f) {
1002 height += (int) ((mHeight - height) * lp.verticalWeight);
1003 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1004 MeasureSpec.EXACTLY);
1005 measureAgain = true;
1006 }
1007
1008 if (measureAgain) {
1009 if (DEBUG_LAYOUT) Log.v(TAG,
1010 "And hey let's measure once more: width=" + width
1011 + " height=" + height);
1012 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1013 }
1014
1015 mLayoutRequested = true;
1016 }
1017 }
1018
1019 final boolean didLayout = mLayoutRequested;
1020 boolean triggerGlobalLayoutListener = didLayout
1021 || attachInfo.mRecomputeGlobalAttributes;
1022 if (didLayout) {
1023 mLayoutRequested = false;
1024 mScrollMayChange = true;
1025 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001026 TAG, "Laying out " + host + " to (" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 host.mMeasuredWidth + ", " + host.mMeasuredHeight + ")");
Romain Guy13922e02009-05-12 17:56:14 -07001028 long startTime = 0L;
Romain Guy5429e1d2010-09-07 12:38:00 -07001029 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 startTime = SystemClock.elapsedRealtime();
1031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
1033
Romain Guy13922e02009-05-12 17:56:14 -07001034 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1035 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
1036 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
1037 + "please refer to the logs with the tag "
1038 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1039 }
1040 }
1041
Romain Guy5429e1d2010-09-07 12:38:00 -07001042 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1044 }
1045
1046 // By this point all views have been sized and positionned
1047 // We can compute the transparent area
1048
1049 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1050 // start out transparent
1051 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1052 host.getLocationInWindow(mTmpLocation);
1053 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1054 mTmpLocation[0] + host.mRight - host.mLeft,
1055 mTmpLocation[1] + host.mBottom - host.mTop);
1056
1057 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001058 if (mTranslator != null) {
1059 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1060 }
1061
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1063 mPreviousTransparentRegion.set(mTransparentRegion);
1064 // reconfigure window manager
1065 try {
1066 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1067 } catch (RemoteException e) {
1068 }
1069 }
1070 }
1071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 if (DBG) {
1073 System.out.println("======================================");
1074 System.out.println("performTraversals -- after setFrame");
1075 host.debug();
1076 }
1077 }
1078
1079 if (triggerGlobalLayoutListener) {
1080 attachInfo.mRecomputeGlobalAttributes = false;
1081 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1082 }
1083
1084 if (computesInternalInsets) {
1085 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1086 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1087 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1088 givenContent.left = givenContent.top = givenContent.right
1089 = givenContent.bottom = givenVisible.left = givenVisible.top
1090 = givenVisible.right = givenVisible.bottom = 0;
1091 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001092 Rect contentInsets = insets.contentInsets;
1093 Rect visibleInsets = insets.visibleInsets;
1094 if (mTranslator != null) {
1095 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1096 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1099 mLastGivenInsets.set(insets);
1100 try {
1101 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001102 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 } catch (RemoteException e) {
1104 }
1105 }
1106 }
Romain Guy8506ab42009-06-11 17:35:47 -07001107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if (mFirst) {
1109 // handle first focus request
1110 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1111 + mView.hasFocus());
1112 if (mView != null) {
1113 if (!mView.hasFocus()) {
1114 mView.requestFocus(View.FOCUS_FORWARD);
1115 mFocusedView = mRealFocusedView = mView.findFocus();
1116 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1117 + mFocusedView);
1118 } else {
1119 mRealFocusedView = mView.findFocus();
1120 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1121 + mRealFocusedView);
1122 }
1123 }
1124 }
1125
1126 mFirst = false;
1127 mWillDrawSoon = false;
1128 mNewSurfaceNeeded = false;
1129 mViewVisibility = viewVisibility;
1130
1131 if (mAttachInfo.mHasWindowFocus) {
1132 final boolean imTarget = WindowManager.LayoutParams
1133 .mayUseInputMethod(mWindowAttributes.flags);
1134 if (imTarget != mLastWasImTarget) {
1135 mLastWasImTarget = imTarget;
1136 InputMethodManager imm = InputMethodManager.peekInstance();
1137 if (imm != null && imTarget) {
1138 imm.startGettingWindowFocus(mView);
1139 imm.onWindowFocus(mView, mView.findFocus(),
1140 mWindowAttributes.softInputMode,
1141 !mHasHadWindowFocus, mWindowAttributes.flags);
1142 }
1143 }
1144 }
Romain Guy8506ab42009-06-11 17:35:47 -07001145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1147
1148 if (!cancelDraw && !newSurface) {
1149 mFullRedrawNeeded = false;
1150 draw(fullRedrawNeeded);
1151
1152 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1153 || mReportNextDraw) {
1154 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001155 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 }
1157 mReportNextDraw = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001158 if (mSurfaceHolder != null && mSurface.isValid()) {
1159 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
1160 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1161 if (callbacks != null) {
1162 for (SurfaceHolder.Callback c : callbacks) {
1163 if (c instanceof SurfaceHolder.Callback2) {
1164 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
1165 mSurfaceHolder);
1166 }
1167 }
1168 }
1169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 try {
1171 sWindowSession.finishDrawing(mWindow);
1172 } catch (RemoteException e) {
1173 }
1174 }
1175 } else {
1176 // We were supposed to report when we are done drawing. Since we canceled the
1177 // draw, remember it here.
1178 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1179 mReportNextDraw = true;
1180 }
1181 if (fullRedrawNeeded) {
1182 mFullRedrawNeeded = true;
1183 }
1184 // Try again
1185 scheduleTraversals();
1186 }
1187 }
1188
1189 public void requestTransparentRegion(View child) {
1190 // the test below should not fail unless someone is messing with us
1191 checkThread();
1192 if (mView == child) {
1193 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1194 // Need to make sure we re-evaluate the window attributes next
1195 // time around, to ensure the window has the correct format.
1196 mWindowAttributesChanged = true;
1197 }
1198 }
1199
1200 /**
1201 * Figures out the measure spec for the root view in a window based on it's
1202 * layout params.
1203 *
1204 * @param windowSize
1205 * The available width or height of the window
1206 *
1207 * @param rootDimension
1208 * The layout params for one dimension (width or height) of the
1209 * window.
1210 *
1211 * @return The measure spec to use to measure the root view.
1212 */
1213 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1214 int measureSpec;
1215 switch (rootDimension) {
1216
Romain Guy980a9382010-01-08 15:06:28 -08001217 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 // Window can't resize. Force root view to be windowSize.
1219 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1220 break;
1221 case ViewGroup.LayoutParams.WRAP_CONTENT:
1222 // Window can resize. Set max size for root view.
1223 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1224 break;
1225 default:
1226 // Window wants to be an exact size. Force root view to be that size.
1227 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1228 break;
1229 }
1230 return measureSpec;
1231 }
1232
1233 private void draw(boolean fullRedrawNeeded) {
1234 Surface surface = mSurface;
1235 if (surface == null || !surface.isValid()) {
1236 return;
1237 }
1238
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001239 if (!sFirstDrawComplete) {
1240 synchronized (sFirstDrawHandlers) {
1241 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07001242 final int count = sFirstDrawHandlers.size();
1243 for (int i = 0; i< count; i++) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001244 post(sFirstDrawHandlers.get(i));
1245 }
1246 }
1247 }
1248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 scrollToRectOrFocus(null, false);
1250
1251 if (mAttachInfo.mViewScrollChanged) {
1252 mAttachInfo.mViewScrollChanged = false;
1253 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1254 }
Romain Guy8506ab42009-06-11 17:35:47 -07001255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 int yoff;
Romain Guy5bcdff42009-05-14 21:27:18 -07001257 final boolean scrolling = mScroller != null && mScroller.computeScrollOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 if (scrolling) {
1259 yoff = mScroller.getCurrY();
1260 } else {
1261 yoff = mScrollY;
1262 }
1263 if (mCurScrollY != yoff) {
1264 mCurScrollY = yoff;
1265 fullRedrawNeeded = true;
1266 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001267 float appScale = mAttachInfo.mApplicationScale;
1268 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269
1270 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001271 if (mSurfaceHolder != null) {
1272 // The app owns the surface, we won't draw.
1273 dirty.setEmpty();
1274 return;
1275 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07001276
1277 if (fullRedrawNeeded) {
1278 mAttachInfo.mIgnoreDirtyState = true;
1279 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1280 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001281
Romain Guyb051e892010-09-28 19:09:36 -07001282 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guyfd507262010-10-10 15:42:49 -07001283 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy101e2ae2010-10-11 12:41:21 -07001284 mIsAnimating = false;
Romain Guyfd507262010-10-10 15:42:49 -07001285 dirty.setEmpty();
Romain Guy101e2ae2010-10-11 12:41:21 -07001286 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, yoff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001289 if (scrolling) {
1290 mFullRedrawNeeded = true;
1291 scheduleTraversals();
1292 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 return;
1295 }
1296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001298 Log.v(TAG, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 + mWindowAttributes.getTitle()
1300 + ": dirty={" + dirty.left + "," + dirty.top
1301 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001302 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1303 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 }
1305
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001306 if (!dirty.isEmpty() || mIsAnimating) {
1307 Canvas canvas;
1308 try {
1309 int left = dirty.left;
1310 int top = dirty.top;
1311 int right = dirty.right;
1312 int bottom = dirty.bottom;
1313 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001314
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001315 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1316 bottom != dirty.bottom) {
1317 mAttachInfo.mIgnoreDirtyState = true;
1318 }
1319
1320 // TODO: Do this in native
1321 canvas.setDensity(mDensity);
1322 } catch (Surface.OutOfResourcesException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001323 Log.e(TAG, "OutOfResourcesException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001324 // TODO: we should ask the window manager to do something!
1325 // for now we just do nothing
1326 return;
1327 } catch (IllegalArgumentException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001328 Log.e(TAG, "IllegalArgumentException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001329 // TODO: we should ask the window manager to do something!
1330 // for now we just do nothing
1331 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001332 }
1333
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001334 try {
1335 if (!dirty.isEmpty() || mIsAnimating) {
1336 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001338 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001339 Log.v(TAG, "Surface " + surface + " drawing to bitmap w="
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001340 + canvas.getWidth() + ", h=" + canvas.getHeight());
1341 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343
Romain Guy5429e1d2010-09-07 12:38:00 -07001344 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001345 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001347
1348 // If this bitmap's format includes an alpha channel, we
1349 // need to clear it before drawing so that the child will
1350 // properly re-composite its drawing on a transparent
1351 // background. This automatically respects the clip/dirty region
1352 // or
1353 // If we are applying an offset, we need to clear the area
1354 // where the offset doesn't appear to avoid having garbage
1355 // left in the blank areas.
1356 if (!canvas.isOpaque() || yoff != 0) {
1357 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1358 }
1359
1360 dirty.setEmpty();
1361 mIsAnimating = false;
1362 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1363 mView.mPrivateFlags |= View.DRAWN;
1364
1365 if (DEBUG_DRAW) {
1366 Context cxt = mView.getContext();
1367 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1368 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1369 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1370 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001371 try {
1372 canvas.translate(0, -yoff);
1373 if (mTranslator != null) {
1374 mTranslator.translateCanvas(canvas);
1375 }
1376 canvas.setScreenDensity(scalingRequired
1377 ? DisplayMetrics.DENSITY_DEVICE : 0);
1378 mView.draw(canvas);
1379 } finally {
1380 mAttachInfo.mIgnoreDirtyState = false;
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001381 }
1382
1383 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1384 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1385 }
1386
Romain Guy5429e1d2010-09-07 12:38:00 -07001387 if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001388 int now = (int)SystemClock.elapsedRealtime();
1389 if (sDrawTime != 0) {
1390 nativeShowFPS(canvas, now - sDrawTime);
1391 }
1392 sDrawTime = now;
1393 }
1394
Romain Guy5429e1d2010-09-07 12:38:00 -07001395 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001396 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 }
1399
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001400 } finally {
1401 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404
1405 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001406 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
Romain Guy8506ab42009-06-11 17:35:47 -07001408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001409 if (scrolling) {
1410 mFullRedrawNeeded = true;
1411 scheduleTraversals();
1412 }
1413 }
1414
1415 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1416 final View.AttachInfo attachInfo = mAttachInfo;
1417 final Rect ci = attachInfo.mContentInsets;
1418 final Rect vi = attachInfo.mVisibleInsets;
1419 int scrollY = 0;
1420 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 if (vi.left > ci.left || vi.top > ci.top
1423 || vi.right > ci.right || vi.bottom > ci.bottom) {
1424 // We'll assume that we aren't going to change the scroll
1425 // offset, since we want to avoid that unless it is actually
1426 // going to make the focus visible... otherwise we scroll
1427 // all over the place.
1428 scrollY = mScrollY;
1429 // We can be called for two different situations: during a draw,
1430 // to update the scroll position if the focus has changed (in which
1431 // case 'rectangle' is null), or in response to a
1432 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1433 // is non-null and we just want to scroll to whatever that
1434 // rectangle is).
1435 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001436
1437 // When in touch mode, focus points to the previously focused view,
1438 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001439 // line checks whether the view is still in our hierarchy.
1440 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001441 mRealFocusedView = null;
1442 return false;
1443 }
1444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001445 if (focus != mLastScrolledFocus) {
1446 // If the focus has changed, then ignore any requests to scroll
1447 // to a rectangle; first we want to make sure the entire focus
1448 // view is visible.
1449 rectangle = null;
1450 }
1451 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1452 + " rectangle=" + rectangle + " ci=" + ci
1453 + " vi=" + vi);
1454 if (focus == mLastScrolledFocus && !mScrollMayChange
1455 && rectangle == null) {
1456 // Optimization: if the focus hasn't changed since last
1457 // time, and no layout has happened, then just leave things
1458 // as they are.
1459 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1460 + mScrollY + " vi=" + vi.toShortString());
1461 } else if (focus != null) {
1462 // We need to determine if the currently focused view is
1463 // within the visible part of the window and, if not, apply
1464 // a pan so it can be seen.
1465 mLastScrolledFocus = focus;
1466 mScrollMayChange = false;
1467 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1468 // Try to find the rectangle from the focus view.
1469 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1470 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1471 + mView.getWidth() + " h=" + mView.getHeight()
1472 + " ci=" + ci.toShortString()
1473 + " vi=" + vi.toShortString());
1474 if (rectangle == null) {
1475 focus.getFocusedRect(mTempRect);
1476 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1477 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001478 if (mView instanceof ViewGroup) {
1479 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1480 focus, mTempRect);
1481 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1483 "Focus in window: focusRect="
1484 + mTempRect.toShortString()
1485 + " visRect=" + mVisRect.toShortString());
1486 } else {
1487 mTempRect.set(rectangle);
1488 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1489 "Request scroll to rect: "
1490 + mTempRect.toShortString()
1491 + " visRect=" + mVisRect.toShortString());
1492 }
1493 if (mTempRect.intersect(mVisRect)) {
1494 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1495 "Focus window visible rect: "
1496 + mTempRect.toShortString());
1497 if (mTempRect.height() >
1498 (mView.getHeight()-vi.top-vi.bottom)) {
1499 // If the focus simply is not going to fit, then
1500 // best is probably just to leave things as-is.
1501 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1502 "Too tall; leaving scrollY=" + scrollY);
1503 } else if ((mTempRect.top-scrollY) < vi.top) {
1504 scrollY -= vi.top - (mTempRect.top-scrollY);
1505 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1506 "Top covered; scrollY=" + scrollY);
1507 } else if ((mTempRect.bottom-scrollY)
1508 > (mView.getHeight()-vi.bottom)) {
1509 scrollY += (mTempRect.bottom-scrollY)
1510 - (mView.getHeight()-vi.bottom);
1511 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1512 "Bottom covered; scrollY=" + scrollY);
1513 }
1514 handled = true;
1515 }
1516 }
1517 }
1518 }
Romain Guy8506ab42009-06-11 17:35:47 -07001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 if (scrollY != mScrollY) {
1521 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1522 + mScrollY + " , new=" + scrollY);
1523 if (!immediate) {
1524 if (mScroller == null) {
1525 mScroller = new Scroller(mView.getContext());
1526 }
1527 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1528 } else if (mScroller != null) {
1529 mScroller.abortAnimation();
1530 }
1531 mScrollY = scrollY;
1532 }
Romain Guy8506ab42009-06-11 17:35:47 -07001533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 return handled;
1535 }
Romain Guy8506ab42009-06-11 17:35:47 -07001536
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 public void requestChildFocus(View child, View focused) {
1538 checkThread();
1539 if (mFocusedView != focused) {
1540 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1541 scheduleTraversals();
1542 }
1543 mFocusedView = mRealFocusedView = focused;
1544 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1545 + mFocusedView);
1546 }
1547
1548 public void clearChildFocus(View child) {
1549 checkThread();
1550
1551 View oldFocus = mFocusedView;
1552
1553 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1554 mFocusedView = mRealFocusedView = null;
1555 if (mView != null && !mView.hasFocus()) {
1556 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1557 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1558 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1559 }
1560 } else if (oldFocus != null) {
1561 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1562 }
1563 }
1564
1565
1566 public void focusableViewAvailable(View v) {
1567 checkThread();
1568
1569 if (mView != null && !mView.hasFocus()) {
1570 v.requestFocus();
1571 } else {
1572 // the one case where will transfer focus away from the current one
1573 // is if the current view is a view group that prefers to give focus
1574 // to its children first AND the view is a descendant of it.
1575 mFocusedView = mView.findFocus();
1576 boolean descendantsHaveDibsOnFocus =
1577 (mFocusedView instanceof ViewGroup) &&
1578 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1579 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1580 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1581 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1582 v.requestFocus();
1583 }
1584 }
1585 }
1586
1587 public void recomputeViewAttributes(View child) {
1588 checkThread();
1589 if (mView == child) {
1590 mAttachInfo.mRecomputeGlobalAttributes = true;
1591 if (!mWillDrawSoon) {
1592 scheduleTraversals();
1593 }
1594 }
1595 }
1596
1597 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 if (mView != null) {
1599 mView.dispatchDetachedFromWindow();
1600 }
1601
1602 mView = null;
1603 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001604 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605
Romain Guy29d89972010-09-22 16:10:57 -07001606 destroyHardwareRenderer();
Romain Guy4caa4ed2010-08-25 14:46:24 -07001607
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001608 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001610 if (mInputChannel != null) {
1611 if (mInputQueueCallback != null) {
1612 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
1613 mInputQueueCallback = null;
1614 } else {
1615 InputQueue.unregisterInputChannel(mInputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001616 }
1617 }
1618
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 try {
1620 sWindowSession.remove(mWindow);
1621 } catch (RemoteException e) {
1622 }
Jeff Brown349703e2010-06-22 01:27:15 -07001623
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001624 // Dispose the input channel after removing the window so the Window Manager
1625 // doesn't interpret the input channel being closed as an abnormal termination.
1626 if (mInputChannel != null) {
1627 mInputChannel.dispose();
1628 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 }
Romain Guy8506ab42009-06-11 17:35:47 -07001631
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001632 void updateConfiguration(Configuration config, boolean force) {
1633 if (DEBUG_CONFIGURATION) Log.v(TAG,
1634 "Applying new config to window "
1635 + mWindowAttributes.getTitle()
1636 + ": " + config);
1637 synchronized (sConfigCallbacks) {
1638 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1639 sConfigCallbacks.get(i).onConfigurationChanged(config);
1640 }
1641 }
1642 if (mView != null) {
1643 // At this point the resources have been updated to
1644 // have the most recent config, whatever that is. Use
1645 // the on in them which may be newer.
1646 if (mView != null) {
1647 config = mView.getResources().getConfiguration();
1648 }
1649 if (force || mLastConfiguration.diff(config) != 0) {
1650 mLastConfiguration.setTo(config);
1651 mView.dispatchConfigurationChanged(config);
1652 }
1653 }
1654 }
1655
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 /**
1657 * Return true if child is an ancestor of parent, (or equal to the parent).
1658 */
1659 private static boolean isViewDescendantOf(View child, View parent) {
1660 if (child == parent) {
1661 return true;
1662 }
1663
1664 final ViewParent theParent = child.getParent();
1665 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1666 }
1667
Romain Guycdb86672010-03-18 18:54:50 -07001668 private static void forceLayout(View view) {
1669 view.forceLayout();
1670 if (view instanceof ViewGroup) {
1671 ViewGroup group = (ViewGroup) view;
1672 final int count = group.getChildCount();
1673 for (int i = 0; i < count; i++) {
1674 forceLayout(group.getChildAt(i));
1675 }
1676 }
1677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678
1679 public final static int DO_TRAVERSAL = 1000;
1680 public final static int DIE = 1001;
1681 public final static int RESIZED = 1002;
1682 public final static int RESIZED_REPORT = 1003;
1683 public final static int WINDOW_FOCUS_CHANGED = 1004;
1684 public final static int DISPATCH_KEY = 1005;
1685 public final static int DISPATCH_POINTER = 1006;
1686 public final static int DISPATCH_TRACKBALL = 1007;
1687 public final static int DISPATCH_APP_VISIBILITY = 1008;
1688 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1689 public final static int FINISHED_EVENT = 1010;
1690 public final static int DISPATCH_KEY_FROM_IME = 1011;
1691 public final static int FINISH_INPUT_CONNECTION = 1012;
1692 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001693 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Christopher Tatea53146c2010-09-07 11:57:52 -07001694 public final static int DISPATCH_DRAG_EVENT = 1015;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001695
1696 @Override
1697 public void handleMessage(Message msg) {
1698 switch (msg.what) {
1699 case View.AttachInfo.INVALIDATE_MSG:
1700 ((View) msg.obj).invalidate();
1701 break;
1702 case View.AttachInfo.INVALIDATE_RECT_MSG:
1703 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1704 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1705 info.release();
1706 break;
1707 case DO_TRAVERSAL:
1708 if (mProfile) {
1709 Debug.startMethodTracing("ViewRoot");
1710 }
1711
1712 performTraversals();
1713
1714 if (mProfile) {
1715 Debug.stopMethodTracing();
1716 mProfile = false;
1717 }
1718 break;
1719 case FINISHED_EVENT:
1720 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1721 break;
1722 case DISPATCH_KEY:
1723 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001724 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001725 + msg.obj + " to " + mView);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001726 deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001728 case DISPATCH_POINTER: {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001729 MotionEvent event = (MotionEvent) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001730 try {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001731 deliverPointerEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 } finally {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001733 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001734 if (msg.arg1 != 0) {
1735 finishInputEvent();
1736 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001737 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001739 } break;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001740 case DISPATCH_TRACKBALL: {
1741 MotionEvent event = (MotionEvent) msg.obj;
1742 try {
1743 deliverTrackballEvent(event);
1744 } finally {
1745 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001746 if (msg.arg1 != 0) {
1747 finishInputEvent();
1748 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001749 }
1750 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001751 case DISPATCH_APP_VISIBILITY:
1752 handleAppVisibility(msg.arg1 != 0);
1753 break;
1754 case DISPATCH_GET_NEW_SURFACE:
1755 handleGetNewSurface();
1756 break;
1757 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001758 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001761 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001762 && mPendingVisibleInsets.equals(ri.visibleInsets)
1763 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 break;
1765 }
1766 // fall through...
1767 case RESIZED_REPORT:
1768 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001769 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1770 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001771 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 mWinFrame.left = 0;
1774 mWinFrame.right = msg.arg1;
1775 mWinFrame.top = 0;
1776 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001777 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1778 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 if (msg.what == RESIZED_REPORT) {
1780 mReportNextDraw = true;
1781 }
Romain Guycdb86672010-03-18 18:54:50 -07001782
1783 if (mView != null) {
1784 forceLayout(mView);
1785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 requestLayout();
1787 }
1788 break;
1789 case WINDOW_FOCUS_CHANGED: {
1790 if (mAdded) {
1791 boolean hasWindowFocus = msg.arg1 != 0;
1792 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1793 if (hasWindowFocus) {
1794 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001795 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001796
Romain Guyb051e892010-09-28 19:09:36 -07001797 if (mAttachInfo.mHardwareRenderer != null) {
1798 mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
1799 mAttachInfo, mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 }
1801 }
Romain Guy8506ab42009-06-11 17:35:47 -07001802
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 mLastWasImTarget = WindowManager.LayoutParams
1804 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 InputMethodManager imm = InputMethodManager.peekInstance();
1807 if (mView != null) {
1808 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1809 imm.startGettingWindowFocus(mView);
1810 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001811 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 mView.dispatchWindowFocusChanged(hasWindowFocus);
1813 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 // Note: must be done after the focus change callbacks,
1816 // so all of the view state is set up correctly.
1817 if (hasWindowFocus) {
1818 if (imm != null && mLastWasImTarget) {
1819 imm.onWindowFocus(mView, mView.findFocus(),
1820 mWindowAttributes.softInputMode,
1821 !mHasHadWindowFocus, mWindowAttributes.flags);
1822 }
1823 // Clear the forward bit. We can just do this directly, since
1824 // the window manager doesn't care about it.
1825 mWindowAttributes.softInputMode &=
1826 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1827 ((WindowManager.LayoutParams)mView.getLayoutParams())
1828 .softInputMode &=
1829 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1830 mHasHadWindowFocus = true;
1831 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001832
1833 if (hasWindowFocus && mView != null) {
1834 sendAccessibilityEvents();
1835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 }
1837 } break;
1838 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07001839 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001841 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001843 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07001845 KeyEvent event = (KeyEvent)msg.obj;
1846 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
1847 // The IME is trying to say this event is from the
1848 // system! Bad bad bad!
Romain Guy812ccbe2010-06-01 14:07:24 -07001849 event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project10592532009-03-18 17:39:46 -07001850 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 deliverKeyEventToViewHierarchy((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07001852 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 case FINISH_INPUT_CONNECTION: {
1854 InputMethodManager imm = InputMethodManager.peekInstance();
1855 if (imm != null) {
1856 imm.reportFinishInputConnection((InputConnection)msg.obj);
1857 }
1858 } break;
1859 case CHECK_FOCUS: {
1860 InputMethodManager imm = InputMethodManager.peekInstance();
1861 if (imm != null) {
1862 imm.checkFocus();
1863 }
1864 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001865 case CLOSE_SYSTEM_DIALOGS: {
1866 if (mView != null) {
1867 mView.onCloseSystemDialogs((String)msg.obj);
1868 }
1869 } break;
Christopher Tatea53146c2010-09-07 11:57:52 -07001870 case DISPATCH_DRAG_EVENT: {
1871 handleDragEvent((DragEvent)msg.obj);
1872 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873 }
1874 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001875
Jeff Brown93ed4e32010-09-23 13:51:48 -07001876 private void startInputEvent(Runnable finishedCallback) {
1877 if (mFinishedCallback != null) {
1878 Slog.w(TAG, "Received a new input event from the input queue but there is "
1879 + "already an unfinished input event in progress.");
1880 }
1881
1882 mFinishedCallback = finishedCallback;
1883 }
1884
1885 private void finishInputEvent() {
1886 if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished");
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001887
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001888 if (mFinishedCallback != null) {
1889 mFinishedCallback.run();
1890 mFinishedCallback = null;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001891 } else {
Jeff Brown93ed4e32010-09-23 13:51:48 -07001892 Slog.w(TAG, "Attempted to tell the input queue that the current input event "
1893 + "is finished but there is no input event actually in progress.");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001894 }
1895 }
1896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 /**
1898 * Something in the current window tells us we need to change the touch mode. For
1899 * example, we are not in touch mode, and the user touches the screen.
1900 *
1901 * If the touch mode has changed, tell the window manager, and handle it locally.
1902 *
1903 * @param inTouchMode Whether we want to be in touch mode.
1904 * @return True if the touch mode changed and focus changed was changed as a result
1905 */
1906 boolean ensureTouchMode(boolean inTouchMode) {
1907 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
1908 + "touch mode is " + mAttachInfo.mInTouchMode);
1909 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1910
1911 // tell the window manager
1912 try {
1913 sWindowSession.setInTouchMode(inTouchMode);
1914 } catch (RemoteException e) {
1915 throw new RuntimeException(e);
1916 }
1917
1918 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07001919 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 }
1921
1922 /**
1923 * Ensure that the touch mode for this window is set, and if it is changing,
1924 * take the appropriate action.
1925 * @param inTouchMode Whether we want to be in touch mode.
1926 * @return True if the touch mode changed and focus changed was changed as a result
1927 */
Romain Guy2d4cff62010-04-09 15:39:00 -07001928 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
1930 + "touch mode is " + mAttachInfo.mInTouchMode);
1931
1932 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1933
1934 mAttachInfo.mInTouchMode = inTouchMode;
1935 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
1936
Romain Guy2d4cff62010-04-09 15:39:00 -07001937 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939
1940 private boolean enterTouchMode() {
1941 if (mView != null) {
1942 if (mView.hasFocus()) {
1943 // note: not relying on mFocusedView here because this could
1944 // be when the window is first being added, and mFocused isn't
1945 // set yet.
1946 final View focused = mView.findFocus();
1947 if (focused != null && !focused.isFocusableInTouchMode()) {
1948
1949 final ViewGroup ancestorToTakeFocus =
1950 findAncestorToTakeFocusInTouchMode(focused);
1951 if (ancestorToTakeFocus != null) {
1952 // there is an ancestor that wants focus after its descendants that
1953 // is focusable in touch mode.. give it focus
1954 return ancestorToTakeFocus.requestFocus();
1955 } else {
1956 // nothing appropriate to have focus in touch mode, clear it out
1957 mView.unFocus();
1958 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
1959 mFocusedView = null;
1960 return true;
1961 }
1962 }
1963 }
1964 }
1965 return false;
1966 }
1967
1968
1969 /**
1970 * Find an ancestor of focused that wants focus after its descendants and is
1971 * focusable in touch mode.
1972 * @param focused The currently focused view.
1973 * @return An appropriate view, or null if no such view exists.
1974 */
1975 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
1976 ViewParent parent = focused.getParent();
1977 while (parent instanceof ViewGroup) {
1978 final ViewGroup vgParent = (ViewGroup) parent;
1979 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
1980 && vgParent.isFocusableInTouchMode()) {
1981 return vgParent;
1982 }
1983 if (vgParent.isRootNamespace()) {
1984 return null;
1985 } else {
1986 parent = vgParent.getParent();
1987 }
1988 }
1989 return null;
1990 }
1991
1992 private boolean leaveTouchMode() {
1993 if (mView != null) {
1994 if (mView.hasFocus()) {
1995 // i learned the hard way to not trust mFocusedView :)
1996 mFocusedView = mView.findFocus();
1997 if (!(mFocusedView instanceof ViewGroup)) {
1998 // some view has focus, let it keep it
1999 return false;
2000 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2001 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2002 // some view group has focus, and doesn't prefer its children
2003 // over itself for focus, so let them keep it.
2004 return false;
2005 }
2006 }
2007
2008 // find the best view to give focus to in this brave new non-touch-mode
2009 // world
2010 final View focused = focusSearch(null, View.FOCUS_DOWN);
2011 if (focused != null) {
2012 return focused.requestFocus(View.FOCUS_DOWN);
2013 }
2014 }
2015 return false;
2016 }
2017
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002018 private void deliverPointerEvent(MotionEvent event) {
2019 if (mTranslator != null) {
2020 mTranslator.translateEventInScreenToAppWindow(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002022
2023 boolean handled;
2024 if (mView != null && mAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002026 // enter touch mode on the down
2027 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
2028 if (isDown) {
2029 ensureTouchMode(true);
2030 }
2031 if(Config.LOGV) {
2032 captureMotionLog("captureDispatchPointer", event);
2033 }
2034 if (mCurScrollY != 0) {
2035 event.offsetLocation(0, mCurScrollY);
2036 }
2037 if (MEASURE_LATENCY) {
2038 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
2039 }
Christopher Tate2c095f32010-10-04 14:13:40 -07002040 // cache for possible drag-initiation
2041 mLastTouchPoint.x = event.getRawX();
2042 mLastTouchPoint.y = event.getRawY();
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002043 handled = mView.dispatchTouchEvent(event);
2044 if (MEASURE_LATENCY) {
2045 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
2046 }
2047 if (!handled && isDown) {
2048 int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
2049
2050 final int edgeFlags = event.getEdgeFlags();
2051 int direction = View.FOCUS_UP;
2052 int x = (int)event.getX();
2053 int y = (int)event.getY();
2054 final int[] deltas = new int[2];
2055
2056 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
2057 direction = View.FOCUS_DOWN;
2058 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2059 deltas[0] = edgeSlop;
2060 x += edgeSlop;
2061 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2062 deltas[0] = -edgeSlop;
2063 x -= edgeSlop;
2064 }
2065 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
2066 direction = View.FOCUS_UP;
2067 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2068 deltas[0] = edgeSlop;
2069 x += edgeSlop;
2070 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2071 deltas[0] = -edgeSlop;
2072 x -= edgeSlop;
2073 }
2074 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2075 direction = View.FOCUS_RIGHT;
2076 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2077 direction = View.FOCUS_LEFT;
2078 }
2079
2080 if (edgeFlags != 0 && mView instanceof ViewGroup) {
2081 View nearest = FocusFinder.getInstance().findNearestTouchable(
2082 ((ViewGroup) mView), x, y, direction, deltas);
2083 if (nearest != null) {
2084 event.offsetLocation(deltas[0], deltas[1]);
2085 event.setEdgeFlags(0);
2086 mView.dispatchTouchEvent(event);
2087 }
2088 }
2089 }
2090 }
2091 }
2092
2093 private void deliverTrackballEvent(MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002094 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2095
2096 boolean handled = false;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002097 if (mView != null && mAdded) {
2098 handled = mView.dispatchTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 if (handled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 // If we reach this, we delivered a trackball event to mView and
2101 // mView consumed it. Because we will not translate the trackball
2102 // event into a key event, touch mode will not exit, so we exit
2103 // touch mode here.
2104 ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 return;
2106 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002107
2108 // Otherwise we could do something here, like changing the focus
2109 // or something?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 }
2111
2112 final TrackballAxis x = mTrackballAxisX;
2113 final TrackballAxis y = mTrackballAxisY;
2114
2115 long curTime = SystemClock.uptimeMillis();
2116 if ((mLastTrackballTime+MAX_TRACKBALL_DELAY) < curTime) {
2117 // It has been too long since the last movement,
2118 // so restart at the beginning.
2119 x.reset(0);
2120 y.reset(0);
2121 mLastTrackballTime = curTime;
2122 }
2123
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002124 final int action = event.getAction();
2125 final int metastate = event.getMetaState();
2126 switch (action) {
2127 case MotionEvent.ACTION_DOWN:
2128 x.reset(2);
2129 y.reset(2);
2130 deliverKeyEvent(new KeyEvent(curTime, curTime,
2131 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER,
2132 0, metastate), false);
2133 break;
2134 case MotionEvent.ACTION_UP:
2135 x.reset(2);
2136 y.reset(2);
2137 deliverKeyEvent(new KeyEvent(curTime, curTime,
2138 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER,
2139 0, metastate), false);
2140 break;
2141 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002143 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2144 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2145 + " move=" + event.getX()
2146 + " / Y=" + y.position + " step="
2147 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2148 + " move=" + event.getY());
2149 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2150 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002152 // Generate DPAD events based on the trackball movement.
2153 // We pick the axis that has moved the most as the direction of
2154 // the DPAD. When we generate DPAD events for one axis, then the
2155 // other axis is reset -- we don't want to perform DPAD jumps due
2156 // to slight movements in the trackball when making major movements
2157 // along the other axis.
2158 int keycode = 0;
2159 int movement = 0;
2160 float accel = 1;
2161 if (xOff > yOff) {
2162 movement = x.generate((2/event.getXPrecision()));
2163 if (movement != 0) {
2164 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2165 : KeyEvent.KEYCODE_DPAD_LEFT;
2166 accel = x.acceleration;
2167 y.reset(2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002169 } else if (yOff > 0) {
2170 movement = y.generate((2/event.getYPrecision()));
2171 if (movement != 0) {
2172 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2173 : KeyEvent.KEYCODE_DPAD_UP;
2174 accel = y.acceleration;
2175 x.reset(2);
2176 }
2177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002179 if (keycode != 0) {
2180 if (movement < 0) movement = -movement;
2181 int accelMovement = (int)(movement * accel);
2182 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2183 + " accelMovement=" + accelMovement
2184 + " accel=" + accel);
2185 if (accelMovement > movement) {
2186 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2187 + keycode);
2188 movement--;
2189 deliverKeyEvent(new KeyEvent(curTime, curTime,
2190 KeyEvent.ACTION_MULTIPLE, keycode,
2191 accelMovement-movement, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002193 while (movement > 0) {
2194 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2195 + keycode);
2196 movement--;
2197 curTime = SystemClock.uptimeMillis();
2198 deliverKeyEvent(new KeyEvent(curTime, curTime,
2199 KeyEvent.ACTION_DOWN, keycode, 0, event.getMetaState()), false);
2200 deliverKeyEvent(new KeyEvent(curTime, curTime,
2201 KeyEvent.ACTION_UP, keycode, 0, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002203 mLastTrackballTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002204 }
2205 }
2206
2207 /**
2208 * @param keyCode The key code
2209 * @return True if the key is directional.
2210 */
2211 static boolean isDirectional(int keyCode) {
2212 switch (keyCode) {
2213 case KeyEvent.KEYCODE_DPAD_LEFT:
2214 case KeyEvent.KEYCODE_DPAD_RIGHT:
2215 case KeyEvent.KEYCODE_DPAD_UP:
2216 case KeyEvent.KEYCODE_DPAD_DOWN:
2217 return true;
2218 }
2219 return false;
2220 }
2221
2222 /**
2223 * Returns true if this key is a keyboard key.
2224 * @param keyEvent The key event.
2225 * @return whether this key is a keyboard key.
2226 */
2227 private static boolean isKeyboardKey(KeyEvent keyEvent) {
2228 final int convertedKey = keyEvent.getUnicodeChar();
2229 return convertedKey > 0;
2230 }
2231
2232
2233
2234 /**
2235 * See if the key event means we should leave touch mode (and leave touch
2236 * mode if so).
2237 * @param event The key event.
2238 * @return Whether this key event should be consumed (meaning the act of
2239 * leaving touch mode alone is considered the event).
2240 */
2241 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Adam Powell51a6bee2010-03-15 14:07:28 -07002242 final int action = event.getAction();
2243 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 return false;
2245 }
2246 if ((event.getFlags()&KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2247 return false;
2248 }
2249
2250 // only relevant if we are in touch mode
2251 if (!mAttachInfo.mInTouchMode) {
2252 return false;
2253 }
2254
2255 // if something like an edit text has focus and the user is typing,
2256 // leave touch mode
2257 //
2258 // note: the condition of not being a keyboard key is kind of a hacky
2259 // approximation of whether we think the focused view will want the
2260 // key; if we knew for sure whether the focused view would consume
2261 // the event, that would be better.
2262 if (isKeyboardKey(event) && mView != null && mView.hasFocus()) {
2263 mFocusedView = mView.findFocus();
2264 if ((mFocusedView instanceof ViewGroup)
2265 && ((ViewGroup) mFocusedView).getDescendantFocusability() ==
2266 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2267 // something has focus, but is holding it weakly as a container
2268 return false;
2269 }
2270 if (ensureTouchMode(false)) {
2271 throw new IllegalStateException("should not have changed focus "
2272 + "when leaving touch mode while a view has focus.");
2273 }
2274 return false;
2275 }
2276
2277 if (isDirectional(event.getKeyCode())) {
2278 // no view has focus, so we leave touch mode (and find something
2279 // to give focus to). the event is consumed if we were able to
2280 // find something to give focus to.
2281 return ensureTouchMode(false);
2282 }
2283 return false;
2284 }
2285
2286 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002287 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 */
2289 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002290 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002291 if (ev == null ||
2292 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2293 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002294 }
Romain Guy8506ab42009-06-11 17:35:47 -07002295
2296 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 sb.append(ev.getDownTime()).append(',');
2298 sb.append(ev.getEventTime()).append(',');
2299 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002300 sb.append(ev.getX()).append(',');
2301 sb.append(ev.getY()).append(',');
2302 sb.append(ev.getPressure()).append(',');
2303 sb.append(ev.getSize()).append(',');
2304 sb.append(ev.getMetaState()).append(',');
2305 sb.append(ev.getXPrecision()).append(',');
2306 sb.append(ev.getYPrecision()).append(',');
2307 sb.append(ev.getDeviceId()).append(',');
2308 sb.append(ev.getEdgeFlags());
2309 Log.d(TAG, sb.toString());
2310 }
2311 /**
2312 * log motion events
2313 */
2314 private static void captureKeyLog(String subTag, KeyEvent ev) {
2315 //check dynamic switch
2316 if (ev == null ||
2317 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2318 return;
2319 }
2320 StringBuilder sb = new StringBuilder(subTag + ": ");
2321 sb.append(ev.getDownTime()).append(',');
2322 sb.append(ev.getEventTime()).append(',');
2323 sb.append(ev.getAction()).append(',');
2324 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 sb.append(ev.getRepeatCount()).append(',');
2326 sb.append(ev.getMetaState()).append(',');
2327 sb.append(ev.getDeviceId()).append(',');
2328 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002329 Log.d(TAG, sb.toString());
2330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331
2332 int enqueuePendingEvent(Object event, boolean sendDone) {
2333 int seq = mPendingEventSeq+1;
2334 if (seq < 0) seq = 0;
2335 mPendingEventSeq = seq;
2336 mPendingEvents.put(seq, event);
2337 return sendDone ? seq : -seq;
2338 }
2339
2340 Object retrievePendingEvent(int seq) {
2341 if (seq < 0) seq = -seq;
2342 Object event = mPendingEvents.get(seq);
2343 if (event != null) {
2344 mPendingEvents.remove(seq);
2345 }
2346 return event;
2347 }
Romain Guy8506ab42009-06-11 17:35:47 -07002348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
2350 // If mView is null, we just consume the key event because it doesn't
2351 // make sense to do anything else with it.
Romain Guy812ccbe2010-06-01 14:07:24 -07002352 boolean handled = mView == null || mView.dispatchKeyEventPreIme(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 if (handled) {
2354 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002355 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 }
2357 return;
2358 }
2359 // If it is possible for this window to interact with the input
2360 // method window, then we want to first dispatch our key events
2361 // to the input method.
2362 if (mLastWasImTarget) {
2363 InputMethodManager imm = InputMethodManager.peekInstance();
2364 if (imm != null && mView != null) {
2365 int seq = enqueuePendingEvent(event, sendDone);
2366 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2367 + seq + " event=" + event);
2368 imm.dispatchKeyEvent(mView.getContext(), seq, event,
2369 mInputMethodCallback);
2370 return;
2371 }
2372 }
2373 deliverKeyEventToViewHierarchy(event, sendDone);
2374 }
2375
2376 void handleFinishedEvent(int seq, boolean handled) {
2377 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2378 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2379 + " handled=" + handled + " event=" + event);
2380 if (event != null) {
2381 final boolean sendDone = seq >= 0;
2382 if (!handled) {
2383 deliverKeyEventToViewHierarchy(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002384 } else if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002385 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002386 } else {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002387 Log.w(TAG, "handleFinishedEvent(seq=" + seq
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002388 + " handled=" + handled + " ev=" + event
2389 + ") neither delivering nor finishing key");
2390 }
2391 }
2392 }
Romain Guy8506ab42009-06-11 17:35:47 -07002393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 private void deliverKeyEventToViewHierarchy(KeyEvent event, boolean sendDone) {
2395 try {
2396 if (mView != null && mAdded) {
2397 final int action = event.getAction();
2398 boolean isDown = (action == KeyEvent.ACTION_DOWN);
2399
2400 if (checkForLeavingTouchModeAndConsume(event)) {
2401 return;
Romain Guy8506ab42009-06-11 17:35:47 -07002402 }
2403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 if (Config.LOGV) {
2405 captureKeyLog("captureDispatchKeyEvent", event);
2406 }
2407 boolean keyHandled = mView.dispatchKeyEvent(event);
2408
2409 if (!keyHandled && isDown) {
2410 int direction = 0;
2411 switch (event.getKeyCode()) {
2412 case KeyEvent.KEYCODE_DPAD_LEFT:
2413 direction = View.FOCUS_LEFT;
2414 break;
2415 case KeyEvent.KEYCODE_DPAD_RIGHT:
2416 direction = View.FOCUS_RIGHT;
2417 break;
2418 case KeyEvent.KEYCODE_DPAD_UP:
2419 direction = View.FOCUS_UP;
2420 break;
2421 case KeyEvent.KEYCODE_DPAD_DOWN:
2422 direction = View.FOCUS_DOWN;
2423 break;
2424 }
2425
2426 if (direction != 0) {
2427
2428 View focused = mView != null ? mView.findFocus() : null;
2429 if (focused != null) {
2430 View v = focused.focusSearch(direction);
2431 boolean focusPassed = false;
2432 if (v != null && v != focused) {
2433 // do the math the get the interesting rect
2434 // of previous focused into the coord system of
2435 // newly focused view
2436 focused.getFocusedRect(mTempRect);
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07002437 if (mView instanceof ViewGroup) {
2438 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2439 focused, mTempRect);
2440 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2441 v, mTempRect);
2442 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 focusPassed = v.requestFocus(direction, mTempRect);
2444 }
2445
2446 if (!focusPassed) {
2447 mView.dispatchUnhandledMove(focused, direction);
2448 } else {
2449 playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
2450 }
2451 }
2452 }
2453 }
2454 }
2455
2456 } finally {
2457 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002458 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 }
2460 // Let the exception fall through -- the looper will catch
2461 // it and take care of the bad app for us.
2462 }
2463 }
2464
Christopher Tatea53146c2010-09-07 11:57:52 -07002465 /* drag/drop */
2466 private void handleDragEvent(DragEvent event) {
2467 // From the root, only drag start/end/location are dispatched. entered/exited
2468 // are determined and dispatched by the viewgroup hierarchy, who then report
2469 // that back here for ultimate reporting back to the framework.
2470 if (mView != null && mAdded) {
2471 final int what = event.mAction;
2472
2473 if (what == DragEvent.ACTION_DRAG_EXITED) {
2474 // A direct EXITED event means that the window manager knows we've just crossed
2475 // a window boundary, so the current drag target within this one must have
2476 // just been exited. Send it the usual notifications and then we're done
2477 // for now.
2478 setDragFocus(event, null);
2479 } else {
2480 // Cache the drag description when the operation starts, then fill it in
2481 // on subsequent calls as a convenience
2482 if (what == DragEvent.ACTION_DRAG_STARTED) {
2483 mDragDescription = event.mClipDescription;
2484 } else {
2485 event.mClipDescription = mDragDescription;
2486 }
2487
2488 // For events with a [screen] location, translate into window coordinates
2489 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
2490 mDragPoint.set(event.mX, event.mY);
2491 if (mTranslator != null) {
2492 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
2493 }
2494
2495 if (mCurScrollY != 0) {
2496 mDragPoint.offset(0, mCurScrollY);
2497 }
2498
2499 event.mX = mDragPoint.x;
2500 event.mY = mDragPoint.y;
2501 }
2502
2503 // Remember who the current drag target is pre-dispatch
2504 final View prevDragView = mCurrentDragView;
2505
2506 // Now dispatch the drag/drop event
2507 mView.dispatchDragEvent(event);
2508
2509 // If we changed apparent drag target, tell the OS about it
2510 if (prevDragView != mCurrentDragView) {
2511 try {
2512 if (prevDragView != null) {
2513 sWindowSession.dragRecipientExited(mWindow);
2514 }
2515 if (mCurrentDragView != null) {
2516 sWindowSession.dragRecipientEntered(mWindow);
2517 }
2518 } catch (RemoteException e) {
2519 Slog.e(TAG, "Unable to note drag target change");
2520 }
2521 mCurrentDragView = prevDragView;
2522 }
2523 }
2524 }
2525 event.recycle();
2526 }
2527
Christopher Tate2c095f32010-10-04 14:13:40 -07002528 public void getLastTouchPoint(Point outLocation) {
2529 outLocation.x = (int) mLastTouchPoint.x;
2530 outLocation.y = (int) mLastTouchPoint.y;
2531 }
2532
Christopher Tatea53146c2010-09-07 11:57:52 -07002533 public void setDragFocus(DragEvent event, View newDragTarget) {
2534 final int action = event.mAction;
2535 // If we've dragged off of a view, send it the EXITED message
2536 if (mCurrentDragView != newDragTarget) {
2537 if (mCurrentDragView != null) {
2538 event.mAction = DragEvent.ACTION_DRAG_EXITED;
2539 mCurrentDragView.dispatchDragEvent(event);
2540 }
2541 }
2542 // If we've dragged over a new view, send it the ENTERED message
2543 if (newDragTarget != null) {
2544 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
2545 newDragTarget.dispatchDragEvent(event);
2546 }
2547 mCurrentDragView = newDragTarget;
2548 event.mAction = action; // restore the event's original state
2549 }
2550
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002551 private AudioManager getAudioManager() {
2552 if (mView == null) {
2553 throw new IllegalStateException("getAudioManager called when there is no mView");
2554 }
2555 if (mAudioManager == null) {
2556 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2557 }
2558 return mAudioManager;
2559 }
2560
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002561 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2562 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002563
2564 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002565 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002566 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002567 restore = true;
2568 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002569 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002570 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002571 if (params != null) {
2572 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002573 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002574 mPendingConfiguration.seq = 0;
Dianne Hackbornf123e492010-09-24 11:16:23 -07002575 //Log.d(TAG, ">>>>>> CALLING relayout");
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002576 int relayoutResult = sWindowSession.relayout(
2577 mWindow, params,
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002578 (int) (mView.mMeasuredWidth * appScale + 0.5f),
2579 (int) (mView.mMeasuredHeight * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002580 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002581 mPendingContentInsets, mPendingVisibleInsets,
2582 mPendingConfiguration, mSurface);
Dianne Hackbornf123e492010-09-24 11:16:23 -07002583 //Log.d(TAG, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002584 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002585 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002586 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002587
2588 if (mTranslator != null) {
2589 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2590 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2591 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002592 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002593 return relayoutResult;
2594 }
Romain Guy8506ab42009-06-11 17:35:47 -07002595
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002597 * {@inheritDoc}
2598 */
2599 public void playSoundEffect(int effectId) {
2600 checkThread();
2601
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002602 try {
2603 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002605 switch (effectId) {
2606 case SoundEffectConstants.CLICK:
2607 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2608 return;
2609 case SoundEffectConstants.NAVIGATION_DOWN:
2610 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2611 return;
2612 case SoundEffectConstants.NAVIGATION_LEFT:
2613 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2614 return;
2615 case SoundEffectConstants.NAVIGATION_RIGHT:
2616 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2617 return;
2618 case SoundEffectConstants.NAVIGATION_UP:
2619 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2620 return;
2621 default:
2622 throw new IllegalArgumentException("unknown effect id " + effectId +
2623 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2624 }
2625 } catch (IllegalStateException e) {
2626 // Exception thrown by getAudioManager() when mView is null
2627 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2628 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 }
2630 }
2631
2632 /**
2633 * {@inheritDoc}
2634 */
2635 public boolean performHapticFeedback(int effectId, boolean always) {
2636 try {
2637 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2638 } catch (RemoteException e) {
2639 return false;
2640 }
2641 }
2642
2643 /**
2644 * {@inheritDoc}
2645 */
2646 public View focusSearch(View focused, int direction) {
2647 checkThread();
2648 if (!(mView instanceof ViewGroup)) {
2649 return null;
2650 }
2651 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2652 }
2653
2654 public void debug() {
2655 mView.debug();
2656 }
2657
2658 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002659 if (immediate) {
2660 doDie();
2661 } else {
2662 sendEmptyMessage(DIE);
2663 }
2664 }
2665
2666 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 checkThread();
Jeff Brownb75fa302010-07-15 23:47:29 -07002668 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002669 synchronized (this) {
2670 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07002671 destroyHardwareRenderer();
2672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002673 int viewVisibility = mView.getVisibility();
2674 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2675 if (mWindowAttributesChanged || viewVisibilityChanged) {
2676 // If layout params have been changed, first give them
2677 // to the window manager to make sure it has the correct
2678 // animation info.
2679 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002680 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2681 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 sWindowSession.finishDrawing(mWindow);
2683 }
2684 } catch (RemoteException e) {
2685 }
2686 }
2687
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002688 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 }
2690 if (mAdded) {
2691 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002692 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 }
2694 }
2695 }
2696
Romain Guy29d89972010-09-22 16:10:57 -07002697 private void destroyHardwareRenderer() {
Romain Guyb051e892010-09-28 19:09:36 -07002698 if (mAttachInfo.mHardwareRenderer != null) {
2699 mAttachInfo.mHardwareRenderer.destroy(true);
2700 mAttachInfo.mHardwareRenderer = null;
Romain Guy29d89972010-09-22 16:10:57 -07002701 mAttachInfo.mHardwareAccelerated = false;
2702 }
2703 }
2704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002705 public void dispatchFinishedEvent(int seq, boolean handled) {
2706 Message msg = obtainMessage(FINISHED_EVENT);
2707 msg.arg1 = seq;
2708 msg.arg2 = handled ? 1 : 0;
2709 sendMessage(msg);
2710 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002711
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002713 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2715 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2716 + " visibleInsets=" + visibleInsets.toShortString()
2717 + " reportDraw=" + reportDraw);
2718 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002719 if (mTranslator != null) {
2720 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2721 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2722 w *= mTranslator.applicationInvertedScale;
2723 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002724 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002725 msg.arg1 = w;
2726 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002727 ResizedInfo ri = new ResizedInfo();
2728 ri.coveredInsets = new Rect(coveredInsets);
2729 ri.visibleInsets = new Rect(visibleInsets);
2730 ri.newConfig = newConfig;
2731 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 sendMessage(msg);
2733 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002734
2735 private Runnable mFinishedCallback;
2736
2737 private final InputHandler mInputHandler = new InputHandler() {
2738 public void handleKey(KeyEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002739 startInputEvent(finishedCallback);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002740 dispatchKey(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002741 }
2742
Jeff Brownc5ed5912010-07-14 18:48:53 -07002743 public void handleMotion(MotionEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002744 startInputEvent(finishedCallback);
2745 dispatchMotion(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002746 }
2747 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002748
2749 public void dispatchKey(KeyEvent event) {
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002750 dispatchKey(event, false);
2751 }
2752
2753 private void dispatchKey(KeyEvent event, boolean sendDone) {
2754 //noinspection ConstantConditions
2755 if (false && event.getAction() == KeyEvent.ACTION_DOWN) {
2756 if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
Romain Guy812ccbe2010-06-01 14:07:24 -07002757 if (DBG) Log.d("keydisp", "===================================================");
2758 if (DBG) Log.d("keydisp", "Focused view Hierarchy is:");
2759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002760 debug();
2761
Romain Guy812ccbe2010-06-01 14:07:24 -07002762 if (DBG) Log.d("keydisp", "===================================================");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 }
2764 }
2765
2766 Message msg = obtainMessage(DISPATCH_KEY);
2767 msg.obj = event;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002768 msg.arg1 = sendDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002769
2770 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07002771 TAG, "sending key " + event + " to " + mView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772
2773 sendMessageAtTime(msg, event.getEventTime());
2774 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002775
2776 public void dispatchMotion(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002777 dispatchMotion(event, false);
2778 }
2779
2780 private void dispatchMotion(MotionEvent event, boolean sendDone) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002781 int source = event.getSource();
2782 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002783 dispatchPointer(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002784 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002785 dispatchTrackball(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002786 } else {
2787 // TODO
2788 Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event);
Jeff Brown93ed4e32010-09-23 13:51:48 -07002789 if (sendDone) {
2790 finishInputEvent();
2791 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002792 }
2793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002795 public void dispatchPointer(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002796 dispatchPointer(event, false);
2797 }
2798
2799 private void dispatchPointer(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002800 Message msg = obtainMessage(DISPATCH_POINTER);
2801 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002802 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002803 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002804 }
2805
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002806 public void dispatchTrackball(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002807 dispatchTrackball(event, false);
2808 }
2809
2810 private void dispatchTrackball(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2812 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002813 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002814 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002815 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 public void dispatchAppVisibility(boolean visible) {
2818 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
2819 msg.arg1 = visible ? 1 : 0;
2820 sendMessage(msg);
2821 }
2822
2823 public void dispatchGetNewSurface() {
2824 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
2825 sendMessage(msg);
2826 }
2827
2828 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2829 Message msg = Message.obtain();
2830 msg.what = WINDOW_FOCUS_CHANGED;
2831 msg.arg1 = hasFocus ? 1 : 0;
2832 msg.arg2 = inTouchMode ? 1 : 0;
2833 sendMessage(msg);
2834 }
2835
Dianne Hackbornffa42482009-09-23 22:20:11 -07002836 public void dispatchCloseSystemDialogs(String reason) {
2837 Message msg = Message.obtain();
2838 msg.what = CLOSE_SYSTEM_DIALOGS;
2839 msg.obj = reason;
2840 sendMessage(msg);
2841 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002842
2843 public void dispatchDragEvent(DragEvent event) {
2844 Message msg = obtainMessage(DISPATCH_DRAG_EVENT, event);
2845 sendMessage(msg);
2846 }
2847
svetoslavganov75986cf2009-05-14 22:28:01 -07002848 /**
2849 * The window is getting focus so if there is anything focused/selected
2850 * send an {@link AccessibilityEvent} to announce that.
2851 */
2852 private void sendAccessibilityEvents() {
2853 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
2854 return;
2855 }
2856 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2857 View focusedView = mView.findFocus();
2858 if (focusedView != null && focusedView != mView) {
2859 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2860 }
2861 }
2862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002863 public boolean showContextMenuForChild(View originalView) {
2864 return false;
2865 }
2866
Adam Powell6e346362010-07-23 10:18:23 -07002867 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
2868 return null;
2869 }
2870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002871 public void createContextMenu(ContextMenu menu) {
2872 }
2873
2874 public void childDrawableStateChanged(View child) {
2875 }
2876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 void checkThread() {
2878 if (mThread != Thread.currentThread()) {
2879 throw new CalledFromWrongThreadException(
2880 "Only the original thread that created a view hierarchy can touch its views.");
2881 }
2882 }
2883
2884 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2885 // ViewRoot never intercepts touch event, so this can be a no-op
2886 }
2887
2888 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
2889 boolean immediate) {
2890 return scrollToRectOrFocus(rectangle, immediate);
2891 }
Romain Guy8506ab42009-06-11 17:35:47 -07002892
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002893 class TakenSurfaceHolder extends BaseSurfaceHolder {
2894 @Override
2895 public boolean onAllowLockCanvas() {
2896 return mDrawingAllowed;
2897 }
2898
2899 @Override
2900 public void onRelayoutContainer() {
2901 // Not currently interesting -- from changing between fixed and layout size.
2902 }
2903
2904 public void setFormat(int format) {
2905 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
2906 }
2907
2908 public void setType(int type) {
2909 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
2910 }
2911
2912 @Override
2913 public void onUpdateSurface() {
2914 // We take care of format and type changes on our own.
2915 throw new IllegalStateException("Shouldn't be here");
2916 }
2917
2918 public boolean isCreating() {
2919 return mIsCreating;
2920 }
2921
2922 @Override
2923 public void setFixedSize(int width, int height) {
2924 throw new UnsupportedOperationException(
2925 "Currently only support sizing from layout");
2926 }
2927
2928 public void setKeepScreenOn(boolean screenOn) {
2929 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
2930 }
2931 }
2932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002933 static class InputMethodCallback extends IInputMethodCallback.Stub {
2934 private WeakReference<ViewRoot> mViewRoot;
2935
2936 public InputMethodCallback(ViewRoot viewRoot) {
2937 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2938 }
Romain Guy8506ab42009-06-11 17:35:47 -07002939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 public void finishedEvent(int seq, boolean handled) {
2941 final ViewRoot viewRoot = mViewRoot.get();
2942 if (viewRoot != null) {
2943 viewRoot.dispatchFinishedEvent(seq, handled);
2944 }
2945 }
2946
2947 public void sessionCreated(IInputMethodSession session) throws RemoteException {
2948 // Stub -- not for use in the client.
2949 }
2950 }
Romain Guy8506ab42009-06-11 17:35:47 -07002951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002953 private final WeakReference<ViewRoot> mViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002954
Romain Guyfb8b7632010-08-23 21:05:08 -07002955 W(ViewRoot viewRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2957 }
2958
Romain Guyfb8b7632010-08-23 21:05:08 -07002959 public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets,
2960 boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 final ViewRoot viewRoot = mViewRoot.get();
2962 if (viewRoot != null) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002963 viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 }
2965 }
2966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 public void dispatchAppVisibility(boolean visible) {
2968 final ViewRoot viewRoot = mViewRoot.get();
2969 if (viewRoot != null) {
2970 viewRoot.dispatchAppVisibility(visible);
2971 }
2972 }
2973
2974 public void dispatchGetNewSurface() {
2975 final ViewRoot viewRoot = mViewRoot.get();
2976 if (viewRoot != null) {
2977 viewRoot.dispatchGetNewSurface();
2978 }
2979 }
2980
2981 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2982 final ViewRoot viewRoot = mViewRoot.get();
2983 if (viewRoot != null) {
2984 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
2985 }
2986 }
2987
2988 private static int checkCallingPermission(String permission) {
2989 if (!Process.supportsProcesses()) {
2990 return PackageManager.PERMISSION_GRANTED;
2991 }
2992
2993 try {
2994 return ActivityManagerNative.getDefault().checkPermission(
2995 permission, Binder.getCallingPid(), Binder.getCallingUid());
2996 } catch (RemoteException e) {
2997 return PackageManager.PERMISSION_DENIED;
2998 }
2999 }
3000
3001 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3002 final ViewRoot viewRoot = mViewRoot.get();
3003 if (viewRoot != null) {
3004 final View view = viewRoot.mView;
3005 if (view != null) {
3006 if (checkCallingPermission(Manifest.permission.DUMP) !=
3007 PackageManager.PERMISSION_GRANTED) {
3008 throw new SecurityException("Insufficient permissions to invoke"
3009 + " executeCommand() from pid=" + Binder.getCallingPid()
3010 + ", uid=" + Binder.getCallingUid());
3011 }
3012
3013 OutputStream clientStream = null;
3014 try {
3015 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3016 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3017 } catch (IOException e) {
3018 e.printStackTrace();
3019 } finally {
3020 if (clientStream != null) {
3021 try {
3022 clientStream.close();
3023 } catch (IOException e) {
3024 e.printStackTrace();
3025 }
3026 }
3027 }
3028 }
3029 }
3030 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003031
Dianne Hackbornffa42482009-09-23 22:20:11 -07003032 public void closeSystemDialogs(String reason) {
3033 final ViewRoot viewRoot = mViewRoot.get();
3034 if (viewRoot != null) {
3035 viewRoot.dispatchCloseSystemDialogs(reason);
3036 }
3037 }
3038
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003039 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3040 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003041 if (sync) {
3042 try {
3043 sWindowSession.wallpaperOffsetsComplete(asBinder());
3044 } catch (RemoteException e) {
3045 }
3046 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003047 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003048
3049 public void dispatchWallpaperCommand(String action, int x, int y,
3050 int z, Bundle extras, boolean sync) {
3051 if (sync) {
3052 try {
3053 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3054 } catch (RemoteException e) {
3055 }
3056 }
3057 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003058
3059 /* Drag/drop */
3060 public void dispatchDragEvent(DragEvent event) {
3061 final ViewRoot viewRoot = mViewRoot.get();
3062 if (viewRoot != null) {
3063 viewRoot.dispatchDragEvent(event);
3064 }
3065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 }
3067
3068 /**
3069 * Maintains state information for a single trackball axis, generating
3070 * discrete (DPAD) movements based on raw trackball motion.
3071 */
3072 static final class TrackballAxis {
3073 /**
3074 * The maximum amount of acceleration we will apply.
3075 */
3076 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 /**
3079 * The maximum amount of time (in milliseconds) between events in order
3080 * for us to consider the user to be doing fast trackball movements,
3081 * and thus apply an acceleration.
3082 */
3083 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003085 /**
3086 * Scaling factor to the time (in milliseconds) between events to how
3087 * much to multiple/divide the current acceleration. When movement
3088 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3089 * FAST_MOVE_TIME it divides it.
3090 */
3091 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 float position;
3094 float absPosition;
3095 float acceleration = 1;
3096 long lastMoveTime = 0;
3097 int step;
3098 int dir;
3099 int nonAccelMovement;
3100
3101 void reset(int _step) {
3102 position = 0;
3103 acceleration = 1;
3104 lastMoveTime = 0;
3105 step = _step;
3106 dir = 0;
3107 }
3108
3109 /**
3110 * Add trackball movement into the state. If the direction of movement
3111 * has been reversed, the state is reset before adding the
3112 * movement (so that you don't have to compensate for any previously
3113 * collected movement before see the result of the movement in the
3114 * new direction).
3115 *
3116 * @return Returns the absolute value of the amount of movement
3117 * collected so far.
3118 */
3119 float collect(float off, long time, String axis) {
3120 long normTime;
3121 if (off > 0) {
3122 normTime = (long)(off * FAST_MOVE_TIME);
3123 if (dir < 0) {
3124 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3125 position = 0;
3126 step = 0;
3127 acceleration = 1;
3128 lastMoveTime = 0;
3129 }
3130 dir = 1;
3131 } else if (off < 0) {
3132 normTime = (long)((-off) * FAST_MOVE_TIME);
3133 if (dir > 0) {
3134 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3135 position = 0;
3136 step = 0;
3137 acceleration = 1;
3138 lastMoveTime = 0;
3139 }
3140 dir = -1;
3141 } else {
3142 normTime = 0;
3143 }
Romain Guy8506ab42009-06-11 17:35:47 -07003144
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 // The number of milliseconds between each movement that is
3146 // considered "normal" and will not result in any acceleration
3147 // or deceleration, scaled by the offset we have here.
3148 if (normTime > 0) {
3149 long delta = time - lastMoveTime;
3150 lastMoveTime = time;
3151 float acc = acceleration;
3152 if (delta < normTime) {
3153 // The user is scrolling rapidly, so increase acceleration.
3154 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3155 if (scale > 1) acc *= scale;
3156 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3157 + off + " normTime=" + normTime + " delta=" + delta
3158 + " scale=" + scale + " acc=" + acc);
3159 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3160 } else {
3161 // The user is scrolling slowly, so decrease acceleration.
3162 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3163 if (scale > 1) acc /= scale;
3164 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3165 + off + " normTime=" + normTime + " delta=" + delta
3166 + " scale=" + scale + " acc=" + acc);
3167 acceleration = acc > 1 ? acc : 1;
3168 }
3169 }
3170 position += off;
3171 return (absPosition = Math.abs(position));
3172 }
3173
3174 /**
3175 * Generate the number of discrete movement events appropriate for
3176 * the currently collected trackball movement.
3177 *
3178 * @param precision The minimum movement required to generate the
3179 * first discrete movement.
3180 *
3181 * @return Returns the number of discrete movements, either positive
3182 * or negative, or 0 if there is not enough trackball movement yet
3183 * for a discrete movement.
3184 */
3185 int generate(float precision) {
3186 int movement = 0;
3187 nonAccelMovement = 0;
3188 do {
3189 final int dir = position >= 0 ? 1 : -1;
3190 switch (step) {
3191 // If we are going to execute the first step, then we want
3192 // to do this as soon as possible instead of waiting for
3193 // a full movement, in order to make things look responsive.
3194 case 0:
3195 if (absPosition < precision) {
3196 return movement;
3197 }
3198 movement += dir;
3199 nonAccelMovement += dir;
3200 step = 1;
3201 break;
3202 // If we have generated the first movement, then we need
3203 // to wait for the second complete trackball motion before
3204 // generating the second discrete movement.
3205 case 1:
3206 if (absPosition < 2) {
3207 return movement;
3208 }
3209 movement += dir;
3210 nonAccelMovement += dir;
3211 position += dir > 0 ? -2 : 2;
3212 absPosition = Math.abs(position);
3213 step = 2;
3214 break;
3215 // After the first two, we generate discrete movements
3216 // consistently with the trackball, applying an acceleration
3217 // if the trackball is moving quickly. This is a simple
3218 // acceleration on top of what we already compute based
3219 // on how quickly the wheel is being turned, to apply
3220 // a longer increasing acceleration to continuous movement
3221 // in one direction.
3222 default:
3223 if (absPosition < 1) {
3224 return movement;
3225 }
3226 movement += dir;
3227 position += dir >= 0 ? -1 : 1;
3228 absPosition = Math.abs(position);
3229 float acc = acceleration;
3230 acc *= 1.1f;
3231 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3232 break;
3233 }
3234 } while (true);
3235 }
3236 }
3237
3238 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3239 public CalledFromWrongThreadException(String msg) {
3240 super(msg);
3241 }
3242 }
3243
3244 private SurfaceHolder mHolder = new SurfaceHolder() {
3245 // we only need a SurfaceHolder for opengl. it would be nice
3246 // to implement everything else though, especially the callback
3247 // support (opengl doesn't make use of it right now, but eventually
3248 // will).
3249 public Surface getSurface() {
3250 return mSurface;
3251 }
3252
3253 public boolean isCreating() {
3254 return false;
3255 }
3256
3257 public void addCallback(Callback callback) {
3258 }
3259
3260 public void removeCallback(Callback callback) {
3261 }
3262
3263 public void setFixedSize(int width, int height) {
3264 }
3265
3266 public void setSizeFromLayout() {
3267 }
3268
3269 public void setFormat(int format) {
3270 }
3271
3272 public void setType(int type) {
3273 }
3274
3275 public void setKeepScreenOn(boolean screenOn) {
3276 }
3277
3278 public Canvas lockCanvas() {
3279 return null;
3280 }
3281
3282 public Canvas lockCanvas(Rect dirty) {
3283 return null;
3284 }
3285
3286 public void unlockCanvasAndPost(Canvas canvas) {
3287 }
3288 public Rect getSurfaceFrame() {
3289 return null;
3290 }
3291 };
3292
3293 static RunQueue getRunQueue() {
3294 RunQueue rq = sRunQueues.get();
3295 if (rq != null) {
3296 return rq;
3297 }
3298 rq = new RunQueue();
3299 sRunQueues.set(rq);
3300 return rq;
3301 }
Romain Guy8506ab42009-06-11 17:35:47 -07003302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 /**
3304 * @hide
3305 */
3306 static final class RunQueue {
3307 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3308
3309 void post(Runnable action) {
3310 postDelayed(action, 0);
3311 }
3312
3313 void postDelayed(Runnable action, long delayMillis) {
3314 HandlerAction handlerAction = new HandlerAction();
3315 handlerAction.action = action;
3316 handlerAction.delay = delayMillis;
3317
3318 synchronized (mActions) {
3319 mActions.add(handlerAction);
3320 }
3321 }
3322
3323 void removeCallbacks(Runnable action) {
3324 final HandlerAction handlerAction = new HandlerAction();
3325 handlerAction.action = action;
3326
3327 synchronized (mActions) {
3328 final ArrayList<HandlerAction> actions = mActions;
3329
3330 while (actions.remove(handlerAction)) {
3331 // Keep going
3332 }
3333 }
3334 }
3335
3336 void executeActions(Handler handler) {
3337 synchronized (mActions) {
3338 final ArrayList<HandlerAction> actions = mActions;
3339 final int count = actions.size();
3340
3341 for (int i = 0; i < count; i++) {
3342 final HandlerAction handlerAction = actions.get(i);
3343 handler.postDelayed(handlerAction.action, handlerAction.delay);
3344 }
3345
Romain Guy15df6702009-08-17 20:17:30 -07003346 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 }
3348 }
3349
3350 private static class HandlerAction {
3351 Runnable action;
3352 long delay;
3353
3354 @Override
3355 public boolean equals(Object o) {
3356 if (this == o) return true;
3357 if (o == null || getClass() != o.getClass()) return false;
3358
3359 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003360 return !(action != null ? !action.equals(that.action) : that.action != null);
3361
3362 }
3363
3364 @Override
3365 public int hashCode() {
3366 int result = action != null ? action.hashCode() : 0;
3367 result = 31 * result + (int) (delay ^ (delay >>> 32));
3368 return result;
3369 }
3370 }
3371 }
3372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003373 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374}