blob: d480bbc5e30047ded215beffd36528db3ebad643 [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
489 // Don't enable hardware acceleration when we're not on the main thread
490 if (Looper.getMainLooper() != Looper.myLooper()) {
491 Log.w(HardwareRenderer.LOG_TAG, "Attempting to initialize hardware acceleration "
492 + "outside of the main thread, aborting");
493 return;
494 }
495
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800496 // Try to enable hardware acceleration if requested
497 if (attrs != null &&
498 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
499 // 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) {
1232 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1233 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1234 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1235 givenContent.left = givenContent.top = givenContent.right
1236 = givenContent.bottom = givenVisible.left = givenVisible.top
1237 = givenVisible.right = givenVisible.bottom = 0;
1238 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001239 Rect contentInsets = insets.contentInsets;
1240 Rect visibleInsets = insets.visibleInsets;
1241 if (mTranslator != null) {
1242 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1243 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1246 mLastGivenInsets.set(insets);
1247 try {
1248 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001249 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 } catch (RemoteException e) {
1251 }
1252 }
1253 }
Romain Guy8506ab42009-06-11 17:35:47 -07001254
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 if (mFirst) {
1256 // handle first focus request
1257 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1258 + mView.hasFocus());
1259 if (mView != null) {
1260 if (!mView.hasFocus()) {
1261 mView.requestFocus(View.FOCUS_FORWARD);
1262 mFocusedView = mRealFocusedView = mView.findFocus();
1263 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1264 + mFocusedView);
1265 } else {
1266 mRealFocusedView = mView.findFocus();
1267 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1268 + mRealFocusedView);
1269 }
1270 }
1271 }
1272
1273 mFirst = false;
1274 mWillDrawSoon = false;
1275 mNewSurfaceNeeded = false;
1276 mViewVisibility = viewVisibility;
1277
1278 if (mAttachInfo.mHasWindowFocus) {
1279 final boolean imTarget = WindowManager.LayoutParams
1280 .mayUseInputMethod(mWindowAttributes.flags);
1281 if (imTarget != mLastWasImTarget) {
1282 mLastWasImTarget = imTarget;
1283 InputMethodManager imm = InputMethodManager.peekInstance();
1284 if (imm != null && imTarget) {
1285 imm.startGettingWindowFocus(mView);
1286 imm.onWindowFocus(mView, mView.findFocus(),
1287 mWindowAttributes.softInputMode,
1288 !mHasHadWindowFocus, mWindowAttributes.flags);
1289 }
1290 }
1291 }
Romain Guy8506ab42009-06-11 17:35:47 -07001292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1294
1295 if (!cancelDraw && !newSurface) {
1296 mFullRedrawNeeded = false;
1297 draw(fullRedrawNeeded);
1298
1299 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1300 || mReportNextDraw) {
1301 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001302 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 }
1304 mReportNextDraw = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001305 if (mSurfaceHolder != null && mSurface.isValid()) {
1306 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
1307 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1308 if (callbacks != null) {
1309 for (SurfaceHolder.Callback c : callbacks) {
1310 if (c instanceof SurfaceHolder.Callback2) {
1311 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
1312 mSurfaceHolder);
1313 }
1314 }
1315 }
1316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 try {
1318 sWindowSession.finishDrawing(mWindow);
1319 } catch (RemoteException e) {
1320 }
1321 }
1322 } else {
1323 // We were supposed to report when we are done drawing. Since we canceled the
1324 // draw, remember it here.
1325 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1326 mReportNextDraw = true;
1327 }
1328 if (fullRedrawNeeded) {
1329 mFullRedrawNeeded = true;
1330 }
1331 // Try again
1332 scheduleTraversals();
1333 }
1334 }
1335
1336 public void requestTransparentRegion(View child) {
1337 // the test below should not fail unless someone is messing with us
1338 checkThread();
1339 if (mView == child) {
1340 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1341 // Need to make sure we re-evaluate the window attributes next
1342 // time around, to ensure the window has the correct format.
1343 mWindowAttributesChanged = true;
Mathias Agopian1bd80ad2010-11-04 17:13:39 -07001344 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001345 }
1346 }
1347
1348 /**
1349 * Figures out the measure spec for the root view in a window based on it's
1350 * layout params.
1351 *
1352 * @param windowSize
1353 * The available width or height of the window
1354 *
1355 * @param rootDimension
1356 * The layout params for one dimension (width or height) of the
1357 * window.
1358 *
1359 * @return The measure spec to use to measure the root view.
1360 */
1361 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1362 int measureSpec;
1363 switch (rootDimension) {
1364
Romain Guy980a9382010-01-08 15:06:28 -08001365 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 // Window can't resize. Force root view to be windowSize.
1367 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1368 break;
1369 case ViewGroup.LayoutParams.WRAP_CONTENT:
1370 // Window can resize. Set max size for root view.
1371 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1372 break;
1373 default:
1374 // Window wants to be an exact size. Force root view to be that size.
1375 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1376 break;
1377 }
1378 return measureSpec;
1379 }
1380
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001381 int mHardwareYOffset;
1382 int mResizeAlpha;
1383 final Paint mResizePaint = new Paint();
1384
1385 public void onHardwarePreDraw(Canvas canvas) {
1386 canvas.translate(0, -mHardwareYOffset);
1387 }
1388
1389 public void onHardwarePostDraw(Canvas canvas) {
1390 if (mResizeBitmap != null) {
1391 canvas.translate(0, mHardwareYOffset);
1392 mResizePaint.setAlpha(mResizeAlpha);
1393 canvas.drawBitmap(mResizeBitmap, 0, 0, mResizePaint);
1394 }
1395 }
1396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 private void draw(boolean fullRedrawNeeded) {
1398 Surface surface = mSurface;
1399 if (surface == null || !surface.isValid()) {
1400 return;
1401 }
1402
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001403 if (!sFirstDrawComplete) {
1404 synchronized (sFirstDrawHandlers) {
1405 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07001406 final int count = sFirstDrawHandlers.size();
1407 for (int i = 0; i< count; i++) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001408 post(sFirstDrawHandlers.get(i));
1409 }
1410 }
1411 }
1412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 scrollToRectOrFocus(null, false);
1414
1415 if (mAttachInfo.mViewScrollChanged) {
1416 mAttachInfo.mViewScrollChanged = false;
1417 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1418 }
Romain Guy8506ab42009-06-11 17:35:47 -07001419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 int yoff;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001421 boolean animating = mScroller != null && mScroller.computeScrollOffset();
1422 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 yoff = mScroller.getCurrY();
1424 } else {
1425 yoff = mScrollY;
1426 }
1427 if (mCurScrollY != yoff) {
1428 mCurScrollY = yoff;
1429 fullRedrawNeeded = true;
1430 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001431 float appScale = mAttachInfo.mApplicationScale;
1432 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001433
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001434 int resizeAlpha = 0;
1435 if (mResizeBitmap != null) {
1436 long deltaTime = SystemClock.uptimeMillis() - mResizeBitmapStartTime;
1437 if (deltaTime < mResizeBitmapDuration) {
1438 float amt = deltaTime/(float)mResizeBitmapDuration;
1439 amt = mResizeInterpolator.getInterpolation(amt);
1440 animating = true;
1441 resizeAlpha = 255 - (int)(amt*255);
1442 } else {
1443 disposeResizeBitmap();
1444 }
1445 }
1446
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001447 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001448 if (mSurfaceHolder != null) {
1449 // The app owns the surface, we won't draw.
1450 dirty.setEmpty();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001451 if (animating) {
1452 if (mScroller != null) {
1453 mScroller.abortAnimation();
1454 }
1455 disposeResizeBitmap();
1456 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001457 return;
1458 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07001459
1460 if (fullRedrawNeeded) {
1461 mAttachInfo.mIgnoreDirtyState = true;
1462 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1463 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001464
Romain Guyb051e892010-09-28 19:09:36 -07001465 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guyfd507262010-10-10 15:42:49 -07001466 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy101e2ae2010-10-11 12:41:21 -07001467 mIsAnimating = false;
Romain Guyfd507262010-10-10 15:42:49 -07001468 dirty.setEmpty();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001469 mHardwareYOffset = yoff;
1470 mResizeAlpha = resizeAlpha;
1471 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001473
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001474 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001475 mFullRedrawNeeded = true;
1476 scheduleTraversals();
1477 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001478
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001479 return;
1480 }
1481
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001482 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001483 Log.v(TAG, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 + mWindowAttributes.getTitle()
1485 + ": dirty={" + dirty.left + "," + dirty.top
1486 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001487 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1488 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
1490
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001491 if (!dirty.isEmpty() || mIsAnimating) {
1492 Canvas canvas;
1493 try {
1494 int left = dirty.left;
1495 int top = dirty.top;
1496 int right = dirty.right;
1497 int bottom = dirty.bottom;
1498 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001499
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001500 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1501 bottom != dirty.bottom) {
1502 mAttachInfo.mIgnoreDirtyState = true;
1503 }
1504
1505 // TODO: Do this in native
1506 canvas.setDensity(mDensity);
1507 } catch (Surface.OutOfResourcesException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001508 Log.e(TAG, "OutOfResourcesException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001509 // TODO: we should ask the window manager to do something!
1510 // for now we just do nothing
1511 return;
1512 } catch (IllegalArgumentException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001513 Log.e(TAG, "IllegalArgumentException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001514 // TODO: we should ask the window manager to do something!
1515 // for now we just do nothing
1516 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001517 }
1518
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001519 try {
1520 if (!dirty.isEmpty() || mIsAnimating) {
1521 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001523 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001524 Log.v(TAG, "Surface " + surface + " drawing to bitmap w="
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001525 + canvas.getWidth() + ", h=" + canvas.getHeight());
1526 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
Romain Guy5429e1d2010-09-07 12:38:00 -07001529 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001530 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001531 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001532
1533 // If this bitmap's format includes an alpha channel, we
1534 // need to clear it before drawing so that the child will
1535 // properly re-composite its drawing on a transparent
1536 // background. This automatically respects the clip/dirty region
1537 // or
1538 // If we are applying an offset, we need to clear the area
1539 // where the offset doesn't appear to avoid having garbage
1540 // left in the blank areas.
1541 if (!canvas.isOpaque() || yoff != 0) {
1542 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1543 }
1544
1545 dirty.setEmpty();
1546 mIsAnimating = false;
1547 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1548 mView.mPrivateFlags |= View.DRAWN;
1549
1550 if (DEBUG_DRAW) {
1551 Context cxt = mView.getContext();
1552 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1553 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1554 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1555 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001556 try {
1557 canvas.translate(0, -yoff);
1558 if (mTranslator != null) {
1559 mTranslator.translateCanvas(canvas);
1560 }
1561 canvas.setScreenDensity(scalingRequired
1562 ? DisplayMetrics.DENSITY_DEVICE : 0);
1563 mView.draw(canvas);
1564 } finally {
1565 mAttachInfo.mIgnoreDirtyState = false;
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001566 }
1567
1568 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1569 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1570 }
1571
Romain Guy5429e1d2010-09-07 12:38:00 -07001572 if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001573 int now = (int)SystemClock.elapsedRealtime();
1574 if (sDrawTime != 0) {
1575 nativeShowFPS(canvas, now - sDrawTime);
1576 }
1577 sDrawTime = now;
1578 }
1579
Romain Guy5429e1d2010-09-07 12:38:00 -07001580 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001581 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1582 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 }
1584
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001585 } finally {
1586 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 }
1589
1590 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001591 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
Romain Guy8506ab42009-06-11 17:35:47 -07001593
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001594 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 mFullRedrawNeeded = true;
1596 scheduleTraversals();
1597 }
1598 }
1599
1600 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1601 final View.AttachInfo attachInfo = mAttachInfo;
1602 final Rect ci = attachInfo.mContentInsets;
1603 final Rect vi = attachInfo.mVisibleInsets;
1604 int scrollY = 0;
1605 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 if (vi.left > ci.left || vi.top > ci.top
1608 || vi.right > ci.right || vi.bottom > ci.bottom) {
1609 // We'll assume that we aren't going to change the scroll
1610 // offset, since we want to avoid that unless it is actually
1611 // going to make the focus visible... otherwise we scroll
1612 // all over the place.
1613 scrollY = mScrollY;
1614 // We can be called for two different situations: during a draw,
1615 // to update the scroll position if the focus has changed (in which
1616 // case 'rectangle' is null), or in response to a
1617 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1618 // is non-null and we just want to scroll to whatever that
1619 // rectangle is).
1620 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001621
1622 // When in touch mode, focus points to the previously focused view,
1623 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001624 // line checks whether the view is still in our hierarchy.
1625 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001626 mRealFocusedView = null;
1627 return false;
1628 }
1629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 if (focus != mLastScrolledFocus) {
1631 // If the focus has changed, then ignore any requests to scroll
1632 // to a rectangle; first we want to make sure the entire focus
1633 // view is visible.
1634 rectangle = null;
1635 }
1636 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1637 + " rectangle=" + rectangle + " ci=" + ci
1638 + " vi=" + vi);
1639 if (focus == mLastScrolledFocus && !mScrollMayChange
1640 && rectangle == null) {
1641 // Optimization: if the focus hasn't changed since last
1642 // time, and no layout has happened, then just leave things
1643 // as they are.
1644 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1645 + mScrollY + " vi=" + vi.toShortString());
1646 } else if (focus != null) {
1647 // We need to determine if the currently focused view is
1648 // within the visible part of the window and, if not, apply
1649 // a pan so it can be seen.
1650 mLastScrolledFocus = focus;
1651 mScrollMayChange = false;
1652 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1653 // Try to find the rectangle from the focus view.
1654 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1655 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1656 + mView.getWidth() + " h=" + mView.getHeight()
1657 + " ci=" + ci.toShortString()
1658 + " vi=" + vi.toShortString());
1659 if (rectangle == null) {
1660 focus.getFocusedRect(mTempRect);
1661 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1662 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001663 if (mView instanceof ViewGroup) {
1664 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1665 focus, mTempRect);
1666 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1668 "Focus in window: focusRect="
1669 + mTempRect.toShortString()
1670 + " visRect=" + mVisRect.toShortString());
1671 } else {
1672 mTempRect.set(rectangle);
1673 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1674 "Request scroll to rect: "
1675 + mTempRect.toShortString()
1676 + " visRect=" + mVisRect.toShortString());
1677 }
1678 if (mTempRect.intersect(mVisRect)) {
1679 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1680 "Focus window visible rect: "
1681 + mTempRect.toShortString());
1682 if (mTempRect.height() >
1683 (mView.getHeight()-vi.top-vi.bottom)) {
1684 // If the focus simply is not going to fit, then
1685 // best is probably just to leave things as-is.
1686 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1687 "Too tall; leaving scrollY=" + scrollY);
1688 } else if ((mTempRect.top-scrollY) < vi.top) {
1689 scrollY -= vi.top - (mTempRect.top-scrollY);
1690 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1691 "Top covered; scrollY=" + scrollY);
1692 } else if ((mTempRect.bottom-scrollY)
1693 > (mView.getHeight()-vi.bottom)) {
1694 scrollY += (mTempRect.bottom-scrollY)
1695 - (mView.getHeight()-vi.bottom);
1696 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1697 "Bottom covered; scrollY=" + scrollY);
1698 }
1699 handled = true;
1700 }
1701 }
1702 }
1703 }
Romain Guy8506ab42009-06-11 17:35:47 -07001704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 if (scrollY != mScrollY) {
1706 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1707 + mScrollY + " , new=" + scrollY);
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001708 if (!immediate && mResizeBitmap == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 if (mScroller == null) {
1710 mScroller = new Scroller(mView.getContext());
1711 }
1712 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1713 } else if (mScroller != null) {
1714 mScroller.abortAnimation();
1715 }
1716 mScrollY = scrollY;
1717 }
Romain Guy8506ab42009-06-11 17:35:47 -07001718
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001719 return handled;
1720 }
Romain Guy8506ab42009-06-11 17:35:47 -07001721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 public void requestChildFocus(View child, View focused) {
1723 checkThread();
1724 if (mFocusedView != focused) {
1725 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1726 scheduleTraversals();
1727 }
1728 mFocusedView = mRealFocusedView = focused;
1729 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1730 + mFocusedView);
1731 }
1732
1733 public void clearChildFocus(View child) {
1734 checkThread();
1735
1736 View oldFocus = mFocusedView;
1737
1738 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1739 mFocusedView = mRealFocusedView = null;
1740 if (mView != null && !mView.hasFocus()) {
1741 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1742 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1743 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1744 }
1745 } else if (oldFocus != null) {
1746 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1747 }
1748 }
1749
1750
1751 public void focusableViewAvailable(View v) {
1752 checkThread();
1753
1754 if (mView != null && !mView.hasFocus()) {
1755 v.requestFocus();
1756 } else {
1757 // the one case where will transfer focus away from the current one
1758 // is if the current view is a view group that prefers to give focus
1759 // to its children first AND the view is a descendant of it.
1760 mFocusedView = mView.findFocus();
1761 boolean descendantsHaveDibsOnFocus =
1762 (mFocusedView instanceof ViewGroup) &&
1763 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1764 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1765 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1766 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1767 v.requestFocus();
1768 }
1769 }
1770 }
1771
1772 public void recomputeViewAttributes(View child) {
1773 checkThread();
1774 if (mView == child) {
1775 mAttachInfo.mRecomputeGlobalAttributes = true;
1776 if (!mWillDrawSoon) {
1777 scheduleTraversals();
1778 }
1779 }
1780 }
1781
1782 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 if (mView != null) {
1784 mView.dispatchDetachedFromWindow();
1785 }
1786
1787 mView = null;
1788 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001789 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790
Romain Guy29d89972010-09-22 16:10:57 -07001791 destroyHardwareRenderer();
Romain Guy4caa4ed2010-08-25 14:46:24 -07001792
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001793 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001795 if (mInputChannel != null) {
1796 if (mInputQueueCallback != null) {
1797 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
1798 mInputQueueCallback = null;
1799 } else {
1800 InputQueue.unregisterInputChannel(mInputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001801 }
1802 }
1803
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 try {
1805 sWindowSession.remove(mWindow);
1806 } catch (RemoteException e) {
1807 }
Jeff Brown349703e2010-06-22 01:27:15 -07001808
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001809 // Dispose the input channel after removing the window so the Window Manager
1810 // doesn't interpret the input channel being closed as an abnormal termination.
1811 if (mInputChannel != null) {
1812 mInputChannel.dispose();
1813 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 }
Romain Guy8506ab42009-06-11 17:35:47 -07001816
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001817 void updateConfiguration(Configuration config, boolean force) {
1818 if (DEBUG_CONFIGURATION) Log.v(TAG,
1819 "Applying new config to window "
1820 + mWindowAttributes.getTitle()
1821 + ": " + config);
1822 synchronized (sConfigCallbacks) {
1823 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1824 sConfigCallbacks.get(i).onConfigurationChanged(config);
1825 }
1826 }
1827 if (mView != null) {
1828 // At this point the resources have been updated to
1829 // have the most recent config, whatever that is. Use
1830 // the on in them which may be newer.
1831 if (mView != null) {
1832 config = mView.getResources().getConfiguration();
1833 }
1834 if (force || mLastConfiguration.diff(config) != 0) {
1835 mLastConfiguration.setTo(config);
1836 mView.dispatchConfigurationChanged(config);
1837 }
1838 }
1839 }
1840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 /**
1842 * Return true if child is an ancestor of parent, (or equal to the parent).
1843 */
1844 private static boolean isViewDescendantOf(View child, View parent) {
1845 if (child == parent) {
1846 return true;
1847 }
1848
1849 final ViewParent theParent = child.getParent();
1850 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1851 }
1852
Romain Guycdb86672010-03-18 18:54:50 -07001853 private static void forceLayout(View view) {
1854 view.forceLayout();
1855 if (view instanceof ViewGroup) {
1856 ViewGroup group = (ViewGroup) view;
1857 final int count = group.getChildCount();
1858 for (int i = 0; i < count; i++) {
1859 forceLayout(group.getChildAt(i));
1860 }
1861 }
1862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863
1864 public final static int DO_TRAVERSAL = 1000;
1865 public final static int DIE = 1001;
1866 public final static int RESIZED = 1002;
1867 public final static int RESIZED_REPORT = 1003;
1868 public final static int WINDOW_FOCUS_CHANGED = 1004;
1869 public final static int DISPATCH_KEY = 1005;
1870 public final static int DISPATCH_POINTER = 1006;
1871 public final static int DISPATCH_TRACKBALL = 1007;
1872 public final static int DISPATCH_APP_VISIBILITY = 1008;
1873 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1874 public final static int FINISHED_EVENT = 1010;
1875 public final static int DISPATCH_KEY_FROM_IME = 1011;
1876 public final static int FINISH_INPUT_CONNECTION = 1012;
1877 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001878 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Christopher Tatea53146c2010-09-07 11:57:52 -07001879 public final static int DISPATCH_DRAG_EVENT = 1015;
Chris Tate91e9bb32010-10-12 12:58:43 -07001880 public final static int DISPATCH_DRAG_LOCATION_EVENT = 1016;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881
1882 @Override
1883 public void handleMessage(Message msg) {
1884 switch (msg.what) {
1885 case View.AttachInfo.INVALIDATE_MSG:
1886 ((View) msg.obj).invalidate();
1887 break;
1888 case View.AttachInfo.INVALIDATE_RECT_MSG:
1889 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1890 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1891 info.release();
1892 break;
1893 case DO_TRAVERSAL:
1894 if (mProfile) {
1895 Debug.startMethodTracing("ViewRoot");
1896 }
1897
1898 performTraversals();
1899
1900 if (mProfile) {
1901 Debug.stopMethodTracing();
1902 mProfile = false;
1903 }
1904 break;
1905 case FINISHED_EVENT:
1906 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1907 break;
1908 case DISPATCH_KEY:
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001909 deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 break;
Jeff Brown3915bb82010-11-05 15:02:16 -07001911 case DISPATCH_POINTER:
1912 deliverPointerEvent((MotionEvent) msg.obj, msg.arg1 != 0);
1913 break;
1914 case DISPATCH_TRACKBALL:
1915 deliverTrackballEvent((MotionEvent) msg.obj, msg.arg1 != 0);
1916 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 case DISPATCH_APP_VISIBILITY:
1918 handleAppVisibility(msg.arg1 != 0);
1919 break;
1920 case DISPATCH_GET_NEW_SURFACE:
1921 handleGetNewSurface();
1922 break;
1923 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001924 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001927 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001928 && mPendingVisibleInsets.equals(ri.visibleInsets)
1929 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 break;
1931 }
1932 // fall through...
1933 case RESIZED_REPORT:
1934 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001935 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1936 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001937 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001938 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 mWinFrame.left = 0;
1940 mWinFrame.right = msg.arg1;
1941 mWinFrame.top = 0;
1942 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001943 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1944 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 if (msg.what == RESIZED_REPORT) {
1946 mReportNextDraw = true;
1947 }
Romain Guycdb86672010-03-18 18:54:50 -07001948
1949 if (mView != null) {
1950 forceLayout(mView);
1951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001952 requestLayout();
1953 }
1954 break;
1955 case WINDOW_FOCUS_CHANGED: {
1956 if (mAdded) {
1957 boolean hasWindowFocus = msg.arg1 != 0;
1958 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1959 if (hasWindowFocus) {
1960 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001961 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962
Romain Guyc361da82010-10-25 15:29:10 -07001963 if (mAttachInfo.mHardwareRenderer != null &&
1964 mSurface != null && mSurface.isValid()) {
Romain Guyb051e892010-09-28 19:09:36 -07001965 mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
1966 mAttachInfo, mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
1968 }
Romain Guy8506ab42009-06-11 17:35:47 -07001969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001970 mLastWasImTarget = WindowManager.LayoutParams
1971 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 InputMethodManager imm = InputMethodManager.peekInstance();
1974 if (mView != null) {
1975 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1976 imm.startGettingWindowFocus(mView);
1977 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001978 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 mView.dispatchWindowFocusChanged(hasWindowFocus);
1980 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 // Note: must be done after the focus change callbacks,
1983 // so all of the view state is set up correctly.
1984 if (hasWindowFocus) {
1985 if (imm != null && mLastWasImTarget) {
1986 imm.onWindowFocus(mView, mView.findFocus(),
1987 mWindowAttributes.softInputMode,
1988 !mHasHadWindowFocus, mWindowAttributes.flags);
1989 }
1990 // Clear the forward bit. We can just do this directly, since
1991 // the window manager doesn't care about it.
1992 mWindowAttributes.softInputMode &=
1993 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1994 ((WindowManager.LayoutParams)mView.getLayoutParams())
1995 .softInputMode &=
1996 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1997 mHasHadWindowFocus = true;
1998 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001999
2000 if (hasWindowFocus && mView != null) {
2001 sendAccessibilityEvents();
2002 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004 } break;
2005 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07002006 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002007 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07002008 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002009 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07002010 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07002012 KeyEvent event = (KeyEvent)msg.obj;
2013 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
2014 // The IME is trying to say this event is from the
2015 // system! Bad bad bad!
Romain Guy812ccbe2010-06-01 14:07:24 -07002016 event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project10592532009-03-18 17:39:46 -07002017 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002018 deliverKeyEventPostIme((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07002019 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 case FINISH_INPUT_CONNECTION: {
2021 InputMethodManager imm = InputMethodManager.peekInstance();
2022 if (imm != null) {
2023 imm.reportFinishInputConnection((InputConnection)msg.obj);
2024 }
2025 } break;
2026 case CHECK_FOCUS: {
2027 InputMethodManager imm = InputMethodManager.peekInstance();
2028 if (imm != null) {
2029 imm.checkFocus();
2030 }
2031 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07002032 case CLOSE_SYSTEM_DIALOGS: {
2033 if (mView != null) {
2034 mView.onCloseSystemDialogs((String)msg.obj);
2035 }
2036 } break;
Chris Tate91e9bb32010-10-12 12:58:43 -07002037 case DISPATCH_DRAG_EVENT:
2038 case DISPATCH_DRAG_LOCATION_EVENT: {
Christopher Tatea53146c2010-09-07 11:57:52 -07002039 handleDragEvent((DragEvent)msg.obj);
2040 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 }
2042 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002043
Jeff Brown3915bb82010-11-05 15:02:16 -07002044 private void startInputEvent(InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002045 if (mFinishedCallback != null) {
2046 Slog.w(TAG, "Received a new input event from the input queue but there is "
2047 + "already an unfinished input event in progress.");
2048 }
2049
2050 mFinishedCallback = finishedCallback;
2051 }
2052
Jeff Brown3915bb82010-11-05 15:02:16 -07002053 private void finishInputEvent(boolean handled) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002054 if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished");
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002055
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002056 if (mFinishedCallback != null) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002057 mFinishedCallback.finished(handled);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002058 mFinishedCallback = null;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002059 } else {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002060 Slog.w(TAG, "Attempted to tell the input queue that the current input event "
2061 + "is finished but there is no input event actually in progress.");
Jeff Brown46b9ac02010-04-22 18:58:52 -07002062 }
2063 }
2064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 /**
2066 * Something in the current window tells us we need to change the touch mode. For
2067 * example, we are not in touch mode, and the user touches the screen.
2068 *
2069 * If the touch mode has changed, tell the window manager, and handle it locally.
2070 *
2071 * @param inTouchMode Whether we want to be in touch mode.
2072 * @return True if the touch mode changed and focus changed was changed as a result
2073 */
2074 boolean ensureTouchMode(boolean inTouchMode) {
2075 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
2076 + "touch mode is " + mAttachInfo.mInTouchMode);
2077 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2078
2079 // tell the window manager
2080 try {
2081 sWindowSession.setInTouchMode(inTouchMode);
2082 } catch (RemoteException e) {
2083 throw new RuntimeException(e);
2084 }
2085
2086 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07002087 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
2089
2090 /**
2091 * Ensure that the touch mode for this window is set, and if it is changing,
2092 * take the appropriate action.
2093 * @param inTouchMode Whether we want to be in touch mode.
2094 * @return True if the touch mode changed and focus changed was changed as a result
2095 */
Romain Guy2d4cff62010-04-09 15:39:00 -07002096 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002097 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
2098 + "touch mode is " + mAttachInfo.mInTouchMode);
2099
2100 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2101
2102 mAttachInfo.mInTouchMode = inTouchMode;
2103 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
2104
Romain Guy2d4cff62010-04-09 15:39:00 -07002105 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 }
2107
2108 private boolean enterTouchMode() {
2109 if (mView != null) {
2110 if (mView.hasFocus()) {
2111 // note: not relying on mFocusedView here because this could
2112 // be when the window is first being added, and mFocused isn't
2113 // set yet.
2114 final View focused = mView.findFocus();
2115 if (focused != null && !focused.isFocusableInTouchMode()) {
2116
2117 final ViewGroup ancestorToTakeFocus =
2118 findAncestorToTakeFocusInTouchMode(focused);
2119 if (ancestorToTakeFocus != null) {
2120 // there is an ancestor that wants focus after its descendants that
2121 // is focusable in touch mode.. give it focus
2122 return ancestorToTakeFocus.requestFocus();
2123 } else {
2124 // nothing appropriate to have focus in touch mode, clear it out
2125 mView.unFocus();
2126 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
2127 mFocusedView = null;
2128 return true;
2129 }
2130 }
2131 }
2132 }
2133 return false;
2134 }
2135
2136
2137 /**
2138 * Find an ancestor of focused that wants focus after its descendants and is
2139 * focusable in touch mode.
2140 * @param focused The currently focused view.
2141 * @return An appropriate view, or null if no such view exists.
2142 */
2143 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
2144 ViewParent parent = focused.getParent();
2145 while (parent instanceof ViewGroup) {
2146 final ViewGroup vgParent = (ViewGroup) parent;
2147 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
2148 && vgParent.isFocusableInTouchMode()) {
2149 return vgParent;
2150 }
2151 if (vgParent.isRootNamespace()) {
2152 return null;
2153 } else {
2154 parent = vgParent.getParent();
2155 }
2156 }
2157 return null;
2158 }
2159
2160 private boolean leaveTouchMode() {
2161 if (mView != null) {
2162 if (mView.hasFocus()) {
2163 // i learned the hard way to not trust mFocusedView :)
2164 mFocusedView = mView.findFocus();
2165 if (!(mFocusedView instanceof ViewGroup)) {
2166 // some view has focus, let it keep it
2167 return false;
2168 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2169 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2170 // some view group has focus, and doesn't prefer its children
2171 // over itself for focus, so let them keep it.
2172 return false;
2173 }
2174 }
2175
2176 // find the best view to give focus to in this brave new non-touch-mode
2177 // world
2178 final View focused = focusSearch(null, View.FOCUS_DOWN);
2179 if (focused != null) {
2180 return focused.requestFocus(View.FOCUS_DOWN);
2181 }
2182 }
2183 return false;
2184 }
2185
Jeff Brown3915bb82010-11-05 15:02:16 -07002186 private void deliverPointerEvent(MotionEvent event, boolean sendDone) {
2187 // If there is no view, then the event will not be handled.
2188 if (mView == null || !mAdded) {
2189 finishPointerEvent(event, sendDone, false);
2190 return;
2191 }
2192
2193 // Translate the pointer event for compatibility, if needed.
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002194 if (mTranslator != null) {
2195 mTranslator.translateEventInScreenToAppWindow(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196 }
2197
Jeff Brown3915bb82010-11-05 15:02:16 -07002198 // Enter touch mode on the down.
2199 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
2200 if (isDown) {
2201 ensureTouchMode(true);
2202 }
2203 if(Config.LOGV) {
2204 captureMotionLog("captureDispatchPointer", event);
2205 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002206
Jeff Brown3915bb82010-11-05 15:02:16 -07002207 // Offset the scroll position.
2208 if (mCurScrollY != 0) {
2209 event.offsetLocation(0, mCurScrollY);
2210 }
2211 if (MEASURE_LATENCY) {
2212 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
2213 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002214
Jeff Brown3915bb82010-11-05 15:02:16 -07002215 // Remember the touch position for possible drag-initiation.
2216 mLastTouchPoint.x = event.getRawX();
2217 mLastTouchPoint.y = event.getRawY();
2218
2219 // Dispatch touch to view hierarchy.
2220 boolean handled = mView.dispatchTouchEvent(event);
2221 if (MEASURE_LATENCY) {
2222 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
2223 }
2224 if (handled) {
2225 finishPointerEvent(event, sendDone, true);
2226 return;
2227 }
2228
2229 // Apply edge slop and try again, if appropriate.
2230 final int edgeFlags = event.getEdgeFlags();
2231 if (edgeFlags != 0 && mView instanceof ViewGroup) {
2232 final int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
2233 int direction = View.FOCUS_UP;
2234 int x = (int)event.getX();
2235 int y = (int)event.getY();
2236 final int[] deltas = new int[2];
2237
2238 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
2239 direction = View.FOCUS_DOWN;
2240 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2241 deltas[0] = edgeSlop;
2242 x += edgeSlop;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002243 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002244 deltas[0] = -edgeSlop;
2245 x -= edgeSlop;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002246 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002247 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
2248 direction = View.FOCUS_UP;
2249 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2250 deltas[0] = edgeSlop;
2251 x += edgeSlop;
2252 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2253 deltas[0] = -edgeSlop;
2254 x -= edgeSlop;
2255 }
2256 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2257 direction = View.FOCUS_RIGHT;
2258 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2259 direction = View.FOCUS_LEFT;
2260 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002261
Jeff Brown3915bb82010-11-05 15:02:16 -07002262 View nearest = FocusFinder.getInstance().findNearestTouchable(
2263 ((ViewGroup) mView), x, y, direction, deltas);
2264 if (nearest != null) {
2265 event.offsetLocation(deltas[0], deltas[1]);
2266 event.setEdgeFlags(0);
2267 if (mView.dispatchTouchEvent(event)) {
2268 finishPointerEvent(event, sendDone, true);
2269 return;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002270 }
2271 }
2272 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002273
2274 // Pointer event was unhandled.
2275 finishPointerEvent(event, sendDone, false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002276 }
2277
Jeff Brown3915bb82010-11-05 15:02:16 -07002278 private void finishPointerEvent(MotionEvent event, boolean sendDone, boolean handled) {
2279 event.recycle();
2280 if (sendDone) {
2281 finishInputEvent(handled);
2282 }
2283 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
2284 }
2285
2286 private void deliverTrackballEvent(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2288
Jeff Brown3915bb82010-11-05 15:02:16 -07002289 // If there is no view, then the event will not be handled.
2290 if (mView == null || !mAdded) {
2291 finishTrackballEvent(event, sendDone, false);
2292 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 }
2294
Jeff Brown3915bb82010-11-05 15:02:16 -07002295 // Deliver the trackball event to the view.
2296 if (mView.dispatchTrackballEvent(event)) {
2297 // If we reach this, we delivered a trackball event to mView and
2298 // mView consumed it. Because we will not translate the trackball
2299 // event into a key event, touch mode will not exit, so we exit
2300 // touch mode here.
2301 ensureTouchMode(false);
2302
2303 finishTrackballEvent(event, sendDone, true);
2304 mLastTrackballTime = Integer.MIN_VALUE;
2305 return;
2306 }
2307
2308 // Translate the trackball event into DPAD keys and try to deliver those.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 final TrackballAxis x = mTrackballAxisX;
2310 final TrackballAxis y = mTrackballAxisY;
2311
2312 long curTime = SystemClock.uptimeMillis();
Jeff Brown3915bb82010-11-05 15:02:16 -07002313 if ((mLastTrackballTime + MAX_TRACKBALL_DELAY) < curTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 // It has been too long since the last movement,
2315 // so restart at the beginning.
2316 x.reset(0);
2317 y.reset(0);
2318 mLastTrackballTime = curTime;
2319 }
2320
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002321 final int action = event.getAction();
Jeff Brown49ed71d2010-12-06 17:13:33 -08002322 final int metaState = event.getMetaState();
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002323 switch (action) {
2324 case MotionEvent.ACTION_DOWN:
2325 x.reset(2);
2326 y.reset(2);
2327 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002328 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
2329 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2330 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002331 break;
2332 case MotionEvent.ACTION_UP:
2333 x.reset(2);
2334 y.reset(2);
2335 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002336 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
2337 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2338 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002339 break;
2340 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002341
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002342 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2343 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2344 + " move=" + event.getX()
2345 + " / Y=" + y.position + " step="
2346 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2347 + " move=" + event.getY());
2348 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2349 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002351 // Generate DPAD events based on the trackball movement.
2352 // We pick the axis that has moved the most as the direction of
2353 // the DPAD. When we generate DPAD events for one axis, then the
2354 // other axis is reset -- we don't want to perform DPAD jumps due
2355 // to slight movements in the trackball when making major movements
2356 // along the other axis.
2357 int keycode = 0;
2358 int movement = 0;
2359 float accel = 1;
2360 if (xOff > yOff) {
2361 movement = x.generate((2/event.getXPrecision()));
2362 if (movement != 0) {
2363 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2364 : KeyEvent.KEYCODE_DPAD_LEFT;
2365 accel = x.acceleration;
2366 y.reset(2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002368 } else if (yOff > 0) {
2369 movement = y.generate((2/event.getYPrecision()));
2370 if (movement != 0) {
2371 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2372 : KeyEvent.KEYCODE_DPAD_UP;
2373 accel = y.acceleration;
2374 x.reset(2);
2375 }
2376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002378 if (keycode != 0) {
2379 if (movement < 0) movement = -movement;
2380 int accelMovement = (int)(movement * accel);
2381 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2382 + " accelMovement=" + accelMovement
2383 + " accel=" + accel);
2384 if (accelMovement > movement) {
2385 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2386 + keycode);
2387 movement--;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002388 int repeatCount = accelMovement - movement;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002389 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002390 KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState,
2391 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2392 InputDevice.SOURCE_KEYBOARD), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002393 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002394 while (movement > 0) {
2395 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2396 + keycode);
2397 movement--;
2398 curTime = SystemClock.uptimeMillis();
2399 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002400 KeyEvent.ACTION_DOWN, keycode, 0, metaState,
2401 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2402 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002403 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002404 KeyEvent.ACTION_UP, keycode, 0, metaState,
2405 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2406 InputDevice.SOURCE_KEYBOARD), false);
2407 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002408 mLastTrackballTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002409 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002410
2411 // Unfortunately we can't tell whether the application consumed the keys, so
2412 // we always consider the trackball event handled.
2413 finishTrackballEvent(event, sendDone, true);
2414 }
2415
2416 private void finishTrackballEvent(MotionEvent event, boolean sendDone, boolean handled) {
2417 event.recycle();
2418 if (sendDone) {
2419 finishInputEvent(handled);
2420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 }
2422
2423 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002424 * Returns true if the key is used for keyboard navigation.
2425 * @param keyEvent The key event.
2426 * @return True if the key is used for keyboard navigation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08002428 private static boolean isNavigationKey(KeyEvent keyEvent) {
2429 switch (keyEvent.getKeyCode()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 case KeyEvent.KEYCODE_DPAD_LEFT:
2431 case KeyEvent.KEYCODE_DPAD_RIGHT:
2432 case KeyEvent.KEYCODE_DPAD_UP:
2433 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002434 case KeyEvent.KEYCODE_DPAD_CENTER:
2435 case KeyEvent.KEYCODE_PAGE_UP:
2436 case KeyEvent.KEYCODE_PAGE_DOWN:
2437 case KeyEvent.KEYCODE_MOVE_HOME:
2438 case KeyEvent.KEYCODE_MOVE_END:
2439 case KeyEvent.KEYCODE_TAB:
2440 case KeyEvent.KEYCODE_SPACE:
2441 case KeyEvent.KEYCODE_ENTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 return true;
2443 }
2444 return false;
2445 }
2446
2447 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002448 * Returns true if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002449 * @param keyEvent The key event.
Jeff Brown4e6319b2010-12-13 10:36:51 -08002450 * @return True if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002451 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08002452 private static boolean isTypingKey(KeyEvent keyEvent) {
2453 return keyEvent.getUnicodeChar() > 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002454 }
2455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002457 * 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 -08002458 * @param event The key event.
2459 * @return Whether this key event should be consumed (meaning the act of
2460 * leaving touch mode alone is considered the event).
2461 */
2462 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08002463 // Only relevant in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002464 if (!mAttachInfo.mInTouchMode) {
2465 return false;
2466 }
2467
Jeff Brown4e6319b2010-12-13 10:36:51 -08002468 // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP.
2469 final int action = event.getAction();
2470 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002471 return false;
2472 }
2473
Jeff Brown4e6319b2010-12-13 10:36:51 -08002474 // Don't leave touch mode if the IME told us not to.
2475 if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2476 return false;
2477 }
2478
2479 // If the key can be used for keyboard navigation then leave touch mode
2480 // and select a focused view if needed (in ensureTouchMode).
2481 // When a new focused view is selected, we consume the navigation key because
2482 // navigation doesn't make much sense unless a view already has focus so
2483 // the key's purpose is to set focus.
2484 if (isNavigationKey(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002485 return ensureTouchMode(false);
2486 }
Jeff Brown4e6319b2010-12-13 10:36:51 -08002487
2488 // If the key can be used for typing then leave touch mode
2489 // and select a focused view if needed (in ensureTouchMode).
2490 // Always allow the view to process the typing key.
2491 if (isTypingKey(event)) {
2492 ensureTouchMode(false);
2493 return false;
2494 }
2495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002496 return false;
2497 }
2498
2499 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002500 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 */
2502 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002503 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 if (ev == null ||
2505 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2506 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002507 }
Romain Guy8506ab42009-06-11 17:35:47 -07002508
2509 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 sb.append(ev.getDownTime()).append(',');
2511 sb.append(ev.getEventTime()).append(',');
2512 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002513 sb.append(ev.getX()).append(',');
2514 sb.append(ev.getY()).append(',');
2515 sb.append(ev.getPressure()).append(',');
2516 sb.append(ev.getSize()).append(',');
2517 sb.append(ev.getMetaState()).append(',');
2518 sb.append(ev.getXPrecision()).append(',');
2519 sb.append(ev.getYPrecision()).append(',');
2520 sb.append(ev.getDeviceId()).append(',');
2521 sb.append(ev.getEdgeFlags());
2522 Log.d(TAG, sb.toString());
2523 }
2524 /**
2525 * log motion events
2526 */
2527 private static void captureKeyLog(String subTag, KeyEvent ev) {
2528 //check dynamic switch
2529 if (ev == null ||
2530 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2531 return;
2532 }
2533 StringBuilder sb = new StringBuilder(subTag + ": ");
2534 sb.append(ev.getDownTime()).append(',');
2535 sb.append(ev.getEventTime()).append(',');
2536 sb.append(ev.getAction()).append(',');
2537 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538 sb.append(ev.getRepeatCount()).append(',');
2539 sb.append(ev.getMetaState()).append(',');
2540 sb.append(ev.getDeviceId()).append(',');
2541 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002542 Log.d(TAG, sb.toString());
2543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544
2545 int enqueuePendingEvent(Object event, boolean sendDone) {
2546 int seq = mPendingEventSeq+1;
2547 if (seq < 0) seq = 0;
2548 mPendingEventSeq = seq;
2549 mPendingEvents.put(seq, event);
2550 return sendDone ? seq : -seq;
2551 }
2552
2553 Object retrievePendingEvent(int seq) {
2554 if (seq < 0) seq = -seq;
2555 Object event = mPendingEvents.get(seq);
2556 if (event != null) {
2557 mPendingEvents.remove(seq);
2558 }
2559 return event;
2560 }
Romain Guy8506ab42009-06-11 17:35:47 -07002561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002563 // If there is no view, then the event will not be handled.
2564 if (mView == null || !mAdded) {
2565 finishKeyEvent(event, sendDone, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002566 return;
2567 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002568
2569 if (LOCAL_LOGV) Log.v(TAG, "Dispatching key " + event + " to " + mView);
2570
2571 // Perform predispatching before the IME.
2572 if (mView.dispatchKeyEventPreIme(event)) {
2573 finishKeyEvent(event, sendDone, true);
2574 return;
2575 }
2576
2577 // Dispatch to the IME before propagating down the view hierarchy.
2578 // The IME will eventually call back into handleFinishedEvent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002579 if (mLastWasImTarget) {
2580 InputMethodManager imm = InputMethodManager.peekInstance();
Jeff Brown3915bb82010-11-05 15:02:16 -07002581 if (imm != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002582 int seq = enqueuePendingEvent(event, sendDone);
2583 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2584 + seq + " event=" + event);
Jeff Brown3915bb82010-11-05 15:02:16 -07002585 imm.dispatchKeyEvent(mView.getContext(), seq, event, mInputMethodCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 return;
2587 }
2588 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002589
2590 // Not dispatching to IME, continue with post IME actions.
2591 deliverKeyEventPostIme(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002592 }
2593
Jeff Brown3915bb82010-11-05 15:02:16 -07002594 private void handleFinishedEvent(int seq, boolean handled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002595 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2596 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2597 + " handled=" + handled + " event=" + event);
2598 if (event != null) {
2599 final boolean sendDone = seq >= 0;
Jeff Brown3915bb82010-11-05 15:02:16 -07002600 if (handled) {
2601 finishKeyEvent(event, sendDone, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 } else {
Jeff Brown3915bb82010-11-05 15:02:16 -07002603 deliverKeyEventPostIme(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002604 }
2605 }
2606 }
Romain Guy8506ab42009-06-11 17:35:47 -07002607
Jeff Brown3915bb82010-11-05 15:02:16 -07002608 private void deliverKeyEventPostIme(KeyEvent event, boolean sendDone) {
2609 // If the view went away, then the event will not be handled.
2610 if (mView == null || !mAdded) {
2611 finishKeyEvent(event, sendDone, false);
2612 return;
2613 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002614
Jeff Brown3915bb82010-11-05 15:02:16 -07002615 // If the key's purpose is to exit touch mode then we consume it and consider it handled.
2616 if (checkForLeavingTouchModeAndConsume(event)) {
2617 finishKeyEvent(event, sendDone, true);
2618 return;
2619 }
Romain Guy8506ab42009-06-11 17:35:47 -07002620
Jeff Brown3915bb82010-11-05 15:02:16 -07002621 if (Config.LOGV) {
2622 captureKeyLog("captureDispatchKeyEvent", event);
2623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624
Jeff Brown90655042010-12-02 13:50:46 -08002625 // Make sure the fallback event policy sees all keys that will be delivered to the
2626 // view hierarchy.
2627 mFallbackEventHandler.preDispatchKeyEvent(event);
2628
Jeff Brown3915bb82010-11-05 15:02:16 -07002629 // Deliver the key to the view hierarchy.
2630 if (mView.dispatchKeyEvent(event)) {
2631 finishKeyEvent(event, sendDone, true);
2632 return;
2633 }
Joe Onorato86f67862010-11-05 18:57:34 -07002634
Jeff Brownc1df9072010-12-21 16:38:50 -08002635 // If the Control modifier is held, try to interpret the key as a shortcut.
2636 if (event.getAction() == KeyEvent.ACTION_UP
2637 && event.isCtrlPressed()
2638 && !KeyEvent.isModifierKey(event.getKeyCode())) {
2639 if (mView.dispatchKeyShortcutEvent(event)) {
2640 finishKeyEvent(event, sendDone, true);
2641 return;
2642 }
2643 }
2644
Jeff Brown3915bb82010-11-05 15:02:16 -07002645 // Apply the fallback event policy.
2646 if (mFallbackEventHandler.dispatchKeyEvent(event)) {
2647 finishKeyEvent(event, sendDone, true);
2648 return;
2649 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002650
Jeff Brown3915bb82010-11-05 15:02:16 -07002651 // Handle automatic focus changes.
2652 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2653 int direction = 0;
2654 switch (event.getKeyCode()) {
2655 case KeyEvent.KEYCODE_DPAD_LEFT:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002656 if (event.hasNoModifiers()) {
2657 direction = View.FOCUS_LEFT;
2658 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002659 break;
2660 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002661 if (event.hasNoModifiers()) {
2662 direction = View.FOCUS_RIGHT;
2663 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002664 break;
2665 case KeyEvent.KEYCODE_DPAD_UP:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002666 if (event.hasNoModifiers()) {
2667 direction = View.FOCUS_UP;
2668 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002669 break;
2670 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002671 if (event.hasNoModifiers()) {
2672 direction = View.FOCUS_DOWN;
2673 }
2674 break;
2675 case KeyEvent.KEYCODE_TAB:
2676 if (event.hasNoModifiers()) {
2677 direction = View.FOCUS_FORWARD;
2678 } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
2679 direction = View.FOCUS_BACKWARD;
2680 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002681 break;
2682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002683
Jeff Brown3915bb82010-11-05 15:02:16 -07002684 if (direction != 0) {
2685 View focused = mView != null ? mView.findFocus() : null;
2686 if (focused != null) {
2687 View v = focused.focusSearch(direction);
2688 if (v != null && v != focused) {
2689 // do the math the get the interesting rect
2690 // of previous focused into the coord system of
2691 // newly focused view
2692 focused.getFocusedRect(mTempRect);
2693 if (mView instanceof ViewGroup) {
2694 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2695 focused, mTempRect);
2696 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2697 v, mTempRect);
2698 }
2699 if (v.requestFocus(direction, mTempRect)) {
2700 playSoundEffect(
2701 SoundEffectConstants.getContantForFocusDirection(direction));
2702 finishKeyEvent(event, sendDone, true);
2703 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 }
2705 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002706
2707 // Give the focused view a last chance to handle the dpad key.
2708 if (mView.dispatchUnhandledMove(focused, direction)) {
2709 finishKeyEvent(event, sendDone, true);
2710 return;
2711 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712 }
2713 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715
Jeff Brown3915bb82010-11-05 15:02:16 -07002716 // Key was unhandled.
2717 finishKeyEvent(event, sendDone, false);
2718 }
2719
2720 private void finishKeyEvent(KeyEvent event, boolean sendDone, boolean handled) {
2721 if (sendDone) {
2722 finishInputEvent(handled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 }
2724 }
2725
Christopher Tatea53146c2010-09-07 11:57:52 -07002726 /* drag/drop */
Christopher Tate407b4e92010-11-30 17:14:08 -08002727 void setLocalDragState(Object obj) {
2728 mLocalDragState = obj;
2729 }
2730
Christopher Tatea53146c2010-09-07 11:57:52 -07002731 private void handleDragEvent(DragEvent event) {
2732 // From the root, only drag start/end/location are dispatched. entered/exited
2733 // are determined and dispatched by the viewgroup hierarchy, who then report
2734 // that back here for ultimate reporting back to the framework.
2735 if (mView != null && mAdded) {
2736 final int what = event.mAction;
2737
2738 if (what == DragEvent.ACTION_DRAG_EXITED) {
2739 // A direct EXITED event means that the window manager knows we've just crossed
2740 // a window boundary, so the current drag target within this one must have
2741 // just been exited. Send it the usual notifications and then we're done
2742 // for now.
Chris Tate9d1ab882010-11-02 15:55:39 -07002743 mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002744 } else {
2745 // Cache the drag description when the operation starts, then fill it in
2746 // on subsequent calls as a convenience
2747 if (what == DragEvent.ACTION_DRAG_STARTED) {
Chris Tate9d1ab882010-11-02 15:55:39 -07002748 mCurrentDragView = null; // Start the current-recipient tracking
Christopher Tatea53146c2010-09-07 11:57:52 -07002749 mDragDescription = event.mClipDescription;
2750 } else {
2751 event.mClipDescription = mDragDescription;
2752 }
2753
2754 // For events with a [screen] location, translate into window coordinates
2755 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
2756 mDragPoint.set(event.mX, event.mY);
2757 if (mTranslator != null) {
2758 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
2759 }
2760
2761 if (mCurScrollY != 0) {
2762 mDragPoint.offset(0, mCurScrollY);
2763 }
2764
2765 event.mX = mDragPoint.x;
2766 event.mY = mDragPoint.y;
2767 }
2768
2769 // Remember who the current drag target is pre-dispatch
2770 final View prevDragView = mCurrentDragView;
2771
2772 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07002773 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002774
2775 // If we changed apparent drag target, tell the OS about it
2776 if (prevDragView != mCurrentDragView) {
2777 try {
2778 if (prevDragView != null) {
2779 sWindowSession.dragRecipientExited(mWindow);
2780 }
2781 if (mCurrentDragView != null) {
2782 sWindowSession.dragRecipientEntered(mWindow);
2783 }
2784 } catch (RemoteException e) {
2785 Slog.e(TAG, "Unable to note drag target change");
2786 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002787 }
Chris Tated4533f12010-10-19 15:15:08 -07002788
Christopher Tate407b4e92010-11-30 17:14:08 -08002789 // Report the drop result when we're done
Chris Tated4533f12010-10-19 15:15:08 -07002790 if (what == DragEvent.ACTION_DROP) {
2791 try {
2792 Log.i(TAG, "Reporting drop result: " + result);
2793 sWindowSession.reportDropResult(mWindow, result);
2794 } catch (RemoteException e) {
2795 Log.e(TAG, "Unable to report drop result");
2796 }
2797 }
Christopher Tate407b4e92010-11-30 17:14:08 -08002798
2799 // When the drag operation ends, release any local state object
2800 // that may have been in use
2801 if (what == DragEvent.ACTION_DRAG_ENDED) {
2802 setLocalDragState(null);
2803 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002804 }
2805 }
2806 event.recycle();
2807 }
2808
Christopher Tate2c095f32010-10-04 14:13:40 -07002809 public void getLastTouchPoint(Point outLocation) {
2810 outLocation.x = (int) mLastTouchPoint.x;
2811 outLocation.y = (int) mLastTouchPoint.y;
2812 }
2813
Chris Tate9d1ab882010-11-02 15:55:39 -07002814 public void setDragFocus(View newDragTarget) {
Christopher Tatea53146c2010-09-07 11:57:52 -07002815 if (mCurrentDragView != newDragTarget) {
Chris Tate048691c2010-10-12 17:39:18 -07002816 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07002817 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002818 }
2819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002820 private AudioManager getAudioManager() {
2821 if (mView == null) {
2822 throw new IllegalStateException("getAudioManager called when there is no mView");
2823 }
2824 if (mAudioManager == null) {
2825 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2826 }
2827 return mAudioManager;
2828 }
2829
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002830 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2831 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002832
2833 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002834 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002835 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002836 restore = true;
2837 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002838 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002839 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002840 if (params != null) {
2841 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002842 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002843 mPendingConfiguration.seq = 0;
Dianne Hackbornf123e492010-09-24 11:16:23 -07002844 //Log.d(TAG, ">>>>>> CALLING relayout");
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002845 int relayoutResult = sWindowSession.relayout(
2846 mWindow, params,
Dianne Hackborn189ee182010-12-02 21:48:53 -08002847 (int) (mView.getMeasuredWidth() * appScale + 0.5f),
2848 (int) (mView.getMeasuredHeight() * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002849 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002850 mPendingContentInsets, mPendingVisibleInsets,
2851 mPendingConfiguration, mSurface);
Dianne Hackbornf123e492010-09-24 11:16:23 -07002852 //Log.d(TAG, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002853 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002854 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002855 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002856
2857 if (mTranslator != null) {
2858 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2859 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2860 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002861 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002862 return relayoutResult;
2863 }
Romain Guy8506ab42009-06-11 17:35:47 -07002864
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002865 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002866 * {@inheritDoc}
2867 */
2868 public void playSoundEffect(int effectId) {
2869 checkThread();
2870
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002871 try {
2872 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002873
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002874 switch (effectId) {
2875 case SoundEffectConstants.CLICK:
2876 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2877 return;
2878 case SoundEffectConstants.NAVIGATION_DOWN:
2879 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2880 return;
2881 case SoundEffectConstants.NAVIGATION_LEFT:
2882 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2883 return;
2884 case SoundEffectConstants.NAVIGATION_RIGHT:
2885 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2886 return;
2887 case SoundEffectConstants.NAVIGATION_UP:
2888 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2889 return;
2890 default:
2891 throw new IllegalArgumentException("unknown effect id " + effectId +
2892 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2893 }
2894 } catch (IllegalStateException e) {
2895 // Exception thrown by getAudioManager() when mView is null
2896 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2897 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002898 }
2899 }
2900
2901 /**
2902 * {@inheritDoc}
2903 */
2904 public boolean performHapticFeedback(int effectId, boolean always) {
2905 try {
2906 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2907 } catch (RemoteException e) {
2908 return false;
2909 }
2910 }
2911
2912 /**
2913 * {@inheritDoc}
2914 */
2915 public View focusSearch(View focused, int direction) {
2916 checkThread();
2917 if (!(mView instanceof ViewGroup)) {
2918 return null;
2919 }
2920 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2921 }
2922
2923 public void debug() {
2924 mView.debug();
2925 }
2926
2927 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002928 if (immediate) {
2929 doDie();
2930 } else {
2931 sendEmptyMessage(DIE);
2932 }
2933 }
2934
2935 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 checkThread();
Jeff Brownb75fa302010-07-15 23:47:29 -07002937 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 synchronized (this) {
2939 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07002940 destroyHardwareRenderer();
2941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002942 int viewVisibility = mView.getVisibility();
2943 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2944 if (mWindowAttributesChanged || viewVisibilityChanged) {
2945 // If layout params have been changed, first give them
2946 // to the window manager to make sure it has the correct
2947 // animation info.
2948 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002949 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2950 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002951 sWindowSession.finishDrawing(mWindow);
2952 }
2953 } catch (RemoteException e) {
2954 }
2955 }
2956
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002957 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002958 }
2959 if (mAdded) {
2960 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002961 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 }
2963 }
2964 }
2965
Romain Guy29d89972010-09-22 16:10:57 -07002966 private void destroyHardwareRenderer() {
Romain Guyb051e892010-09-28 19:09:36 -07002967 if (mAttachInfo.mHardwareRenderer != null) {
2968 mAttachInfo.mHardwareRenderer.destroy(true);
2969 mAttachInfo.mHardwareRenderer = null;
Romain Guy29d89972010-09-22 16:10:57 -07002970 mAttachInfo.mHardwareAccelerated = false;
2971 }
2972 }
2973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 public void dispatchFinishedEvent(int seq, boolean handled) {
2975 Message msg = obtainMessage(FINISHED_EVENT);
2976 msg.arg1 = seq;
2977 msg.arg2 = handled ? 1 : 0;
2978 sendMessage(msg);
2979 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002982 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2984 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2985 + " visibleInsets=" + visibleInsets.toShortString()
2986 + " reportDraw=" + reportDraw);
2987 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002988 if (mTranslator != null) {
2989 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2990 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2991 w *= mTranslator.applicationInvertedScale;
2992 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002993 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002994 msg.arg1 = w;
2995 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002996 ResizedInfo ri = new ResizedInfo();
2997 ri.coveredInsets = new Rect(coveredInsets);
2998 ri.visibleInsets = new Rect(visibleInsets);
2999 ri.newConfig = newConfig;
3000 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 sendMessage(msg);
3002 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07003003
Jeff Brown3915bb82010-11-05 15:02:16 -07003004 private InputQueue.FinishedCallback mFinishedCallback;
Jeff Brown46b9ac02010-04-22 18:58:52 -07003005
3006 private final InputHandler mInputHandler = new InputHandler() {
Jeff Brown3915bb82010-11-05 15:02:16 -07003007 public void handleKey(KeyEvent event, InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003008 startInputEvent(finishedCallback);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003009 dispatchKey(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003010 }
3011
Jeff Brown3915bb82010-11-05 15:02:16 -07003012 public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003013 startInputEvent(finishedCallback);
3014 dispatchMotion(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003015 }
3016 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003017
3018 public void dispatchKey(KeyEvent event) {
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003019 dispatchKey(event, false);
3020 }
3021
3022 private void dispatchKey(KeyEvent event, boolean sendDone) {
3023 //noinspection ConstantConditions
3024 if (false && event.getAction() == KeyEvent.ACTION_DOWN) {
3025 if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
Romain Guy812ccbe2010-06-01 14:07:24 -07003026 if (DBG) Log.d("keydisp", "===================================================");
3027 if (DBG) Log.d("keydisp", "Focused view Hierarchy is:");
3028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003029 debug();
3030
Romain Guy812ccbe2010-06-01 14:07:24 -07003031 if (DBG) Log.d("keydisp", "===================================================");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 }
3033 }
3034
3035 Message msg = obtainMessage(DISPATCH_KEY);
3036 msg.obj = event;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003037 msg.arg1 = sendDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003038
3039 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07003040 TAG, "sending key " + event + " to " + mView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003041
3042 sendMessageAtTime(msg, event.getEventTime());
3043 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07003044
3045 public void dispatchMotion(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003046 dispatchMotion(event, false);
3047 }
3048
3049 private void dispatchMotion(MotionEvent event, boolean sendDone) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07003050 int source = event.getSource();
3051 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003052 dispatchPointer(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07003053 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003054 dispatchTrackball(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07003055 } else {
3056 // TODO
3057 Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event);
Jeff Brown93ed4e32010-09-23 13:51:48 -07003058 if (sendDone) {
Jeff Brown3915bb82010-11-05 15:02:16 -07003059 finishInputEvent(false);
Jeff Brown93ed4e32010-09-23 13:51:48 -07003060 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07003061 }
3062 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003064 public void dispatchPointer(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003065 dispatchPointer(event, false);
3066 }
3067
3068 private void dispatchPointer(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 Message msg = obtainMessage(DISPATCH_POINTER);
3070 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07003071 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003072 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003073 }
3074
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003075 public void dispatchTrackball(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003076 dispatchTrackball(event, false);
3077 }
3078
3079 private void dispatchTrackball(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 Message msg = obtainMessage(DISPATCH_TRACKBALL);
3081 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07003082 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003083 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003084 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003086 public void dispatchAppVisibility(boolean visible) {
3087 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
3088 msg.arg1 = visible ? 1 : 0;
3089 sendMessage(msg);
3090 }
3091
3092 public void dispatchGetNewSurface() {
3093 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
3094 sendMessage(msg);
3095 }
3096
3097 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3098 Message msg = Message.obtain();
3099 msg.what = WINDOW_FOCUS_CHANGED;
3100 msg.arg1 = hasFocus ? 1 : 0;
3101 msg.arg2 = inTouchMode ? 1 : 0;
3102 sendMessage(msg);
3103 }
3104
Dianne Hackbornffa42482009-09-23 22:20:11 -07003105 public void dispatchCloseSystemDialogs(String reason) {
3106 Message msg = Message.obtain();
3107 msg.what = CLOSE_SYSTEM_DIALOGS;
3108 msg.obj = reason;
3109 sendMessage(msg);
3110 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003111
3112 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07003113 final int what;
3114 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
3115 what = DISPATCH_DRAG_LOCATION_EVENT;
3116 removeMessages(what);
3117 } else {
3118 what = DISPATCH_DRAG_EVENT;
3119 }
Christopher Tate407b4e92010-11-30 17:14:08 -08003120 event.mLocalState = mLocalDragState; // only present when this app called startDrag()
Chris Tate91e9bb32010-10-12 12:58:43 -07003121 Message msg = obtainMessage(what, event);
Christopher Tatea53146c2010-09-07 11:57:52 -07003122 sendMessage(msg);
3123 }
3124
svetoslavganov75986cf2009-05-14 22:28:01 -07003125 /**
3126 * The window is getting focus so if there is anything focused/selected
3127 * send an {@link AccessibilityEvent} to announce that.
3128 */
3129 private void sendAccessibilityEvents() {
3130 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
3131 return;
3132 }
3133 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3134 View focusedView = mView.findFocus();
3135 if (focusedView != null && focusedView != mView) {
3136 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3137 }
3138 }
3139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 public boolean showContextMenuForChild(View originalView) {
3141 return false;
3142 }
3143
Adam Powell6e346362010-07-23 10:18:23 -07003144 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
3145 return null;
3146 }
3147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 public void createContextMenu(ContextMenu menu) {
3149 }
3150
3151 public void childDrawableStateChanged(View child) {
3152 }
3153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 void checkThread() {
3155 if (mThread != Thread.currentThread()) {
3156 throw new CalledFromWrongThreadException(
3157 "Only the original thread that created a view hierarchy can touch its views.");
3158 }
3159 }
3160
3161 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
3162 // ViewRoot never intercepts touch event, so this can be a no-op
3163 }
3164
3165 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
3166 boolean immediate) {
3167 return scrollToRectOrFocus(rectangle, immediate);
3168 }
Romain Guy8506ab42009-06-11 17:35:47 -07003169
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07003170 class TakenSurfaceHolder extends BaseSurfaceHolder {
3171 @Override
3172 public boolean onAllowLockCanvas() {
3173 return mDrawingAllowed;
3174 }
3175
3176 @Override
3177 public void onRelayoutContainer() {
3178 // Not currently interesting -- from changing between fixed and layout size.
3179 }
3180
3181 public void setFormat(int format) {
3182 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
3183 }
3184
3185 public void setType(int type) {
3186 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
3187 }
3188
3189 @Override
3190 public void onUpdateSurface() {
3191 // We take care of format and type changes on our own.
3192 throw new IllegalStateException("Shouldn't be here");
3193 }
3194
3195 public boolean isCreating() {
3196 return mIsCreating;
3197 }
3198
3199 @Override
3200 public void setFixedSize(int width, int height) {
3201 throw new UnsupportedOperationException(
3202 "Currently only support sizing from layout");
3203 }
3204
3205 public void setKeepScreenOn(boolean screenOn) {
3206 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
3207 }
3208 }
3209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003210 static class InputMethodCallback extends IInputMethodCallback.Stub {
3211 private WeakReference<ViewRoot> mViewRoot;
3212
3213 public InputMethodCallback(ViewRoot viewRoot) {
3214 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
3215 }
Romain Guy8506ab42009-06-11 17:35:47 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 public void finishedEvent(int seq, boolean handled) {
3218 final ViewRoot viewRoot = mViewRoot.get();
3219 if (viewRoot != null) {
3220 viewRoot.dispatchFinishedEvent(seq, handled);
3221 }
3222 }
3223
3224 public void sessionCreated(IInputMethodSession session) throws RemoteException {
3225 // Stub -- not for use in the client.
3226 }
3227 }
Romain Guy8506ab42009-06-11 17:35:47 -07003228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003229 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003230 private final WeakReference<ViewRoot> mViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231
Romain Guyfb8b7632010-08-23 21:05:08 -07003232 W(ViewRoot viewRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
3234 }
3235
Romain Guyfb8b7632010-08-23 21:05:08 -07003236 public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets,
3237 boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 final ViewRoot viewRoot = mViewRoot.get();
3239 if (viewRoot != null) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003240 viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 }
3242 }
3243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003244 public void dispatchAppVisibility(boolean visible) {
3245 final ViewRoot viewRoot = mViewRoot.get();
3246 if (viewRoot != null) {
3247 viewRoot.dispatchAppVisibility(visible);
3248 }
3249 }
3250
3251 public void dispatchGetNewSurface() {
3252 final ViewRoot viewRoot = mViewRoot.get();
3253 if (viewRoot != null) {
3254 viewRoot.dispatchGetNewSurface();
3255 }
3256 }
3257
3258 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3259 final ViewRoot viewRoot = mViewRoot.get();
3260 if (viewRoot != null) {
3261 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
3262 }
3263 }
3264
3265 private static int checkCallingPermission(String permission) {
3266 if (!Process.supportsProcesses()) {
3267 return PackageManager.PERMISSION_GRANTED;
3268 }
3269
3270 try {
3271 return ActivityManagerNative.getDefault().checkPermission(
3272 permission, Binder.getCallingPid(), Binder.getCallingUid());
3273 } catch (RemoteException e) {
3274 return PackageManager.PERMISSION_DENIED;
3275 }
3276 }
3277
3278 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3279 final ViewRoot viewRoot = mViewRoot.get();
3280 if (viewRoot != null) {
3281 final View view = viewRoot.mView;
3282 if (view != null) {
3283 if (checkCallingPermission(Manifest.permission.DUMP) !=
3284 PackageManager.PERMISSION_GRANTED) {
3285 throw new SecurityException("Insufficient permissions to invoke"
3286 + " executeCommand() from pid=" + Binder.getCallingPid()
3287 + ", uid=" + Binder.getCallingUid());
3288 }
3289
3290 OutputStream clientStream = null;
3291 try {
3292 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3293 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3294 } catch (IOException e) {
3295 e.printStackTrace();
3296 } finally {
3297 if (clientStream != null) {
3298 try {
3299 clientStream.close();
3300 } catch (IOException e) {
3301 e.printStackTrace();
3302 }
3303 }
3304 }
3305 }
3306 }
3307 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003308
Dianne Hackbornffa42482009-09-23 22:20:11 -07003309 public void closeSystemDialogs(String reason) {
3310 final ViewRoot viewRoot = mViewRoot.get();
3311 if (viewRoot != null) {
3312 viewRoot.dispatchCloseSystemDialogs(reason);
3313 }
3314 }
3315
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003316 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3317 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003318 if (sync) {
3319 try {
3320 sWindowSession.wallpaperOffsetsComplete(asBinder());
3321 } catch (RemoteException e) {
3322 }
3323 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003324 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003325
3326 public void dispatchWallpaperCommand(String action, int x, int y,
3327 int z, Bundle extras, boolean sync) {
3328 if (sync) {
3329 try {
3330 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3331 } catch (RemoteException e) {
3332 }
3333 }
3334 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003335
3336 /* Drag/drop */
3337 public void dispatchDragEvent(DragEvent event) {
3338 final ViewRoot viewRoot = mViewRoot.get();
3339 if (viewRoot != null) {
3340 viewRoot.dispatchDragEvent(event);
3341 }
3342 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003343 }
3344
3345 /**
3346 * Maintains state information for a single trackball axis, generating
3347 * discrete (DPAD) movements based on raw trackball motion.
3348 */
3349 static final class TrackballAxis {
3350 /**
3351 * The maximum amount of acceleration we will apply.
3352 */
3353 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 /**
3356 * The maximum amount of time (in milliseconds) between events in order
3357 * for us to consider the user to be doing fast trackball movements,
3358 * and thus apply an acceleration.
3359 */
3360 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 /**
3363 * Scaling factor to the time (in milliseconds) between events to how
3364 * much to multiple/divide the current acceleration. When movement
3365 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3366 * FAST_MOVE_TIME it divides it.
3367 */
3368 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003370 float position;
3371 float absPosition;
3372 float acceleration = 1;
3373 long lastMoveTime = 0;
3374 int step;
3375 int dir;
3376 int nonAccelMovement;
3377
3378 void reset(int _step) {
3379 position = 0;
3380 acceleration = 1;
3381 lastMoveTime = 0;
3382 step = _step;
3383 dir = 0;
3384 }
3385
3386 /**
3387 * Add trackball movement into the state. If the direction of movement
3388 * has been reversed, the state is reset before adding the
3389 * movement (so that you don't have to compensate for any previously
3390 * collected movement before see the result of the movement in the
3391 * new direction).
3392 *
3393 * @return Returns the absolute value of the amount of movement
3394 * collected so far.
3395 */
3396 float collect(float off, long time, String axis) {
3397 long normTime;
3398 if (off > 0) {
3399 normTime = (long)(off * FAST_MOVE_TIME);
3400 if (dir < 0) {
3401 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3402 position = 0;
3403 step = 0;
3404 acceleration = 1;
3405 lastMoveTime = 0;
3406 }
3407 dir = 1;
3408 } else if (off < 0) {
3409 normTime = (long)((-off) * FAST_MOVE_TIME);
3410 if (dir > 0) {
3411 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3412 position = 0;
3413 step = 0;
3414 acceleration = 1;
3415 lastMoveTime = 0;
3416 }
3417 dir = -1;
3418 } else {
3419 normTime = 0;
3420 }
Romain Guy8506ab42009-06-11 17:35:47 -07003421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003422 // The number of milliseconds between each movement that is
3423 // considered "normal" and will not result in any acceleration
3424 // or deceleration, scaled by the offset we have here.
3425 if (normTime > 0) {
3426 long delta = time - lastMoveTime;
3427 lastMoveTime = time;
3428 float acc = acceleration;
3429 if (delta < normTime) {
3430 // The user is scrolling rapidly, so increase acceleration.
3431 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3432 if (scale > 1) acc *= scale;
3433 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3434 + off + " normTime=" + normTime + " delta=" + delta
3435 + " scale=" + scale + " acc=" + acc);
3436 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3437 } else {
3438 // The user is scrolling slowly, so decrease acceleration.
3439 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3440 if (scale > 1) acc /= scale;
3441 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3442 + off + " normTime=" + normTime + " delta=" + delta
3443 + " scale=" + scale + " acc=" + acc);
3444 acceleration = acc > 1 ? acc : 1;
3445 }
3446 }
3447 position += off;
3448 return (absPosition = Math.abs(position));
3449 }
3450
3451 /**
3452 * Generate the number of discrete movement events appropriate for
3453 * the currently collected trackball movement.
3454 *
3455 * @param precision The minimum movement required to generate the
3456 * first discrete movement.
3457 *
3458 * @return Returns the number of discrete movements, either positive
3459 * or negative, or 0 if there is not enough trackball movement yet
3460 * for a discrete movement.
3461 */
3462 int generate(float precision) {
3463 int movement = 0;
3464 nonAccelMovement = 0;
3465 do {
3466 final int dir = position >= 0 ? 1 : -1;
3467 switch (step) {
3468 // If we are going to execute the first step, then we want
3469 // to do this as soon as possible instead of waiting for
3470 // a full movement, in order to make things look responsive.
3471 case 0:
3472 if (absPosition < precision) {
3473 return movement;
3474 }
3475 movement += dir;
3476 nonAccelMovement += dir;
3477 step = 1;
3478 break;
3479 // If we have generated the first movement, then we need
3480 // to wait for the second complete trackball motion before
3481 // generating the second discrete movement.
3482 case 1:
3483 if (absPosition < 2) {
3484 return movement;
3485 }
3486 movement += dir;
3487 nonAccelMovement += dir;
3488 position += dir > 0 ? -2 : 2;
3489 absPosition = Math.abs(position);
3490 step = 2;
3491 break;
3492 // After the first two, we generate discrete movements
3493 // consistently with the trackball, applying an acceleration
3494 // if the trackball is moving quickly. This is a simple
3495 // acceleration on top of what we already compute based
3496 // on how quickly the wheel is being turned, to apply
3497 // a longer increasing acceleration to continuous movement
3498 // in one direction.
3499 default:
3500 if (absPosition < 1) {
3501 return movement;
3502 }
3503 movement += dir;
3504 position += dir >= 0 ? -1 : 1;
3505 absPosition = Math.abs(position);
3506 float acc = acceleration;
3507 acc *= 1.1f;
3508 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3509 break;
3510 }
3511 } while (true);
3512 }
3513 }
3514
3515 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3516 public CalledFromWrongThreadException(String msg) {
3517 super(msg);
3518 }
3519 }
3520
3521 private SurfaceHolder mHolder = new SurfaceHolder() {
3522 // we only need a SurfaceHolder for opengl. it would be nice
3523 // to implement everything else though, especially the callback
3524 // support (opengl doesn't make use of it right now, but eventually
3525 // will).
3526 public Surface getSurface() {
3527 return mSurface;
3528 }
3529
3530 public boolean isCreating() {
3531 return false;
3532 }
3533
3534 public void addCallback(Callback callback) {
3535 }
3536
3537 public void removeCallback(Callback callback) {
3538 }
3539
3540 public void setFixedSize(int width, int height) {
3541 }
3542
3543 public void setSizeFromLayout() {
3544 }
3545
3546 public void setFormat(int format) {
3547 }
3548
3549 public void setType(int type) {
3550 }
3551
3552 public void setKeepScreenOn(boolean screenOn) {
3553 }
3554
3555 public Canvas lockCanvas() {
3556 return null;
3557 }
3558
3559 public Canvas lockCanvas(Rect dirty) {
3560 return null;
3561 }
3562
3563 public void unlockCanvasAndPost(Canvas canvas) {
3564 }
3565 public Rect getSurfaceFrame() {
3566 return null;
3567 }
3568 };
3569
3570 static RunQueue getRunQueue() {
3571 RunQueue rq = sRunQueues.get();
3572 if (rq != null) {
3573 return rq;
3574 }
3575 rq = new RunQueue();
3576 sRunQueues.set(rq);
3577 return rq;
3578 }
Romain Guy8506ab42009-06-11 17:35:47 -07003579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003580 /**
3581 * @hide
3582 */
3583 static final class RunQueue {
3584 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3585
3586 void post(Runnable action) {
3587 postDelayed(action, 0);
3588 }
3589
3590 void postDelayed(Runnable action, long delayMillis) {
3591 HandlerAction handlerAction = new HandlerAction();
3592 handlerAction.action = action;
3593 handlerAction.delay = delayMillis;
3594
3595 synchronized (mActions) {
3596 mActions.add(handlerAction);
3597 }
3598 }
3599
3600 void removeCallbacks(Runnable action) {
3601 final HandlerAction handlerAction = new HandlerAction();
3602 handlerAction.action = action;
3603
3604 synchronized (mActions) {
3605 final ArrayList<HandlerAction> actions = mActions;
3606
3607 while (actions.remove(handlerAction)) {
3608 // Keep going
3609 }
3610 }
3611 }
3612
3613 void executeActions(Handler handler) {
3614 synchronized (mActions) {
3615 final ArrayList<HandlerAction> actions = mActions;
3616 final int count = actions.size();
3617
3618 for (int i = 0; i < count; i++) {
3619 final HandlerAction handlerAction = actions.get(i);
3620 handler.postDelayed(handlerAction.action, handlerAction.delay);
3621 }
3622
Romain Guy15df6702009-08-17 20:17:30 -07003623 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 }
3625 }
3626
3627 private static class HandlerAction {
3628 Runnable action;
3629 long delay;
3630
3631 @Override
3632 public boolean equals(Object o) {
3633 if (this == o) return true;
3634 if (o == null || getClass() != o.getClass()) return false;
3635
3636 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003637 return !(action != null ? !action.equals(that.action) : that.action != null);
3638
3639 }
3640
3641 @Override
3642 public int hashCode() {
3643 int result = action != null ? action.hashCode() : 0;
3644 result = 31 * result + (int) (delay ^ (delay >>> 32));
3645 return result;
3646 }
3647 }
3648 }
3649
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003651}