blob: b7ab3c163b449767e346f50c08c68f9baa14b517 [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;
Dianne Hackborn0f761d62010-11-30 22:06:10 -080028import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.Canvas;
Dianne Hackborn0f761d62010-11-30 22:06:10 -080030import android.graphics.Paint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.graphics.PixelFormat;
Christopher Tate2c095f32010-10-04 14:13:40 -070032import android.graphics.Point;
Christopher Tatea53146c2010-09-07 11:57:52 -070033import android.graphics.PointF;
Romain Guy6b7bd242010-10-06 19:49:23 -070034import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.Rect;
36import android.graphics.Region;
Romain Guy6b7bd242010-10-06 19:49:23 -070037import android.media.AudioManager;
38import android.os.Binder;
39import android.os.Bundle;
40import android.os.Debug;
41import android.os.Handler;
42import android.os.LatencyTimer;
43import android.os.Looper;
44import android.os.Message;
45import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Process;
Romain Guy6b7bd242010-10-06 19:49:23 -070047import android.os.RemoteException;
48import android.os.ServiceManager;
49import android.os.SystemClock;
50import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.AndroidRuntimeException;
52import android.util.Config;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070053import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.EventLog;
Romain Guy6b7bd242010-10-06 19:49:23 -070055import android.util.Log;
Chet Haase949dbf72010-08-11 18:41:06 -070056import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.util.SparseArray;
Dianne Hackborn711e62a2010-11-29 16:38:22 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityManager;
Dianne Hackborn0f761d62010-11-30 22:06:10 -080062import android.view.animation.AccelerateDecelerateInterpolator;
63import android.view.animation.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.inputmethod.InputConnection;
65import android.view.inputmethod.InputMethodManager;
66import android.widget.Scroller;
Joe Onorato86f67862010-11-05 18:57:34 -070067import com.android.internal.policy.PolicyManager;
Romain Guy6b7bd242010-10-06 19:49:23 -070068import com.android.internal.view.BaseSurfaceHolder;
69import com.android.internal.view.IInputMethodCallback;
70import com.android.internal.view.IInputMethodSession;
71import com.android.internal.view.RootViewSurfaceTaker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import java.io.IOException;
74import java.io.OutputStream;
Romain Guy6b7bd242010-10-06 19:49:23 -070075import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import java.util.ArrayList;
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078/**
79 * The top of a view hierarchy, implementing the needed protocol between View
80 * and the WindowManager. This is for the most part an internal implementation
81 * detail of {@link WindowManagerImpl}.
82 *
83 * {@hide}
84 */
Romain Guy812ccbe2010-06-01 14:07:24 -070085@SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"})
Dianne Hackborn0f761d62010-11-30 22:06:10 -080086public final class ViewRoot extends Handler implements ViewParent,
87 View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 private static final String TAG = "ViewRoot";
89 private static final boolean DBG = false;
Mike Reedfd716532009-10-12 14:42:56 -040090 private static final boolean SHOW_FPS = false;
Romain Guy812ccbe2010-06-01 14:07:24 -070091 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 /** @noinspection PointlessBooleanExpression*/
93 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
94 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
Dianne Hackborn711e62a2010-11-29 16:38:22 -080095 private static final boolean DEBUG_DIALOG = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
97 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
98 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
99 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700100 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 private static final boolean WATCH_POINTER = false;
102
Michael Chan53071d62009-05-13 17:29:48 -0700103 private static final boolean MEASURE_LATENCY = false;
104 private static LatencyTimer lt;
105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 /**
107 * Maximum time we allow the user to roll the trackball enough to generate
108 * a key event, before resetting the counters.
109 */
110 static final int MAX_TRACKBALL_DELAY = 250;
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 static IWindowSession sWindowSession;
113
114 static final Object mStaticInit = new Object();
115 static boolean mInitialized = false;
116
117 static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>();
118
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800119 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList<Runnable>();
120 static boolean sFirstDrawComplete = false;
121
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800122 static final ArrayList<ComponentCallbacks> sConfigCallbacks
123 = new ArrayList<ComponentCallbacks>();
124
Romain Guy8506ab42009-06-11 17:35:47 -0700125 private static int sDrawTime;
Romain Guy13922e02009-05-12 17:56:14 -0700126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 long mLastTrackballTime = 0;
128 final TrackballAxis mTrackballAxisX = new TrackballAxis();
129 final TrackballAxis mTrackballAxisY = new TrackballAxis();
130
131 final int[] mTmpLocation = new int[2];
Romain Guy8506ab42009-06-11 17:35:47 -0700132
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800133 final TypedValue mTmpValue = new TypedValue();
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 final InputMethodCallback mInputMethodCallback;
136 final SparseArray<Object> mPendingEvents = new SparseArray<Object>();
137 int mPendingEventSeq = 0;
Romain Guy8506ab42009-06-11 17:35:47 -0700138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 final Thread mThread;
140
141 final WindowLeaked mLocation;
142
143 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
144
145 final W mWindow;
146
147 View mView;
148 View mFocusedView;
149 View mRealFocusedView; // this is not set to null in touch mode
150 int mViewVisibility;
151 boolean mAppVisible = true;
152
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700153 SurfaceHolder.Callback2 mSurfaceHolderCallback;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700154 BaseSurfaceHolder mSurfaceHolder;
155 boolean mIsCreating;
156 boolean mDrawingAllowed;
157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 final Region mTransparentRegion;
159 final Region mPreviousTransparentRegion;
160
161 int mWidth;
162 int mHeight;
163 Rect mDirty; // will be a graphics.Region soon
Romain Guybb93d552009-03-24 21:04:15 -0700164 boolean mIsAnimating;
Romain Guy8506ab42009-06-11 17:35:47 -0700165
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700166 CompatibilityInfo.Translator mTranslator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167
168 final View.AttachInfo mAttachInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700169 InputChannel mInputChannel;
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700170 InputQueue.Callback mInputQueueCallback;
171 InputQueue mInputQueue;
Joe Onorato86f67862010-11-05 18:57:34 -0700172 FallbackEventHandler mFallbackEventHandler;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 final Rect mTempRect; // used in the transaction to not thrash the heap.
175 final Rect mVisRect; // used to retrieve visible rect of focused view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
177 boolean mTraversalScheduled;
178 boolean mWillDrawSoon;
179 boolean mLayoutRequested;
180 boolean mFirst;
181 boolean mReportNextDraw;
182 boolean mFullRedrawNeeded;
183 boolean mNewSurfaceNeeded;
184 boolean mHasHadWindowFocus;
185 boolean mLastWasImTarget;
186
187 boolean mWindowAttributesChanged = false;
188
189 // These can be accessed by any thread, must be protected with a lock.
Mathias Agopian5583dc62009-07-09 16:28:11 -0700190 // Surface can never be reassigned or cleared (use Surface.clear()).
191 private final Surface mSurface = new Surface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
193 boolean mAdded;
194 boolean mAddedTouchMode;
195
196 /*package*/ int mAddNesting;
197
198 // These are accessed by multiple threads.
199 final Rect mWinFrame; // frame given by window manager.
200
201 final Rect mPendingVisibleInsets = new Rect();
202 final Rect mPendingContentInsets = new Rect();
203 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
204 = new ViewTreeObserver.InternalInsetsInfo();
205
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700206 final Configuration mLastConfiguration = new Configuration();
207 final Configuration mPendingConfiguration = new Configuration();
208
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800209 class ResizedInfo {
210 Rect coveredInsets;
211 Rect visibleInsets;
212 Configuration newConfig;
213 }
214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 boolean mScrollMayChange;
216 int mSoftInputMode;
217 View mLastScrolledFocus;
218 int mScrollY;
219 int mCurScrollY;
220 Scroller mScroller;
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800221 Bitmap mResizeBitmap;
222 long mResizeBitmapStartTime;
223 int mResizeBitmapDuration;
224 static final Interpolator mResizeInterpolator = new AccelerateDecelerateInterpolator();
Romain Guy8506ab42009-06-11 17:35:47 -0700225
Romain Guy8506ab42009-06-11 17:35:47 -0700226 final ViewConfiguration mViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227
Christopher Tatea53146c2010-09-07 11:57:52 -0700228 /* Drag/drop */
229 ClipDescription mDragDescription;
230 View mCurrentDragView;
Christopher Tate407b4e92010-11-30 17:14:08 -0800231 Object mLocalDragState;
Christopher Tatea53146c2010-09-07 11:57:52 -0700232 final PointF mDragPoint = new PointF();
Christopher Tate2c095f32010-10-04 14:13:40 -0700233 final PointF mLastTouchPoint = new PointF();
Christopher Tatea53146c2010-09-07 11:57:52 -0700234
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 /**
236 * see {@link #playSoundEffect(int)}
237 */
238 AudioManager mAudioManager;
239
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700240 private final int mDensity;
Adam Powellb08013c2010-09-16 16:28:11 -0700241
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700242 public static IWindowSession getWindowSession(Looper mainLooper) {
243 synchronized (mStaticInit) {
244 if (!mInitialized) {
245 try {
246 InputMethodManager imm = InputMethodManager.getInstance(mainLooper);
247 sWindowSession = IWindowManager.Stub.asInterface(
248 ServiceManager.getService("window"))
249 .openSession(imm.getClient(), imm.getInputContext());
250 mInitialized = true;
251 } catch (RemoteException e) {
252 }
253 }
254 return sWindowSession;
255 }
256 }
257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 public ViewRoot(Context context) {
259 super();
260
Romain Guy812ccbe2010-06-01 14:07:24 -0700261 if (MEASURE_LATENCY) {
262 if (lt == null) {
263 lt = new LatencyTimer(100, 1000);
264 }
Michael Chan53071d62009-05-13 17:29:48 -0700265 }
266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 // Initialize the statics when this class is first instantiated. This is
268 // done here instead of in the static block because Zygote does not
269 // allow the spawning of threads.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700270 getWindowSession(context.getMainLooper());
271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mThread = Thread.currentThread();
273 mLocation = new WindowLeaked(null);
274 mLocation.fillInStackTrace();
275 mWidth = -1;
276 mHeight = -1;
277 mDirty = new Rect();
278 mTempRect = new Rect();
279 mVisRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 mWinFrame = new Rect();
Romain Guyfb8b7632010-08-23 21:05:08 -0700281 mWindow = new W(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 mInputMethodCallback = new InputMethodCallback(this);
283 mViewVisibility = View.GONE;
284 mTransparentRegion = new Region();
285 mPreviousTransparentRegion = new Region();
286 mFirst = true; // true for the first time the view is added
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 mAdded = false;
288 mAttachInfo = new View.AttachInfo(sWindowSession, mWindow, this, this);
289 mViewConfiguration = ViewConfiguration.get(context);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700290 mDensity = context.getResources().getDisplayMetrics().densityDpi;
Joe Onorato86f67862010-11-05 18:57:34 -0700291 mFallbackEventHandler = PolicyManager.makeNewFallbackEventHandler(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 }
293
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800294 public static void addFirstDrawHandler(Runnable callback) {
295 synchronized (sFirstDrawHandlers) {
296 if (!sFirstDrawComplete) {
297 sFirstDrawHandlers.add(callback);
298 }
299 }
300 }
301
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800302 public static void addConfigCallback(ComponentCallbacks callback) {
303 synchronized (sConfigCallbacks) {
304 sConfigCallbacks.add(callback);
305 }
306 }
307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 // FIXME for perf testing only
309 private boolean mProfile = false;
310
311 /**
312 * Call this to profile the next traversal call.
313 * FIXME for perf testing only. Remove eventually
314 */
315 public void profile() {
316 mProfile = true;
317 }
318
319 /**
320 * Indicates whether we are in touch mode. Calling this method triggers an IPC
321 * call and should be avoided whenever possible.
322 *
323 * @return True, if the device is in touch mode, false otherwise.
324 *
325 * @hide
326 */
327 static boolean isInTouchMode() {
328 if (mInitialized) {
329 try {
330 return sWindowSession.getInTouchMode();
331 } catch (RemoteException e) {
332 }
333 }
334 return false;
335 }
336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 /**
338 * We have one child
339 */
Romain Guye4d01122010-06-16 18:44:05 -0700340 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 synchronized (this) {
342 if (mView == null) {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700343 mView = view;
Joe Onorato86f67862010-11-05 18:57:34 -0700344 mFallbackEventHandler.setView(view);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700345 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700346 attrs = mWindowAttributes;
Romain Guye4d01122010-06-16 18:44:05 -0700347
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700348 if (view instanceof RootViewSurfaceTaker) {
349 mSurfaceHolderCallback =
350 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
351 if (mSurfaceHolderCallback != null) {
352 mSurfaceHolder = new TakenSurfaceHolder();
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700353 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700354 }
355 }
Romain Guy1aec9a22011-01-05 09:37:12 -0800356
357 // If the application owns the surface, don't enable hardware acceleration
358 if (mSurfaceHolder == null) {
359 enableHardwareAcceleration(attrs);
360 }
361
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700362 Resources resources = mView.getContext().getResources();
363 CompatibilityInfo compatibilityInfo = resources.getCompatibilityInfo();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700364 mTranslator = compatibilityInfo.getTranslator();
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700365
366 if (mTranslator != null || !compatibilityInfo.supportsScreen()) {
Mitsuru Oshima240f8a72009-07-22 20:39:14 -0700367 mSurface.setCompatibleDisplayMetrics(resources.getDisplayMetrics(),
368 mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700369 }
370
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700371 boolean restore = false;
Romain Guy35b38ce2009-10-07 13:38:55 -0700372 if (mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700373 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700374 attrs.backup();
375 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700376 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700377 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs);
378
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700379 if (!compatibilityInfo.supportsScreen()) {
380 attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
381 }
382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 mSoftInputMode = attrs.softInputMode;
384 mWindowAttributesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 mAttachInfo.mRootView = view;
Romain Guy35b38ce2009-10-07 13:38:55 -0700386 mAttachInfo.mScalingRequired = mTranslator != null;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700387 mAttachInfo.mApplicationScale =
388 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 if (panelParentView != null) {
390 mAttachInfo.mPanelParentWindowToken
391 = panelParentView.getApplicationWindowToken();
392 }
393 mAdded = true;
394 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 // Schedule the first layout -before- adding to the window
397 // manager, to make sure we do the relayout before receiving
398 // any other events from the system.
399 requestLayout();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700400 mInputChannel = new InputChannel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 try {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700402 res = sWindowSession.add(mWindow, mWindowAttributes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700403 getHostVisibility(), mAttachInfo.mContentInsets,
404 mInputChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 } catch (RemoteException e) {
406 mAdded = false;
407 mView = null;
408 mAttachInfo.mRootView = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700409 mInputChannel = null;
Joe Onorato86f67862010-11-05 18:57:34 -0700410 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 unscheduleTraversals();
412 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700413 } finally {
414 if (restore) {
415 attrs.restore();
416 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700418
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700419 if (mTranslator != null) {
420 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700421 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 mPendingContentInsets.set(mAttachInfo.mContentInsets);
423 mPendingVisibleInsets.set(0, 0, 0, 0);
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800424 if (DEBUG_LAYOUT) Log.v(TAG, "Added window " + mWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 if (res < WindowManagerImpl.ADD_OKAY) {
426 mView = null;
427 mAttachInfo.mRootView = null;
428 mAdded = false;
Joe Onorato86f67862010-11-05 18:57:34 -0700429 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 unscheduleTraversals();
431 switch (res) {
432 case WindowManagerImpl.ADD_BAD_APP_TOKEN:
433 case WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN:
434 throw new WindowManagerImpl.BadTokenException(
435 "Unable to add window -- token " + attrs.token
436 + " is not valid; is your activity running?");
437 case WindowManagerImpl.ADD_NOT_APP_TOKEN:
438 throw new WindowManagerImpl.BadTokenException(
439 "Unable to add window -- token " + attrs.token
440 + " is not for an application");
441 case WindowManagerImpl.ADD_APP_EXITING:
442 throw new WindowManagerImpl.BadTokenException(
443 "Unable to add window -- app for token " + attrs.token
444 + " is exiting");
445 case WindowManagerImpl.ADD_DUPLICATE_ADD:
446 throw new WindowManagerImpl.BadTokenException(
447 "Unable to add window -- window " + mWindow
448 + " has already been added");
449 case WindowManagerImpl.ADD_STARTING_NOT_NEEDED:
450 // Silently ignore -- we would have just removed it
451 // right away, anyway.
452 return;
453 case WindowManagerImpl.ADD_MULTIPLE_SINGLETON:
454 throw new WindowManagerImpl.BadTokenException(
455 "Unable to add window " + mWindow +
456 " -- another window of this type already exists");
457 case WindowManagerImpl.ADD_PERMISSION_DENIED:
458 throw new WindowManagerImpl.BadTokenException(
459 "Unable to add window " + mWindow +
460 " -- permission denied for this window type");
461 }
462 throw new RuntimeException(
463 "Unable to add window -- unknown error code " + res);
464 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700465
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700466 if (view instanceof RootViewSurfaceTaker) {
467 mInputQueueCallback =
468 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
469 }
470 if (mInputQueueCallback != null) {
471 mInputQueue = new InputQueue(mInputChannel);
472 mInputQueueCallback.onInputQueueCreated(mInputQueue);
473 } else {
474 InputQueue.registerInputChannel(mInputChannel, mInputHandler,
475 Looper.myQueue());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700476 }
477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 view.assignParent(this);
479 mAddedTouchMode = (res&WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE) != 0;
480 mAppVisible = (res&WindowManagerImpl.ADD_FLAG_APP_VISIBLE) != 0;
481 }
482 }
483 }
484
Romain Guy529b60a2010-08-03 18:05:47 -0700485 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800486 mAttachInfo.mHardwareAccelerated = false;
487 mAttachInfo.mHardwareAccelerationRequested = false;
Romain Guy4f6aff32011-01-12 16:21:41 -0800488
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800489 // Try to enable hardware acceleration if requested
490 if (attrs != null &&
491 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
Romain Guy501053a2011-01-13 18:38:45 -0800492 // Don't enable hardware acceleration when we're not on the main thread
493 if (Looper.getMainLooper() != Looper.myLooper()) {
494 Log.w(HardwareRenderer.LOG_TAG, "Attempting to initialize hardware acceleration "
495 + "outside of the main thread, aborting");
496 return;
497 }
498
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800499 // Only enable hardware acceleration if we are not in the system process
500 // The window manager creates ViewRoots to display animated preview windows
501 // of launching apps and we don't want those to be hardware accelerated
502 if (!HardwareRenderer.sRendererDisabled) {
Romain Guye4d01122010-06-16 18:44:05 -0700503 final boolean translucent = attrs.format != PixelFormat.OPAQUE;
Romain Guyb051e892010-09-28 19:09:36 -0700504 if (mAttachInfo.mHardwareRenderer != null) {
505 mAttachInfo.mHardwareRenderer.destroy(true);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700506 }
Romain Guyb051e892010-09-28 19:09:36 -0700507 mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent);
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800508 mAttachInfo.mHardwareAccelerated = mAttachInfo.mHardwareAccelerationRequested
509 = mAttachInfo.mHardwareRenderer != null;
510 } else if (HardwareRenderer.isAvailable()) {
511 mAttachInfo.mHardwareAccelerationRequested = true;
Romain Guye4d01122010-06-16 18:44:05 -0700512 }
513 }
514 }
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public View getView() {
517 return mView;
518 }
519
520 final WindowLeaked getLocation() {
521 return mLocation;
522 }
523
524 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
525 synchronized (this) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700526 int oldSoftInputMode = mWindowAttributes.softInputMode;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700527 // preserve compatible window flag if exists.
528 int compatibleWindowFlag =
529 mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700531 mWindowAttributes.flags |= compatibleWindowFlag;
532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 if (newView) {
534 mSoftInputMode = attrs.softInputMode;
535 requestLayout();
536 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700537 // Don't lose the mode we last auto-computed.
538 if ((attrs.softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
539 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
540 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
541 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
542 | (oldSoftInputMode
543 & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545 mWindowAttributesChanged = true;
546 scheduleTraversals();
547 }
548 }
549
550 void handleAppVisibility(boolean visible) {
551 if (mAppVisible != visible) {
552 mAppVisible = visible;
553 scheduleTraversals();
554 }
555 }
556
557 void handleGetNewSurface() {
558 mNewSurfaceNeeded = true;
559 mFullRedrawNeeded = true;
560 scheduleTraversals();
561 }
562
563 /**
564 * {@inheritDoc}
565 */
566 public void requestLayout() {
567 checkThread();
568 mLayoutRequested = true;
569 scheduleTraversals();
570 }
571
572 /**
573 * {@inheritDoc}
574 */
575 public boolean isLayoutRequested() {
576 return mLayoutRequested;
577 }
578
579 public void invalidateChild(View child, Rect dirty) {
580 checkThread();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700581 if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
Chet Haase70d4ba12010-10-06 09:46:45 -0700582 if (dirty == null) {
583 // Fast invalidation for GL-enabled applications; GL must redraw everything
584 invalidate();
585 return;
586 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700587 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -0700589 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700590 if (mCurScrollY != 0) {
Romain Guy1e095972009-07-07 11:22:45 -0700591 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700592 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700593 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -0700594 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700595 }
Romain Guy1e095972009-07-07 11:22:45 -0700596 if (mAttachInfo.mScalingRequired) {
597 dirty.inset(-1, -1);
598 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 }
Romain Guy7d695942010-12-01 17:22:29 -0800600 if (!mDirty.isEmpty()) {
601 mAttachInfo.mIgnoreDirtyState = true;
602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 mDirty.union(dirty);
604 if (!mWillDrawSoon) {
605 scheduleTraversals();
606 }
607 }
Romain Guy0d9275e2010-10-26 14:22:30 -0700608
609 void invalidate() {
610 mDirty.set(0, 0, mWidth, mHeight);
611 scheduleTraversals();
612 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613
614 public ViewParent getParent() {
615 return null;
616 }
617
618 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
619 invalidateChild(null, dirty);
620 return null;
621 }
622
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700623 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 if (child != mView) {
625 throw new RuntimeException("child is not mine, honest!");
626 }
627 // Note: don't apply scroll offset, because we want to know its
628 // visibility in the virtual canvas being given to the view hierarchy.
629 return r.intersect(0, 0, mWidth, mHeight);
630 }
631
632 public void bringChildToFront(View child) {
633 }
634
635 public void scheduleTraversals() {
636 if (!mTraversalScheduled) {
637 mTraversalScheduled = true;
638 sendEmptyMessage(DO_TRAVERSAL);
639 }
640 }
641
642 public void unscheduleTraversals() {
643 if (mTraversalScheduled) {
644 mTraversalScheduled = false;
645 removeMessages(DO_TRAVERSAL);
646 }
647 }
648
649 int getHostVisibility() {
650 return mAppVisible ? mView.getVisibility() : View.GONE;
651 }
Romain Guy8506ab42009-06-11 17:35:47 -0700652
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800653 void disposeResizeBitmap() {
654 if (mResizeBitmap != null) {
655 mResizeBitmap.recycle();
656 mResizeBitmap = null;
657 }
658 }
659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 private void performTraversals() {
661 // cache mView since it is used so much below...
662 final View host = mView;
663
664 if (DBG) {
665 System.out.println("======================================");
666 System.out.println("performTraversals");
667 host.debug();
668 }
669
670 if (host == null || !mAdded)
671 return;
672
673 mTraversalScheduled = false;
674 mWillDrawSoon = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800675 boolean windowSizeMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 boolean fullRedrawNeeded = mFullRedrawNeeded;
677 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700678 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 WindowManager.LayoutParams lp = mWindowAttributes;
680
681 int desiredWindowWidth;
682 int desiredWindowHeight;
683 int childWidthMeasureSpec;
684 int childHeightMeasureSpec;
685
686 final View.AttachInfo attachInfo = mAttachInfo;
687
688 final int viewVisibility = getHostVisibility();
689 boolean viewVisibilityChanged = mViewVisibility != viewVisibility
690 || mNewSurfaceNeeded;
691
692 WindowManager.LayoutParams params = null;
693 if (mWindowAttributesChanged) {
694 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700695 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 params = lp;
697 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700698 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (mFirst) {
700 fullRedrawNeeded = true;
701 mLayoutRequested = true;
702
Romain Guy8506ab42009-06-11 17:35:47 -0700703 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700704 mView.getContext().getResources().getDisplayMetrics();
705 desiredWindowWidth = packageMetrics.widthPixels;
706 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707
708 // For the very first time, tell the view hierarchy that it
709 // is attached to the window. Note that at this point the surface
710 // object is not initialized to its backing store, but soon it
711 // will be (assuming the window is visible).
712 attachInfo.mSurface = mSurface;
Adam Powell26153a32010-11-08 15:22:27 -0800713 attachInfo.mUse32BitDrawingCache = PixelFormat.formatHasAlpha(lp.format) ||
714 lp.format == PixelFormat.RGBX_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 attachInfo.mHasWindowFocus = false;
716 attachInfo.mWindowVisibility = viewVisibility;
717 attachInfo.mRecomputeGlobalAttributes = false;
718 attachInfo.mKeepScreenOn = false;
719 viewVisibilityChanged = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700720 mLastConfiguration.setTo(host.getResources().getConfiguration());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 host.dispatchAttachedToWindow(attachInfo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -0700723
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700725 desiredWindowWidth = frame.width();
726 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700728 if (DEBUG_ORIENTATION) Log.v(TAG,
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700729 "View " + host + " resized to: " + frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 fullRedrawNeeded = true;
731 mLayoutRequested = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800732 windowSizeMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734 }
735
736 if (viewVisibilityChanged) {
737 attachInfo.mWindowVisibility = viewVisibility;
738 host.dispatchWindowVisibilityChanged(viewVisibility);
739 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
Romain Guyb051e892010-09-28 19:09:36 -0700740 if (mAttachInfo.mHardwareRenderer != null) {
741 mAttachInfo.mHardwareRenderer.destroy(false);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 }
744 if (viewVisibility == View.GONE) {
745 // After making a window gone, we will count it as being
746 // shown for the first time the next time it gets focus.
747 mHasHadWindowFocus = false;
748 }
749 }
750
751 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -0700752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 if (mLayoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -0700754 // Execute enqueued actions on every layout in case a view that was detached
755 // enqueued an action after being detached
756 getRunQueue().executeActions(attachInfo.mHandler);
757
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800758 final Resources res = mView.getContext().getResources();
759
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800760 if (mFirst) {
761 host.fitSystemWindows(mAttachInfo.mContentInsets);
762 // make sure touch mode code executes by setting cached value
763 // to opposite of the added touch mode.
764 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -0700765 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766 } else {
767 if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) {
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800768 if (mWidth > 0 && mHeight > 0 &&
769 mSurface != null && mSurface.isValid() &&
770 mAttachInfo.mHardwareRenderer != null &&
771 mAttachInfo.mHardwareRenderer.isEnabled() &&
772 lp != null && !PixelFormat.formatHasAlpha(lp.format)) {
773
774 disposeResizeBitmap();
775
776 boolean completed = false;
777 try {
778 mResizeBitmap = Bitmap.createBitmap(mWidth, mHeight,
779 Bitmap.Config.ARGB_8888);
780 mResizeBitmap.setHasAlpha(false);
781 Canvas canvas = new Canvas(mResizeBitmap);
782 int yoff;
783 final boolean scrolling = mScroller != null
784 && mScroller.computeScrollOffset();
785 if (scrolling) {
786 yoff = mScroller.getCurrY();
787 mScroller.abortAnimation();
788 } else {
789 yoff = mScrollY;
790 }
791 canvas.translate(0, -yoff);
792 if (mTranslator != null) {
793 mTranslator.translateCanvas(canvas);
794 }
795 canvas.setScreenDensity(mAttachInfo.mScalingRequired
796 ? DisplayMetrics.DENSITY_DEVICE : 0);
797 mView.draw(canvas);
798 mResizeBitmapStartTime = SystemClock.uptimeMillis();
799 mResizeBitmapDuration = mView.getResources().getInteger(
800 com.android.internal.R.integer.config_mediumAnimTime);
801 completed = true;
802 } catch (OutOfMemoryError e) {
803 Log.w(TAG, "Not enough memory for content change anim buffer", e);
804 } finally {
805 if (!completed) {
806 mResizeBitmap = null;
807 }
808 }
809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 mAttachInfo.mContentInsets.set(mPendingContentInsets);
811 host.fitSystemWindows(mAttachInfo.mContentInsets);
812 insetsChanged = true;
813 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
814 + mAttachInfo.mContentInsets);
815 }
816 if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) {
817 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
818 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
819 + mAttachInfo.mVisibleInsets);
820 }
821 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
822 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800823 windowSizeMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800825 DisplayMetrics packageMetrics = res.getDisplayMetrics();
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700826 desiredWindowWidth = packageMetrics.widthPixels;
827 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
829 }
830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 // Ask host how big it wants to be
Jeff Brownc5ed5912010-07-14 18:48:53 -0700832 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 "Measuring " + host + " in display " + desiredWindowWidth
834 + "x" + desiredWindowHeight + "...");
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800835
836 boolean goodMeasure = false;
837 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
838 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
839 // On large screens, we don't want to allow dialogs to just
840 // stretch to fill the entire width of the screen to display
841 // one line of text. First try doing the layout at a smaller
842 // size to see if it will fit.
843 final DisplayMetrics packageMetrics = res.getDisplayMetrics();
844 res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true);
845 int baseSize = 0;
846 if (mTmpValue.type == TypedValue.TYPE_DIMENSION) {
847 baseSize = (int)mTmpValue.getDimension(packageMetrics);
848 }
849 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": baseSize=" + baseSize);
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800850 if (baseSize != 0 && desiredWindowWidth > baseSize) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800851 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
852 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
853 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
854 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": measured ("
Dianne Hackborn189ee182010-12-02 21:48:53 -0800855 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")");
856 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800857 goodMeasure = true;
858 } else {
859 // Didn't fit in that size... try expanding a bit.
860 baseSize = (baseSize+desiredWindowWidth)/2;
861 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": next baseSize="
862 + baseSize);
Dianne Hackborn189ee182010-12-02 21:48:53 -0800863 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800864 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
865 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": measured ("
Dianne Hackborn189ee182010-12-02 21:48:53 -0800866 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")");
867 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800868 if (DEBUG_DIALOG) Log.v(TAG, "Good!");
869 goodMeasure = true;
870 }
871 }
872 }
873 }
874
875 if (!goodMeasure) {
876 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
877 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
878 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Powellaa0b92c2010-12-13 22:38:53 -0800879 if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) {
880 windowSizeMayChange = true;
881 }
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883
884 if (DBG) {
885 System.out.println("======================================");
886 System.out.println("performTraversals -- after measure");
887 host.debug();
888 }
889 }
890
891 if (attachInfo.mRecomputeGlobalAttributes) {
892 //Log.i(TAG, "Computing screen on!");
893 attachInfo.mRecomputeGlobalAttributes = false;
894 boolean oldVal = attachInfo.mKeepScreenOn;
895 attachInfo.mKeepScreenOn = false;
896 host.dispatchCollectViewAttributes(0);
897 if (attachInfo.mKeepScreenOn != oldVal) {
898 params = lp;
899 //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn);
900 }
901 }
902
903 if (mFirst || attachInfo.mViewVisibilityChanged) {
904 attachInfo.mViewVisibilityChanged = false;
905 int resizeMode = mSoftInputMode &
906 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
907 // If we are in auto resize mode, then we need to determine
908 // what mode to use now.
909 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
910 final int N = attachInfo.mScrollContainers.size();
911 for (int i=0; i<N; i++) {
912 if (attachInfo.mScrollContainers.get(i).isShown()) {
913 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
914 }
915 }
916 if (resizeMode == 0) {
917 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
918 }
919 if ((lp.softInputMode &
920 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
921 lp.softInputMode = (lp.softInputMode &
922 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
923 resizeMode;
924 params = lp;
925 }
926 }
927 }
Romain Guy8506ab42009-06-11 17:35:47 -0700928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
930 if (!PixelFormat.formatHasAlpha(params.format)) {
931 params.format = PixelFormat.TRANSLUCENT;
932 }
933 }
934
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800935 boolean windowShouldResize = mLayoutRequested && windowSizeMayChange
Dianne Hackborn189ee182010-12-02 21:48:53 -0800936 && ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight())
Romain Guy2e4f4262010-04-06 11:07:52 -0700937 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
938 frame.width() < desiredWindowWidth && frame.width() != mWidth)
939 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
940 frame.height() < desiredWindowHeight && frame.height() != mHeight));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941
942 final boolean computesInternalInsets =
943 attachInfo.mTreeObserver.hasComputeInternalInsetsListeners();
Romain Guy812ccbe2010-06-01 14:07:24 -0700944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 boolean insetsPending = false;
946 int relayoutResult = 0;
Romain Guy812ccbe2010-06-01 14:07:24 -0700947
948 if (mFirst || windowShouldResize || insetsChanged ||
949 viewVisibilityChanged || params != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950
951 if (viewVisibility == View.VISIBLE) {
952 // If this window is giving internal insets to the window
953 // manager, and it is being added or changing its visibility,
954 // then we want to first give the window manager "fake"
955 // insets to cause it to effectively ignore the content of
956 // the window during layout. This avoids it briefly causing
957 // other windows to resize/move based on the raw frame of the
958 // window, waiting until we can finish laying out this window
959 // and get back to the window manager with the ultimately
960 // computed insets.
Romain Guy812ccbe2010-06-01 14:07:24 -0700961 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700964 if (mSurfaceHolder != null) {
965 mSurfaceHolder.mSurfaceLock.lock();
966 mDrawingAllowed = true;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700967 }
Romain Guy812ccbe2010-06-01 14:07:24 -0700968
Romain Guyc361da82010-10-25 15:29:10 -0700969 boolean hwInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800970 boolean contentInsetsChanged = false;
Romain Guy13922e02009-05-12 17:56:14 -0700971 boolean visibleInsetsChanged;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700972 boolean hadSurface = mSurface.isValid();
Romain Guy812ccbe2010-06-01 14:07:24 -0700973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 int fl = 0;
976 if (params != null) {
977 fl = params.flags;
978 if (attachInfo.mKeepScreenOn) {
979 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
980 }
981 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700982 if (DEBUG_LAYOUT) {
Dianne Hackborn189ee182010-12-02 21:48:53 -0800983 Log.i(TAG, "host=w:" + host.getMeasuredWidth() + ", h:" +
984 host.getMeasuredHeight() + ", params=" + params);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700985 }
986 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
987
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 if (params != null) {
989 params.flags = fl;
990 }
991
992 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString()
993 + " content=" + mPendingContentInsets.toShortString()
994 + " visible=" + mPendingVisibleInsets.toShortString()
995 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -0700996
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700997 if (mPendingConfiguration.seq != 0) {
998 if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: "
999 + mPendingConfiguration);
1000 updateConfiguration(mPendingConfiguration, !mFirst);
1001 mPendingConfiguration.seq = 0;
1002 }
1003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 contentInsetsChanged = !mPendingContentInsets.equals(
1005 mAttachInfo.mContentInsets);
1006 visibleInsetsChanged = !mPendingVisibleInsets.equals(
1007 mAttachInfo.mVisibleInsets);
1008 if (contentInsetsChanged) {
1009 mAttachInfo.mContentInsets.set(mPendingContentInsets);
1010 host.fitSystemWindows(mAttachInfo.mContentInsets);
1011 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
1012 + mAttachInfo.mContentInsets);
1013 }
1014 if (visibleInsetsChanged) {
1015 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
1016 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
1017 + mAttachInfo.mVisibleInsets);
1018 }
1019
1020 if (!hadSurface) {
1021 if (mSurface.isValid()) {
1022 // If we are creating a new surface, then we need to
1023 // completely redraw it. Also, when we get to the
1024 // point of drawing it we will hold off and schedule
1025 // a new traversal instead. This is so we can tell the
1026 // window manager about all of the windows being displayed
1027 // before actually drawing them, so it can display then
1028 // all at once.
1029 newSurface = true;
1030 fullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -07001031 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -07001032
Romain Guyb051e892010-09-28 19:09:36 -07001033 if (mAttachInfo.mHardwareRenderer != null) {
Romain Guyc361da82010-10-25 15:29:10 -07001034 hwInitialized = mAttachInfo.mHardwareRenderer.initialize(mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 }
1036 }
1037 } else if (!mSurface.isValid()) {
1038 // If the surface has been removed, then reset the scroll
1039 // positions.
1040 mLastScrolledFocus = null;
1041 mScrollY = mCurScrollY = 0;
1042 if (mScroller != null) {
1043 mScroller.abortAnimation();
1044 }
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001045 disposeResizeBitmap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047 } catch (RemoteException e) {
1048 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 if (DEBUG_ORIENTATION) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001051 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052
1053 attachInfo.mWindowLeft = frame.left;
1054 attachInfo.mWindowTop = frame.top;
1055
1056 // !!FIXME!! This next section handles the case where we did not get the
1057 // window size we asked for. We should avoid this by getting a maximum size from
1058 // the window session beforehand.
1059 mWidth = frame.width();
1060 mHeight = frame.height();
1061
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001062 if (mSurfaceHolder != null) {
1063 // The app owns the surface; tell it about what is going on.
1064 if (mSurface.isValid()) {
1065 // XXX .copyFrom() doesn't work!
1066 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1067 mSurfaceHolder.mSurface = mSurface;
1068 }
1069 mSurfaceHolder.mSurfaceLock.unlock();
1070 if (mSurface.isValid()) {
1071 if (!hadSurface) {
1072 mSurfaceHolder.ungetCallbacks();
1073
1074 mIsCreating = true;
1075 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
1076 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1077 if (callbacks != null) {
1078 for (SurfaceHolder.Callback c : callbacks) {
1079 c.surfaceCreated(mSurfaceHolder);
1080 }
1081 }
1082 surfaceChanged = true;
1083 }
1084 if (surfaceChanged) {
1085 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
1086 lp.format, mWidth, mHeight);
1087 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1088 if (callbacks != null) {
1089 for (SurfaceHolder.Callback c : callbacks) {
1090 c.surfaceChanged(mSurfaceHolder, lp.format,
1091 mWidth, mHeight);
1092 }
1093 }
1094 }
1095 mIsCreating = false;
1096 } else if (hadSurface) {
1097 mSurfaceHolder.ungetCallbacks();
1098 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1099 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
1100 if (callbacks != null) {
1101 for (SurfaceHolder.Callback c : callbacks) {
1102 c.surfaceDestroyed(mSurfaceHolder);
1103 }
1104 }
1105 mSurfaceHolder.mSurfaceLock.lock();
1106 // Make surface invalid.
1107 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1108 mSurfaceHolder.mSurface = new Surface();
1109 mSurfaceHolder.mSurfaceLock.unlock();
1110 }
1111 }
Romain Guy53389bd2010-09-07 17:16:32 -07001112
Romain Guy6b5108b2011-01-04 16:11:10 -08001113 if (hwInitialized || ((windowShouldResize || params != null) &&
Romain Guydbf78bd2010-12-07 17:04:03 -08001114 mAttachInfo.mHardwareRenderer != null &&
1115 mAttachInfo.mHardwareRenderer.isEnabled())) {
Romain Guyb051e892010-09-28 19:09:36 -07001116 mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 }
1118
1119 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Romain Guy2d4cff62010-04-09 15:39:00 -07001120 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
Dianne Hackborn189ee182010-12-02 21:48:53 -08001121 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
1122 || mHeight != host.getMeasuredHeight() || contentInsetsChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
1124 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
1125
1126 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
Dianne Hackborn189ee182010-12-02 21:48:53 -08001127 + mWidth + " measuredWidth=" + host.getMeasuredWidth()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 + " mHeight=" + mHeight
Dianne Hackborn189ee182010-12-02 21:48:53 -08001129 + " measuredHeight" + host.getMeasuredHeight()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -07001131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 // Ask host how big it wants to be
1133 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1134
1135 // Implementation of weights from WindowManager.LayoutParams
1136 // We just grow the dimensions as needed and re-measure if
1137 // needs be
Dianne Hackborn189ee182010-12-02 21:48:53 -08001138 int width = host.getMeasuredWidth();
1139 int height = host.getMeasuredHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 boolean measureAgain = false;
1141
1142 if (lp.horizontalWeight > 0.0f) {
1143 width += (int) ((mWidth - width) * lp.horizontalWeight);
1144 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1145 MeasureSpec.EXACTLY);
1146 measureAgain = true;
1147 }
1148 if (lp.verticalWeight > 0.0f) {
1149 height += (int) ((mHeight - height) * lp.verticalWeight);
1150 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1151 MeasureSpec.EXACTLY);
1152 measureAgain = true;
1153 }
1154
1155 if (measureAgain) {
1156 if (DEBUG_LAYOUT) Log.v(TAG,
1157 "And hey let's measure once more: width=" + width
1158 + " height=" + height);
1159 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1160 }
1161
1162 mLayoutRequested = true;
1163 }
1164 }
1165
1166 final boolean didLayout = mLayoutRequested;
1167 boolean triggerGlobalLayoutListener = didLayout
1168 || attachInfo.mRecomputeGlobalAttributes;
1169 if (didLayout) {
1170 mLayoutRequested = false;
1171 mScrollMayChange = true;
1172 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001173 TAG, "Laying out " + host + " to (" +
Dianne Hackborn189ee182010-12-02 21:48:53 -08001174 host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")");
Romain Guy13922e02009-05-12 17:56:14 -07001175 long startTime = 0L;
Romain Guy5429e1d2010-09-07 12:38:00 -07001176 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001177 startTime = SystemClock.elapsedRealtime();
1178 }
Dianne Hackborn189ee182010-12-02 21:48:53 -08001179 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180
Romain Guy13922e02009-05-12 17:56:14 -07001181 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1182 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
1183 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
1184 + "please refer to the logs with the tag "
1185 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1186 }
1187 }
1188
Romain Guy5429e1d2010-09-07 12:38:00 -07001189 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1191 }
1192
1193 // By this point all views have been sized and positionned
1194 // We can compute the transparent area
1195
1196 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1197 // start out transparent
1198 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1199 host.getLocationInWindow(mTmpLocation);
1200 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1201 mTmpLocation[0] + host.mRight - host.mLeft,
1202 mTmpLocation[1] + host.mBottom - host.mTop);
1203
1204 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001205 if (mTranslator != null) {
1206 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1207 }
1208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1210 mPreviousTransparentRegion.set(mTransparentRegion);
1211 // reconfigure window manager
1212 try {
1213 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1214 } catch (RemoteException e) {
1215 }
1216 }
1217 }
1218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 if (DBG) {
1220 System.out.println("======================================");
1221 System.out.println("performTraversals -- after setFrame");
1222 host.debug();
1223 }
1224 }
1225
1226 if (triggerGlobalLayoutListener) {
1227 attachInfo.mRecomputeGlobalAttributes = false;
1228 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1229 }
1230
1231 if (computesInternalInsets) {
Jeff Brownfbf09772011-01-16 14:06:57 -08001232 // Clear the original insets.
1233 final ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1234 insets.reset();
1235
1236 // Compute new insets in place.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Jeff Brownfbf09772011-01-16 14:06:57 -08001238
1239 // Tell the window manager.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1241 mLastGivenInsets.set(insets);
Jeff Brownfbf09772011-01-16 14:06:57 -08001242
1243 // Translate insets to screen coordinates if needed.
1244 final Rect contentInsets;
1245 final Rect visibleInsets;
1246 final Region touchableRegion;
1247 if (mTranslator != null) {
1248 contentInsets = mTranslator.getTranslatedContentInsets(insets.contentInsets);
1249 visibleInsets = mTranslator.getTranslatedVisibleInsets(insets.visibleInsets);
1250 touchableRegion = mTranslator.getTranslatedTouchableArea(insets.touchableRegion);
1251 } else {
1252 contentInsets = insets.contentInsets;
1253 visibleInsets = insets.visibleInsets;
1254 touchableRegion = insets.touchableRegion;
1255 }
1256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257 try {
1258 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Jeff Brownfbf09772011-01-16 14:06:57 -08001259 contentInsets, visibleInsets, touchableRegion);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 } catch (RemoteException e) {
1261 }
1262 }
1263 }
Romain Guy8506ab42009-06-11 17:35:47 -07001264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 if (mFirst) {
1266 // handle first focus request
1267 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1268 + mView.hasFocus());
1269 if (mView != null) {
1270 if (!mView.hasFocus()) {
1271 mView.requestFocus(View.FOCUS_FORWARD);
1272 mFocusedView = mRealFocusedView = mView.findFocus();
1273 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1274 + mFocusedView);
1275 } else {
1276 mRealFocusedView = mView.findFocus();
1277 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1278 + mRealFocusedView);
1279 }
1280 }
1281 }
1282
1283 mFirst = false;
1284 mWillDrawSoon = false;
1285 mNewSurfaceNeeded = false;
1286 mViewVisibility = viewVisibility;
1287
1288 if (mAttachInfo.mHasWindowFocus) {
1289 final boolean imTarget = WindowManager.LayoutParams
1290 .mayUseInputMethod(mWindowAttributes.flags);
1291 if (imTarget != mLastWasImTarget) {
1292 mLastWasImTarget = imTarget;
1293 InputMethodManager imm = InputMethodManager.peekInstance();
1294 if (imm != null && imTarget) {
1295 imm.startGettingWindowFocus(mView);
1296 imm.onWindowFocus(mView, mView.findFocus(),
1297 mWindowAttributes.softInputMode,
1298 !mHasHadWindowFocus, mWindowAttributes.flags);
1299 }
1300 }
1301 }
Romain Guy8506ab42009-06-11 17:35:47 -07001302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1304
1305 if (!cancelDraw && !newSurface) {
1306 mFullRedrawNeeded = false;
1307 draw(fullRedrawNeeded);
1308
1309 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1310 || mReportNextDraw) {
1311 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001312 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001313 }
1314 mReportNextDraw = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001315 if (mSurfaceHolder != null && mSurface.isValid()) {
1316 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
1317 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1318 if (callbacks != null) {
1319 for (SurfaceHolder.Callback c : callbacks) {
1320 if (c instanceof SurfaceHolder.Callback2) {
1321 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
1322 mSurfaceHolder);
1323 }
1324 }
1325 }
1326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 try {
1328 sWindowSession.finishDrawing(mWindow);
1329 } catch (RemoteException e) {
1330 }
1331 }
1332 } else {
1333 // We were supposed to report when we are done drawing. Since we canceled the
1334 // draw, remember it here.
1335 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1336 mReportNextDraw = true;
1337 }
1338 if (fullRedrawNeeded) {
1339 mFullRedrawNeeded = true;
1340 }
1341 // Try again
1342 scheduleTraversals();
1343 }
1344 }
1345
1346 public void requestTransparentRegion(View child) {
1347 // the test below should not fail unless someone is messing with us
1348 checkThread();
1349 if (mView == child) {
1350 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1351 // Need to make sure we re-evaluate the window attributes next
1352 // time around, to ensure the window has the correct format.
1353 mWindowAttributesChanged = true;
Mathias Agopian1bd80ad2010-11-04 17:13:39 -07001354 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 }
1356 }
1357
1358 /**
1359 * Figures out the measure spec for the root view in a window based on it's
1360 * layout params.
1361 *
1362 * @param windowSize
1363 * The available width or height of the window
1364 *
1365 * @param rootDimension
1366 * The layout params for one dimension (width or height) of the
1367 * window.
1368 *
1369 * @return The measure spec to use to measure the root view.
1370 */
1371 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1372 int measureSpec;
1373 switch (rootDimension) {
1374
Romain Guy980a9382010-01-08 15:06:28 -08001375 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 // Window can't resize. Force root view to be windowSize.
1377 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1378 break;
1379 case ViewGroup.LayoutParams.WRAP_CONTENT:
1380 // Window can resize. Set max size for root view.
1381 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1382 break;
1383 default:
1384 // Window wants to be an exact size. Force root view to be that size.
1385 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1386 break;
1387 }
1388 return measureSpec;
1389 }
1390
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001391 int mHardwareYOffset;
1392 int mResizeAlpha;
1393 final Paint mResizePaint = new Paint();
1394
1395 public void onHardwarePreDraw(Canvas canvas) {
1396 canvas.translate(0, -mHardwareYOffset);
1397 }
1398
1399 public void onHardwarePostDraw(Canvas canvas) {
1400 if (mResizeBitmap != null) {
1401 canvas.translate(0, mHardwareYOffset);
1402 mResizePaint.setAlpha(mResizeAlpha);
1403 canvas.drawBitmap(mResizeBitmap, 0, 0, mResizePaint);
1404 }
1405 }
1406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 private void draw(boolean fullRedrawNeeded) {
1408 Surface surface = mSurface;
1409 if (surface == null || !surface.isValid()) {
1410 return;
1411 }
1412
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001413 if (!sFirstDrawComplete) {
1414 synchronized (sFirstDrawHandlers) {
1415 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07001416 final int count = sFirstDrawHandlers.size();
1417 for (int i = 0; i< count; i++) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001418 post(sFirstDrawHandlers.get(i));
1419 }
1420 }
1421 }
1422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 scrollToRectOrFocus(null, false);
1424
1425 if (mAttachInfo.mViewScrollChanged) {
1426 mAttachInfo.mViewScrollChanged = false;
1427 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1428 }
Romain Guy8506ab42009-06-11 17:35:47 -07001429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 int yoff;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001431 boolean animating = mScroller != null && mScroller.computeScrollOffset();
1432 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433 yoff = mScroller.getCurrY();
1434 } else {
1435 yoff = mScrollY;
1436 }
1437 if (mCurScrollY != yoff) {
1438 mCurScrollY = yoff;
1439 fullRedrawNeeded = true;
1440 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001441 float appScale = mAttachInfo.mApplicationScale;
1442 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001443
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001444 int resizeAlpha = 0;
1445 if (mResizeBitmap != null) {
1446 long deltaTime = SystemClock.uptimeMillis() - mResizeBitmapStartTime;
1447 if (deltaTime < mResizeBitmapDuration) {
1448 float amt = deltaTime/(float)mResizeBitmapDuration;
1449 amt = mResizeInterpolator.getInterpolation(amt);
1450 animating = true;
1451 resizeAlpha = 255 - (int)(amt*255);
1452 } else {
1453 disposeResizeBitmap();
1454 }
1455 }
1456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001457 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001458 if (mSurfaceHolder != null) {
1459 // The app owns the surface, we won't draw.
1460 dirty.setEmpty();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001461 if (animating) {
1462 if (mScroller != null) {
1463 mScroller.abortAnimation();
1464 }
1465 disposeResizeBitmap();
1466 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001467 return;
1468 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07001469
1470 if (fullRedrawNeeded) {
1471 mAttachInfo.mIgnoreDirtyState = true;
1472 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1473 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001474
Romain Guyb051e892010-09-28 19:09:36 -07001475 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guyfd507262010-10-10 15:42:49 -07001476 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy101e2ae2010-10-11 12:41:21 -07001477 mIsAnimating = false;
Romain Guyfd507262010-10-10 15:42:49 -07001478 dirty.setEmpty();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001479 mHardwareYOffset = yoff;
1480 mResizeAlpha = resizeAlpha;
1481 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001483
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001484 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 mFullRedrawNeeded = true;
1486 scheduleTraversals();
1487 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 return;
1490 }
1491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001493 Log.v(TAG, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001494 + mWindowAttributes.getTitle()
1495 + ": dirty={" + dirty.left + "," + dirty.top
1496 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001497 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1498 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 }
1500
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001501 if (!dirty.isEmpty() || mIsAnimating) {
1502 Canvas canvas;
1503 try {
1504 int left = dirty.left;
1505 int top = dirty.top;
1506 int right = dirty.right;
1507 int bottom = dirty.bottom;
1508 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001509
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001510 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1511 bottom != dirty.bottom) {
1512 mAttachInfo.mIgnoreDirtyState = true;
1513 }
1514
1515 // TODO: Do this in native
1516 canvas.setDensity(mDensity);
1517 } catch (Surface.OutOfResourcesException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001518 Log.e(TAG, "OutOfResourcesException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001519 // TODO: we should ask the window manager to do something!
1520 // for now we just do nothing
1521 return;
1522 } catch (IllegalArgumentException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001523 Log.e(TAG, "IllegalArgumentException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001524 // TODO: we should ask the window manager to do something!
1525 // for now we just do nothing
1526 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001527 }
1528
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001529 try {
1530 if (!dirty.isEmpty() || mIsAnimating) {
1531 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001532
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001533 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001534 Log.v(TAG, "Surface " + surface + " drawing to bitmap w="
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001535 + canvas.getWidth() + ", h=" + canvas.getHeight());
1536 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538
Romain Guy5429e1d2010-09-07 12:38:00 -07001539 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001540 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001542
1543 // If this bitmap's format includes an alpha channel, we
1544 // need to clear it before drawing so that the child will
1545 // properly re-composite its drawing on a transparent
1546 // background. This automatically respects the clip/dirty region
1547 // or
1548 // If we are applying an offset, we need to clear the area
1549 // where the offset doesn't appear to avoid having garbage
1550 // left in the blank areas.
1551 if (!canvas.isOpaque() || yoff != 0) {
1552 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1553 }
1554
1555 dirty.setEmpty();
1556 mIsAnimating = false;
1557 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1558 mView.mPrivateFlags |= View.DRAWN;
1559
1560 if (DEBUG_DRAW) {
1561 Context cxt = mView.getContext();
1562 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1563 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1564 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1565 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001566 try {
1567 canvas.translate(0, -yoff);
1568 if (mTranslator != null) {
1569 mTranslator.translateCanvas(canvas);
1570 }
1571 canvas.setScreenDensity(scalingRequired
1572 ? DisplayMetrics.DENSITY_DEVICE : 0);
1573 mView.draw(canvas);
1574 } finally {
1575 mAttachInfo.mIgnoreDirtyState = false;
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001576 }
1577
1578 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1579 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1580 }
1581
Romain Guy5429e1d2010-09-07 12:38:00 -07001582 if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001583 int now = (int)SystemClock.elapsedRealtime();
1584 if (sDrawTime != 0) {
1585 nativeShowFPS(canvas, now - sDrawTime);
1586 }
1587 sDrawTime = now;
1588 }
1589
Romain Guy5429e1d2010-09-07 12:38:00 -07001590 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001591 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1592 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 }
1594
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001595 } finally {
1596 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 }
1599
1600 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001601 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
Romain Guy8506ab42009-06-11 17:35:47 -07001603
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001604 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001605 mFullRedrawNeeded = true;
1606 scheduleTraversals();
1607 }
1608 }
1609
1610 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1611 final View.AttachInfo attachInfo = mAttachInfo;
1612 final Rect ci = attachInfo.mContentInsets;
1613 final Rect vi = attachInfo.mVisibleInsets;
1614 int scrollY = 0;
1615 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 if (vi.left > ci.left || vi.top > ci.top
1618 || vi.right > ci.right || vi.bottom > ci.bottom) {
1619 // We'll assume that we aren't going to change the scroll
1620 // offset, since we want to avoid that unless it is actually
1621 // going to make the focus visible... otherwise we scroll
1622 // all over the place.
1623 scrollY = mScrollY;
1624 // We can be called for two different situations: during a draw,
1625 // to update the scroll position if the focus has changed (in which
1626 // case 'rectangle' is null), or in response to a
1627 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1628 // is non-null and we just want to scroll to whatever that
1629 // rectangle is).
1630 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001631
1632 // When in touch mode, focus points to the previously focused view,
1633 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001634 // line checks whether the view is still in our hierarchy.
1635 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001636 mRealFocusedView = null;
1637 return false;
1638 }
1639
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 if (focus != mLastScrolledFocus) {
1641 // If the focus has changed, then ignore any requests to scroll
1642 // to a rectangle; first we want to make sure the entire focus
1643 // view is visible.
1644 rectangle = null;
1645 }
1646 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1647 + " rectangle=" + rectangle + " ci=" + ci
1648 + " vi=" + vi);
1649 if (focus == mLastScrolledFocus && !mScrollMayChange
1650 && rectangle == null) {
1651 // Optimization: if the focus hasn't changed since last
1652 // time, and no layout has happened, then just leave things
1653 // as they are.
1654 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1655 + mScrollY + " vi=" + vi.toShortString());
1656 } else if (focus != null) {
1657 // We need to determine if the currently focused view is
1658 // within the visible part of the window and, if not, apply
1659 // a pan so it can be seen.
1660 mLastScrolledFocus = focus;
1661 mScrollMayChange = false;
1662 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1663 // Try to find the rectangle from the focus view.
1664 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1665 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1666 + mView.getWidth() + " h=" + mView.getHeight()
1667 + " ci=" + ci.toShortString()
1668 + " vi=" + vi.toShortString());
1669 if (rectangle == null) {
1670 focus.getFocusedRect(mTempRect);
1671 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1672 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001673 if (mView instanceof ViewGroup) {
1674 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1675 focus, mTempRect);
1676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1678 "Focus in window: focusRect="
1679 + mTempRect.toShortString()
1680 + " visRect=" + mVisRect.toShortString());
1681 } else {
1682 mTempRect.set(rectangle);
1683 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1684 "Request scroll to rect: "
1685 + mTempRect.toShortString()
1686 + " visRect=" + mVisRect.toShortString());
1687 }
1688 if (mTempRect.intersect(mVisRect)) {
1689 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1690 "Focus window visible rect: "
1691 + mTempRect.toShortString());
1692 if (mTempRect.height() >
1693 (mView.getHeight()-vi.top-vi.bottom)) {
1694 // If the focus simply is not going to fit, then
1695 // best is probably just to leave things as-is.
1696 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1697 "Too tall; leaving scrollY=" + scrollY);
1698 } else if ((mTempRect.top-scrollY) < vi.top) {
1699 scrollY -= vi.top - (mTempRect.top-scrollY);
1700 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1701 "Top covered; scrollY=" + scrollY);
1702 } else if ((mTempRect.bottom-scrollY)
1703 > (mView.getHeight()-vi.bottom)) {
1704 scrollY += (mTempRect.bottom-scrollY)
1705 - (mView.getHeight()-vi.bottom);
1706 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1707 "Bottom covered; scrollY=" + scrollY);
1708 }
1709 handled = true;
1710 }
1711 }
1712 }
1713 }
Romain Guy8506ab42009-06-11 17:35:47 -07001714
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001715 if (scrollY != mScrollY) {
1716 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1717 + mScrollY + " , new=" + scrollY);
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001718 if (!immediate && mResizeBitmap == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 if (mScroller == null) {
1720 mScroller = new Scroller(mView.getContext());
1721 }
1722 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1723 } else if (mScroller != null) {
1724 mScroller.abortAnimation();
1725 }
1726 mScrollY = scrollY;
1727 }
Romain Guy8506ab42009-06-11 17:35:47 -07001728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 return handled;
1730 }
Romain Guy8506ab42009-06-11 17:35:47 -07001731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001732 public void requestChildFocus(View child, View focused) {
1733 checkThread();
1734 if (mFocusedView != focused) {
1735 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1736 scheduleTraversals();
1737 }
1738 mFocusedView = mRealFocusedView = focused;
1739 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1740 + mFocusedView);
1741 }
1742
1743 public void clearChildFocus(View child) {
1744 checkThread();
1745
1746 View oldFocus = mFocusedView;
1747
1748 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1749 mFocusedView = mRealFocusedView = null;
1750 if (mView != null && !mView.hasFocus()) {
1751 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1752 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1753 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1754 }
1755 } else if (oldFocus != null) {
1756 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1757 }
1758 }
1759
1760
1761 public void focusableViewAvailable(View v) {
1762 checkThread();
1763
1764 if (mView != null && !mView.hasFocus()) {
1765 v.requestFocus();
1766 } else {
1767 // the one case where will transfer focus away from the current one
1768 // is if the current view is a view group that prefers to give focus
1769 // to its children first AND the view is a descendant of it.
1770 mFocusedView = mView.findFocus();
1771 boolean descendantsHaveDibsOnFocus =
1772 (mFocusedView instanceof ViewGroup) &&
1773 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1774 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1775 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1776 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1777 v.requestFocus();
1778 }
1779 }
1780 }
1781
1782 public void recomputeViewAttributes(View child) {
1783 checkThread();
1784 if (mView == child) {
1785 mAttachInfo.mRecomputeGlobalAttributes = true;
1786 if (!mWillDrawSoon) {
1787 scheduleTraversals();
1788 }
1789 }
1790 }
1791
1792 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 if (mView != null) {
1794 mView.dispatchDetachedFromWindow();
1795 }
1796
1797 mView = null;
1798 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001799 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800
Romain Guy29d89972010-09-22 16:10:57 -07001801 destroyHardwareRenderer();
Romain Guy4caa4ed2010-08-25 14:46:24 -07001802
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001803 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001805 if (mInputChannel != null) {
1806 if (mInputQueueCallback != null) {
1807 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
1808 mInputQueueCallback = null;
1809 } else {
1810 InputQueue.unregisterInputChannel(mInputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001811 }
1812 }
1813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 try {
1815 sWindowSession.remove(mWindow);
1816 } catch (RemoteException e) {
1817 }
Jeff Brown349703e2010-06-22 01:27:15 -07001818
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001819 // Dispose the input channel after removing the window so the Window Manager
1820 // doesn't interpret the input channel being closed as an abnormal termination.
1821 if (mInputChannel != null) {
1822 mInputChannel.dispose();
1823 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
Romain Guy8506ab42009-06-11 17:35:47 -07001826
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001827 void updateConfiguration(Configuration config, boolean force) {
1828 if (DEBUG_CONFIGURATION) Log.v(TAG,
1829 "Applying new config to window "
1830 + mWindowAttributes.getTitle()
1831 + ": " + config);
1832 synchronized (sConfigCallbacks) {
1833 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1834 sConfigCallbacks.get(i).onConfigurationChanged(config);
1835 }
1836 }
1837 if (mView != null) {
1838 // At this point the resources have been updated to
1839 // have the most recent config, whatever that is. Use
1840 // the on in them which may be newer.
1841 if (mView != null) {
1842 config = mView.getResources().getConfiguration();
1843 }
1844 if (force || mLastConfiguration.diff(config) != 0) {
1845 mLastConfiguration.setTo(config);
1846 mView.dispatchConfigurationChanged(config);
1847 }
1848 }
1849 }
1850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 /**
1852 * Return true if child is an ancestor of parent, (or equal to the parent).
1853 */
1854 private static boolean isViewDescendantOf(View child, View parent) {
1855 if (child == parent) {
1856 return true;
1857 }
1858
1859 final ViewParent theParent = child.getParent();
1860 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1861 }
1862
Romain Guycdb86672010-03-18 18:54:50 -07001863 private static void forceLayout(View view) {
1864 view.forceLayout();
1865 if (view instanceof ViewGroup) {
1866 ViewGroup group = (ViewGroup) view;
1867 final int count = group.getChildCount();
1868 for (int i = 0; i < count; i++) {
1869 forceLayout(group.getChildAt(i));
1870 }
1871 }
1872 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001873
1874 public final static int DO_TRAVERSAL = 1000;
1875 public final static int DIE = 1001;
1876 public final static int RESIZED = 1002;
1877 public final static int RESIZED_REPORT = 1003;
1878 public final static int WINDOW_FOCUS_CHANGED = 1004;
1879 public final static int DISPATCH_KEY = 1005;
1880 public final static int DISPATCH_POINTER = 1006;
1881 public final static int DISPATCH_TRACKBALL = 1007;
1882 public final static int DISPATCH_APP_VISIBILITY = 1008;
1883 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1884 public final static int FINISHED_EVENT = 1010;
1885 public final static int DISPATCH_KEY_FROM_IME = 1011;
1886 public final static int FINISH_INPUT_CONNECTION = 1012;
1887 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001888 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Christopher Tatea53146c2010-09-07 11:57:52 -07001889 public final static int DISPATCH_DRAG_EVENT = 1015;
Chris Tate91e9bb32010-10-12 12:58:43 -07001890 public final static int DISPATCH_DRAG_LOCATION_EVENT = 1016;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891
1892 @Override
1893 public void handleMessage(Message msg) {
1894 switch (msg.what) {
1895 case View.AttachInfo.INVALIDATE_MSG:
1896 ((View) msg.obj).invalidate();
1897 break;
1898 case View.AttachInfo.INVALIDATE_RECT_MSG:
1899 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1900 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1901 info.release();
1902 break;
1903 case DO_TRAVERSAL:
1904 if (mProfile) {
1905 Debug.startMethodTracing("ViewRoot");
1906 }
1907
1908 performTraversals();
1909
1910 if (mProfile) {
1911 Debug.stopMethodTracing();
1912 mProfile = false;
1913 }
1914 break;
1915 case FINISHED_EVENT:
1916 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1917 break;
1918 case DISPATCH_KEY:
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001919 deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 break;
Jeff Brown3915bb82010-11-05 15:02:16 -07001921 case DISPATCH_POINTER:
1922 deliverPointerEvent((MotionEvent) msg.obj, msg.arg1 != 0);
1923 break;
1924 case DISPATCH_TRACKBALL:
1925 deliverTrackballEvent((MotionEvent) msg.obj, msg.arg1 != 0);
1926 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001927 case DISPATCH_APP_VISIBILITY:
1928 handleAppVisibility(msg.arg1 != 0);
1929 break;
1930 case DISPATCH_GET_NEW_SURFACE:
1931 handleGetNewSurface();
1932 break;
1933 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001934 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001937 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001938 && mPendingVisibleInsets.equals(ri.visibleInsets)
1939 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 break;
1941 }
1942 // fall through...
1943 case RESIZED_REPORT:
1944 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001945 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1946 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001947 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 mWinFrame.left = 0;
1950 mWinFrame.right = msg.arg1;
1951 mWinFrame.top = 0;
1952 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001953 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1954 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 if (msg.what == RESIZED_REPORT) {
1956 mReportNextDraw = true;
1957 }
Romain Guycdb86672010-03-18 18:54:50 -07001958
1959 if (mView != null) {
1960 forceLayout(mView);
1961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 requestLayout();
1963 }
1964 break;
1965 case WINDOW_FOCUS_CHANGED: {
1966 if (mAdded) {
1967 boolean hasWindowFocus = msg.arg1 != 0;
1968 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1969 if (hasWindowFocus) {
1970 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001971 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972
Romain Guyc361da82010-10-25 15:29:10 -07001973 if (mAttachInfo.mHardwareRenderer != null &&
1974 mSurface != null && mSurface.isValid()) {
Romain Guyb051e892010-09-28 19:09:36 -07001975 mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
1976 mAttachInfo, mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
1978 }
Romain Guy8506ab42009-06-11 17:35:47 -07001979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 mLastWasImTarget = WindowManager.LayoutParams
1981 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001982
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 InputMethodManager imm = InputMethodManager.peekInstance();
1984 if (mView != null) {
1985 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1986 imm.startGettingWindowFocus(mView);
1987 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001988 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 mView.dispatchWindowFocusChanged(hasWindowFocus);
1990 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 // Note: must be done after the focus change callbacks,
1993 // so all of the view state is set up correctly.
1994 if (hasWindowFocus) {
1995 if (imm != null && mLastWasImTarget) {
1996 imm.onWindowFocus(mView, mView.findFocus(),
1997 mWindowAttributes.softInputMode,
1998 !mHasHadWindowFocus, mWindowAttributes.flags);
1999 }
2000 // Clear the forward bit. We can just do this directly, since
2001 // the window manager doesn't care about it.
2002 mWindowAttributes.softInputMode &=
2003 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
2004 ((WindowManager.LayoutParams)mView.getLayoutParams())
2005 .softInputMode &=
2006 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
2007 mHasHadWindowFocus = true;
2008 }
svetoslavganov75986cf2009-05-14 22:28:01 -07002009
2010 if (hasWindowFocus && mView != null) {
2011 sendAccessibilityEvents();
2012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 }
2014 } break;
2015 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07002016 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07002018 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07002020 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07002022 KeyEvent event = (KeyEvent)msg.obj;
2023 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
2024 // The IME is trying to say this event is from the
2025 // system! Bad bad bad!
Romain Guy812ccbe2010-06-01 14:07:24 -07002026 event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project10592532009-03-18 17:39:46 -07002027 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002028 deliverKeyEventPostIme((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07002029 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 case FINISH_INPUT_CONNECTION: {
2031 InputMethodManager imm = InputMethodManager.peekInstance();
2032 if (imm != null) {
2033 imm.reportFinishInputConnection((InputConnection)msg.obj);
2034 }
2035 } break;
2036 case CHECK_FOCUS: {
2037 InputMethodManager imm = InputMethodManager.peekInstance();
2038 if (imm != null) {
2039 imm.checkFocus();
2040 }
2041 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07002042 case CLOSE_SYSTEM_DIALOGS: {
2043 if (mView != null) {
2044 mView.onCloseSystemDialogs((String)msg.obj);
2045 }
2046 } break;
Chris Tate91e9bb32010-10-12 12:58:43 -07002047 case DISPATCH_DRAG_EVENT:
2048 case DISPATCH_DRAG_LOCATION_EVENT: {
Christopher Tatea53146c2010-09-07 11:57:52 -07002049 handleDragEvent((DragEvent)msg.obj);
2050 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 }
2052 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002053
Jeff Brown3915bb82010-11-05 15:02:16 -07002054 private void startInputEvent(InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002055 if (mFinishedCallback != null) {
2056 Slog.w(TAG, "Received a new input event from the input queue but there is "
2057 + "already an unfinished input event in progress.");
2058 }
2059
2060 mFinishedCallback = finishedCallback;
2061 }
2062
Jeff Brown3915bb82010-11-05 15:02:16 -07002063 private void finishInputEvent(boolean handled) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002064 if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished");
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002065
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002066 if (mFinishedCallback != null) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002067 mFinishedCallback.finished(handled);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002068 mFinishedCallback = null;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002069 } else {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002070 Slog.w(TAG, "Attempted to tell the input queue that the current input event "
2071 + "is finished but there is no input event actually in progress.");
Jeff Brown46b9ac02010-04-22 18:58:52 -07002072 }
2073 }
2074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 /**
2076 * Something in the current window tells us we need to change the touch mode. For
2077 * example, we are not in touch mode, and the user touches the screen.
2078 *
2079 * If the touch mode has changed, tell the window manager, and handle it locally.
2080 *
2081 * @param inTouchMode Whether we want to be in touch mode.
2082 * @return True if the touch mode changed and focus changed was changed as a result
2083 */
2084 boolean ensureTouchMode(boolean inTouchMode) {
2085 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
2086 + "touch mode is " + mAttachInfo.mInTouchMode);
2087 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2088
2089 // tell the window manager
2090 try {
2091 sWindowSession.setInTouchMode(inTouchMode);
2092 } catch (RemoteException e) {
2093 throw new RuntimeException(e);
2094 }
2095
2096 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07002097 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 }
2099
2100 /**
2101 * Ensure that the touch mode for this window is set, and if it is changing,
2102 * take the appropriate action.
2103 * @param inTouchMode Whether we want to be in touch mode.
2104 * @return True if the touch mode changed and focus changed was changed as a result
2105 */
Romain Guy2d4cff62010-04-09 15:39:00 -07002106 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
2108 + "touch mode is " + mAttachInfo.mInTouchMode);
2109
2110 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2111
2112 mAttachInfo.mInTouchMode = inTouchMode;
2113 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
2114
Romain Guy2d4cff62010-04-09 15:39:00 -07002115 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 }
2117
2118 private boolean enterTouchMode() {
2119 if (mView != null) {
2120 if (mView.hasFocus()) {
2121 // note: not relying on mFocusedView here because this could
2122 // be when the window is first being added, and mFocused isn't
2123 // set yet.
2124 final View focused = mView.findFocus();
2125 if (focused != null && !focused.isFocusableInTouchMode()) {
2126
2127 final ViewGroup ancestorToTakeFocus =
2128 findAncestorToTakeFocusInTouchMode(focused);
2129 if (ancestorToTakeFocus != null) {
2130 // there is an ancestor that wants focus after its descendants that
2131 // is focusable in touch mode.. give it focus
2132 return ancestorToTakeFocus.requestFocus();
2133 } else {
2134 // nothing appropriate to have focus in touch mode, clear it out
2135 mView.unFocus();
2136 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
2137 mFocusedView = null;
2138 return true;
2139 }
2140 }
2141 }
2142 }
2143 return false;
2144 }
2145
2146
2147 /**
2148 * Find an ancestor of focused that wants focus after its descendants and is
2149 * focusable in touch mode.
2150 * @param focused The currently focused view.
2151 * @return An appropriate view, or null if no such view exists.
2152 */
2153 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
2154 ViewParent parent = focused.getParent();
2155 while (parent instanceof ViewGroup) {
2156 final ViewGroup vgParent = (ViewGroup) parent;
2157 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
2158 && vgParent.isFocusableInTouchMode()) {
2159 return vgParent;
2160 }
2161 if (vgParent.isRootNamespace()) {
2162 return null;
2163 } else {
2164 parent = vgParent.getParent();
2165 }
2166 }
2167 return null;
2168 }
2169
2170 private boolean leaveTouchMode() {
2171 if (mView != null) {
2172 if (mView.hasFocus()) {
2173 // i learned the hard way to not trust mFocusedView :)
2174 mFocusedView = mView.findFocus();
2175 if (!(mFocusedView instanceof ViewGroup)) {
2176 // some view has focus, let it keep it
2177 return false;
2178 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2179 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2180 // some view group has focus, and doesn't prefer its children
2181 // over itself for focus, so let them keep it.
2182 return false;
2183 }
2184 }
2185
2186 // find the best view to give focus to in this brave new non-touch-mode
2187 // world
2188 final View focused = focusSearch(null, View.FOCUS_DOWN);
2189 if (focused != null) {
2190 return focused.requestFocus(View.FOCUS_DOWN);
2191 }
2192 }
2193 return false;
2194 }
2195
Jeff Brown3915bb82010-11-05 15:02:16 -07002196 private void deliverPointerEvent(MotionEvent event, boolean sendDone) {
2197 // If there is no view, then the event will not be handled.
2198 if (mView == null || !mAdded) {
2199 finishPointerEvent(event, sendDone, false);
2200 return;
2201 }
2202
2203 // Translate the pointer event for compatibility, if needed.
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002204 if (mTranslator != null) {
2205 mTranslator.translateEventInScreenToAppWindow(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 }
2207
Jeff Brown3915bb82010-11-05 15:02:16 -07002208 // Enter touch mode on the down.
2209 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
2210 if (isDown) {
2211 ensureTouchMode(true);
2212 }
2213 if(Config.LOGV) {
2214 captureMotionLog("captureDispatchPointer", event);
2215 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002216
Jeff Brown3915bb82010-11-05 15:02:16 -07002217 // Offset the scroll position.
2218 if (mCurScrollY != 0) {
2219 event.offsetLocation(0, mCurScrollY);
2220 }
2221 if (MEASURE_LATENCY) {
2222 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
2223 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002224
Jeff Brown3915bb82010-11-05 15:02:16 -07002225 // Remember the touch position for possible drag-initiation.
2226 mLastTouchPoint.x = event.getRawX();
2227 mLastTouchPoint.y = event.getRawY();
2228
2229 // Dispatch touch to view hierarchy.
2230 boolean handled = mView.dispatchTouchEvent(event);
2231 if (MEASURE_LATENCY) {
2232 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
2233 }
2234 if (handled) {
2235 finishPointerEvent(event, sendDone, true);
2236 return;
2237 }
2238
2239 // Apply edge slop and try again, if appropriate.
2240 final int edgeFlags = event.getEdgeFlags();
2241 if (edgeFlags != 0 && mView instanceof ViewGroup) {
2242 final int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
2243 int direction = View.FOCUS_UP;
2244 int x = (int)event.getX();
2245 int y = (int)event.getY();
2246 final int[] deltas = new int[2];
2247
2248 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
2249 direction = View.FOCUS_DOWN;
2250 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2251 deltas[0] = edgeSlop;
2252 x += edgeSlop;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002253 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002254 deltas[0] = -edgeSlop;
2255 x -= edgeSlop;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002256 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002257 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
2258 direction = View.FOCUS_UP;
2259 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2260 deltas[0] = edgeSlop;
2261 x += edgeSlop;
2262 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2263 deltas[0] = -edgeSlop;
2264 x -= edgeSlop;
2265 }
2266 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2267 direction = View.FOCUS_RIGHT;
2268 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2269 direction = View.FOCUS_LEFT;
2270 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002271
Jeff Brown3915bb82010-11-05 15:02:16 -07002272 View nearest = FocusFinder.getInstance().findNearestTouchable(
2273 ((ViewGroup) mView), x, y, direction, deltas);
2274 if (nearest != null) {
2275 event.offsetLocation(deltas[0], deltas[1]);
2276 event.setEdgeFlags(0);
2277 if (mView.dispatchTouchEvent(event)) {
2278 finishPointerEvent(event, sendDone, true);
2279 return;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002280 }
2281 }
2282 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002283
2284 // Pointer event was unhandled.
2285 finishPointerEvent(event, sendDone, false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002286 }
2287
Jeff Brown3915bb82010-11-05 15:02:16 -07002288 private void finishPointerEvent(MotionEvent event, boolean sendDone, boolean handled) {
2289 event.recycle();
2290 if (sendDone) {
2291 finishInputEvent(handled);
2292 }
2293 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
2294 }
2295
2296 private void deliverTrackballEvent(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2298
Jeff Brown3915bb82010-11-05 15:02:16 -07002299 // If there is no view, then the event will not be handled.
2300 if (mView == null || !mAdded) {
2301 finishTrackballEvent(event, sendDone, false);
2302 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 }
2304
Jeff Brown3915bb82010-11-05 15:02:16 -07002305 // Deliver the trackball event to the view.
2306 if (mView.dispatchTrackballEvent(event)) {
2307 // If we reach this, we delivered a trackball event to mView and
2308 // mView consumed it. Because we will not translate the trackball
2309 // event into a key event, touch mode will not exit, so we exit
2310 // touch mode here.
2311 ensureTouchMode(false);
2312
2313 finishTrackballEvent(event, sendDone, true);
2314 mLastTrackballTime = Integer.MIN_VALUE;
2315 return;
2316 }
2317
2318 // Translate the trackball event into DPAD keys and try to deliver those.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 final TrackballAxis x = mTrackballAxisX;
2320 final TrackballAxis y = mTrackballAxisY;
2321
2322 long curTime = SystemClock.uptimeMillis();
Jeff Brown3915bb82010-11-05 15:02:16 -07002323 if ((mLastTrackballTime + MAX_TRACKBALL_DELAY) < curTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 // It has been too long since the last movement,
2325 // so restart at the beginning.
2326 x.reset(0);
2327 y.reset(0);
2328 mLastTrackballTime = curTime;
2329 }
2330
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002331 final int action = event.getAction();
Jeff Brown49ed71d2010-12-06 17:13:33 -08002332 final int metaState = event.getMetaState();
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002333 switch (action) {
2334 case MotionEvent.ACTION_DOWN:
2335 x.reset(2);
2336 y.reset(2);
2337 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002338 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
2339 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2340 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002341 break;
2342 case MotionEvent.ACTION_UP:
2343 x.reset(2);
2344 y.reset(2);
2345 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002346 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
2347 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2348 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002349 break;
2350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002352 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2353 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2354 + " move=" + event.getX()
2355 + " / Y=" + y.position + " step="
2356 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2357 + " move=" + event.getY());
2358 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2359 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002360
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002361 // Generate DPAD events based on the trackball movement.
2362 // We pick the axis that has moved the most as the direction of
2363 // the DPAD. When we generate DPAD events for one axis, then the
2364 // other axis is reset -- we don't want to perform DPAD jumps due
2365 // to slight movements in the trackball when making major movements
2366 // along the other axis.
2367 int keycode = 0;
2368 int movement = 0;
2369 float accel = 1;
2370 if (xOff > yOff) {
2371 movement = x.generate((2/event.getXPrecision()));
2372 if (movement != 0) {
2373 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2374 : KeyEvent.KEYCODE_DPAD_LEFT;
2375 accel = x.acceleration;
2376 y.reset(2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002378 } else if (yOff > 0) {
2379 movement = y.generate((2/event.getYPrecision()));
2380 if (movement != 0) {
2381 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2382 : KeyEvent.KEYCODE_DPAD_UP;
2383 accel = y.acceleration;
2384 x.reset(2);
2385 }
2386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002388 if (keycode != 0) {
2389 if (movement < 0) movement = -movement;
2390 int accelMovement = (int)(movement * accel);
2391 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2392 + " accelMovement=" + accelMovement
2393 + " accel=" + accel);
2394 if (accelMovement > movement) {
2395 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2396 + keycode);
2397 movement--;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002398 int repeatCount = accelMovement - movement;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002399 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002400 KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState,
2401 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2402 InputDevice.SOURCE_KEYBOARD), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002404 while (movement > 0) {
2405 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2406 + keycode);
2407 movement--;
2408 curTime = SystemClock.uptimeMillis();
2409 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002410 KeyEvent.ACTION_DOWN, keycode, 0, metaState,
2411 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2412 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002413 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002414 KeyEvent.ACTION_UP, keycode, 0, metaState,
2415 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2416 InputDevice.SOURCE_KEYBOARD), false);
2417 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002418 mLastTrackballTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002420
2421 // Unfortunately we can't tell whether the application consumed the keys, so
2422 // we always consider the trackball event handled.
2423 finishTrackballEvent(event, sendDone, true);
2424 }
2425
2426 private void finishTrackballEvent(MotionEvent event, boolean sendDone, boolean handled) {
2427 event.recycle();
2428 if (sendDone) {
2429 finishInputEvent(handled);
2430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 }
2432
2433 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002434 * Returns true if the key is used for keyboard navigation.
2435 * @param keyEvent The key event.
2436 * @return True if the key is used for keyboard navigation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002437 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08002438 private static boolean isNavigationKey(KeyEvent keyEvent) {
2439 switch (keyEvent.getKeyCode()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002440 case KeyEvent.KEYCODE_DPAD_LEFT:
2441 case KeyEvent.KEYCODE_DPAD_RIGHT:
2442 case KeyEvent.KEYCODE_DPAD_UP:
2443 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002444 case KeyEvent.KEYCODE_DPAD_CENTER:
2445 case KeyEvent.KEYCODE_PAGE_UP:
2446 case KeyEvent.KEYCODE_PAGE_DOWN:
2447 case KeyEvent.KEYCODE_MOVE_HOME:
2448 case KeyEvent.KEYCODE_MOVE_END:
2449 case KeyEvent.KEYCODE_TAB:
2450 case KeyEvent.KEYCODE_SPACE:
2451 case KeyEvent.KEYCODE_ENTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002452 return true;
2453 }
2454 return false;
2455 }
2456
2457 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002458 * Returns true if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002459 * @param keyEvent The key event.
Jeff Brown4e6319b2010-12-13 10:36:51 -08002460 * @return True if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08002462 private static boolean isTypingKey(KeyEvent keyEvent) {
2463 return keyEvent.getUnicodeChar() > 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 }
2465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002467 * See if the key event means we should leave touch mode (and leave touch mode if so).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002468 * @param event The key event.
2469 * @return Whether this key event should be consumed (meaning the act of
2470 * leaving touch mode alone is considered the event).
2471 */
2472 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08002473 // Only relevant in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002474 if (!mAttachInfo.mInTouchMode) {
2475 return false;
2476 }
2477
Jeff Brown4e6319b2010-12-13 10:36:51 -08002478 // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP.
2479 final int action = event.getAction();
2480 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 return false;
2482 }
2483
Jeff Brown4e6319b2010-12-13 10:36:51 -08002484 // Don't leave touch mode if the IME told us not to.
2485 if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2486 return false;
2487 }
2488
2489 // If the key can be used for keyboard navigation then leave touch mode
2490 // and select a focused view if needed (in ensureTouchMode).
2491 // When a new focused view is selected, we consume the navigation key because
2492 // navigation doesn't make much sense unless a view already has focus so
2493 // the key's purpose is to set focus.
2494 if (isNavigationKey(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 return ensureTouchMode(false);
2496 }
Jeff Brown4e6319b2010-12-13 10:36:51 -08002497
2498 // If the key can be used for typing then leave touch mode
2499 // and select a focused view if needed (in ensureTouchMode).
2500 // Always allow the view to process the typing key.
2501 if (isTypingKey(event)) {
2502 ensureTouchMode(false);
2503 return false;
2504 }
2505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002506 return false;
2507 }
2508
2509 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002510 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 */
2512 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002513 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002514 if (ev == null ||
2515 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2516 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 }
Romain Guy8506ab42009-06-11 17:35:47 -07002518
2519 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002520 sb.append(ev.getDownTime()).append(',');
2521 sb.append(ev.getEventTime()).append(',');
2522 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002523 sb.append(ev.getX()).append(',');
2524 sb.append(ev.getY()).append(',');
2525 sb.append(ev.getPressure()).append(',');
2526 sb.append(ev.getSize()).append(',');
2527 sb.append(ev.getMetaState()).append(',');
2528 sb.append(ev.getXPrecision()).append(',');
2529 sb.append(ev.getYPrecision()).append(',');
2530 sb.append(ev.getDeviceId()).append(',');
2531 sb.append(ev.getEdgeFlags());
2532 Log.d(TAG, sb.toString());
2533 }
2534 /**
2535 * log motion events
2536 */
2537 private static void captureKeyLog(String subTag, KeyEvent ev) {
2538 //check dynamic switch
2539 if (ev == null ||
2540 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2541 return;
2542 }
2543 StringBuilder sb = new StringBuilder(subTag + ": ");
2544 sb.append(ev.getDownTime()).append(',');
2545 sb.append(ev.getEventTime()).append(',');
2546 sb.append(ev.getAction()).append(',');
2547 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002548 sb.append(ev.getRepeatCount()).append(',');
2549 sb.append(ev.getMetaState()).append(',');
2550 sb.append(ev.getDeviceId()).append(',');
2551 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002552 Log.d(TAG, sb.toString());
2553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002554
2555 int enqueuePendingEvent(Object event, boolean sendDone) {
2556 int seq = mPendingEventSeq+1;
2557 if (seq < 0) seq = 0;
2558 mPendingEventSeq = seq;
2559 mPendingEvents.put(seq, event);
2560 return sendDone ? seq : -seq;
2561 }
2562
2563 Object retrievePendingEvent(int seq) {
2564 if (seq < 0) seq = -seq;
2565 Object event = mPendingEvents.get(seq);
2566 if (event != null) {
2567 mPendingEvents.remove(seq);
2568 }
2569 return event;
2570 }
Romain Guy8506ab42009-06-11 17:35:47 -07002571
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002572 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002573 // If there is no view, then the event will not be handled.
2574 if (mView == null || !mAdded) {
2575 finishKeyEvent(event, sendDone, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 return;
2577 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002578
2579 if (LOCAL_LOGV) Log.v(TAG, "Dispatching key " + event + " to " + mView);
2580
2581 // Perform predispatching before the IME.
2582 if (mView.dispatchKeyEventPreIme(event)) {
2583 finishKeyEvent(event, sendDone, true);
2584 return;
2585 }
2586
2587 // Dispatch to the IME before propagating down the view hierarchy.
2588 // The IME will eventually call back into handleFinishedEvent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 if (mLastWasImTarget) {
2590 InputMethodManager imm = InputMethodManager.peekInstance();
Jeff Brown3915bb82010-11-05 15:02:16 -07002591 if (imm != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 int seq = enqueuePendingEvent(event, sendDone);
2593 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2594 + seq + " event=" + event);
Jeff Brown3915bb82010-11-05 15:02:16 -07002595 imm.dispatchKeyEvent(mView.getContext(), seq, event, mInputMethodCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 return;
2597 }
2598 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002599
2600 // Not dispatching to IME, continue with post IME actions.
2601 deliverKeyEventPostIme(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 }
2603
Jeff Brown3915bb82010-11-05 15:02:16 -07002604 private void handleFinishedEvent(int seq, boolean handled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2606 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2607 + " handled=" + handled + " event=" + event);
2608 if (event != null) {
2609 final boolean sendDone = seq >= 0;
Jeff Brown3915bb82010-11-05 15:02:16 -07002610 if (handled) {
2611 finishKeyEvent(event, sendDone, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002612 } else {
Jeff Brown3915bb82010-11-05 15:02:16 -07002613 deliverKeyEventPostIme(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614 }
2615 }
2616 }
Romain Guy8506ab42009-06-11 17:35:47 -07002617
Jeff Brown3915bb82010-11-05 15:02:16 -07002618 private void deliverKeyEventPostIme(KeyEvent event, boolean sendDone) {
2619 // If the view went away, then the event will not be handled.
2620 if (mView == null || !mAdded) {
2621 finishKeyEvent(event, sendDone, false);
2622 return;
2623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624
Jeff Brown3915bb82010-11-05 15:02:16 -07002625 // If the key's purpose is to exit touch mode then we consume it and consider it handled.
2626 if (checkForLeavingTouchModeAndConsume(event)) {
2627 finishKeyEvent(event, sendDone, true);
2628 return;
2629 }
Romain Guy8506ab42009-06-11 17:35:47 -07002630
Jeff Brown3915bb82010-11-05 15:02:16 -07002631 if (Config.LOGV) {
2632 captureKeyLog("captureDispatchKeyEvent", event);
2633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002634
Jeff Brown90655042010-12-02 13:50:46 -08002635 // Make sure the fallback event policy sees all keys that will be delivered to the
2636 // view hierarchy.
2637 mFallbackEventHandler.preDispatchKeyEvent(event);
2638
Jeff Brown3915bb82010-11-05 15:02:16 -07002639 // Deliver the key to the view hierarchy.
2640 if (mView.dispatchKeyEvent(event)) {
2641 finishKeyEvent(event, sendDone, true);
2642 return;
2643 }
Joe Onorato86f67862010-11-05 18:57:34 -07002644
Jeff Brownc1df9072010-12-21 16:38:50 -08002645 // If the Control modifier is held, try to interpret the key as a shortcut.
2646 if (event.getAction() == KeyEvent.ACTION_UP
2647 && event.isCtrlPressed()
2648 && !KeyEvent.isModifierKey(event.getKeyCode())) {
2649 if (mView.dispatchKeyShortcutEvent(event)) {
2650 finishKeyEvent(event, sendDone, true);
2651 return;
2652 }
2653 }
2654
Jeff Brown3915bb82010-11-05 15:02:16 -07002655 // Apply the fallback event policy.
2656 if (mFallbackEventHandler.dispatchKeyEvent(event)) {
2657 finishKeyEvent(event, sendDone, true);
2658 return;
2659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002660
Jeff Brown3915bb82010-11-05 15:02:16 -07002661 // Handle automatic focus changes.
2662 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2663 int direction = 0;
2664 switch (event.getKeyCode()) {
2665 case KeyEvent.KEYCODE_DPAD_LEFT:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002666 if (event.hasNoModifiers()) {
2667 direction = View.FOCUS_LEFT;
2668 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002669 break;
2670 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002671 if (event.hasNoModifiers()) {
2672 direction = View.FOCUS_RIGHT;
2673 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002674 break;
2675 case KeyEvent.KEYCODE_DPAD_UP:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002676 if (event.hasNoModifiers()) {
2677 direction = View.FOCUS_UP;
2678 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002679 break;
2680 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002681 if (event.hasNoModifiers()) {
2682 direction = View.FOCUS_DOWN;
2683 }
2684 break;
2685 case KeyEvent.KEYCODE_TAB:
2686 if (event.hasNoModifiers()) {
2687 direction = View.FOCUS_FORWARD;
2688 } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
2689 direction = View.FOCUS_BACKWARD;
2690 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002691 break;
2692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693
Jeff Brown3915bb82010-11-05 15:02:16 -07002694 if (direction != 0) {
2695 View focused = mView != null ? mView.findFocus() : null;
2696 if (focused != null) {
2697 View v = focused.focusSearch(direction);
2698 if (v != null && v != focused) {
2699 // do the math the get the interesting rect
2700 // of previous focused into the coord system of
2701 // newly focused view
2702 focused.getFocusedRect(mTempRect);
2703 if (mView instanceof ViewGroup) {
2704 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2705 focused, mTempRect);
2706 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2707 v, mTempRect);
2708 }
2709 if (v.requestFocus(direction, mTempRect)) {
2710 playSoundEffect(
2711 SoundEffectConstants.getContantForFocusDirection(direction));
2712 finishKeyEvent(event, sendDone, true);
2713 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714 }
2715 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002716
2717 // Give the focused view a last chance to handle the dpad key.
2718 if (mView.dispatchUnhandledMove(focused, direction)) {
2719 finishKeyEvent(event, sendDone, true);
2720 return;
2721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002722 }
2723 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002725
Jeff Brown3915bb82010-11-05 15:02:16 -07002726 // Key was unhandled.
2727 finishKeyEvent(event, sendDone, false);
2728 }
2729
2730 private void finishKeyEvent(KeyEvent event, boolean sendDone, boolean handled) {
2731 if (sendDone) {
2732 finishInputEvent(handled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002733 }
2734 }
2735
Christopher Tatea53146c2010-09-07 11:57:52 -07002736 /* drag/drop */
Christopher Tate407b4e92010-11-30 17:14:08 -08002737 void setLocalDragState(Object obj) {
2738 mLocalDragState = obj;
2739 }
2740
Christopher Tatea53146c2010-09-07 11:57:52 -07002741 private void handleDragEvent(DragEvent event) {
2742 // From the root, only drag start/end/location are dispatched. entered/exited
2743 // are determined and dispatched by the viewgroup hierarchy, who then report
2744 // that back here for ultimate reporting back to the framework.
2745 if (mView != null && mAdded) {
2746 final int what = event.mAction;
2747
2748 if (what == DragEvent.ACTION_DRAG_EXITED) {
2749 // A direct EXITED event means that the window manager knows we've just crossed
2750 // a window boundary, so the current drag target within this one must have
2751 // just been exited. Send it the usual notifications and then we're done
2752 // for now.
Chris Tate9d1ab882010-11-02 15:55:39 -07002753 mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002754 } else {
2755 // Cache the drag description when the operation starts, then fill it in
2756 // on subsequent calls as a convenience
2757 if (what == DragEvent.ACTION_DRAG_STARTED) {
Chris Tate9d1ab882010-11-02 15:55:39 -07002758 mCurrentDragView = null; // Start the current-recipient tracking
Christopher Tatea53146c2010-09-07 11:57:52 -07002759 mDragDescription = event.mClipDescription;
2760 } else {
2761 event.mClipDescription = mDragDescription;
2762 }
2763
2764 // For events with a [screen] location, translate into window coordinates
2765 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
2766 mDragPoint.set(event.mX, event.mY);
2767 if (mTranslator != null) {
2768 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
2769 }
2770
2771 if (mCurScrollY != 0) {
2772 mDragPoint.offset(0, mCurScrollY);
2773 }
2774
2775 event.mX = mDragPoint.x;
2776 event.mY = mDragPoint.y;
2777 }
2778
2779 // Remember who the current drag target is pre-dispatch
2780 final View prevDragView = mCurrentDragView;
2781
2782 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07002783 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002784
2785 // If we changed apparent drag target, tell the OS about it
2786 if (prevDragView != mCurrentDragView) {
2787 try {
2788 if (prevDragView != null) {
2789 sWindowSession.dragRecipientExited(mWindow);
2790 }
2791 if (mCurrentDragView != null) {
2792 sWindowSession.dragRecipientEntered(mWindow);
2793 }
2794 } catch (RemoteException e) {
2795 Slog.e(TAG, "Unable to note drag target change");
2796 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002797 }
Chris Tated4533f12010-10-19 15:15:08 -07002798
Christopher Tate407b4e92010-11-30 17:14:08 -08002799 // Report the drop result when we're done
Chris Tated4533f12010-10-19 15:15:08 -07002800 if (what == DragEvent.ACTION_DROP) {
2801 try {
2802 Log.i(TAG, "Reporting drop result: " + result);
2803 sWindowSession.reportDropResult(mWindow, result);
2804 } catch (RemoteException e) {
2805 Log.e(TAG, "Unable to report drop result");
2806 }
2807 }
Christopher Tate407b4e92010-11-30 17:14:08 -08002808
2809 // When the drag operation ends, release any local state object
2810 // that may have been in use
2811 if (what == DragEvent.ACTION_DRAG_ENDED) {
2812 setLocalDragState(null);
2813 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002814 }
2815 }
2816 event.recycle();
2817 }
2818
Christopher Tate2c095f32010-10-04 14:13:40 -07002819 public void getLastTouchPoint(Point outLocation) {
2820 outLocation.x = (int) mLastTouchPoint.x;
2821 outLocation.y = (int) mLastTouchPoint.y;
2822 }
2823
Chris Tate9d1ab882010-11-02 15:55:39 -07002824 public void setDragFocus(View newDragTarget) {
Christopher Tatea53146c2010-09-07 11:57:52 -07002825 if (mCurrentDragView != newDragTarget) {
Chris Tate048691c2010-10-12 17:39:18 -07002826 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07002827 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002828 }
2829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002830 private AudioManager getAudioManager() {
2831 if (mView == null) {
2832 throw new IllegalStateException("getAudioManager called when there is no mView");
2833 }
2834 if (mAudioManager == null) {
2835 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2836 }
2837 return mAudioManager;
2838 }
2839
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002840 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2841 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002842
2843 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002844 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002845 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002846 restore = true;
2847 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002848 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002849 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002850 if (params != null) {
2851 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002852 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002853 mPendingConfiguration.seq = 0;
Dianne Hackbornf123e492010-09-24 11:16:23 -07002854 //Log.d(TAG, ">>>>>> CALLING relayout");
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002855 int relayoutResult = sWindowSession.relayout(
2856 mWindow, params,
Dianne Hackborn189ee182010-12-02 21:48:53 -08002857 (int) (mView.getMeasuredWidth() * appScale + 0.5f),
2858 (int) (mView.getMeasuredHeight() * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002859 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002860 mPendingContentInsets, mPendingVisibleInsets,
2861 mPendingConfiguration, mSurface);
Dianne Hackbornf123e492010-09-24 11:16:23 -07002862 //Log.d(TAG, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002863 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002864 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002865 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002866
2867 if (mTranslator != null) {
2868 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2869 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2870 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002871 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002872 return relayoutResult;
2873 }
Romain Guy8506ab42009-06-11 17:35:47 -07002874
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002875 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002876 * {@inheritDoc}
2877 */
2878 public void playSoundEffect(int effectId) {
2879 checkThread();
2880
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002881 try {
2882 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002884 switch (effectId) {
2885 case SoundEffectConstants.CLICK:
2886 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2887 return;
2888 case SoundEffectConstants.NAVIGATION_DOWN:
2889 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2890 return;
2891 case SoundEffectConstants.NAVIGATION_LEFT:
2892 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2893 return;
2894 case SoundEffectConstants.NAVIGATION_RIGHT:
2895 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2896 return;
2897 case SoundEffectConstants.NAVIGATION_UP:
2898 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2899 return;
2900 default:
2901 throw new IllegalArgumentException("unknown effect id " + effectId +
2902 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2903 }
2904 } catch (IllegalStateException e) {
2905 // Exception thrown by getAudioManager() when mView is null
2906 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2907 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002908 }
2909 }
2910
2911 /**
2912 * {@inheritDoc}
2913 */
2914 public boolean performHapticFeedback(int effectId, boolean always) {
2915 try {
2916 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2917 } catch (RemoteException e) {
2918 return false;
2919 }
2920 }
2921
2922 /**
2923 * {@inheritDoc}
2924 */
2925 public View focusSearch(View focused, int direction) {
2926 checkThread();
2927 if (!(mView instanceof ViewGroup)) {
2928 return null;
2929 }
2930 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2931 }
2932
2933 public void debug() {
2934 mView.debug();
2935 }
2936
2937 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002938 if (immediate) {
2939 doDie();
2940 } else {
2941 sendEmptyMessage(DIE);
2942 }
2943 }
2944
2945 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002946 checkThread();
Jeff Brownb75fa302010-07-15 23:47:29 -07002947 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002948 synchronized (this) {
2949 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07002950 destroyHardwareRenderer();
2951
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 int viewVisibility = mView.getVisibility();
2953 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2954 if (mWindowAttributesChanged || viewVisibilityChanged) {
2955 // If layout params have been changed, first give them
2956 // to the window manager to make sure it has the correct
2957 // animation info.
2958 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002959 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2960 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002961 sWindowSession.finishDrawing(mWindow);
2962 }
2963 } catch (RemoteException e) {
2964 }
2965 }
2966
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002967 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 }
2969 if (mAdded) {
2970 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002971 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002972 }
2973 }
2974 }
2975
Romain Guy29d89972010-09-22 16:10:57 -07002976 private void destroyHardwareRenderer() {
Romain Guyb051e892010-09-28 19:09:36 -07002977 if (mAttachInfo.mHardwareRenderer != null) {
2978 mAttachInfo.mHardwareRenderer.destroy(true);
2979 mAttachInfo.mHardwareRenderer = null;
Romain Guy29d89972010-09-22 16:10:57 -07002980 mAttachInfo.mHardwareAccelerated = false;
2981 }
2982 }
2983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002984 public void dispatchFinishedEvent(int seq, boolean handled) {
2985 Message msg = obtainMessage(FINISHED_EVENT);
2986 msg.arg1 = seq;
2987 msg.arg2 = handled ? 1 : 0;
2988 sendMessage(msg);
2989 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002992 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2994 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2995 + " visibleInsets=" + visibleInsets.toShortString()
2996 + " reportDraw=" + reportDraw);
2997 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002998 if (mTranslator != null) {
2999 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
3000 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
3001 w *= mTranslator.applicationInvertedScale;
3002 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07003003 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07003004 msg.arg1 = w;
3005 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08003006 ResizedInfo ri = new ResizedInfo();
3007 ri.coveredInsets = new Rect(coveredInsets);
3008 ri.visibleInsets = new Rect(visibleInsets);
3009 ri.newConfig = newConfig;
3010 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 sendMessage(msg);
3012 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003013
Jeff Brown3915bb82010-11-05 15:02:16 -07003014 private InputQueue.FinishedCallback mFinishedCallback;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003015
3016 private final InputHandler mInputHandler = new InputHandler() {
Jeff Brown3915bb82010-11-05 15:02:16 -07003017 public void handleKey(KeyEvent event, InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003018 startInputEvent(finishedCallback);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003019 dispatchKey(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003020 }
3021
Jeff Brown3915bb82010-11-05 15:02:16 -07003022 public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003023 startInputEvent(finishedCallback);
3024 dispatchMotion(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003025 }
3026 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027
3028 public void dispatchKey(KeyEvent event) {
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003029 dispatchKey(event, false);
3030 }
3031
3032 private void dispatchKey(KeyEvent event, boolean sendDone) {
3033 //noinspection ConstantConditions
3034 if (false && event.getAction() == KeyEvent.ACTION_DOWN) {
3035 if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
Romain Guy812ccbe2010-06-01 14:07:24 -07003036 if (DBG) Log.d("keydisp", "===================================================");
3037 if (DBG) Log.d("keydisp", "Focused view Hierarchy is:");
3038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 debug();
3040
Romain Guy812ccbe2010-06-01 14:07:24 -07003041 if (DBG) Log.d("keydisp", "===================================================");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003042 }
3043 }
3044
3045 Message msg = obtainMessage(DISPATCH_KEY);
3046 msg.obj = event;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003047 msg.arg1 = sendDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003048
3049 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07003050 TAG, "sending key " + event + " to " + mView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003051
3052 sendMessageAtTime(msg, event.getEventTime());
3053 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07003054
3055 public void dispatchMotion(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003056 dispatchMotion(event, false);
3057 }
3058
3059 private void dispatchMotion(MotionEvent event, boolean sendDone) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07003060 int source = event.getSource();
3061 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003062 dispatchPointer(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07003063 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003064 dispatchTrackball(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07003065 } else {
3066 // TODO
3067 Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event);
Jeff Brown93ed4e32010-09-23 13:51:48 -07003068 if (sendDone) {
Jeff Brown3915bb82010-11-05 15:02:16 -07003069 finishInputEvent(false);
Jeff Brown93ed4e32010-09-23 13:51:48 -07003070 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07003071 }
3072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003074 public void dispatchPointer(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003075 dispatchPointer(event, false);
3076 }
3077
3078 private void dispatchPointer(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003079 Message msg = obtainMessage(DISPATCH_POINTER);
3080 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07003081 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003082 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 }
3084
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003085 public void dispatchTrackball(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003086 dispatchTrackball(event, false);
3087 }
3088
3089 private void dispatchTrackball(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003090 Message msg = obtainMessage(DISPATCH_TRACKBALL);
3091 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07003092 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003093 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003094 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 public void dispatchAppVisibility(boolean visible) {
3097 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
3098 msg.arg1 = visible ? 1 : 0;
3099 sendMessage(msg);
3100 }
3101
3102 public void dispatchGetNewSurface() {
3103 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
3104 sendMessage(msg);
3105 }
3106
3107 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3108 Message msg = Message.obtain();
3109 msg.what = WINDOW_FOCUS_CHANGED;
3110 msg.arg1 = hasFocus ? 1 : 0;
3111 msg.arg2 = inTouchMode ? 1 : 0;
3112 sendMessage(msg);
3113 }
3114
Dianne Hackbornffa42482009-09-23 22:20:11 -07003115 public void dispatchCloseSystemDialogs(String reason) {
3116 Message msg = Message.obtain();
3117 msg.what = CLOSE_SYSTEM_DIALOGS;
3118 msg.obj = reason;
3119 sendMessage(msg);
3120 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003121
3122 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07003123 final int what;
3124 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
3125 what = DISPATCH_DRAG_LOCATION_EVENT;
3126 removeMessages(what);
3127 } else {
3128 what = DISPATCH_DRAG_EVENT;
3129 }
Christopher Tate407b4e92010-11-30 17:14:08 -08003130 event.mLocalState = mLocalDragState; // only present when this app called startDrag()
Chris Tate91e9bb32010-10-12 12:58:43 -07003131 Message msg = obtainMessage(what, event);
Christopher Tatea53146c2010-09-07 11:57:52 -07003132 sendMessage(msg);
3133 }
3134
svetoslavganov75986cf2009-05-14 22:28:01 -07003135 /**
3136 * The window is getting focus so if there is anything focused/selected
3137 * send an {@link AccessibilityEvent} to announce that.
3138 */
3139 private void sendAccessibilityEvents() {
3140 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
3141 return;
3142 }
3143 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3144 View focusedView = mView.findFocus();
3145 if (focusedView != null && focusedView != mView) {
3146 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3147 }
3148 }
3149
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 public boolean showContextMenuForChild(View originalView) {
3151 return false;
3152 }
3153
Adam Powell6e346362010-07-23 10:18:23 -07003154 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
3155 return null;
3156 }
3157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 public void createContextMenu(ContextMenu menu) {
3159 }
3160
3161 public void childDrawableStateChanged(View child) {
3162 }
3163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003164 void checkThread() {
3165 if (mThread != Thread.currentThread()) {
3166 throw new CalledFromWrongThreadException(
3167 "Only the original thread that created a view hierarchy can touch its views.");
3168 }
3169 }
3170
3171 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
3172 // ViewRoot never intercepts touch event, so this can be a no-op
3173 }
3174
3175 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
3176 boolean immediate) {
3177 return scrollToRectOrFocus(rectangle, immediate);
3178 }
Romain Guy8506ab42009-06-11 17:35:47 -07003179
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07003180 class TakenSurfaceHolder extends BaseSurfaceHolder {
3181 @Override
3182 public boolean onAllowLockCanvas() {
3183 return mDrawingAllowed;
3184 }
3185
3186 @Override
3187 public void onRelayoutContainer() {
3188 // Not currently interesting -- from changing between fixed and layout size.
3189 }
3190
3191 public void setFormat(int format) {
3192 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
3193 }
3194
3195 public void setType(int type) {
3196 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
3197 }
3198
3199 @Override
3200 public void onUpdateSurface() {
3201 // We take care of format and type changes on our own.
3202 throw new IllegalStateException("Shouldn't be here");
3203 }
3204
3205 public boolean isCreating() {
3206 return mIsCreating;
3207 }
3208
3209 @Override
3210 public void setFixedSize(int width, int height) {
3211 throw new UnsupportedOperationException(
3212 "Currently only support sizing from layout");
3213 }
3214
3215 public void setKeepScreenOn(boolean screenOn) {
3216 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
3217 }
3218 }
3219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003220 static class InputMethodCallback extends IInputMethodCallback.Stub {
3221 private WeakReference<ViewRoot> mViewRoot;
3222
3223 public InputMethodCallback(ViewRoot viewRoot) {
3224 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
3225 }
Romain Guy8506ab42009-06-11 17:35:47 -07003226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 public void finishedEvent(int seq, boolean handled) {
3228 final ViewRoot viewRoot = mViewRoot.get();
3229 if (viewRoot != null) {
3230 viewRoot.dispatchFinishedEvent(seq, handled);
3231 }
3232 }
3233
3234 public void sessionCreated(IInputMethodSession session) throws RemoteException {
3235 // Stub -- not for use in the client.
3236 }
3237 }
Romain Guy8506ab42009-06-11 17:35:47 -07003238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003240 private final WeakReference<ViewRoot> mViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241
Romain Guyfb8b7632010-08-23 21:05:08 -07003242 W(ViewRoot viewRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003243 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
3244 }
3245
Romain Guyfb8b7632010-08-23 21:05:08 -07003246 public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets,
3247 boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248 final ViewRoot viewRoot = mViewRoot.get();
3249 if (viewRoot != null) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003250 viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003251 }
3252 }
3253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 public void dispatchAppVisibility(boolean visible) {
3255 final ViewRoot viewRoot = mViewRoot.get();
3256 if (viewRoot != null) {
3257 viewRoot.dispatchAppVisibility(visible);
3258 }
3259 }
3260
3261 public void dispatchGetNewSurface() {
3262 final ViewRoot viewRoot = mViewRoot.get();
3263 if (viewRoot != null) {
3264 viewRoot.dispatchGetNewSurface();
3265 }
3266 }
3267
3268 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3269 final ViewRoot viewRoot = mViewRoot.get();
3270 if (viewRoot != null) {
3271 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
3272 }
3273 }
3274
3275 private static int checkCallingPermission(String permission) {
3276 if (!Process.supportsProcesses()) {
3277 return PackageManager.PERMISSION_GRANTED;
3278 }
3279
3280 try {
3281 return ActivityManagerNative.getDefault().checkPermission(
3282 permission, Binder.getCallingPid(), Binder.getCallingUid());
3283 } catch (RemoteException e) {
3284 return PackageManager.PERMISSION_DENIED;
3285 }
3286 }
3287
3288 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3289 final ViewRoot viewRoot = mViewRoot.get();
3290 if (viewRoot != null) {
3291 final View view = viewRoot.mView;
3292 if (view != null) {
3293 if (checkCallingPermission(Manifest.permission.DUMP) !=
3294 PackageManager.PERMISSION_GRANTED) {
3295 throw new SecurityException("Insufficient permissions to invoke"
3296 + " executeCommand() from pid=" + Binder.getCallingPid()
3297 + ", uid=" + Binder.getCallingUid());
3298 }
3299
3300 OutputStream clientStream = null;
3301 try {
3302 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3303 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3304 } catch (IOException e) {
3305 e.printStackTrace();
3306 } finally {
3307 if (clientStream != null) {
3308 try {
3309 clientStream.close();
3310 } catch (IOException e) {
3311 e.printStackTrace();
3312 }
3313 }
3314 }
3315 }
3316 }
3317 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003318
Dianne Hackbornffa42482009-09-23 22:20:11 -07003319 public void closeSystemDialogs(String reason) {
3320 final ViewRoot viewRoot = mViewRoot.get();
3321 if (viewRoot != null) {
3322 viewRoot.dispatchCloseSystemDialogs(reason);
3323 }
3324 }
3325
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003326 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3327 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003328 if (sync) {
3329 try {
3330 sWindowSession.wallpaperOffsetsComplete(asBinder());
3331 } catch (RemoteException e) {
3332 }
3333 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003334 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003335
3336 public void dispatchWallpaperCommand(String action, int x, int y,
3337 int z, Bundle extras, boolean sync) {
3338 if (sync) {
3339 try {
3340 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3341 } catch (RemoteException e) {
3342 }
3343 }
3344 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003345
3346 /* Drag/drop */
3347 public void dispatchDragEvent(DragEvent event) {
3348 final ViewRoot viewRoot = mViewRoot.get();
3349 if (viewRoot != null) {
3350 viewRoot.dispatchDragEvent(event);
3351 }
3352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 }
3354
3355 /**
3356 * Maintains state information for a single trackball axis, generating
3357 * discrete (DPAD) movements based on raw trackball motion.
3358 */
3359 static final class TrackballAxis {
3360 /**
3361 * The maximum amount of acceleration we will apply.
3362 */
3363 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 /**
3366 * The maximum amount of time (in milliseconds) between events in order
3367 * for us to consider the user to be doing fast trackball movements,
3368 * and thus apply an acceleration.
3369 */
3370 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 /**
3373 * Scaling factor to the time (in milliseconds) between events to how
3374 * much to multiple/divide the current acceleration. When movement
3375 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3376 * FAST_MOVE_TIME it divides it.
3377 */
3378 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 float position;
3381 float absPosition;
3382 float acceleration = 1;
3383 long lastMoveTime = 0;
3384 int step;
3385 int dir;
3386 int nonAccelMovement;
3387
3388 void reset(int _step) {
3389 position = 0;
3390 acceleration = 1;
3391 lastMoveTime = 0;
3392 step = _step;
3393 dir = 0;
3394 }
3395
3396 /**
3397 * Add trackball movement into the state. If the direction of movement
3398 * has been reversed, the state is reset before adding the
3399 * movement (so that you don't have to compensate for any previously
3400 * collected movement before see the result of the movement in the
3401 * new direction).
3402 *
3403 * @return Returns the absolute value of the amount of movement
3404 * collected so far.
3405 */
3406 float collect(float off, long time, String axis) {
3407 long normTime;
3408 if (off > 0) {
3409 normTime = (long)(off * FAST_MOVE_TIME);
3410 if (dir < 0) {
3411 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3412 position = 0;
3413 step = 0;
3414 acceleration = 1;
3415 lastMoveTime = 0;
3416 }
3417 dir = 1;
3418 } else if (off < 0) {
3419 normTime = (long)((-off) * FAST_MOVE_TIME);
3420 if (dir > 0) {
3421 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3422 position = 0;
3423 step = 0;
3424 acceleration = 1;
3425 lastMoveTime = 0;
3426 }
3427 dir = -1;
3428 } else {
3429 normTime = 0;
3430 }
Romain Guy8506ab42009-06-11 17:35:47 -07003431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 // The number of milliseconds between each movement that is
3433 // considered "normal" and will not result in any acceleration
3434 // or deceleration, scaled by the offset we have here.
3435 if (normTime > 0) {
3436 long delta = time - lastMoveTime;
3437 lastMoveTime = time;
3438 float acc = acceleration;
3439 if (delta < normTime) {
3440 // The user is scrolling rapidly, so increase acceleration.
3441 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3442 if (scale > 1) acc *= scale;
3443 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3444 + off + " normTime=" + normTime + " delta=" + delta
3445 + " scale=" + scale + " acc=" + acc);
3446 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3447 } else {
3448 // The user is scrolling slowly, so decrease acceleration.
3449 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3450 if (scale > 1) acc /= scale;
3451 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3452 + off + " normTime=" + normTime + " delta=" + delta
3453 + " scale=" + scale + " acc=" + acc);
3454 acceleration = acc > 1 ? acc : 1;
3455 }
3456 }
3457 position += off;
3458 return (absPosition = Math.abs(position));
3459 }
3460
3461 /**
3462 * Generate the number of discrete movement events appropriate for
3463 * the currently collected trackball movement.
3464 *
3465 * @param precision The minimum movement required to generate the
3466 * first discrete movement.
3467 *
3468 * @return Returns the number of discrete movements, either positive
3469 * or negative, or 0 if there is not enough trackball movement yet
3470 * for a discrete movement.
3471 */
3472 int generate(float precision) {
3473 int movement = 0;
3474 nonAccelMovement = 0;
3475 do {
3476 final int dir = position >= 0 ? 1 : -1;
3477 switch (step) {
3478 // If we are going to execute the first step, then we want
3479 // to do this as soon as possible instead of waiting for
3480 // a full movement, in order to make things look responsive.
3481 case 0:
3482 if (absPosition < precision) {
3483 return movement;
3484 }
3485 movement += dir;
3486 nonAccelMovement += dir;
3487 step = 1;
3488 break;
3489 // If we have generated the first movement, then we need
3490 // to wait for the second complete trackball motion before
3491 // generating the second discrete movement.
3492 case 1:
3493 if (absPosition < 2) {
3494 return movement;
3495 }
3496 movement += dir;
3497 nonAccelMovement += dir;
3498 position += dir > 0 ? -2 : 2;
3499 absPosition = Math.abs(position);
3500 step = 2;
3501 break;
3502 // After the first two, we generate discrete movements
3503 // consistently with the trackball, applying an acceleration
3504 // if the trackball is moving quickly. This is a simple
3505 // acceleration on top of what we already compute based
3506 // on how quickly the wheel is being turned, to apply
3507 // a longer increasing acceleration to continuous movement
3508 // in one direction.
3509 default:
3510 if (absPosition < 1) {
3511 return movement;
3512 }
3513 movement += dir;
3514 position += dir >= 0 ? -1 : 1;
3515 absPosition = Math.abs(position);
3516 float acc = acceleration;
3517 acc *= 1.1f;
3518 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3519 break;
3520 }
3521 } while (true);
3522 }
3523 }
3524
3525 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3526 public CalledFromWrongThreadException(String msg) {
3527 super(msg);
3528 }
3529 }
3530
3531 private SurfaceHolder mHolder = new SurfaceHolder() {
3532 // we only need a SurfaceHolder for opengl. it would be nice
3533 // to implement everything else though, especially the callback
3534 // support (opengl doesn't make use of it right now, but eventually
3535 // will).
3536 public Surface getSurface() {
3537 return mSurface;
3538 }
3539
3540 public boolean isCreating() {
3541 return false;
3542 }
3543
3544 public void addCallback(Callback callback) {
3545 }
3546
3547 public void removeCallback(Callback callback) {
3548 }
3549
3550 public void setFixedSize(int width, int height) {
3551 }
3552
3553 public void setSizeFromLayout() {
3554 }
3555
3556 public void setFormat(int format) {
3557 }
3558
3559 public void setType(int type) {
3560 }
3561
3562 public void setKeepScreenOn(boolean screenOn) {
3563 }
3564
3565 public Canvas lockCanvas() {
3566 return null;
3567 }
3568
3569 public Canvas lockCanvas(Rect dirty) {
3570 return null;
3571 }
3572
3573 public void unlockCanvasAndPost(Canvas canvas) {
3574 }
3575 public Rect getSurfaceFrame() {
3576 return null;
3577 }
3578 };
3579
3580 static RunQueue getRunQueue() {
3581 RunQueue rq = sRunQueues.get();
3582 if (rq != null) {
3583 return rq;
3584 }
3585 rq = new RunQueue();
3586 sRunQueues.set(rq);
3587 return rq;
3588 }
Romain Guy8506ab42009-06-11 17:35:47 -07003589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003590 /**
3591 * @hide
3592 */
3593 static final class RunQueue {
3594 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3595
3596 void post(Runnable action) {
3597 postDelayed(action, 0);
3598 }
3599
3600 void postDelayed(Runnable action, long delayMillis) {
3601 HandlerAction handlerAction = new HandlerAction();
3602 handlerAction.action = action;
3603 handlerAction.delay = delayMillis;
3604
3605 synchronized (mActions) {
3606 mActions.add(handlerAction);
3607 }
3608 }
3609
3610 void removeCallbacks(Runnable action) {
3611 final HandlerAction handlerAction = new HandlerAction();
3612 handlerAction.action = action;
3613
3614 synchronized (mActions) {
3615 final ArrayList<HandlerAction> actions = mActions;
3616
3617 while (actions.remove(handlerAction)) {
3618 // Keep going
3619 }
3620 }
3621 }
3622
3623 void executeActions(Handler handler) {
3624 synchronized (mActions) {
3625 final ArrayList<HandlerAction> actions = mActions;
3626 final int count = actions.size();
3627
3628 for (int i = 0; i < count; i++) {
3629 final HandlerAction handlerAction = actions.get(i);
3630 handler.postDelayed(handlerAction.action, handlerAction.delay);
3631 }
3632
Romain Guy15df6702009-08-17 20:17:30 -07003633 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003634 }
3635 }
3636
3637 private static class HandlerAction {
3638 Runnable action;
3639 long delay;
3640
3641 @Override
3642 public boolean equals(Object o) {
3643 if (this == o) return true;
3644 if (o == null || getClass() != o.getClass()) return false;
3645
3646 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 return !(action != null ? !action.equals(that.action) : that.action != null);
3648
3649 }
3650
3651 @Override
3652 public int hashCode() {
3653 int result = action != null ? action.hashCode() : 0;
3654 result = 31 * result + (int) (delay ^ (delay >>> 32));
3655 return result;
3656 }
3657 }
3658 }
3659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003660 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003661}