blob: 2e368b811a286a03616149e0c989948131c48884 [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 Guy53ca03d2010-10-08 18:55:27 -0700475 mAttachInfo.mHardwareAccelerated = mAttachInfo.mHardwareRenderer != null;
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
Romain Guyc361da82010-10-25 15:29:10 -0700825 boolean hwInitialized = 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) {
Romain Guyc361da82010-10-25 15:29:10 -0700890 hwInitialized = 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;
Romain Guyc361da82010-10-25 15:29:10 -0700938
939 if (mAttachInfo.mHardwareRenderer != null) {
940 // This will bail out early if already initialized
941 mAttachInfo.mHardwareRenderer.initialize(mHolder);
942 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700943 }
944 if (surfaceChanged) {
945 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
946 lp.format, mWidth, mHeight);
947 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
948 if (callbacks != null) {
949 for (SurfaceHolder.Callback c : callbacks) {
950 c.surfaceChanged(mSurfaceHolder, lp.format,
951 mWidth, mHeight);
952 }
953 }
954 }
955 mIsCreating = false;
956 } else if (hadSurface) {
957 mSurfaceHolder.ungetCallbacks();
958 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
959 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
960 if (callbacks != null) {
961 for (SurfaceHolder.Callback c : callbacks) {
962 c.surfaceDestroyed(mSurfaceHolder);
963 }
964 }
965 mSurfaceHolder.mSurfaceLock.lock();
966 // Make surface invalid.
967 //mSurfaceHolder.mSurface.copyFrom(mSurface);
968 mSurfaceHolder.mSurface = new Surface();
969 mSurfaceHolder.mSurfaceLock.unlock();
970 }
971 }
Romain Guy53389bd2010-09-07 17:16:32 -0700972
Romain Guyc361da82010-10-25 15:29:10 -0700973 if (hwInitialized || (windowShouldResize && mAttachInfo.mHardwareRenderer != null)) {
Romain Guyb051e892010-09-28 19:09:36 -0700974 mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976
977 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Romain Guy2d4cff62010-04-09 15:39:00 -0700978 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 if (focusChangedDueToTouchMode || mWidth != host.mMeasuredWidth
980 || mHeight != host.mMeasuredHeight || contentInsetsChanged) {
981 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
982 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
983
984 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
985 + mWidth + " measuredWidth=" + host.mMeasuredWidth
986 + " mHeight=" + mHeight
987 + " measuredHeight" + host.mMeasuredHeight
988 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -0700989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 // Ask host how big it wants to be
991 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
992
993 // Implementation of weights from WindowManager.LayoutParams
994 // We just grow the dimensions as needed and re-measure if
995 // needs be
996 int width = host.mMeasuredWidth;
997 int height = host.mMeasuredHeight;
998 boolean measureAgain = false;
999
1000 if (lp.horizontalWeight > 0.0f) {
1001 width += (int) ((mWidth - width) * lp.horizontalWeight);
1002 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1003 MeasureSpec.EXACTLY);
1004 measureAgain = true;
1005 }
1006 if (lp.verticalWeight > 0.0f) {
1007 height += (int) ((mHeight - height) * lp.verticalWeight);
1008 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1009 MeasureSpec.EXACTLY);
1010 measureAgain = true;
1011 }
1012
1013 if (measureAgain) {
1014 if (DEBUG_LAYOUT) Log.v(TAG,
1015 "And hey let's measure once more: width=" + width
1016 + " height=" + height);
1017 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1018 }
1019
1020 mLayoutRequested = true;
1021 }
1022 }
1023
1024 final boolean didLayout = mLayoutRequested;
1025 boolean triggerGlobalLayoutListener = didLayout
1026 || attachInfo.mRecomputeGlobalAttributes;
1027 if (didLayout) {
1028 mLayoutRequested = false;
1029 mScrollMayChange = true;
1030 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001031 TAG, "Laying out " + host + " to (" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 host.mMeasuredWidth + ", " + host.mMeasuredHeight + ")");
Romain Guy13922e02009-05-12 17:56:14 -07001033 long startTime = 0L;
Romain Guy5429e1d2010-09-07 12:38:00 -07001034 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 startTime = SystemClock.elapsedRealtime();
1036 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
1038
Romain Guy13922e02009-05-12 17:56:14 -07001039 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1040 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
1041 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
1042 + "please refer to the logs with the tag "
1043 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1044 }
1045 }
1046
Romain Guy5429e1d2010-09-07 12:38:00 -07001047 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1049 }
1050
1051 // By this point all views have been sized and positionned
1052 // We can compute the transparent area
1053
1054 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1055 // start out transparent
1056 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1057 host.getLocationInWindow(mTmpLocation);
1058 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1059 mTmpLocation[0] + host.mRight - host.mLeft,
1060 mTmpLocation[1] + host.mBottom - host.mTop);
1061
1062 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001063 if (mTranslator != null) {
1064 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1065 }
1066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1068 mPreviousTransparentRegion.set(mTransparentRegion);
1069 // reconfigure window manager
1070 try {
1071 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1072 } catch (RemoteException e) {
1073 }
1074 }
1075 }
1076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 if (DBG) {
1078 System.out.println("======================================");
1079 System.out.println("performTraversals -- after setFrame");
1080 host.debug();
1081 }
1082 }
1083
1084 if (triggerGlobalLayoutListener) {
1085 attachInfo.mRecomputeGlobalAttributes = false;
1086 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1087 }
1088
1089 if (computesInternalInsets) {
1090 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1091 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1092 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1093 givenContent.left = givenContent.top = givenContent.right
1094 = givenContent.bottom = givenVisible.left = givenVisible.top
1095 = givenVisible.right = givenVisible.bottom = 0;
1096 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001097 Rect contentInsets = insets.contentInsets;
1098 Rect visibleInsets = insets.visibleInsets;
1099 if (mTranslator != null) {
1100 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1101 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1104 mLastGivenInsets.set(insets);
1105 try {
1106 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001107 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 } catch (RemoteException e) {
1109 }
1110 }
1111 }
Romain Guy8506ab42009-06-11 17:35:47 -07001112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 if (mFirst) {
1114 // handle first focus request
1115 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1116 + mView.hasFocus());
1117 if (mView != null) {
1118 if (!mView.hasFocus()) {
1119 mView.requestFocus(View.FOCUS_FORWARD);
1120 mFocusedView = mRealFocusedView = mView.findFocus();
1121 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1122 + mFocusedView);
1123 } else {
1124 mRealFocusedView = mView.findFocus();
1125 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1126 + mRealFocusedView);
1127 }
1128 }
1129 }
1130
1131 mFirst = false;
1132 mWillDrawSoon = false;
1133 mNewSurfaceNeeded = false;
1134 mViewVisibility = viewVisibility;
1135
1136 if (mAttachInfo.mHasWindowFocus) {
1137 final boolean imTarget = WindowManager.LayoutParams
1138 .mayUseInputMethod(mWindowAttributes.flags);
1139 if (imTarget != mLastWasImTarget) {
1140 mLastWasImTarget = imTarget;
1141 InputMethodManager imm = InputMethodManager.peekInstance();
1142 if (imm != null && imTarget) {
1143 imm.startGettingWindowFocus(mView);
1144 imm.onWindowFocus(mView, mView.findFocus(),
1145 mWindowAttributes.softInputMode,
1146 !mHasHadWindowFocus, mWindowAttributes.flags);
1147 }
1148 }
1149 }
Romain Guy8506ab42009-06-11 17:35:47 -07001150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1152
1153 if (!cancelDraw && !newSurface) {
1154 mFullRedrawNeeded = false;
1155 draw(fullRedrawNeeded);
1156
1157 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1158 || mReportNextDraw) {
1159 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001160 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 }
1162 mReportNextDraw = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001163 if (mSurfaceHolder != null && mSurface.isValid()) {
1164 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
1165 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1166 if (callbacks != null) {
1167 for (SurfaceHolder.Callback c : callbacks) {
1168 if (c instanceof SurfaceHolder.Callback2) {
1169 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
1170 mSurfaceHolder);
1171 }
1172 }
1173 }
1174 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 try {
1176 sWindowSession.finishDrawing(mWindow);
1177 } catch (RemoteException e) {
1178 }
1179 }
1180 } else {
1181 // We were supposed to report when we are done drawing. Since we canceled the
1182 // draw, remember it here.
1183 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1184 mReportNextDraw = true;
1185 }
1186 if (fullRedrawNeeded) {
1187 mFullRedrawNeeded = true;
1188 }
1189 // Try again
1190 scheduleTraversals();
1191 }
1192 }
1193
1194 public void requestTransparentRegion(View child) {
1195 // the test below should not fail unless someone is messing with us
1196 checkThread();
1197 if (mView == child) {
1198 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1199 // Need to make sure we re-evaluate the window attributes next
1200 // time around, to ensure the window has the correct format.
1201 mWindowAttributesChanged = true;
1202 }
1203 }
1204
1205 /**
1206 * Figures out the measure spec for the root view in a window based on it's
1207 * layout params.
1208 *
1209 * @param windowSize
1210 * The available width or height of the window
1211 *
1212 * @param rootDimension
1213 * The layout params for one dimension (width or height) of the
1214 * window.
1215 *
1216 * @return The measure spec to use to measure the root view.
1217 */
1218 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1219 int measureSpec;
1220 switch (rootDimension) {
1221
Romain Guy980a9382010-01-08 15:06:28 -08001222 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 // Window can't resize. Force root view to be windowSize.
1224 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1225 break;
1226 case ViewGroup.LayoutParams.WRAP_CONTENT:
1227 // Window can resize. Set max size for root view.
1228 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1229 break;
1230 default:
1231 // Window wants to be an exact size. Force root view to be that size.
1232 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1233 break;
1234 }
1235 return measureSpec;
1236 }
1237
1238 private void draw(boolean fullRedrawNeeded) {
1239 Surface surface = mSurface;
1240 if (surface == null || !surface.isValid()) {
1241 return;
1242 }
1243
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001244 if (!sFirstDrawComplete) {
1245 synchronized (sFirstDrawHandlers) {
1246 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07001247 final int count = sFirstDrawHandlers.size();
1248 for (int i = 0; i< count; i++) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001249 post(sFirstDrawHandlers.get(i));
1250 }
1251 }
1252 }
1253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 scrollToRectOrFocus(null, false);
1255
1256 if (mAttachInfo.mViewScrollChanged) {
1257 mAttachInfo.mViewScrollChanged = false;
1258 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1259 }
Romain Guy8506ab42009-06-11 17:35:47 -07001260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 int yoff;
Romain Guy5bcdff42009-05-14 21:27:18 -07001262 final boolean scrolling = mScroller != null && mScroller.computeScrollOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 if (scrolling) {
1264 yoff = mScroller.getCurrY();
1265 } else {
1266 yoff = mScrollY;
1267 }
1268 if (mCurScrollY != yoff) {
1269 mCurScrollY = yoff;
1270 fullRedrawNeeded = true;
1271 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001272 float appScale = mAttachInfo.mApplicationScale;
1273 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274
1275 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001276 if (mSurfaceHolder != null) {
1277 // The app owns the surface, we won't draw.
1278 dirty.setEmpty();
1279 return;
1280 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07001281
1282 if (fullRedrawNeeded) {
1283 mAttachInfo.mIgnoreDirtyState = true;
1284 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1285 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001286
Romain Guyb051e892010-09-28 19:09:36 -07001287 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guyfd507262010-10-10 15:42:49 -07001288 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy101e2ae2010-10-11 12:41:21 -07001289 mIsAnimating = false;
Romain Guyfd507262010-10-10 15:42:49 -07001290 dirty.setEmpty();
Romain Guy101e2ae2010-10-11 12:41:21 -07001291 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, yoff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 if (scrolling) {
1295 mFullRedrawNeeded = true;
1296 scheduleTraversals();
1297 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 return;
1300 }
1301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001303 Log.v(TAG, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 + mWindowAttributes.getTitle()
1305 + ": dirty={" + dirty.left + "," + dirty.top
1306 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001307 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1308 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 }
1310
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001311 if (!dirty.isEmpty() || mIsAnimating) {
1312 Canvas canvas;
1313 try {
1314 int left = dirty.left;
1315 int top = dirty.top;
1316 int right = dirty.right;
1317 int bottom = dirty.bottom;
1318 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001319
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001320 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1321 bottom != dirty.bottom) {
1322 mAttachInfo.mIgnoreDirtyState = true;
1323 }
1324
1325 // TODO: Do this in native
1326 canvas.setDensity(mDensity);
1327 } catch (Surface.OutOfResourcesException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001328 Log.e(TAG, "OutOfResourcesException 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;
1332 } catch (IllegalArgumentException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001333 Log.e(TAG, "IllegalArgumentException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001334 // TODO: we should ask the window manager to do something!
1335 // for now we just do nothing
1336 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001337 }
1338
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001339 try {
1340 if (!dirty.isEmpty() || mIsAnimating) {
1341 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001343 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001344 Log.v(TAG, "Surface " + surface + " drawing to bitmap w="
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001345 + canvas.getWidth() + ", h=" + canvas.getHeight());
1346 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348
Romain Guy5429e1d2010-09-07 12:38:00 -07001349 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001350 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001352
1353 // If this bitmap's format includes an alpha channel, we
1354 // need to clear it before drawing so that the child will
1355 // properly re-composite its drawing on a transparent
1356 // background. This automatically respects the clip/dirty region
1357 // or
1358 // If we are applying an offset, we need to clear the area
1359 // where the offset doesn't appear to avoid having garbage
1360 // left in the blank areas.
1361 if (!canvas.isOpaque() || yoff != 0) {
1362 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1363 }
1364
1365 dirty.setEmpty();
1366 mIsAnimating = false;
1367 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1368 mView.mPrivateFlags |= View.DRAWN;
1369
1370 if (DEBUG_DRAW) {
1371 Context cxt = mView.getContext();
1372 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1373 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1374 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1375 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001376 try {
1377 canvas.translate(0, -yoff);
1378 if (mTranslator != null) {
1379 mTranslator.translateCanvas(canvas);
1380 }
1381 canvas.setScreenDensity(scalingRequired
1382 ? DisplayMetrics.DENSITY_DEVICE : 0);
1383 mView.draw(canvas);
1384 } finally {
1385 mAttachInfo.mIgnoreDirtyState = false;
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001386 }
1387
1388 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1389 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1390 }
1391
Romain Guy5429e1d2010-09-07 12:38:00 -07001392 if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001393 int now = (int)SystemClock.elapsedRealtime();
1394 if (sDrawTime != 0) {
1395 nativeShowFPS(canvas, now - sDrawTime);
1396 }
1397 sDrawTime = now;
1398 }
1399
Romain Guy5429e1d2010-09-07 12:38:00 -07001400 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001401 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1402 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001405 } finally {
1406 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409
1410 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001411 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
Romain Guy8506ab42009-06-11 17:35:47 -07001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 if (scrolling) {
1415 mFullRedrawNeeded = true;
1416 scheduleTraversals();
1417 }
1418 }
1419
1420 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1421 final View.AttachInfo attachInfo = mAttachInfo;
1422 final Rect ci = attachInfo.mContentInsets;
1423 final Rect vi = attachInfo.mVisibleInsets;
1424 int scrollY = 0;
1425 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 if (vi.left > ci.left || vi.top > ci.top
1428 || vi.right > ci.right || vi.bottom > ci.bottom) {
1429 // We'll assume that we aren't going to change the scroll
1430 // offset, since we want to avoid that unless it is actually
1431 // going to make the focus visible... otherwise we scroll
1432 // all over the place.
1433 scrollY = mScrollY;
1434 // We can be called for two different situations: during a draw,
1435 // to update the scroll position if the focus has changed (in which
1436 // case 'rectangle' is null), or in response to a
1437 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1438 // is non-null and we just want to scroll to whatever that
1439 // rectangle is).
1440 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001441
1442 // When in touch mode, focus points to the previously focused view,
1443 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001444 // line checks whether the view is still in our hierarchy.
1445 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001446 mRealFocusedView = null;
1447 return false;
1448 }
1449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 if (focus != mLastScrolledFocus) {
1451 // If the focus has changed, then ignore any requests to scroll
1452 // to a rectangle; first we want to make sure the entire focus
1453 // view is visible.
1454 rectangle = null;
1455 }
1456 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1457 + " rectangle=" + rectangle + " ci=" + ci
1458 + " vi=" + vi);
1459 if (focus == mLastScrolledFocus && !mScrollMayChange
1460 && rectangle == null) {
1461 // Optimization: if the focus hasn't changed since last
1462 // time, and no layout has happened, then just leave things
1463 // as they are.
1464 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1465 + mScrollY + " vi=" + vi.toShortString());
1466 } else if (focus != null) {
1467 // We need to determine if the currently focused view is
1468 // within the visible part of the window and, if not, apply
1469 // a pan so it can be seen.
1470 mLastScrolledFocus = focus;
1471 mScrollMayChange = false;
1472 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1473 // Try to find the rectangle from the focus view.
1474 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1475 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1476 + mView.getWidth() + " h=" + mView.getHeight()
1477 + " ci=" + ci.toShortString()
1478 + " vi=" + vi.toShortString());
1479 if (rectangle == null) {
1480 focus.getFocusedRect(mTempRect);
1481 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1482 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001483 if (mView instanceof ViewGroup) {
1484 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1485 focus, mTempRect);
1486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1488 "Focus in window: focusRect="
1489 + mTempRect.toShortString()
1490 + " visRect=" + mVisRect.toShortString());
1491 } else {
1492 mTempRect.set(rectangle);
1493 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1494 "Request scroll to rect: "
1495 + mTempRect.toShortString()
1496 + " visRect=" + mVisRect.toShortString());
1497 }
1498 if (mTempRect.intersect(mVisRect)) {
1499 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1500 "Focus window visible rect: "
1501 + mTempRect.toShortString());
1502 if (mTempRect.height() >
1503 (mView.getHeight()-vi.top-vi.bottom)) {
1504 // If the focus simply is not going to fit, then
1505 // best is probably just to leave things as-is.
1506 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1507 "Too tall; leaving scrollY=" + scrollY);
1508 } else if ((mTempRect.top-scrollY) < vi.top) {
1509 scrollY -= vi.top - (mTempRect.top-scrollY);
1510 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1511 "Top covered; scrollY=" + scrollY);
1512 } else if ((mTempRect.bottom-scrollY)
1513 > (mView.getHeight()-vi.bottom)) {
1514 scrollY += (mTempRect.bottom-scrollY)
1515 - (mView.getHeight()-vi.bottom);
1516 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1517 "Bottom covered; scrollY=" + scrollY);
1518 }
1519 handled = true;
1520 }
1521 }
1522 }
1523 }
Romain Guy8506ab42009-06-11 17:35:47 -07001524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 if (scrollY != mScrollY) {
1526 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1527 + mScrollY + " , new=" + scrollY);
1528 if (!immediate) {
1529 if (mScroller == null) {
1530 mScroller = new Scroller(mView.getContext());
1531 }
1532 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1533 } else if (mScroller != null) {
1534 mScroller.abortAnimation();
1535 }
1536 mScrollY = scrollY;
1537 }
Romain Guy8506ab42009-06-11 17:35:47 -07001538
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 return handled;
1540 }
Romain Guy8506ab42009-06-11 17:35:47 -07001541
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 public void requestChildFocus(View child, View focused) {
1543 checkThread();
1544 if (mFocusedView != focused) {
1545 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1546 scheduleTraversals();
1547 }
1548 mFocusedView = mRealFocusedView = focused;
1549 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1550 + mFocusedView);
1551 }
1552
1553 public void clearChildFocus(View child) {
1554 checkThread();
1555
1556 View oldFocus = mFocusedView;
1557
1558 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1559 mFocusedView = mRealFocusedView = null;
1560 if (mView != null && !mView.hasFocus()) {
1561 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1562 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1563 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1564 }
1565 } else if (oldFocus != null) {
1566 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1567 }
1568 }
1569
1570
1571 public void focusableViewAvailable(View v) {
1572 checkThread();
1573
1574 if (mView != null && !mView.hasFocus()) {
1575 v.requestFocus();
1576 } else {
1577 // the one case where will transfer focus away from the current one
1578 // is if the current view is a view group that prefers to give focus
1579 // to its children first AND the view is a descendant of it.
1580 mFocusedView = mView.findFocus();
1581 boolean descendantsHaveDibsOnFocus =
1582 (mFocusedView instanceof ViewGroup) &&
1583 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1584 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1585 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1586 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1587 v.requestFocus();
1588 }
1589 }
1590 }
1591
1592 public void recomputeViewAttributes(View child) {
1593 checkThread();
1594 if (mView == child) {
1595 mAttachInfo.mRecomputeGlobalAttributes = true;
1596 if (!mWillDrawSoon) {
1597 scheduleTraversals();
1598 }
1599 }
1600 }
1601
1602 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 if (mView != null) {
1604 mView.dispatchDetachedFromWindow();
1605 }
1606
1607 mView = null;
1608 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001609 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610
Romain Guy29d89972010-09-22 16:10:57 -07001611 destroyHardwareRenderer();
Romain Guy4caa4ed2010-08-25 14:46:24 -07001612
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001613 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001614
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001615 if (mInputChannel != null) {
1616 if (mInputQueueCallback != null) {
1617 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
1618 mInputQueueCallback = null;
1619 } else {
1620 InputQueue.unregisterInputChannel(mInputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001621 }
1622 }
1623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 try {
1625 sWindowSession.remove(mWindow);
1626 } catch (RemoteException e) {
1627 }
Jeff Brown349703e2010-06-22 01:27:15 -07001628
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001629 // Dispose the input channel after removing the window so the Window Manager
1630 // doesn't interpret the input channel being closed as an abnormal termination.
1631 if (mInputChannel != null) {
1632 mInputChannel.dispose();
1633 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 }
Romain Guy8506ab42009-06-11 17:35:47 -07001636
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001637 void updateConfiguration(Configuration config, boolean force) {
1638 if (DEBUG_CONFIGURATION) Log.v(TAG,
1639 "Applying new config to window "
1640 + mWindowAttributes.getTitle()
1641 + ": " + config);
1642 synchronized (sConfigCallbacks) {
1643 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1644 sConfigCallbacks.get(i).onConfigurationChanged(config);
1645 }
1646 }
1647 if (mView != null) {
1648 // At this point the resources have been updated to
1649 // have the most recent config, whatever that is. Use
1650 // the on in them which may be newer.
1651 if (mView != null) {
1652 config = mView.getResources().getConfiguration();
1653 }
1654 if (force || mLastConfiguration.diff(config) != 0) {
1655 mLastConfiguration.setTo(config);
1656 mView.dispatchConfigurationChanged(config);
1657 }
1658 }
1659 }
1660
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 /**
1662 * Return true if child is an ancestor of parent, (or equal to the parent).
1663 */
1664 private static boolean isViewDescendantOf(View child, View parent) {
1665 if (child == parent) {
1666 return true;
1667 }
1668
1669 final ViewParent theParent = child.getParent();
1670 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1671 }
1672
Romain Guycdb86672010-03-18 18:54:50 -07001673 private static void forceLayout(View view) {
1674 view.forceLayout();
1675 if (view instanceof ViewGroup) {
1676 ViewGroup group = (ViewGroup) view;
1677 final int count = group.getChildCount();
1678 for (int i = 0; i < count; i++) {
1679 forceLayout(group.getChildAt(i));
1680 }
1681 }
1682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683
1684 public final static int DO_TRAVERSAL = 1000;
1685 public final static int DIE = 1001;
1686 public final static int RESIZED = 1002;
1687 public final static int RESIZED_REPORT = 1003;
1688 public final static int WINDOW_FOCUS_CHANGED = 1004;
1689 public final static int DISPATCH_KEY = 1005;
1690 public final static int DISPATCH_POINTER = 1006;
1691 public final static int DISPATCH_TRACKBALL = 1007;
1692 public final static int DISPATCH_APP_VISIBILITY = 1008;
1693 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1694 public final static int FINISHED_EVENT = 1010;
1695 public final static int DISPATCH_KEY_FROM_IME = 1011;
1696 public final static int FINISH_INPUT_CONNECTION = 1012;
1697 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001698 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Christopher Tatea53146c2010-09-07 11:57:52 -07001699 public final static int DISPATCH_DRAG_EVENT = 1015;
Chris Tate91e9bb32010-10-12 12:58:43 -07001700 public final static int DISPATCH_DRAG_LOCATION_EVENT = 1016;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001701
1702 @Override
1703 public void handleMessage(Message msg) {
1704 switch (msg.what) {
1705 case View.AttachInfo.INVALIDATE_MSG:
1706 ((View) msg.obj).invalidate();
1707 break;
1708 case View.AttachInfo.INVALIDATE_RECT_MSG:
1709 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1710 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1711 info.release();
1712 break;
1713 case DO_TRAVERSAL:
1714 if (mProfile) {
1715 Debug.startMethodTracing("ViewRoot");
1716 }
1717
1718 performTraversals();
1719
1720 if (mProfile) {
1721 Debug.stopMethodTracing();
1722 mProfile = false;
1723 }
1724 break;
1725 case FINISHED_EVENT:
1726 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1727 break;
1728 case DISPATCH_KEY:
1729 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001730 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 + msg.obj + " to " + mView);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001732 deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001734 case DISPATCH_POINTER: {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001735 MotionEvent event = (MotionEvent) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 try {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001737 deliverPointerEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 } finally {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001739 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001740 if (msg.arg1 != 0) {
1741 finishInputEvent();
1742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001745 } break;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001746 case DISPATCH_TRACKBALL: {
1747 MotionEvent event = (MotionEvent) msg.obj;
1748 try {
1749 deliverTrackballEvent(event);
1750 } finally {
1751 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001752 if (msg.arg1 != 0) {
1753 finishInputEvent();
1754 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001755 }
1756 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 case DISPATCH_APP_VISIBILITY:
1758 handleAppVisibility(msg.arg1 != 0);
1759 break;
1760 case DISPATCH_GET_NEW_SURFACE:
1761 handleGetNewSurface();
1762 break;
1763 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001764 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001766 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001767 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001768 && mPendingVisibleInsets.equals(ri.visibleInsets)
1769 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 break;
1771 }
1772 // fall through...
1773 case RESIZED_REPORT:
1774 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001775 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1776 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001777 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001778 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 mWinFrame.left = 0;
1780 mWinFrame.right = msg.arg1;
1781 mWinFrame.top = 0;
1782 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001783 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1784 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 if (msg.what == RESIZED_REPORT) {
1786 mReportNextDraw = true;
1787 }
Romain Guycdb86672010-03-18 18:54:50 -07001788
1789 if (mView != null) {
1790 forceLayout(mView);
1791 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792 requestLayout();
1793 }
1794 break;
1795 case WINDOW_FOCUS_CHANGED: {
1796 if (mAdded) {
1797 boolean hasWindowFocus = msg.arg1 != 0;
1798 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1799 if (hasWindowFocus) {
1800 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001801 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802
Romain Guyc361da82010-10-25 15:29:10 -07001803 if (mAttachInfo.mHardwareRenderer != null &&
1804 mSurface != null && mSurface.isValid()) {
Romain Guyb051e892010-09-28 19:09:36 -07001805 mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
1806 mAttachInfo, mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 }
1808 }
Romain Guy8506ab42009-06-11 17:35:47 -07001809
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 mLastWasImTarget = WindowManager.LayoutParams
1811 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001812
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001813 InputMethodManager imm = InputMethodManager.peekInstance();
1814 if (mView != null) {
1815 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1816 imm.startGettingWindowFocus(mView);
1817 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001818 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 mView.dispatchWindowFocusChanged(hasWindowFocus);
1820 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001821
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 // Note: must be done after the focus change callbacks,
1823 // so all of the view state is set up correctly.
1824 if (hasWindowFocus) {
1825 if (imm != null && mLastWasImTarget) {
1826 imm.onWindowFocus(mView, mView.findFocus(),
1827 mWindowAttributes.softInputMode,
1828 !mHasHadWindowFocus, mWindowAttributes.flags);
1829 }
1830 // Clear the forward bit. We can just do this directly, since
1831 // the window manager doesn't care about it.
1832 mWindowAttributes.softInputMode &=
1833 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1834 ((WindowManager.LayoutParams)mView.getLayoutParams())
1835 .softInputMode &=
1836 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1837 mHasHadWindowFocus = true;
1838 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001839
1840 if (hasWindowFocus && mView != null) {
1841 sendAccessibilityEvents();
1842 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 }
1844 } break;
1845 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07001846 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001848 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001850 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07001852 KeyEvent event = (KeyEvent)msg.obj;
1853 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
1854 // The IME is trying to say this event is from the
1855 // system! Bad bad bad!
Romain Guy812ccbe2010-06-01 14:07:24 -07001856 event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project10592532009-03-18 17:39:46 -07001857 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 deliverKeyEventToViewHierarchy((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07001859 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 case FINISH_INPUT_CONNECTION: {
1861 InputMethodManager imm = InputMethodManager.peekInstance();
1862 if (imm != null) {
1863 imm.reportFinishInputConnection((InputConnection)msg.obj);
1864 }
1865 } break;
1866 case CHECK_FOCUS: {
1867 InputMethodManager imm = InputMethodManager.peekInstance();
1868 if (imm != null) {
1869 imm.checkFocus();
1870 }
1871 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001872 case CLOSE_SYSTEM_DIALOGS: {
1873 if (mView != null) {
1874 mView.onCloseSystemDialogs((String)msg.obj);
1875 }
1876 } break;
Chris Tate91e9bb32010-10-12 12:58:43 -07001877 case DISPATCH_DRAG_EVENT:
1878 case DISPATCH_DRAG_LOCATION_EVENT: {
Christopher Tatea53146c2010-09-07 11:57:52 -07001879 handleDragEvent((DragEvent)msg.obj);
1880 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001883
Jeff Brown93ed4e32010-09-23 13:51:48 -07001884 private void startInputEvent(Runnable finishedCallback) {
1885 if (mFinishedCallback != null) {
1886 Slog.w(TAG, "Received a new input event from the input queue but there is "
1887 + "already an unfinished input event in progress.");
1888 }
1889
1890 mFinishedCallback = finishedCallback;
1891 }
1892
1893 private void finishInputEvent() {
1894 if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished");
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001895
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001896 if (mFinishedCallback != null) {
1897 mFinishedCallback.run();
1898 mFinishedCallback = null;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001899 } else {
Jeff Brown93ed4e32010-09-23 13:51:48 -07001900 Slog.w(TAG, "Attempted to tell the input queue that the current input event "
1901 + "is finished but there is no input event actually in progress.");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001902 }
1903 }
1904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 /**
1906 * Something in the current window tells us we need to change the touch mode. For
1907 * example, we are not in touch mode, and the user touches the screen.
1908 *
1909 * If the touch mode has changed, tell the window manager, and handle it locally.
1910 *
1911 * @param inTouchMode Whether we want to be in touch mode.
1912 * @return True if the touch mode changed and focus changed was changed as a result
1913 */
1914 boolean ensureTouchMode(boolean inTouchMode) {
1915 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
1916 + "touch mode is " + mAttachInfo.mInTouchMode);
1917 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1918
1919 // tell the window manager
1920 try {
1921 sWindowSession.setInTouchMode(inTouchMode);
1922 } catch (RemoteException e) {
1923 throw new RuntimeException(e);
1924 }
1925
1926 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07001927 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 }
1929
1930 /**
1931 * Ensure that the touch mode for this window is set, and if it is changing,
1932 * take the appropriate action.
1933 * @param inTouchMode Whether we want to be in touch mode.
1934 * @return True if the touch mode changed and focus changed was changed as a result
1935 */
Romain Guy2d4cff62010-04-09 15:39:00 -07001936 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
1938 + "touch mode is " + mAttachInfo.mInTouchMode);
1939
1940 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1941
1942 mAttachInfo.mInTouchMode = inTouchMode;
1943 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
1944
Romain Guy2d4cff62010-04-09 15:39:00 -07001945 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 }
1947
1948 private boolean enterTouchMode() {
1949 if (mView != null) {
1950 if (mView.hasFocus()) {
1951 // note: not relying on mFocusedView here because this could
1952 // be when the window is first being added, and mFocused isn't
1953 // set yet.
1954 final View focused = mView.findFocus();
1955 if (focused != null && !focused.isFocusableInTouchMode()) {
1956
1957 final ViewGroup ancestorToTakeFocus =
1958 findAncestorToTakeFocusInTouchMode(focused);
1959 if (ancestorToTakeFocus != null) {
1960 // there is an ancestor that wants focus after its descendants that
1961 // is focusable in touch mode.. give it focus
1962 return ancestorToTakeFocus.requestFocus();
1963 } else {
1964 // nothing appropriate to have focus in touch mode, clear it out
1965 mView.unFocus();
1966 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
1967 mFocusedView = null;
1968 return true;
1969 }
1970 }
1971 }
1972 }
1973 return false;
1974 }
1975
1976
1977 /**
1978 * Find an ancestor of focused that wants focus after its descendants and is
1979 * focusable in touch mode.
1980 * @param focused The currently focused view.
1981 * @return An appropriate view, or null if no such view exists.
1982 */
1983 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
1984 ViewParent parent = focused.getParent();
1985 while (parent instanceof ViewGroup) {
1986 final ViewGroup vgParent = (ViewGroup) parent;
1987 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
1988 && vgParent.isFocusableInTouchMode()) {
1989 return vgParent;
1990 }
1991 if (vgParent.isRootNamespace()) {
1992 return null;
1993 } else {
1994 parent = vgParent.getParent();
1995 }
1996 }
1997 return null;
1998 }
1999
2000 private boolean leaveTouchMode() {
2001 if (mView != null) {
2002 if (mView.hasFocus()) {
2003 // i learned the hard way to not trust mFocusedView :)
2004 mFocusedView = mView.findFocus();
2005 if (!(mFocusedView instanceof ViewGroup)) {
2006 // some view has focus, let it keep it
2007 return false;
2008 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2009 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2010 // some view group has focus, and doesn't prefer its children
2011 // over itself for focus, so let them keep it.
2012 return false;
2013 }
2014 }
2015
2016 // find the best view to give focus to in this brave new non-touch-mode
2017 // world
2018 final View focused = focusSearch(null, View.FOCUS_DOWN);
2019 if (focused != null) {
2020 return focused.requestFocus(View.FOCUS_DOWN);
2021 }
2022 }
2023 return false;
2024 }
2025
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002026 private void deliverPointerEvent(MotionEvent event) {
2027 if (mTranslator != null) {
2028 mTranslator.translateEventInScreenToAppWindow(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002030
2031 boolean handled;
2032 if (mView != null && mAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002034 // enter touch mode on the down
2035 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
2036 if (isDown) {
2037 ensureTouchMode(true);
2038 }
2039 if(Config.LOGV) {
2040 captureMotionLog("captureDispatchPointer", event);
2041 }
2042 if (mCurScrollY != 0) {
2043 event.offsetLocation(0, mCurScrollY);
2044 }
2045 if (MEASURE_LATENCY) {
2046 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
2047 }
Christopher Tate2c095f32010-10-04 14:13:40 -07002048 // cache for possible drag-initiation
2049 mLastTouchPoint.x = event.getRawX();
2050 mLastTouchPoint.y = event.getRawY();
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002051 handled = mView.dispatchTouchEvent(event);
2052 if (MEASURE_LATENCY) {
2053 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
2054 }
2055 if (!handled && isDown) {
2056 int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
2057
2058 final int edgeFlags = event.getEdgeFlags();
2059 int direction = View.FOCUS_UP;
2060 int x = (int)event.getX();
2061 int y = (int)event.getY();
2062 final int[] deltas = new int[2];
2063
2064 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
2065 direction = View.FOCUS_DOWN;
2066 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2067 deltas[0] = edgeSlop;
2068 x += edgeSlop;
2069 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2070 deltas[0] = -edgeSlop;
2071 x -= edgeSlop;
2072 }
2073 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
2074 direction = View.FOCUS_UP;
2075 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2076 deltas[0] = edgeSlop;
2077 x += edgeSlop;
2078 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2079 deltas[0] = -edgeSlop;
2080 x -= edgeSlop;
2081 }
2082 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2083 direction = View.FOCUS_RIGHT;
2084 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2085 direction = View.FOCUS_LEFT;
2086 }
2087
2088 if (edgeFlags != 0 && mView instanceof ViewGroup) {
2089 View nearest = FocusFinder.getInstance().findNearestTouchable(
2090 ((ViewGroup) mView), x, y, direction, deltas);
2091 if (nearest != null) {
2092 event.offsetLocation(deltas[0], deltas[1]);
2093 event.setEdgeFlags(0);
2094 mView.dispatchTouchEvent(event);
2095 }
2096 }
2097 }
2098 }
2099 }
2100
2101 private void deliverTrackballEvent(MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2103
2104 boolean handled = false;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002105 if (mView != null && mAdded) {
2106 handled = mView.dispatchTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 if (handled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 // If we reach this, we delivered a trackball event to mView and
2109 // mView consumed it. Because we will not translate the trackball
2110 // event into a key event, touch mode will not exit, so we exit
2111 // touch mode here.
2112 ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 return;
2114 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002115
2116 // Otherwise we could do something here, like changing the focus
2117 // or something?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 }
2119
2120 final TrackballAxis x = mTrackballAxisX;
2121 final TrackballAxis y = mTrackballAxisY;
2122
2123 long curTime = SystemClock.uptimeMillis();
2124 if ((mLastTrackballTime+MAX_TRACKBALL_DELAY) < curTime) {
2125 // It has been too long since the last movement,
2126 // so restart at the beginning.
2127 x.reset(0);
2128 y.reset(0);
2129 mLastTrackballTime = curTime;
2130 }
2131
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002132 final int action = event.getAction();
2133 final int metastate = event.getMetaState();
2134 switch (action) {
2135 case MotionEvent.ACTION_DOWN:
2136 x.reset(2);
2137 y.reset(2);
2138 deliverKeyEvent(new KeyEvent(curTime, curTime,
2139 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER,
2140 0, metastate), false);
2141 break;
2142 case MotionEvent.ACTION_UP:
2143 x.reset(2);
2144 y.reset(2);
2145 deliverKeyEvent(new KeyEvent(curTime, curTime,
2146 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER,
2147 0, metastate), false);
2148 break;
2149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002151 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2152 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2153 + " move=" + event.getX()
2154 + " / Y=" + y.position + " step="
2155 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2156 + " move=" + event.getY());
2157 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2158 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002160 // Generate DPAD events based on the trackball movement.
2161 // We pick the axis that has moved the most as the direction of
2162 // the DPAD. When we generate DPAD events for one axis, then the
2163 // other axis is reset -- we don't want to perform DPAD jumps due
2164 // to slight movements in the trackball when making major movements
2165 // along the other axis.
2166 int keycode = 0;
2167 int movement = 0;
2168 float accel = 1;
2169 if (xOff > yOff) {
2170 movement = x.generate((2/event.getXPrecision()));
2171 if (movement != 0) {
2172 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2173 : KeyEvent.KEYCODE_DPAD_LEFT;
2174 accel = x.acceleration;
2175 y.reset(2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002177 } else if (yOff > 0) {
2178 movement = y.generate((2/event.getYPrecision()));
2179 if (movement != 0) {
2180 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2181 : KeyEvent.KEYCODE_DPAD_UP;
2182 accel = y.acceleration;
2183 x.reset(2);
2184 }
2185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002187 if (keycode != 0) {
2188 if (movement < 0) movement = -movement;
2189 int accelMovement = (int)(movement * accel);
2190 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2191 + " accelMovement=" + accelMovement
2192 + " accel=" + accel);
2193 if (accelMovement > movement) {
2194 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2195 + keycode);
2196 movement--;
2197 deliverKeyEvent(new KeyEvent(curTime, curTime,
2198 KeyEvent.ACTION_MULTIPLE, keycode,
2199 accelMovement-movement, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002201 while (movement > 0) {
2202 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2203 + keycode);
2204 movement--;
2205 curTime = SystemClock.uptimeMillis();
2206 deliverKeyEvent(new KeyEvent(curTime, curTime,
2207 KeyEvent.ACTION_DOWN, keycode, 0, event.getMetaState()), false);
2208 deliverKeyEvent(new KeyEvent(curTime, curTime,
2209 KeyEvent.ACTION_UP, keycode, 0, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002211 mLastTrackballTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 }
2213 }
2214
2215 /**
2216 * @param keyCode The key code
2217 * @return True if the key is directional.
2218 */
2219 static boolean isDirectional(int keyCode) {
2220 switch (keyCode) {
2221 case KeyEvent.KEYCODE_DPAD_LEFT:
2222 case KeyEvent.KEYCODE_DPAD_RIGHT:
2223 case KeyEvent.KEYCODE_DPAD_UP:
2224 case KeyEvent.KEYCODE_DPAD_DOWN:
2225 return true;
2226 }
2227 return false;
2228 }
2229
2230 /**
2231 * Returns true if this key is a keyboard key.
2232 * @param keyEvent The key event.
2233 * @return whether this key is a keyboard key.
2234 */
2235 private static boolean isKeyboardKey(KeyEvent keyEvent) {
2236 final int convertedKey = keyEvent.getUnicodeChar();
2237 return convertedKey > 0;
2238 }
2239
2240
2241
2242 /**
2243 * See if the key event means we should leave touch mode (and leave touch
2244 * mode if so).
2245 * @param event The key event.
2246 * @return Whether this key event should be consumed (meaning the act of
2247 * leaving touch mode alone is considered the event).
2248 */
2249 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Adam Powell51a6bee2010-03-15 14:07:28 -07002250 final int action = event.getAction();
2251 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002252 return false;
2253 }
2254 if ((event.getFlags()&KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2255 return false;
2256 }
2257
2258 // only relevant if we are in touch mode
2259 if (!mAttachInfo.mInTouchMode) {
2260 return false;
2261 }
2262
2263 // if something like an edit text has focus and the user is typing,
2264 // leave touch mode
2265 //
2266 // note: the condition of not being a keyboard key is kind of a hacky
2267 // approximation of whether we think the focused view will want the
2268 // key; if we knew for sure whether the focused view would consume
2269 // the event, that would be better.
2270 if (isKeyboardKey(event) && mView != null && mView.hasFocus()) {
2271 mFocusedView = mView.findFocus();
2272 if ((mFocusedView instanceof ViewGroup)
2273 && ((ViewGroup) mFocusedView).getDescendantFocusability() ==
2274 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2275 // something has focus, but is holding it weakly as a container
2276 return false;
2277 }
2278 if (ensureTouchMode(false)) {
2279 throw new IllegalStateException("should not have changed focus "
2280 + "when leaving touch mode while a view has focus.");
2281 }
2282 return false;
2283 }
2284
2285 if (isDirectional(event.getKeyCode())) {
2286 // no view has focus, so we leave touch mode (and find something
2287 // to give focus to). the event is consumed if we were able to
2288 // find something to give focus to.
2289 return ensureTouchMode(false);
2290 }
2291 return false;
2292 }
2293
2294 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002295 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 */
2297 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002298 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 if (ev == null ||
2300 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2301 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002302 }
Romain Guy8506ab42009-06-11 17:35:47 -07002303
2304 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002305 sb.append(ev.getDownTime()).append(',');
2306 sb.append(ev.getEventTime()).append(',');
2307 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002308 sb.append(ev.getX()).append(',');
2309 sb.append(ev.getY()).append(',');
2310 sb.append(ev.getPressure()).append(',');
2311 sb.append(ev.getSize()).append(',');
2312 sb.append(ev.getMetaState()).append(',');
2313 sb.append(ev.getXPrecision()).append(',');
2314 sb.append(ev.getYPrecision()).append(',');
2315 sb.append(ev.getDeviceId()).append(',');
2316 sb.append(ev.getEdgeFlags());
2317 Log.d(TAG, sb.toString());
2318 }
2319 /**
2320 * log motion events
2321 */
2322 private static void captureKeyLog(String subTag, KeyEvent ev) {
2323 //check dynamic switch
2324 if (ev == null ||
2325 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2326 return;
2327 }
2328 StringBuilder sb = new StringBuilder(subTag + ": ");
2329 sb.append(ev.getDownTime()).append(',');
2330 sb.append(ev.getEventTime()).append(',');
2331 sb.append(ev.getAction()).append(',');
2332 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002333 sb.append(ev.getRepeatCount()).append(',');
2334 sb.append(ev.getMetaState()).append(',');
2335 sb.append(ev.getDeviceId()).append(',');
2336 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002337 Log.d(TAG, sb.toString());
2338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339
2340 int enqueuePendingEvent(Object event, boolean sendDone) {
2341 int seq = mPendingEventSeq+1;
2342 if (seq < 0) seq = 0;
2343 mPendingEventSeq = seq;
2344 mPendingEvents.put(seq, event);
2345 return sendDone ? seq : -seq;
2346 }
2347
2348 Object retrievePendingEvent(int seq) {
2349 if (seq < 0) seq = -seq;
2350 Object event = mPendingEvents.get(seq);
2351 if (event != null) {
2352 mPendingEvents.remove(seq);
2353 }
2354 return event;
2355 }
Romain Guy8506ab42009-06-11 17:35:47 -07002356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002357 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
2358 // If mView is null, we just consume the key event because it doesn't
2359 // make sense to do anything else with it.
Romain Guy812ccbe2010-06-01 14:07:24 -07002360 boolean handled = mView == null || mView.dispatchKeyEventPreIme(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 if (handled) {
2362 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002363 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002364 }
2365 return;
2366 }
2367 // If it is possible for this window to interact with the input
2368 // method window, then we want to first dispatch our key events
2369 // to the input method.
2370 if (mLastWasImTarget) {
2371 InputMethodManager imm = InputMethodManager.peekInstance();
2372 if (imm != null && mView != null) {
2373 int seq = enqueuePendingEvent(event, sendDone);
2374 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2375 + seq + " event=" + event);
2376 imm.dispatchKeyEvent(mView.getContext(), seq, event,
2377 mInputMethodCallback);
2378 return;
2379 }
2380 }
2381 deliverKeyEventToViewHierarchy(event, sendDone);
2382 }
2383
2384 void handleFinishedEvent(int seq, boolean handled) {
2385 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2386 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2387 + " handled=" + handled + " event=" + event);
2388 if (event != null) {
2389 final boolean sendDone = seq >= 0;
2390 if (!handled) {
2391 deliverKeyEventToViewHierarchy(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002392 } else if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002393 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002394 } else {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002395 Log.w(TAG, "handleFinishedEvent(seq=" + seq
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002396 + " handled=" + handled + " ev=" + event
2397 + ") neither delivering nor finishing key");
2398 }
2399 }
2400 }
Romain Guy8506ab42009-06-11 17:35:47 -07002401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 private void deliverKeyEventToViewHierarchy(KeyEvent event, boolean sendDone) {
2403 try {
2404 if (mView != null && mAdded) {
2405 final int action = event.getAction();
2406 boolean isDown = (action == KeyEvent.ACTION_DOWN);
2407
2408 if (checkForLeavingTouchModeAndConsume(event)) {
2409 return;
Romain Guy8506ab42009-06-11 17:35:47 -07002410 }
2411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 if (Config.LOGV) {
2413 captureKeyLog("captureDispatchKeyEvent", event);
2414 }
2415 boolean keyHandled = mView.dispatchKeyEvent(event);
2416
2417 if (!keyHandled && isDown) {
2418 int direction = 0;
2419 switch (event.getKeyCode()) {
2420 case KeyEvent.KEYCODE_DPAD_LEFT:
2421 direction = View.FOCUS_LEFT;
2422 break;
2423 case KeyEvent.KEYCODE_DPAD_RIGHT:
2424 direction = View.FOCUS_RIGHT;
2425 break;
2426 case KeyEvent.KEYCODE_DPAD_UP:
2427 direction = View.FOCUS_UP;
2428 break;
2429 case KeyEvent.KEYCODE_DPAD_DOWN:
2430 direction = View.FOCUS_DOWN;
2431 break;
2432 }
2433
2434 if (direction != 0) {
2435
2436 View focused = mView != null ? mView.findFocus() : null;
2437 if (focused != null) {
2438 View v = focused.focusSearch(direction);
2439 boolean focusPassed = false;
2440 if (v != null && v != focused) {
2441 // do the math the get the interesting rect
2442 // of previous focused into the coord system of
2443 // newly focused view
2444 focused.getFocusedRect(mTempRect);
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07002445 if (mView instanceof ViewGroup) {
2446 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2447 focused, mTempRect);
2448 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2449 v, mTempRect);
2450 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 focusPassed = v.requestFocus(direction, mTempRect);
2452 }
2453
2454 if (!focusPassed) {
2455 mView.dispatchUnhandledMove(focused, direction);
2456 } else {
2457 playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
2458 }
2459 }
2460 }
2461 }
2462 }
2463
2464 } finally {
2465 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002466 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002467 }
2468 // Let the exception fall through -- the looper will catch
2469 // it and take care of the bad app for us.
2470 }
2471 }
2472
Christopher Tatea53146c2010-09-07 11:57:52 -07002473 /* drag/drop */
2474 private void handleDragEvent(DragEvent event) {
2475 // From the root, only drag start/end/location are dispatched. entered/exited
2476 // are determined and dispatched by the viewgroup hierarchy, who then report
2477 // that back here for ultimate reporting back to the framework.
2478 if (mView != null && mAdded) {
2479 final int what = event.mAction;
2480
2481 if (what == DragEvent.ACTION_DRAG_EXITED) {
2482 // A direct EXITED event means that the window manager knows we've just crossed
2483 // a window boundary, so the current drag target within this one must have
2484 // just been exited. Send it the usual notifications and then we're done
2485 // for now.
2486 setDragFocus(event, null);
2487 } else {
2488 // Cache the drag description when the operation starts, then fill it in
2489 // on subsequent calls as a convenience
2490 if (what == DragEvent.ACTION_DRAG_STARTED) {
2491 mDragDescription = event.mClipDescription;
2492 } else {
2493 event.mClipDescription = mDragDescription;
2494 }
2495
2496 // For events with a [screen] location, translate into window coordinates
2497 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
2498 mDragPoint.set(event.mX, event.mY);
2499 if (mTranslator != null) {
2500 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
2501 }
2502
2503 if (mCurScrollY != 0) {
2504 mDragPoint.offset(0, mCurScrollY);
2505 }
2506
2507 event.mX = mDragPoint.x;
2508 event.mY = mDragPoint.y;
2509 }
2510
2511 // Remember who the current drag target is pre-dispatch
2512 final View prevDragView = mCurrentDragView;
2513
2514 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07002515 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002516
2517 // If we changed apparent drag target, tell the OS about it
2518 if (prevDragView != mCurrentDragView) {
2519 try {
2520 if (prevDragView != null) {
2521 sWindowSession.dragRecipientExited(mWindow);
2522 }
2523 if (mCurrentDragView != null) {
2524 sWindowSession.dragRecipientEntered(mWindow);
2525 }
2526 } catch (RemoteException e) {
2527 Slog.e(TAG, "Unable to note drag target change");
2528 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002529 }
Chris Tated4533f12010-10-19 15:15:08 -07002530
2531 // Report the drop result if necessary
2532 if (what == DragEvent.ACTION_DROP) {
2533 try {
2534 Log.i(TAG, "Reporting drop result: " + result);
2535 sWindowSession.reportDropResult(mWindow, result);
2536 } catch (RemoteException e) {
2537 Log.e(TAG, "Unable to report drop result");
2538 }
2539 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002540 }
2541 }
2542 event.recycle();
2543 }
2544
Christopher Tate2c095f32010-10-04 14:13:40 -07002545 public void getLastTouchPoint(Point outLocation) {
2546 outLocation.x = (int) mLastTouchPoint.x;
2547 outLocation.y = (int) mLastTouchPoint.y;
2548 }
2549
Christopher Tatea53146c2010-09-07 11:57:52 -07002550 public void setDragFocus(DragEvent event, View newDragTarget) {
2551 final int action = event.mAction;
2552 // If we've dragged off of a view, send it the EXITED message
2553 if (mCurrentDragView != newDragTarget) {
2554 if (mCurrentDragView != null) {
2555 event.mAction = DragEvent.ACTION_DRAG_EXITED;
2556 mCurrentDragView.dispatchDragEvent(event);
2557 }
Chris Tate048691c2010-10-12 17:39:18 -07002558 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07002559 }
2560 // If we've dragged over a new view, send it the ENTERED message
2561 if (newDragTarget != null) {
2562 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
2563 newDragTarget.dispatchDragEvent(event);
2564 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002565 event.mAction = action; // restore the event's original state
2566 }
2567
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 private AudioManager getAudioManager() {
2569 if (mView == null) {
2570 throw new IllegalStateException("getAudioManager called when there is no mView");
2571 }
2572 if (mAudioManager == null) {
2573 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2574 }
2575 return mAudioManager;
2576 }
2577
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002578 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2579 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002580
2581 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002582 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002583 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002584 restore = true;
2585 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002586 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002587 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002588 if (params != null) {
2589 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002590 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002591 mPendingConfiguration.seq = 0;
Dianne Hackbornf123e492010-09-24 11:16:23 -07002592 //Log.d(TAG, ">>>>>> CALLING relayout");
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002593 int relayoutResult = sWindowSession.relayout(
2594 mWindow, params,
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002595 (int) (mView.mMeasuredWidth * appScale + 0.5f),
2596 (int) (mView.mMeasuredHeight * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002597 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002598 mPendingContentInsets, mPendingVisibleInsets,
2599 mPendingConfiguration, mSurface);
Dianne Hackbornf123e492010-09-24 11:16:23 -07002600 //Log.d(TAG, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002601 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002602 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002603 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002604
2605 if (mTranslator != null) {
2606 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2607 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2608 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002609 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002610 return relayoutResult;
2611 }
Romain Guy8506ab42009-06-11 17:35:47 -07002612
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 * {@inheritDoc}
2615 */
2616 public void playSoundEffect(int effectId) {
2617 checkThread();
2618
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002619 try {
2620 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002621
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002622 switch (effectId) {
2623 case SoundEffectConstants.CLICK:
2624 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2625 return;
2626 case SoundEffectConstants.NAVIGATION_DOWN:
2627 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2628 return;
2629 case SoundEffectConstants.NAVIGATION_LEFT:
2630 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2631 return;
2632 case SoundEffectConstants.NAVIGATION_RIGHT:
2633 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2634 return;
2635 case SoundEffectConstants.NAVIGATION_UP:
2636 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2637 return;
2638 default:
2639 throw new IllegalArgumentException("unknown effect id " + effectId +
2640 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2641 }
2642 } catch (IllegalStateException e) {
2643 // Exception thrown by getAudioManager() when mView is null
2644 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2645 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 }
2647 }
2648
2649 /**
2650 * {@inheritDoc}
2651 */
2652 public boolean performHapticFeedback(int effectId, boolean always) {
2653 try {
2654 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2655 } catch (RemoteException e) {
2656 return false;
2657 }
2658 }
2659
2660 /**
2661 * {@inheritDoc}
2662 */
2663 public View focusSearch(View focused, int direction) {
2664 checkThread();
2665 if (!(mView instanceof ViewGroup)) {
2666 return null;
2667 }
2668 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2669 }
2670
2671 public void debug() {
2672 mView.debug();
2673 }
2674
2675 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002676 if (immediate) {
2677 doDie();
2678 } else {
2679 sendEmptyMessage(DIE);
2680 }
2681 }
2682
2683 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 checkThread();
Jeff Brownb75fa302010-07-15 23:47:29 -07002685 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002686 synchronized (this) {
2687 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07002688 destroyHardwareRenderer();
2689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 int viewVisibility = mView.getVisibility();
2691 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2692 if (mWindowAttributesChanged || viewVisibilityChanged) {
2693 // If layout params have been changed, first give them
2694 // to the window manager to make sure it has the correct
2695 // animation info.
2696 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002697 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2698 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 sWindowSession.finishDrawing(mWindow);
2700 }
2701 } catch (RemoteException e) {
2702 }
2703 }
2704
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002705 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
2707 if (mAdded) {
2708 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002709 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002710 }
2711 }
2712 }
2713
Romain Guy29d89972010-09-22 16:10:57 -07002714 private void destroyHardwareRenderer() {
Romain Guyb051e892010-09-28 19:09:36 -07002715 if (mAttachInfo.mHardwareRenderer != null) {
2716 mAttachInfo.mHardwareRenderer.destroy(true);
2717 mAttachInfo.mHardwareRenderer = null;
Romain Guy29d89972010-09-22 16:10:57 -07002718 mAttachInfo.mHardwareAccelerated = false;
2719 }
2720 }
2721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 public void dispatchFinishedEvent(int seq, boolean handled) {
2723 Message msg = obtainMessage(FINISHED_EVENT);
2724 msg.arg1 = seq;
2725 msg.arg2 = handled ? 1 : 0;
2726 sendMessage(msg);
2727 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002729 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002730 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002731 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2732 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2733 + " visibleInsets=" + visibleInsets.toShortString()
2734 + " reportDraw=" + reportDraw);
2735 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002736 if (mTranslator != null) {
2737 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2738 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2739 w *= mTranslator.applicationInvertedScale;
2740 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002741 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002742 msg.arg1 = w;
2743 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002744 ResizedInfo ri = new ResizedInfo();
2745 ri.coveredInsets = new Rect(coveredInsets);
2746 ri.visibleInsets = new Rect(visibleInsets);
2747 ri.newConfig = newConfig;
2748 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002749 sendMessage(msg);
2750 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002751
2752 private Runnable mFinishedCallback;
2753
2754 private final InputHandler mInputHandler = new InputHandler() {
2755 public void handleKey(KeyEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002756 startInputEvent(finishedCallback);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002757 dispatchKey(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002758 }
2759
Jeff Brownc5ed5912010-07-14 18:48:53 -07002760 public void handleMotion(MotionEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002761 startInputEvent(finishedCallback);
2762 dispatchMotion(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002763 }
2764 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002765
2766 public void dispatchKey(KeyEvent event) {
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002767 dispatchKey(event, false);
2768 }
2769
2770 private void dispatchKey(KeyEvent event, boolean sendDone) {
2771 //noinspection ConstantConditions
2772 if (false && event.getAction() == KeyEvent.ACTION_DOWN) {
2773 if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
Romain Guy812ccbe2010-06-01 14:07:24 -07002774 if (DBG) Log.d("keydisp", "===================================================");
2775 if (DBG) Log.d("keydisp", "Focused view Hierarchy is:");
2776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002777 debug();
2778
Romain Guy812ccbe2010-06-01 14:07:24 -07002779 if (DBG) Log.d("keydisp", "===================================================");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781 }
2782
2783 Message msg = obtainMessage(DISPATCH_KEY);
2784 msg.obj = event;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002785 msg.arg1 = sendDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002786
2787 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07002788 TAG, "sending key " + event + " to " + mView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789
2790 sendMessageAtTime(msg, event.getEventTime());
2791 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002792
2793 public void dispatchMotion(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002794 dispatchMotion(event, false);
2795 }
2796
2797 private void dispatchMotion(MotionEvent event, boolean sendDone) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002798 int source = event.getSource();
2799 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002800 dispatchPointer(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002801 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002802 dispatchTrackball(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002803 } else {
2804 // TODO
2805 Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event);
Jeff Brown93ed4e32010-09-23 13:51:48 -07002806 if (sendDone) {
2807 finishInputEvent();
2808 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002809 }
2810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002811
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002812 public void dispatchPointer(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002813 dispatchPointer(event, false);
2814 }
2815
2816 private void dispatchPointer(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002817 Message msg = obtainMessage(DISPATCH_POINTER);
2818 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002819 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002820 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 }
2822
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002823 public void dispatchTrackball(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002824 dispatchTrackball(event, false);
2825 }
2826
2827 private void dispatchTrackball(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002828 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2829 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002830 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002831 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002833
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002834 public void dispatchAppVisibility(boolean visible) {
2835 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
2836 msg.arg1 = visible ? 1 : 0;
2837 sendMessage(msg);
2838 }
2839
2840 public void dispatchGetNewSurface() {
2841 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
2842 sendMessage(msg);
2843 }
2844
2845 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2846 Message msg = Message.obtain();
2847 msg.what = WINDOW_FOCUS_CHANGED;
2848 msg.arg1 = hasFocus ? 1 : 0;
2849 msg.arg2 = inTouchMode ? 1 : 0;
2850 sendMessage(msg);
2851 }
2852
Dianne Hackbornffa42482009-09-23 22:20:11 -07002853 public void dispatchCloseSystemDialogs(String reason) {
2854 Message msg = Message.obtain();
2855 msg.what = CLOSE_SYSTEM_DIALOGS;
2856 msg.obj = reason;
2857 sendMessage(msg);
2858 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002859
2860 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07002861 final int what;
2862 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
2863 what = DISPATCH_DRAG_LOCATION_EVENT;
2864 removeMessages(what);
2865 } else {
2866 what = DISPATCH_DRAG_EVENT;
2867 }
2868 Message msg = obtainMessage(what, event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002869 sendMessage(msg);
2870 }
2871
svetoslavganov75986cf2009-05-14 22:28:01 -07002872 /**
2873 * The window is getting focus so if there is anything focused/selected
2874 * send an {@link AccessibilityEvent} to announce that.
2875 */
2876 private void sendAccessibilityEvents() {
2877 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
2878 return;
2879 }
2880 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2881 View focusedView = mView.findFocus();
2882 if (focusedView != null && focusedView != mView) {
2883 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2884 }
2885 }
2886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 public boolean showContextMenuForChild(View originalView) {
2888 return false;
2889 }
2890
Adam Powell6e346362010-07-23 10:18:23 -07002891 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
2892 return null;
2893 }
2894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 public void createContextMenu(ContextMenu menu) {
2896 }
2897
2898 public void childDrawableStateChanged(View child) {
2899 }
2900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002901 void checkThread() {
2902 if (mThread != Thread.currentThread()) {
2903 throw new CalledFromWrongThreadException(
2904 "Only the original thread that created a view hierarchy can touch its views.");
2905 }
2906 }
2907
2908 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2909 // ViewRoot never intercepts touch event, so this can be a no-op
2910 }
2911
2912 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
2913 boolean immediate) {
2914 return scrollToRectOrFocus(rectangle, immediate);
2915 }
Romain Guy8506ab42009-06-11 17:35:47 -07002916
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002917 class TakenSurfaceHolder extends BaseSurfaceHolder {
2918 @Override
2919 public boolean onAllowLockCanvas() {
2920 return mDrawingAllowed;
2921 }
2922
2923 @Override
2924 public void onRelayoutContainer() {
2925 // Not currently interesting -- from changing between fixed and layout size.
2926 }
2927
2928 public void setFormat(int format) {
2929 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
2930 }
2931
2932 public void setType(int type) {
2933 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
2934 }
2935
2936 @Override
2937 public void onUpdateSurface() {
2938 // We take care of format and type changes on our own.
2939 throw new IllegalStateException("Shouldn't be here");
2940 }
2941
2942 public boolean isCreating() {
2943 return mIsCreating;
2944 }
2945
2946 @Override
2947 public void setFixedSize(int width, int height) {
2948 throw new UnsupportedOperationException(
2949 "Currently only support sizing from layout");
2950 }
2951
2952 public void setKeepScreenOn(boolean screenOn) {
2953 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
2954 }
2955 }
2956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002957 static class InputMethodCallback extends IInputMethodCallback.Stub {
2958 private WeakReference<ViewRoot> mViewRoot;
2959
2960 public InputMethodCallback(ViewRoot viewRoot) {
2961 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2962 }
Romain Guy8506ab42009-06-11 17:35:47 -07002963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002964 public void finishedEvent(int seq, boolean handled) {
2965 final ViewRoot viewRoot = mViewRoot.get();
2966 if (viewRoot != null) {
2967 viewRoot.dispatchFinishedEvent(seq, handled);
2968 }
2969 }
2970
2971 public void sessionCreated(IInputMethodSession session) throws RemoteException {
2972 // Stub -- not for use in the client.
2973 }
2974 }
Romain Guy8506ab42009-06-11 17:35:47 -07002975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002977 private final WeakReference<ViewRoot> mViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002978
Romain Guyfb8b7632010-08-23 21:05:08 -07002979 W(ViewRoot viewRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002980 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2981 }
2982
Romain Guyfb8b7632010-08-23 21:05:08 -07002983 public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets,
2984 boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 final ViewRoot viewRoot = mViewRoot.get();
2986 if (viewRoot != null) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002987 viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 }
2989 }
2990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 public void dispatchAppVisibility(boolean visible) {
2992 final ViewRoot viewRoot = mViewRoot.get();
2993 if (viewRoot != null) {
2994 viewRoot.dispatchAppVisibility(visible);
2995 }
2996 }
2997
2998 public void dispatchGetNewSurface() {
2999 final ViewRoot viewRoot = mViewRoot.get();
3000 if (viewRoot != null) {
3001 viewRoot.dispatchGetNewSurface();
3002 }
3003 }
3004
3005 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3006 final ViewRoot viewRoot = mViewRoot.get();
3007 if (viewRoot != null) {
3008 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
3009 }
3010 }
3011
3012 private static int checkCallingPermission(String permission) {
3013 if (!Process.supportsProcesses()) {
3014 return PackageManager.PERMISSION_GRANTED;
3015 }
3016
3017 try {
3018 return ActivityManagerNative.getDefault().checkPermission(
3019 permission, Binder.getCallingPid(), Binder.getCallingUid());
3020 } catch (RemoteException e) {
3021 return PackageManager.PERMISSION_DENIED;
3022 }
3023 }
3024
3025 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3026 final ViewRoot viewRoot = mViewRoot.get();
3027 if (viewRoot != null) {
3028 final View view = viewRoot.mView;
3029 if (view != null) {
3030 if (checkCallingPermission(Manifest.permission.DUMP) !=
3031 PackageManager.PERMISSION_GRANTED) {
3032 throw new SecurityException("Insufficient permissions to invoke"
3033 + " executeCommand() from pid=" + Binder.getCallingPid()
3034 + ", uid=" + Binder.getCallingUid());
3035 }
3036
3037 OutputStream clientStream = null;
3038 try {
3039 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3040 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3041 } catch (IOException e) {
3042 e.printStackTrace();
3043 } finally {
3044 if (clientStream != null) {
3045 try {
3046 clientStream.close();
3047 } catch (IOException e) {
3048 e.printStackTrace();
3049 }
3050 }
3051 }
3052 }
3053 }
3054 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003055
Dianne Hackbornffa42482009-09-23 22:20:11 -07003056 public void closeSystemDialogs(String reason) {
3057 final ViewRoot viewRoot = mViewRoot.get();
3058 if (viewRoot != null) {
3059 viewRoot.dispatchCloseSystemDialogs(reason);
3060 }
3061 }
3062
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003063 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3064 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003065 if (sync) {
3066 try {
3067 sWindowSession.wallpaperOffsetsComplete(asBinder());
3068 } catch (RemoteException e) {
3069 }
3070 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003071 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003072
3073 public void dispatchWallpaperCommand(String action, int x, int y,
3074 int z, Bundle extras, boolean sync) {
3075 if (sync) {
3076 try {
3077 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3078 } catch (RemoteException e) {
3079 }
3080 }
3081 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003082
3083 /* Drag/drop */
3084 public void dispatchDragEvent(DragEvent event) {
3085 final ViewRoot viewRoot = mViewRoot.get();
3086 if (viewRoot != null) {
3087 viewRoot.dispatchDragEvent(event);
3088 }
3089 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 }
3091
3092 /**
3093 * Maintains state information for a single trackball axis, generating
3094 * discrete (DPAD) movements based on raw trackball motion.
3095 */
3096 static final class TrackballAxis {
3097 /**
3098 * The maximum amount of acceleration we will apply.
3099 */
3100 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003102 /**
3103 * The maximum amount of time (in milliseconds) between events in order
3104 * for us to consider the user to be doing fast trackball movements,
3105 * and thus apply an acceleration.
3106 */
3107 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 /**
3110 * Scaling factor to the time (in milliseconds) between events to how
3111 * much to multiple/divide the current acceleration. When movement
3112 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3113 * FAST_MOVE_TIME it divides it.
3114 */
3115 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003117 float position;
3118 float absPosition;
3119 float acceleration = 1;
3120 long lastMoveTime = 0;
3121 int step;
3122 int dir;
3123 int nonAccelMovement;
3124
3125 void reset(int _step) {
3126 position = 0;
3127 acceleration = 1;
3128 lastMoveTime = 0;
3129 step = _step;
3130 dir = 0;
3131 }
3132
3133 /**
3134 * Add trackball movement into the state. If the direction of movement
3135 * has been reversed, the state is reset before adding the
3136 * movement (so that you don't have to compensate for any previously
3137 * collected movement before see the result of the movement in the
3138 * new direction).
3139 *
3140 * @return Returns the absolute value of the amount of movement
3141 * collected so far.
3142 */
3143 float collect(float off, long time, String axis) {
3144 long normTime;
3145 if (off > 0) {
3146 normTime = (long)(off * FAST_MOVE_TIME);
3147 if (dir < 0) {
3148 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3149 position = 0;
3150 step = 0;
3151 acceleration = 1;
3152 lastMoveTime = 0;
3153 }
3154 dir = 1;
3155 } else if (off < 0) {
3156 normTime = (long)((-off) * FAST_MOVE_TIME);
3157 if (dir > 0) {
3158 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3159 position = 0;
3160 step = 0;
3161 acceleration = 1;
3162 lastMoveTime = 0;
3163 }
3164 dir = -1;
3165 } else {
3166 normTime = 0;
3167 }
Romain Guy8506ab42009-06-11 17:35:47 -07003168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003169 // The number of milliseconds between each movement that is
3170 // considered "normal" and will not result in any acceleration
3171 // or deceleration, scaled by the offset we have here.
3172 if (normTime > 0) {
3173 long delta = time - lastMoveTime;
3174 lastMoveTime = time;
3175 float acc = acceleration;
3176 if (delta < normTime) {
3177 // The user is scrolling rapidly, so increase acceleration.
3178 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3179 if (scale > 1) acc *= scale;
3180 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3181 + off + " normTime=" + normTime + " delta=" + delta
3182 + " scale=" + scale + " acc=" + acc);
3183 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3184 } else {
3185 // The user is scrolling slowly, so decrease acceleration.
3186 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3187 if (scale > 1) acc /= scale;
3188 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3189 + off + " normTime=" + normTime + " delta=" + delta
3190 + " scale=" + scale + " acc=" + acc);
3191 acceleration = acc > 1 ? acc : 1;
3192 }
3193 }
3194 position += off;
3195 return (absPosition = Math.abs(position));
3196 }
3197
3198 /**
3199 * Generate the number of discrete movement events appropriate for
3200 * the currently collected trackball movement.
3201 *
3202 * @param precision The minimum movement required to generate the
3203 * first discrete movement.
3204 *
3205 * @return Returns the number of discrete movements, either positive
3206 * or negative, or 0 if there is not enough trackball movement yet
3207 * for a discrete movement.
3208 */
3209 int generate(float precision) {
3210 int movement = 0;
3211 nonAccelMovement = 0;
3212 do {
3213 final int dir = position >= 0 ? 1 : -1;
3214 switch (step) {
3215 // If we are going to execute the first step, then we want
3216 // to do this as soon as possible instead of waiting for
3217 // a full movement, in order to make things look responsive.
3218 case 0:
3219 if (absPosition < precision) {
3220 return movement;
3221 }
3222 movement += dir;
3223 nonAccelMovement += dir;
3224 step = 1;
3225 break;
3226 // If we have generated the first movement, then we need
3227 // to wait for the second complete trackball motion before
3228 // generating the second discrete movement.
3229 case 1:
3230 if (absPosition < 2) {
3231 return movement;
3232 }
3233 movement += dir;
3234 nonAccelMovement += dir;
3235 position += dir > 0 ? -2 : 2;
3236 absPosition = Math.abs(position);
3237 step = 2;
3238 break;
3239 // After the first two, we generate discrete movements
3240 // consistently with the trackball, applying an acceleration
3241 // if the trackball is moving quickly. This is a simple
3242 // acceleration on top of what we already compute based
3243 // on how quickly the wheel is being turned, to apply
3244 // a longer increasing acceleration to continuous movement
3245 // in one direction.
3246 default:
3247 if (absPosition < 1) {
3248 return movement;
3249 }
3250 movement += dir;
3251 position += dir >= 0 ? -1 : 1;
3252 absPosition = Math.abs(position);
3253 float acc = acceleration;
3254 acc *= 1.1f;
3255 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3256 break;
3257 }
3258 } while (true);
3259 }
3260 }
3261
3262 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3263 public CalledFromWrongThreadException(String msg) {
3264 super(msg);
3265 }
3266 }
3267
3268 private SurfaceHolder mHolder = new SurfaceHolder() {
3269 // we only need a SurfaceHolder for opengl. it would be nice
3270 // to implement everything else though, especially the callback
3271 // support (opengl doesn't make use of it right now, but eventually
3272 // will).
3273 public Surface getSurface() {
3274 return mSurface;
3275 }
3276
3277 public boolean isCreating() {
3278 return false;
3279 }
3280
3281 public void addCallback(Callback callback) {
3282 }
3283
3284 public void removeCallback(Callback callback) {
3285 }
3286
3287 public void setFixedSize(int width, int height) {
3288 }
3289
3290 public void setSizeFromLayout() {
3291 }
3292
3293 public void setFormat(int format) {
3294 }
3295
3296 public void setType(int type) {
3297 }
3298
3299 public void setKeepScreenOn(boolean screenOn) {
3300 }
3301
3302 public Canvas lockCanvas() {
3303 return null;
3304 }
3305
3306 public Canvas lockCanvas(Rect dirty) {
3307 return null;
3308 }
3309
3310 public void unlockCanvasAndPost(Canvas canvas) {
3311 }
3312 public Rect getSurfaceFrame() {
3313 return null;
3314 }
3315 };
3316
3317 static RunQueue getRunQueue() {
3318 RunQueue rq = sRunQueues.get();
3319 if (rq != null) {
3320 return rq;
3321 }
3322 rq = new RunQueue();
3323 sRunQueues.set(rq);
3324 return rq;
3325 }
Romain Guy8506ab42009-06-11 17:35:47 -07003326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003327 /**
3328 * @hide
3329 */
3330 static final class RunQueue {
3331 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3332
3333 void post(Runnable action) {
3334 postDelayed(action, 0);
3335 }
3336
3337 void postDelayed(Runnable action, long delayMillis) {
3338 HandlerAction handlerAction = new HandlerAction();
3339 handlerAction.action = action;
3340 handlerAction.delay = delayMillis;
3341
3342 synchronized (mActions) {
3343 mActions.add(handlerAction);
3344 }
3345 }
3346
3347 void removeCallbacks(Runnable action) {
3348 final HandlerAction handlerAction = new HandlerAction();
3349 handlerAction.action = action;
3350
3351 synchronized (mActions) {
3352 final ArrayList<HandlerAction> actions = mActions;
3353
3354 while (actions.remove(handlerAction)) {
3355 // Keep going
3356 }
3357 }
3358 }
3359
3360 void executeActions(Handler handler) {
3361 synchronized (mActions) {
3362 final ArrayList<HandlerAction> actions = mActions;
3363 final int count = actions.size();
3364
3365 for (int i = 0; i < count; i++) {
3366 final HandlerAction handlerAction = actions.get(i);
3367 handler.postDelayed(handlerAction.action, handlerAction.delay);
3368 }
3369
Romain Guy15df6702009-08-17 20:17:30 -07003370 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 }
3372 }
3373
3374 private static class HandlerAction {
3375 Runnable action;
3376 long delay;
3377
3378 @Override
3379 public boolean equals(Object o) {
3380 if (this == o) return true;
3381 if (o == null || getClass() != o.getClass()) return false;
3382
3383 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 return !(action != null ? !action.equals(that.action) : that.action != null);
3385
3386 }
3387
3388 @Override
3389 public int hashCode() {
3390 int result = action != null ? action.hashCode() : 0;
3391 result = 31 * result + (int) (delay ^ (delay >>> 32));
3392 return result;
3393 }
3394 }
3395 }
3396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398}