blob: 9b897eb2213893588ff8cbdc76c97a3c5cf464be [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
Romain Guy529b60a2010-08-03 18:05:47 -0700348 enableHardwareAcceleration(attrs);
Romain Guye4d01122010-06-16 18:44:05 -0700349
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700350 if (view instanceof RootViewSurfaceTaker) {
351 mSurfaceHolderCallback =
352 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
353 if (mSurfaceHolderCallback != null) {
354 mSurfaceHolder = new TakenSurfaceHolder();
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700355 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700356 }
357 }
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700358 Resources resources = mView.getContext().getResources();
359 CompatibilityInfo compatibilityInfo = resources.getCompatibilityInfo();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700360 mTranslator = compatibilityInfo.getTranslator();
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700361
362 if (mTranslator != null || !compatibilityInfo.supportsScreen()) {
Mitsuru Oshima240f8a72009-07-22 20:39:14 -0700363 mSurface.setCompatibleDisplayMetrics(resources.getDisplayMetrics(),
364 mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700365 }
366
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700367 boolean restore = false;
Romain Guy35b38ce2009-10-07 13:38:55 -0700368 if (mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700369 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700370 attrs.backup();
371 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700372 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700373 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs);
374
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700375 if (!compatibilityInfo.supportsScreen()) {
376 attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
377 }
378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 mSoftInputMode = attrs.softInputMode;
380 mWindowAttributesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 mAttachInfo.mRootView = view;
Romain Guy35b38ce2009-10-07 13:38:55 -0700382 mAttachInfo.mScalingRequired = mTranslator != null;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700383 mAttachInfo.mApplicationScale =
384 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 if (panelParentView != null) {
386 mAttachInfo.mPanelParentWindowToken
387 = panelParentView.getApplicationWindowToken();
388 }
389 mAdded = true;
390 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 // Schedule the first layout -before- adding to the window
393 // manager, to make sure we do the relayout before receiving
394 // any other events from the system.
395 requestLayout();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396 mInputChannel = new InputChannel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 try {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700398 res = sWindowSession.add(mWindow, mWindowAttributes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700399 getHostVisibility(), mAttachInfo.mContentInsets,
400 mInputChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401 } catch (RemoteException e) {
402 mAdded = false;
403 mView = null;
404 mAttachInfo.mRootView = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700405 mInputChannel = null;
Joe Onorato86f67862010-11-05 18:57:34 -0700406 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 unscheduleTraversals();
408 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700409 } finally {
410 if (restore) {
411 attrs.restore();
412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700414
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700415 if (mTranslator != null) {
416 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 mPendingContentInsets.set(mAttachInfo.mContentInsets);
419 mPendingVisibleInsets.set(0, 0, 0, 0);
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800420 if (DEBUG_LAYOUT) Log.v(TAG, "Added window " + mWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 if (res < WindowManagerImpl.ADD_OKAY) {
422 mView = null;
423 mAttachInfo.mRootView = null;
424 mAdded = false;
Joe Onorato86f67862010-11-05 18:57:34 -0700425 mFallbackEventHandler.setView(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 unscheduleTraversals();
427 switch (res) {
428 case WindowManagerImpl.ADD_BAD_APP_TOKEN:
429 case WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN:
430 throw new WindowManagerImpl.BadTokenException(
431 "Unable to add window -- token " + attrs.token
432 + " is not valid; is your activity running?");
433 case WindowManagerImpl.ADD_NOT_APP_TOKEN:
434 throw new WindowManagerImpl.BadTokenException(
435 "Unable to add window -- token " + attrs.token
436 + " is not for an application");
437 case WindowManagerImpl.ADD_APP_EXITING:
438 throw new WindowManagerImpl.BadTokenException(
439 "Unable to add window -- app for token " + attrs.token
440 + " is exiting");
441 case WindowManagerImpl.ADD_DUPLICATE_ADD:
442 throw new WindowManagerImpl.BadTokenException(
443 "Unable to add window -- window " + mWindow
444 + " has already been added");
445 case WindowManagerImpl.ADD_STARTING_NOT_NEEDED:
446 // Silently ignore -- we would have just removed it
447 // right away, anyway.
448 return;
449 case WindowManagerImpl.ADD_MULTIPLE_SINGLETON:
450 throw new WindowManagerImpl.BadTokenException(
451 "Unable to add window " + mWindow +
452 " -- another window of this type already exists");
453 case WindowManagerImpl.ADD_PERMISSION_DENIED:
454 throw new WindowManagerImpl.BadTokenException(
455 "Unable to add window " + mWindow +
456 " -- permission denied for this window type");
457 }
458 throw new RuntimeException(
459 "Unable to add window -- unknown error code " + res);
460 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700461
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700462 if (view instanceof RootViewSurfaceTaker) {
463 mInputQueueCallback =
464 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
465 }
466 if (mInputQueueCallback != null) {
467 mInputQueue = new InputQueue(mInputChannel);
468 mInputQueueCallback.onInputQueueCreated(mInputQueue);
469 } else {
470 InputQueue.registerInputChannel(mInputChannel, mInputHandler,
471 Looper.myQueue());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700472 }
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 view.assignParent(this);
475 mAddedTouchMode = (res&WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE) != 0;
476 mAppVisible = (res&WindowManagerImpl.ADD_FLAG_APP_VISIBLE) != 0;
477 }
478 }
479 }
480
Romain Guy529b60a2010-08-03 18:05:47 -0700481 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800482 mAttachInfo.mHardwareAccelerated = false;
483 mAttachInfo.mHardwareAccelerationRequested = false;
484
485 // Try to enable hardware acceleration if requested
486 if (attrs != null &&
487 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
488 // Only enable hardware acceleration if we are not in the system process
489 // The window manager creates ViewRoots to display animated preview windows
490 // of launching apps and we don't want those to be hardware accelerated
491 if (!HardwareRenderer.sRendererDisabled) {
Romain Guye4d01122010-06-16 18:44:05 -0700492 final boolean translucent = attrs.format != PixelFormat.OPAQUE;
Romain Guyb051e892010-09-28 19:09:36 -0700493 if (mAttachInfo.mHardwareRenderer != null) {
494 mAttachInfo.mHardwareRenderer.destroy(true);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700495 }
Romain Guyb051e892010-09-28 19:09:36 -0700496 mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent);
Dianne Hackborn7eec10e2010-11-12 18:03:47 -0800497 mAttachInfo.mHardwareAccelerated = mAttachInfo.mHardwareAccelerationRequested
498 = mAttachInfo.mHardwareRenderer != null;
499 } else if (HardwareRenderer.isAvailable()) {
500 mAttachInfo.mHardwareAccelerationRequested = true;
Romain Guye4d01122010-06-16 18:44:05 -0700501 }
502 }
503 }
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 public View getView() {
506 return mView;
507 }
508
509 final WindowLeaked getLocation() {
510 return mLocation;
511 }
512
513 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
514 synchronized (this) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700515 int oldSoftInputMode = mWindowAttributes.softInputMode;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700516 // preserve compatible window flag if exists.
517 int compatibleWindowFlag =
518 mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700520 mWindowAttributes.flags |= compatibleWindowFlag;
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 if (newView) {
523 mSoftInputMode = attrs.softInputMode;
524 requestLayout();
525 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700526 // Don't lose the mode we last auto-computed.
527 if ((attrs.softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
528 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
529 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
530 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
531 | (oldSoftInputMode
532 & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 mWindowAttributesChanged = true;
535 scheduleTraversals();
536 }
537 }
538
539 void handleAppVisibility(boolean visible) {
540 if (mAppVisible != visible) {
541 mAppVisible = visible;
542 scheduleTraversals();
543 }
544 }
545
546 void handleGetNewSurface() {
547 mNewSurfaceNeeded = true;
548 mFullRedrawNeeded = true;
549 scheduleTraversals();
550 }
551
552 /**
553 * {@inheritDoc}
554 */
555 public void requestLayout() {
556 checkThread();
557 mLayoutRequested = true;
558 scheduleTraversals();
559 }
560
561 /**
562 * {@inheritDoc}
563 */
564 public boolean isLayoutRequested() {
565 return mLayoutRequested;
566 }
567
568 public void invalidateChild(View child, Rect dirty) {
569 checkThread();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700570 if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
Chet Haase70d4ba12010-10-06 09:46:45 -0700571 if (dirty == null) {
572 // Fast invalidation for GL-enabled applications; GL must redraw everything
573 invalidate();
574 return;
575 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700576 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -0700578 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700579 if (mCurScrollY != 0) {
Romain Guy1e095972009-07-07 11:22:45 -0700580 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700581 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700582 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -0700583 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700584 }
Romain Guy1e095972009-07-07 11:22:45 -0700585 if (mAttachInfo.mScalingRequired) {
586 dirty.inset(-1, -1);
587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 }
Romain Guy7d695942010-12-01 17:22:29 -0800589 if (!mDirty.isEmpty()) {
590 mAttachInfo.mIgnoreDirtyState = true;
591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 mDirty.union(dirty);
593 if (!mWillDrawSoon) {
594 scheduleTraversals();
595 }
596 }
Romain Guy0d9275e2010-10-26 14:22:30 -0700597
598 void invalidate() {
599 mDirty.set(0, 0, mWidth, mHeight);
600 scheduleTraversals();
601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602
603 public ViewParent getParent() {
604 return null;
605 }
606
607 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
608 invalidateChild(null, dirty);
609 return null;
610 }
611
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700612 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 if (child != mView) {
614 throw new RuntimeException("child is not mine, honest!");
615 }
616 // Note: don't apply scroll offset, because we want to know its
617 // visibility in the virtual canvas being given to the view hierarchy.
618 return r.intersect(0, 0, mWidth, mHeight);
619 }
620
621 public void bringChildToFront(View child) {
622 }
623
624 public void scheduleTraversals() {
625 if (!mTraversalScheduled) {
626 mTraversalScheduled = true;
627 sendEmptyMessage(DO_TRAVERSAL);
628 }
629 }
630
631 public void unscheduleTraversals() {
632 if (mTraversalScheduled) {
633 mTraversalScheduled = false;
634 removeMessages(DO_TRAVERSAL);
635 }
636 }
637
638 int getHostVisibility() {
639 return mAppVisible ? mView.getVisibility() : View.GONE;
640 }
Romain Guy8506ab42009-06-11 17:35:47 -0700641
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800642 void disposeResizeBitmap() {
643 if (mResizeBitmap != null) {
644 mResizeBitmap.recycle();
645 mResizeBitmap = null;
646 }
647 }
648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 private void performTraversals() {
650 // cache mView since it is used so much below...
651 final View host = mView;
652
653 if (DBG) {
654 System.out.println("======================================");
655 System.out.println("performTraversals");
656 host.debug();
657 }
658
659 if (host == null || !mAdded)
660 return;
661
662 mTraversalScheduled = false;
663 mWillDrawSoon = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800664 boolean windowSizeMayChange = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 boolean fullRedrawNeeded = mFullRedrawNeeded;
666 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700667 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 WindowManager.LayoutParams lp = mWindowAttributes;
669
670 int desiredWindowWidth;
671 int desiredWindowHeight;
672 int childWidthMeasureSpec;
673 int childHeightMeasureSpec;
674
675 final View.AttachInfo attachInfo = mAttachInfo;
676
677 final int viewVisibility = getHostVisibility();
678 boolean viewVisibilityChanged = mViewVisibility != viewVisibility
679 || mNewSurfaceNeeded;
680
681 WindowManager.LayoutParams params = null;
682 if (mWindowAttributesChanged) {
683 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700684 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 params = lp;
686 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700687 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 if (mFirst) {
689 fullRedrawNeeded = true;
690 mLayoutRequested = true;
691
Romain Guy8506ab42009-06-11 17:35:47 -0700692 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700693 mView.getContext().getResources().getDisplayMetrics();
694 desiredWindowWidth = packageMetrics.widthPixels;
695 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
697 // For the very first time, tell the view hierarchy that it
698 // is attached to the window. Note that at this point the surface
699 // object is not initialized to its backing store, but soon it
700 // will be (assuming the window is visible).
701 attachInfo.mSurface = mSurface;
Adam Powell26153a32010-11-08 15:22:27 -0800702 attachInfo.mUse32BitDrawingCache = PixelFormat.formatHasAlpha(lp.format) ||
703 lp.format == PixelFormat.RGBX_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 attachInfo.mHasWindowFocus = false;
705 attachInfo.mWindowVisibility = viewVisibility;
706 attachInfo.mRecomputeGlobalAttributes = false;
707 attachInfo.mKeepScreenOn = false;
708 viewVisibilityChanged = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700709 mLastConfiguration.setTo(host.getResources().getConfiguration());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 host.dispatchAttachedToWindow(attachInfo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -0700712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700714 desiredWindowWidth = frame.width();
715 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700717 if (DEBUG_ORIENTATION) Log.v(TAG,
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700718 "View " + host + " resized to: " + frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800719 fullRedrawNeeded = true;
720 mLayoutRequested = true;
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800721 windowSizeMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723 }
724
725 if (viewVisibilityChanged) {
726 attachInfo.mWindowVisibility = viewVisibility;
727 host.dispatchWindowVisibilityChanged(viewVisibility);
728 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
Romain Guyb051e892010-09-28 19:09:36 -0700729 if (mAttachInfo.mHardwareRenderer != null) {
730 mAttachInfo.mHardwareRenderer.destroy(false);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700731 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 }
733 if (viewVisibility == View.GONE) {
734 // After making a window gone, we will count it as being
735 // shown for the first time the next time it gets focus.
736 mHasHadWindowFocus = false;
737 }
738 }
739
740 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -0700741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 if (mLayoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -0700743 // Execute enqueued actions on every layout in case a view that was detached
744 // enqueued an action after being detached
745 getRunQueue().executeActions(attachInfo.mHandler);
746
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800747 final Resources res = mView.getContext().getResources();
748
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 if (mFirst) {
750 host.fitSystemWindows(mAttachInfo.mContentInsets);
751 // make sure touch mode code executes by setting cached value
752 // to opposite of the added touch mode.
753 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -0700754 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 } else {
756 if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) {
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800757 if (mWidth > 0 && mHeight > 0 &&
758 mSurface != null && mSurface.isValid() &&
759 mAttachInfo.mHardwareRenderer != null &&
760 mAttachInfo.mHardwareRenderer.isEnabled() &&
761 lp != null && !PixelFormat.formatHasAlpha(lp.format)) {
762
763 disposeResizeBitmap();
764
765 boolean completed = false;
766 try {
767 mResizeBitmap = Bitmap.createBitmap(mWidth, mHeight,
768 Bitmap.Config.ARGB_8888);
769 mResizeBitmap.setHasAlpha(false);
770 Canvas canvas = new Canvas(mResizeBitmap);
771 int yoff;
772 final boolean scrolling = mScroller != null
773 && mScroller.computeScrollOffset();
774 if (scrolling) {
775 yoff = mScroller.getCurrY();
776 mScroller.abortAnimation();
777 } else {
778 yoff = mScrollY;
779 }
780 canvas.translate(0, -yoff);
781 if (mTranslator != null) {
782 mTranslator.translateCanvas(canvas);
783 }
784 canvas.setScreenDensity(mAttachInfo.mScalingRequired
785 ? DisplayMetrics.DENSITY_DEVICE : 0);
786 mView.draw(canvas);
787 mResizeBitmapStartTime = SystemClock.uptimeMillis();
788 mResizeBitmapDuration = mView.getResources().getInteger(
789 com.android.internal.R.integer.config_mediumAnimTime);
790 completed = true;
791 } catch (OutOfMemoryError e) {
792 Log.w(TAG, "Not enough memory for content change anim buffer", e);
793 } finally {
794 if (!completed) {
795 mResizeBitmap = null;
796 }
797 }
798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 mAttachInfo.mContentInsets.set(mPendingContentInsets);
800 host.fitSystemWindows(mAttachInfo.mContentInsets);
801 insetsChanged = true;
802 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
803 + mAttachInfo.mContentInsets);
804 }
805 if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) {
806 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
807 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
808 + mAttachInfo.mVisibleInsets);
809 }
810 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
811 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800812 windowSizeMayChange = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800814 DisplayMetrics packageMetrics = res.getDisplayMetrics();
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700815 desiredWindowWidth = packageMetrics.widthPixels;
816 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818 }
819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 // Ask host how big it wants to be
Jeff Brownc5ed5912010-07-14 18:48:53 -0700821 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 "Measuring " + host + " in display " + desiredWindowWidth
823 + "x" + desiredWindowHeight + "...");
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800824
825 boolean goodMeasure = false;
826 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
827 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
828 // On large screens, we don't want to allow dialogs to just
829 // stretch to fill the entire width of the screen to display
830 // one line of text. First try doing the layout at a smaller
831 // size to see if it will fit.
832 final DisplayMetrics packageMetrics = res.getDisplayMetrics();
833 res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true);
834 int baseSize = 0;
835 if (mTmpValue.type == TypedValue.TYPE_DIMENSION) {
836 baseSize = (int)mTmpValue.getDimension(packageMetrics);
837 }
838 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": baseSize=" + baseSize);
Dianne Hackborn7d3a5bc2010-11-29 22:52:12 -0800839 if (baseSize != 0 && desiredWindowWidth > baseSize) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800840 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
841 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
842 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
843 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": measured ("
Dianne Hackborn189ee182010-12-02 21:48:53 -0800844 + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")");
845 if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) {
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800846 goodMeasure = true;
847 } else {
848 // Didn't fit in that size... try expanding a bit.
849 baseSize = (baseSize+desiredWindowWidth)/2;
850 if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": next baseSize="
851 + baseSize);
Dianne Hackborn189ee182010-12-02 21:48:53 -0800852 childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800853 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 if (DEBUG_DIALOG) Log.v(TAG, "Good!");
858 goodMeasure = true;
859 }
860 }
861 }
862 }
863
864 if (!goodMeasure) {
865 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
866 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
867 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Adam Powellaa0b92c2010-12-13 22:38:53 -0800868 if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) {
869 windowSizeMayChange = true;
870 }
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872
873 if (DBG) {
874 System.out.println("======================================");
875 System.out.println("performTraversals -- after measure");
876 host.debug();
877 }
878 }
879
880 if (attachInfo.mRecomputeGlobalAttributes) {
881 //Log.i(TAG, "Computing screen on!");
882 attachInfo.mRecomputeGlobalAttributes = false;
883 boolean oldVal = attachInfo.mKeepScreenOn;
884 attachInfo.mKeepScreenOn = false;
885 host.dispatchCollectViewAttributes(0);
886 if (attachInfo.mKeepScreenOn != oldVal) {
887 params = lp;
888 //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn);
889 }
890 }
891
892 if (mFirst || attachInfo.mViewVisibilityChanged) {
893 attachInfo.mViewVisibilityChanged = false;
894 int resizeMode = mSoftInputMode &
895 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
896 // If we are in auto resize mode, then we need to determine
897 // what mode to use now.
898 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
899 final int N = attachInfo.mScrollContainers.size();
900 for (int i=0; i<N; i++) {
901 if (attachInfo.mScrollContainers.get(i).isShown()) {
902 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
903 }
904 }
905 if (resizeMode == 0) {
906 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
907 }
908 if ((lp.softInputMode &
909 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
910 lp.softInputMode = (lp.softInputMode &
911 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
912 resizeMode;
913 params = lp;
914 }
915 }
916 }
Romain Guy8506ab42009-06-11 17:35:47 -0700917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
919 if (!PixelFormat.formatHasAlpha(params.format)) {
920 params.format = PixelFormat.TRANSLUCENT;
921 }
922 }
923
Dianne Hackborn711e62a2010-11-29 16:38:22 -0800924 boolean windowShouldResize = mLayoutRequested && windowSizeMayChange
Dianne Hackborn189ee182010-12-02 21:48:53 -0800925 && ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight())
Romain Guy2e4f4262010-04-06 11:07:52 -0700926 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
927 frame.width() < desiredWindowWidth && frame.width() != mWidth)
928 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
929 frame.height() < desiredWindowHeight && frame.height() != mHeight));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930
931 final boolean computesInternalInsets =
932 attachInfo.mTreeObserver.hasComputeInternalInsetsListeners();
Romain Guy812ccbe2010-06-01 14:07:24 -0700933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 boolean insetsPending = false;
935 int relayoutResult = 0;
Romain Guy812ccbe2010-06-01 14:07:24 -0700936
937 if (mFirst || windowShouldResize || insetsChanged ||
938 viewVisibilityChanged || params != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939
940 if (viewVisibility == View.VISIBLE) {
941 // If this window is giving internal insets to the window
942 // manager, and it is being added or changing its visibility,
943 // then we want to first give the window manager "fake"
944 // insets to cause it to effectively ignore the content of
945 // the window during layout. This avoids it briefly causing
946 // other windows to resize/move based on the raw frame of the
947 // window, waiting until we can finish laying out this window
948 // and get back to the window manager with the ultimately
949 // computed insets.
Romain Guy812ccbe2010-06-01 14:07:24 -0700950 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 }
952
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700953 if (mSurfaceHolder != null) {
954 mSurfaceHolder.mSurfaceLock.lock();
955 mDrawingAllowed = true;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700956 }
Romain Guy812ccbe2010-06-01 14:07:24 -0700957
Romain Guyc361da82010-10-25 15:29:10 -0700958 boolean hwInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 boolean contentInsetsChanged = false;
Romain Guy13922e02009-05-12 17:56:14 -0700960 boolean visibleInsetsChanged;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700961 boolean hadSurface = mSurface.isValid();
Romain Guy812ccbe2010-06-01 14:07:24 -0700962
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 int fl = 0;
965 if (params != null) {
966 fl = params.flags;
967 if (attachInfo.mKeepScreenOn) {
968 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
969 }
970 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700971 if (DEBUG_LAYOUT) {
Dianne Hackborn189ee182010-12-02 21:48:53 -0800972 Log.i(TAG, "host=w:" + host.getMeasuredWidth() + ", h:" +
973 host.getMeasuredHeight() + ", params=" + params);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700974 }
975 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 if (params != null) {
978 params.flags = fl;
979 }
980
981 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString()
982 + " content=" + mPendingContentInsets.toShortString()
983 + " visible=" + mPendingVisibleInsets.toShortString()
984 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -0700985
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700986 if (mPendingConfiguration.seq != 0) {
987 if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: "
988 + mPendingConfiguration);
989 updateConfiguration(mPendingConfiguration, !mFirst);
990 mPendingConfiguration.seq = 0;
991 }
992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 contentInsetsChanged = !mPendingContentInsets.equals(
994 mAttachInfo.mContentInsets);
995 visibleInsetsChanged = !mPendingVisibleInsets.equals(
996 mAttachInfo.mVisibleInsets);
997 if (contentInsetsChanged) {
998 mAttachInfo.mContentInsets.set(mPendingContentInsets);
999 host.fitSystemWindows(mAttachInfo.mContentInsets);
1000 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
1001 + mAttachInfo.mContentInsets);
1002 }
1003 if (visibleInsetsChanged) {
1004 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
1005 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
1006 + mAttachInfo.mVisibleInsets);
1007 }
1008
1009 if (!hadSurface) {
1010 if (mSurface.isValid()) {
1011 // If we are creating a new surface, then we need to
1012 // completely redraw it. Also, when we get to the
1013 // point of drawing it we will hold off and schedule
1014 // a new traversal instead. This is so we can tell the
1015 // window manager about all of the windows being displayed
1016 // before actually drawing them, so it can display then
1017 // all at once.
1018 newSurface = true;
1019 fullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -07001020 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -07001021
Romain Guyb051e892010-09-28 19:09:36 -07001022 if (mAttachInfo.mHardwareRenderer != null) {
Romain Guyc361da82010-10-25 15:29:10 -07001023 hwInitialized = mAttachInfo.mHardwareRenderer.initialize(mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025 }
1026 } else if (!mSurface.isValid()) {
1027 // If the surface has been removed, then reset the scroll
1028 // positions.
1029 mLastScrolledFocus = null;
1030 mScrollY = mCurScrollY = 0;
1031 if (mScroller != null) {
1032 mScroller.abortAnimation();
1033 }
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001034 disposeResizeBitmap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 }
1036 } catch (RemoteException e) {
1037 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 if (DEBUG_ORIENTATION) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001040 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041
1042 attachInfo.mWindowLeft = frame.left;
1043 attachInfo.mWindowTop = frame.top;
1044
1045 // !!FIXME!! This next section handles the case where we did not get the
1046 // window size we asked for. We should avoid this by getting a maximum size from
1047 // the window session beforehand.
1048 mWidth = frame.width();
1049 mHeight = frame.height();
1050
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001051 if (mSurfaceHolder != null) {
1052 // The app owns the surface; tell it about what is going on.
1053 if (mSurface.isValid()) {
1054 // XXX .copyFrom() doesn't work!
1055 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1056 mSurfaceHolder.mSurface = mSurface;
1057 }
1058 mSurfaceHolder.mSurfaceLock.unlock();
1059 if (mSurface.isValid()) {
1060 if (!hadSurface) {
1061 mSurfaceHolder.ungetCallbacks();
1062
1063 mIsCreating = true;
1064 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
1065 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1066 if (callbacks != null) {
1067 for (SurfaceHolder.Callback c : callbacks) {
1068 c.surfaceCreated(mSurfaceHolder);
1069 }
1070 }
1071 surfaceChanged = true;
Romain Guyc361da82010-10-25 15:29:10 -07001072
1073 if (mAttachInfo.mHardwareRenderer != null) {
1074 // This will bail out early if already initialized
1075 mAttachInfo.mHardwareRenderer.initialize(mHolder);
1076 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001077 }
1078 if (surfaceChanged) {
1079 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
1080 lp.format, mWidth, mHeight);
1081 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1082 if (callbacks != null) {
1083 for (SurfaceHolder.Callback c : callbacks) {
1084 c.surfaceChanged(mSurfaceHolder, lp.format,
1085 mWidth, mHeight);
1086 }
1087 }
1088 }
1089 mIsCreating = false;
1090 } else if (hadSurface) {
1091 mSurfaceHolder.ungetCallbacks();
1092 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1093 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
1094 if (callbacks != null) {
1095 for (SurfaceHolder.Callback c : callbacks) {
1096 c.surfaceDestroyed(mSurfaceHolder);
1097 }
1098 }
1099 mSurfaceHolder.mSurfaceLock.lock();
1100 // Make surface invalid.
1101 //mSurfaceHolder.mSurface.copyFrom(mSurface);
1102 mSurfaceHolder.mSurface = new Surface();
1103 mSurfaceHolder.mSurfaceLock.unlock();
1104 }
1105 }
Romain Guy53389bd2010-09-07 17:16:32 -07001106
Romain Guy6b5108b2011-01-04 16:11:10 -08001107 if (hwInitialized || ((windowShouldResize || params != null) &&
Romain Guydbf78bd2010-12-07 17:04:03 -08001108 mAttachInfo.mHardwareRenderer != null &&
1109 mAttachInfo.mHardwareRenderer.isEnabled())) {
Romain Guyb051e892010-09-28 19:09:36 -07001110 mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112
1113 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Romain Guy2d4cff62010-04-09 15:39:00 -07001114 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
Dianne Hackborn189ee182010-12-02 21:48:53 -08001115 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
1116 || mHeight != host.getMeasuredHeight() || contentInsetsChanged) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
1118 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
1119
1120 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
Dianne Hackborn189ee182010-12-02 21:48:53 -08001121 + mWidth + " measuredWidth=" + host.getMeasuredWidth()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 + " mHeight=" + mHeight
Dianne Hackborn189ee182010-12-02 21:48:53 -08001123 + " measuredHeight" + host.getMeasuredHeight()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -07001125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Ask host how big it wants to be
1127 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1128
1129 // Implementation of weights from WindowManager.LayoutParams
1130 // We just grow the dimensions as needed and re-measure if
1131 // needs be
Dianne Hackborn189ee182010-12-02 21:48:53 -08001132 int width = host.getMeasuredWidth();
1133 int height = host.getMeasuredHeight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 boolean measureAgain = false;
1135
1136 if (lp.horizontalWeight > 0.0f) {
1137 width += (int) ((mWidth - width) * lp.horizontalWeight);
1138 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1139 MeasureSpec.EXACTLY);
1140 measureAgain = true;
1141 }
1142 if (lp.verticalWeight > 0.0f) {
1143 height += (int) ((mHeight - height) * lp.verticalWeight);
1144 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1145 MeasureSpec.EXACTLY);
1146 measureAgain = true;
1147 }
1148
1149 if (measureAgain) {
1150 if (DEBUG_LAYOUT) Log.v(TAG,
1151 "And hey let's measure once more: width=" + width
1152 + " height=" + height);
1153 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1154 }
1155
1156 mLayoutRequested = true;
1157 }
1158 }
1159
1160 final boolean didLayout = mLayoutRequested;
1161 boolean triggerGlobalLayoutListener = didLayout
1162 || attachInfo.mRecomputeGlobalAttributes;
1163 if (didLayout) {
1164 mLayoutRequested = false;
1165 mScrollMayChange = true;
1166 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001167 TAG, "Laying out " + host + " to (" +
Dianne Hackborn189ee182010-12-02 21:48:53 -08001168 host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")");
Romain Guy13922e02009-05-12 17:56:14 -07001169 long startTime = 0L;
Romain Guy5429e1d2010-09-07 12:38:00 -07001170 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 startTime = SystemClock.elapsedRealtime();
1172 }
Dianne Hackborn189ee182010-12-02 21:48:53 -08001173 host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174
Romain Guy13922e02009-05-12 17:56:14 -07001175 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1176 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
1177 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
1178 + "please refer to the logs with the tag "
1179 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1180 }
1181 }
1182
Romain Guy5429e1d2010-09-07 12:38:00 -07001183 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1185 }
1186
1187 // By this point all views have been sized and positionned
1188 // We can compute the transparent area
1189
1190 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1191 // start out transparent
1192 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1193 host.getLocationInWindow(mTmpLocation);
1194 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1195 mTmpLocation[0] + host.mRight - host.mLeft,
1196 mTmpLocation[1] + host.mBottom - host.mTop);
1197
1198 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001199 if (mTranslator != null) {
1200 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1201 }
1202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1204 mPreviousTransparentRegion.set(mTransparentRegion);
1205 // reconfigure window manager
1206 try {
1207 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1208 } catch (RemoteException e) {
1209 }
1210 }
1211 }
1212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 if (DBG) {
1214 System.out.println("======================================");
1215 System.out.println("performTraversals -- after setFrame");
1216 host.debug();
1217 }
1218 }
1219
1220 if (triggerGlobalLayoutListener) {
1221 attachInfo.mRecomputeGlobalAttributes = false;
1222 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1223 }
1224
1225 if (computesInternalInsets) {
1226 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1227 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1228 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1229 givenContent.left = givenContent.top = givenContent.right
1230 = givenContent.bottom = givenVisible.left = givenVisible.top
1231 = givenVisible.right = givenVisible.bottom = 0;
1232 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001233 Rect contentInsets = insets.contentInsets;
1234 Rect visibleInsets = insets.visibleInsets;
1235 if (mTranslator != null) {
1236 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1237 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1240 mLastGivenInsets.set(insets);
1241 try {
1242 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001243 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 } catch (RemoteException e) {
1245 }
1246 }
1247 }
Romain Guy8506ab42009-06-11 17:35:47 -07001248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 if (mFirst) {
1250 // handle first focus request
1251 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1252 + mView.hasFocus());
1253 if (mView != null) {
1254 if (!mView.hasFocus()) {
1255 mView.requestFocus(View.FOCUS_FORWARD);
1256 mFocusedView = mRealFocusedView = mView.findFocus();
1257 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1258 + mFocusedView);
1259 } else {
1260 mRealFocusedView = mView.findFocus();
1261 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1262 + mRealFocusedView);
1263 }
1264 }
1265 }
1266
1267 mFirst = false;
1268 mWillDrawSoon = false;
1269 mNewSurfaceNeeded = false;
1270 mViewVisibility = viewVisibility;
1271
1272 if (mAttachInfo.mHasWindowFocus) {
1273 final boolean imTarget = WindowManager.LayoutParams
1274 .mayUseInputMethod(mWindowAttributes.flags);
1275 if (imTarget != mLastWasImTarget) {
1276 mLastWasImTarget = imTarget;
1277 InputMethodManager imm = InputMethodManager.peekInstance();
1278 if (imm != null && imTarget) {
1279 imm.startGettingWindowFocus(mView);
1280 imm.onWindowFocus(mView, mView.findFocus(),
1281 mWindowAttributes.softInputMode,
1282 !mHasHadWindowFocus, mWindowAttributes.flags);
1283 }
1284 }
1285 }
Romain Guy8506ab42009-06-11 17:35:47 -07001286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1288
1289 if (!cancelDraw && !newSurface) {
1290 mFullRedrawNeeded = false;
1291 draw(fullRedrawNeeded);
1292
1293 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1294 || mReportNextDraw) {
1295 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001296 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
1298 mReportNextDraw = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001299 if (mSurfaceHolder != null && mSurface.isValid()) {
1300 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
1301 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1302 if (callbacks != null) {
1303 for (SurfaceHolder.Callback c : callbacks) {
1304 if (c instanceof SurfaceHolder.Callback2) {
1305 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
1306 mSurfaceHolder);
1307 }
1308 }
1309 }
1310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 try {
1312 sWindowSession.finishDrawing(mWindow);
1313 } catch (RemoteException e) {
1314 }
1315 }
1316 } else {
1317 // We were supposed to report when we are done drawing. Since we canceled the
1318 // draw, remember it here.
1319 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1320 mReportNextDraw = true;
1321 }
1322 if (fullRedrawNeeded) {
1323 mFullRedrawNeeded = true;
1324 }
1325 // Try again
1326 scheduleTraversals();
1327 }
1328 }
1329
1330 public void requestTransparentRegion(View child) {
1331 // the test below should not fail unless someone is messing with us
1332 checkThread();
1333 if (mView == child) {
1334 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1335 // Need to make sure we re-evaluate the window attributes next
1336 // time around, to ensure the window has the correct format.
1337 mWindowAttributesChanged = true;
Mathias Agopian1bd80ad2010-11-04 17:13:39 -07001338 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 }
1340 }
1341
1342 /**
1343 * Figures out the measure spec for the root view in a window based on it's
1344 * layout params.
1345 *
1346 * @param windowSize
1347 * The available width or height of the window
1348 *
1349 * @param rootDimension
1350 * The layout params for one dimension (width or height) of the
1351 * window.
1352 *
1353 * @return The measure spec to use to measure the root view.
1354 */
1355 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1356 int measureSpec;
1357 switch (rootDimension) {
1358
Romain Guy980a9382010-01-08 15:06:28 -08001359 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001360 // Window can't resize. Force root view to be windowSize.
1361 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1362 break;
1363 case ViewGroup.LayoutParams.WRAP_CONTENT:
1364 // Window can resize. Set max size for root view.
1365 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1366 break;
1367 default:
1368 // Window wants to be an exact size. Force root view to be that size.
1369 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1370 break;
1371 }
1372 return measureSpec;
1373 }
1374
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001375 int mHardwareYOffset;
1376 int mResizeAlpha;
1377 final Paint mResizePaint = new Paint();
1378
1379 public void onHardwarePreDraw(Canvas canvas) {
1380 canvas.translate(0, -mHardwareYOffset);
1381 }
1382
1383 public void onHardwarePostDraw(Canvas canvas) {
1384 if (mResizeBitmap != null) {
1385 canvas.translate(0, mHardwareYOffset);
1386 mResizePaint.setAlpha(mResizeAlpha);
1387 canvas.drawBitmap(mResizeBitmap, 0, 0, mResizePaint);
1388 }
1389 }
1390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001391 private void draw(boolean fullRedrawNeeded) {
1392 Surface surface = mSurface;
1393 if (surface == null || !surface.isValid()) {
1394 return;
1395 }
1396
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001397 if (!sFirstDrawComplete) {
1398 synchronized (sFirstDrawHandlers) {
1399 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07001400 final int count = sFirstDrawHandlers.size();
1401 for (int i = 0; i< count; i++) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001402 post(sFirstDrawHandlers.get(i));
1403 }
1404 }
1405 }
1406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 scrollToRectOrFocus(null, false);
1408
1409 if (mAttachInfo.mViewScrollChanged) {
1410 mAttachInfo.mViewScrollChanged = false;
1411 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1412 }
Romain Guy8506ab42009-06-11 17:35:47 -07001413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001414 int yoff;
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001415 boolean animating = mScroller != null && mScroller.computeScrollOffset();
1416 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 yoff = mScroller.getCurrY();
1418 } else {
1419 yoff = mScrollY;
1420 }
1421 if (mCurScrollY != yoff) {
1422 mCurScrollY = yoff;
1423 fullRedrawNeeded = true;
1424 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001425 float appScale = mAttachInfo.mApplicationScale;
1426 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001428 int resizeAlpha = 0;
1429 if (mResizeBitmap != null) {
1430 long deltaTime = SystemClock.uptimeMillis() - mResizeBitmapStartTime;
1431 if (deltaTime < mResizeBitmapDuration) {
1432 float amt = deltaTime/(float)mResizeBitmapDuration;
1433 amt = mResizeInterpolator.getInterpolation(amt);
1434 animating = true;
1435 resizeAlpha = 255 - (int)(amt*255);
1436 } else {
1437 disposeResizeBitmap();
1438 }
1439 }
1440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001442 if (mSurfaceHolder != null) {
1443 // The app owns the surface, we won't draw.
1444 dirty.setEmpty();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001445 if (animating) {
1446 if (mScroller != null) {
1447 mScroller.abortAnimation();
1448 }
1449 disposeResizeBitmap();
1450 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001451 return;
1452 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07001453
1454 if (fullRedrawNeeded) {
1455 mAttachInfo.mIgnoreDirtyState = true;
1456 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1457 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001458
Romain Guyb051e892010-09-28 19:09:36 -07001459 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guyfd507262010-10-10 15:42:49 -07001460 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy101e2ae2010-10-11 12:41:21 -07001461 mIsAnimating = false;
Romain Guyfd507262010-10-10 15:42:49 -07001462 dirty.setEmpty();
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001463 mHardwareYOffset = yoff;
1464 mResizeAlpha = resizeAlpha;
1465 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001466 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001467
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001468 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 mFullRedrawNeeded = true;
1470 scheduleTraversals();
1471 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 return;
1474 }
1475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001477 Log.v(TAG, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 + mWindowAttributes.getTitle()
1479 + ": dirty={" + dirty.left + "," + dirty.top
1480 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001481 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1482 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 }
1484
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001485 if (!dirty.isEmpty() || mIsAnimating) {
1486 Canvas canvas;
1487 try {
1488 int left = dirty.left;
1489 int top = dirty.top;
1490 int right = dirty.right;
1491 int bottom = dirty.bottom;
1492 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001493
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001494 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1495 bottom != dirty.bottom) {
1496 mAttachInfo.mIgnoreDirtyState = true;
1497 }
1498
1499 // TODO: Do this in native
1500 canvas.setDensity(mDensity);
1501 } catch (Surface.OutOfResourcesException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001502 Log.e(TAG, "OutOfResourcesException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001503 // TODO: we should ask the window manager to do something!
1504 // for now we just do nothing
1505 return;
1506 } catch (IllegalArgumentException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001507 Log.e(TAG, "IllegalArgumentException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001508 // TODO: we should ask the window manager to do something!
1509 // for now we just do nothing
1510 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001511 }
1512
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001513 try {
1514 if (!dirty.isEmpty() || mIsAnimating) {
1515 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001517 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001518 Log.v(TAG, "Surface " + surface + " drawing to bitmap w="
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001519 + canvas.getWidth() + ", h=" + canvas.getHeight());
1520 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001521 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522
Romain Guy5429e1d2010-09-07 12:38:00 -07001523 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001524 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001526
1527 // If this bitmap's format includes an alpha channel, we
1528 // need to clear it before drawing so that the child will
1529 // properly re-composite its drawing on a transparent
1530 // background. This automatically respects the clip/dirty region
1531 // or
1532 // If we are applying an offset, we need to clear the area
1533 // where the offset doesn't appear to avoid having garbage
1534 // left in the blank areas.
1535 if (!canvas.isOpaque() || yoff != 0) {
1536 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1537 }
1538
1539 dirty.setEmpty();
1540 mIsAnimating = false;
1541 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1542 mView.mPrivateFlags |= View.DRAWN;
1543
1544 if (DEBUG_DRAW) {
1545 Context cxt = mView.getContext();
1546 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1547 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1548 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1549 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001550 try {
1551 canvas.translate(0, -yoff);
1552 if (mTranslator != null) {
1553 mTranslator.translateCanvas(canvas);
1554 }
1555 canvas.setScreenDensity(scalingRequired
1556 ? DisplayMetrics.DENSITY_DEVICE : 0);
1557 mView.draw(canvas);
1558 } finally {
1559 mAttachInfo.mIgnoreDirtyState = false;
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001560 }
1561
1562 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1563 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1564 }
1565
Romain Guy5429e1d2010-09-07 12:38:00 -07001566 if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001567 int now = (int)SystemClock.elapsedRealtime();
1568 if (sDrawTime != 0) {
1569 nativeShowFPS(canvas, now - sDrawTime);
1570 }
1571 sDrawTime = now;
1572 }
1573
Romain Guy5429e1d2010-09-07 12:38:00 -07001574 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001575 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1576 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 }
1578
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001579 } finally {
1580 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001582 }
1583
1584 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001585 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 }
Romain Guy8506ab42009-06-11 17:35:47 -07001587
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001588 if (animating) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 mFullRedrawNeeded = true;
1590 scheduleTraversals();
1591 }
1592 }
1593
1594 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1595 final View.AttachInfo attachInfo = mAttachInfo;
1596 final Rect ci = attachInfo.mContentInsets;
1597 final Rect vi = attachInfo.mVisibleInsets;
1598 int scrollY = 0;
1599 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001600
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 if (vi.left > ci.left || vi.top > ci.top
1602 || vi.right > ci.right || vi.bottom > ci.bottom) {
1603 // We'll assume that we aren't going to change the scroll
1604 // offset, since we want to avoid that unless it is actually
1605 // going to make the focus visible... otherwise we scroll
1606 // all over the place.
1607 scrollY = mScrollY;
1608 // We can be called for two different situations: during a draw,
1609 // to update the scroll position if the focus has changed (in which
1610 // case 'rectangle' is null), or in response to a
1611 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1612 // is non-null and we just want to scroll to whatever that
1613 // rectangle is).
1614 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001615
1616 // When in touch mode, focus points to the previously focused view,
1617 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001618 // line checks whether the view is still in our hierarchy.
1619 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001620 mRealFocusedView = null;
1621 return false;
1622 }
1623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 if (focus != mLastScrolledFocus) {
1625 // If the focus has changed, then ignore any requests to scroll
1626 // to a rectangle; first we want to make sure the entire focus
1627 // view is visible.
1628 rectangle = null;
1629 }
1630 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1631 + " rectangle=" + rectangle + " ci=" + ci
1632 + " vi=" + vi);
1633 if (focus == mLastScrolledFocus && !mScrollMayChange
1634 && rectangle == null) {
1635 // Optimization: if the focus hasn't changed since last
1636 // time, and no layout has happened, then just leave things
1637 // as they are.
1638 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1639 + mScrollY + " vi=" + vi.toShortString());
1640 } else if (focus != null) {
1641 // We need to determine if the currently focused view is
1642 // within the visible part of the window and, if not, apply
1643 // a pan so it can be seen.
1644 mLastScrolledFocus = focus;
1645 mScrollMayChange = false;
1646 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1647 // Try to find the rectangle from the focus view.
1648 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1649 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1650 + mView.getWidth() + " h=" + mView.getHeight()
1651 + " ci=" + ci.toShortString()
1652 + " vi=" + vi.toShortString());
1653 if (rectangle == null) {
1654 focus.getFocusedRect(mTempRect);
1655 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1656 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001657 if (mView instanceof ViewGroup) {
1658 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1659 focus, mTempRect);
1660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1662 "Focus in window: focusRect="
1663 + mTempRect.toShortString()
1664 + " visRect=" + mVisRect.toShortString());
1665 } else {
1666 mTempRect.set(rectangle);
1667 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1668 "Request scroll to rect: "
1669 + mTempRect.toShortString()
1670 + " visRect=" + mVisRect.toShortString());
1671 }
1672 if (mTempRect.intersect(mVisRect)) {
1673 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1674 "Focus window visible rect: "
1675 + mTempRect.toShortString());
1676 if (mTempRect.height() >
1677 (mView.getHeight()-vi.top-vi.bottom)) {
1678 // If the focus simply is not going to fit, then
1679 // best is probably just to leave things as-is.
1680 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1681 "Too tall; leaving scrollY=" + scrollY);
1682 } else if ((mTempRect.top-scrollY) < vi.top) {
1683 scrollY -= vi.top - (mTempRect.top-scrollY);
1684 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1685 "Top covered; scrollY=" + scrollY);
1686 } else if ((mTempRect.bottom-scrollY)
1687 > (mView.getHeight()-vi.bottom)) {
1688 scrollY += (mTempRect.bottom-scrollY)
1689 - (mView.getHeight()-vi.bottom);
1690 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1691 "Bottom covered; scrollY=" + scrollY);
1692 }
1693 handled = true;
1694 }
1695 }
1696 }
1697 }
Romain Guy8506ab42009-06-11 17:35:47 -07001698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 if (scrollY != mScrollY) {
1700 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1701 + mScrollY + " , new=" + scrollY);
Dianne Hackborn0f761d62010-11-30 22:06:10 -08001702 if (!immediate && mResizeBitmap == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 if (mScroller == null) {
1704 mScroller = new Scroller(mView.getContext());
1705 }
1706 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1707 } else if (mScroller != null) {
1708 mScroller.abortAnimation();
1709 }
1710 mScrollY = scrollY;
1711 }
Romain Guy8506ab42009-06-11 17:35:47 -07001712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 return handled;
1714 }
Romain Guy8506ab42009-06-11 17:35:47 -07001715
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 public void requestChildFocus(View child, View focused) {
1717 checkThread();
1718 if (mFocusedView != focused) {
1719 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1720 scheduleTraversals();
1721 }
1722 mFocusedView = mRealFocusedView = focused;
1723 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1724 + mFocusedView);
1725 }
1726
1727 public void clearChildFocus(View child) {
1728 checkThread();
1729
1730 View oldFocus = mFocusedView;
1731
1732 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1733 mFocusedView = mRealFocusedView = null;
1734 if (mView != null && !mView.hasFocus()) {
1735 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1736 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1737 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1738 }
1739 } else if (oldFocus != null) {
1740 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1741 }
1742 }
1743
1744
1745 public void focusableViewAvailable(View v) {
1746 checkThread();
1747
1748 if (mView != null && !mView.hasFocus()) {
1749 v.requestFocus();
1750 } else {
1751 // the one case where will transfer focus away from the current one
1752 // is if the current view is a view group that prefers to give focus
1753 // to its children first AND the view is a descendant of it.
1754 mFocusedView = mView.findFocus();
1755 boolean descendantsHaveDibsOnFocus =
1756 (mFocusedView instanceof ViewGroup) &&
1757 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1758 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1759 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1760 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1761 v.requestFocus();
1762 }
1763 }
1764 }
1765
1766 public void recomputeViewAttributes(View child) {
1767 checkThread();
1768 if (mView == child) {
1769 mAttachInfo.mRecomputeGlobalAttributes = true;
1770 if (!mWillDrawSoon) {
1771 scheduleTraversals();
1772 }
1773 }
1774 }
1775
1776 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001777 if (mView != null) {
1778 mView.dispatchDetachedFromWindow();
1779 }
1780
1781 mView = null;
1782 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001783 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784
Romain Guy29d89972010-09-22 16:10:57 -07001785 destroyHardwareRenderer();
Romain Guy4caa4ed2010-08-25 14:46:24 -07001786
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001787 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001789 if (mInputChannel != null) {
1790 if (mInputQueueCallback != null) {
1791 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
1792 mInputQueueCallback = null;
1793 } else {
1794 InputQueue.unregisterInputChannel(mInputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001795 }
1796 }
1797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 try {
1799 sWindowSession.remove(mWindow);
1800 } catch (RemoteException e) {
1801 }
Jeff Brown349703e2010-06-22 01:27:15 -07001802
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001803 // Dispose the input channel after removing the window so the Window Manager
1804 // doesn't interpret the input channel being closed as an abnormal termination.
1805 if (mInputChannel != null) {
1806 mInputChannel.dispose();
1807 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001809 }
Romain Guy8506ab42009-06-11 17:35:47 -07001810
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001811 void updateConfiguration(Configuration config, boolean force) {
1812 if (DEBUG_CONFIGURATION) Log.v(TAG,
1813 "Applying new config to window "
1814 + mWindowAttributes.getTitle()
1815 + ": " + config);
1816 synchronized (sConfigCallbacks) {
1817 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1818 sConfigCallbacks.get(i).onConfigurationChanged(config);
1819 }
1820 }
1821 if (mView != null) {
1822 // At this point the resources have been updated to
1823 // have the most recent config, whatever that is. Use
1824 // the on in them which may be newer.
1825 if (mView != null) {
1826 config = mView.getResources().getConfiguration();
1827 }
1828 if (force || mLastConfiguration.diff(config) != 0) {
1829 mLastConfiguration.setTo(config);
1830 mView.dispatchConfigurationChanged(config);
1831 }
1832 }
1833 }
1834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 /**
1836 * Return true if child is an ancestor of parent, (or equal to the parent).
1837 */
1838 private static boolean isViewDescendantOf(View child, View parent) {
1839 if (child == parent) {
1840 return true;
1841 }
1842
1843 final ViewParent theParent = child.getParent();
1844 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1845 }
1846
Romain Guycdb86672010-03-18 18:54:50 -07001847 private static void forceLayout(View view) {
1848 view.forceLayout();
1849 if (view instanceof ViewGroup) {
1850 ViewGroup group = (ViewGroup) view;
1851 final int count = group.getChildCount();
1852 for (int i = 0; i < count; i++) {
1853 forceLayout(group.getChildAt(i));
1854 }
1855 }
1856 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857
1858 public final static int DO_TRAVERSAL = 1000;
1859 public final static int DIE = 1001;
1860 public final static int RESIZED = 1002;
1861 public final static int RESIZED_REPORT = 1003;
1862 public final static int WINDOW_FOCUS_CHANGED = 1004;
1863 public final static int DISPATCH_KEY = 1005;
1864 public final static int DISPATCH_POINTER = 1006;
1865 public final static int DISPATCH_TRACKBALL = 1007;
1866 public final static int DISPATCH_APP_VISIBILITY = 1008;
1867 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1868 public final static int FINISHED_EVENT = 1010;
1869 public final static int DISPATCH_KEY_FROM_IME = 1011;
1870 public final static int FINISH_INPUT_CONNECTION = 1012;
1871 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001872 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Christopher Tatea53146c2010-09-07 11:57:52 -07001873 public final static int DISPATCH_DRAG_EVENT = 1015;
Chris Tate91e9bb32010-10-12 12:58:43 -07001874 public final static int DISPATCH_DRAG_LOCATION_EVENT = 1016;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875
1876 @Override
1877 public void handleMessage(Message msg) {
1878 switch (msg.what) {
1879 case View.AttachInfo.INVALIDATE_MSG:
1880 ((View) msg.obj).invalidate();
1881 break;
1882 case View.AttachInfo.INVALIDATE_RECT_MSG:
1883 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1884 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1885 info.release();
1886 break;
1887 case DO_TRAVERSAL:
1888 if (mProfile) {
1889 Debug.startMethodTracing("ViewRoot");
1890 }
1891
1892 performTraversals();
1893
1894 if (mProfile) {
1895 Debug.stopMethodTracing();
1896 mProfile = false;
1897 }
1898 break;
1899 case FINISHED_EVENT:
1900 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1901 break;
1902 case DISPATCH_KEY:
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001903 deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 break;
Jeff Brown3915bb82010-11-05 15:02:16 -07001905 case DISPATCH_POINTER:
1906 deliverPointerEvent((MotionEvent) msg.obj, msg.arg1 != 0);
1907 break;
1908 case DISPATCH_TRACKBALL:
1909 deliverTrackballEvent((MotionEvent) msg.obj, msg.arg1 != 0);
1910 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 case DISPATCH_APP_VISIBILITY:
1912 handleAppVisibility(msg.arg1 != 0);
1913 break;
1914 case DISPATCH_GET_NEW_SURFACE:
1915 handleGetNewSurface();
1916 break;
1917 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001918 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001921 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001922 && mPendingVisibleInsets.equals(ri.visibleInsets)
1923 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 break;
1925 }
1926 // fall through...
1927 case RESIZED_REPORT:
1928 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001929 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1930 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001931 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 mWinFrame.left = 0;
1934 mWinFrame.right = msg.arg1;
1935 mWinFrame.top = 0;
1936 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001937 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1938 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 if (msg.what == RESIZED_REPORT) {
1940 mReportNextDraw = true;
1941 }
Romain Guycdb86672010-03-18 18:54:50 -07001942
1943 if (mView != null) {
1944 forceLayout(mView);
1945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 requestLayout();
1947 }
1948 break;
1949 case WINDOW_FOCUS_CHANGED: {
1950 if (mAdded) {
1951 boolean hasWindowFocus = msg.arg1 != 0;
1952 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1953 if (hasWindowFocus) {
1954 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001955 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956
Romain Guyc361da82010-10-25 15:29:10 -07001957 if (mAttachInfo.mHardwareRenderer != null &&
1958 mSurface != null && mSurface.isValid()) {
Romain Guyb051e892010-09-28 19:09:36 -07001959 mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
1960 mAttachInfo, mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 }
Romain Guy8506ab42009-06-11 17:35:47 -07001963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 mLastWasImTarget = WindowManager.LayoutParams
1965 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 InputMethodManager imm = InputMethodManager.peekInstance();
1968 if (mView != null) {
1969 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1970 imm.startGettingWindowFocus(mView);
1971 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001972 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001973 mView.dispatchWindowFocusChanged(hasWindowFocus);
1974 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001975
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 // Note: must be done after the focus change callbacks,
1977 // so all of the view state is set up correctly.
1978 if (hasWindowFocus) {
1979 if (imm != null && mLastWasImTarget) {
1980 imm.onWindowFocus(mView, mView.findFocus(),
1981 mWindowAttributes.softInputMode,
1982 !mHasHadWindowFocus, mWindowAttributes.flags);
1983 }
1984 // Clear the forward bit. We can just do this directly, since
1985 // the window manager doesn't care about it.
1986 mWindowAttributes.softInputMode &=
1987 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1988 ((WindowManager.LayoutParams)mView.getLayoutParams())
1989 .softInputMode &=
1990 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1991 mHasHadWindowFocus = true;
1992 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001993
1994 if (hasWindowFocus && mView != null) {
1995 sendAccessibilityEvents();
1996 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 }
1998 } break;
1999 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07002000 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07002002 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07002004 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07002006 KeyEvent event = (KeyEvent)msg.obj;
2007 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
2008 // The IME is trying to say this event is from the
2009 // system! Bad bad bad!
Romain Guy812ccbe2010-06-01 14:07:24 -07002010 event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project10592532009-03-18 17:39:46 -07002011 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002012 deliverKeyEventPostIme((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07002013 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 case FINISH_INPUT_CONNECTION: {
2015 InputMethodManager imm = InputMethodManager.peekInstance();
2016 if (imm != null) {
2017 imm.reportFinishInputConnection((InputConnection)msg.obj);
2018 }
2019 } break;
2020 case CHECK_FOCUS: {
2021 InputMethodManager imm = InputMethodManager.peekInstance();
2022 if (imm != null) {
2023 imm.checkFocus();
2024 }
2025 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07002026 case CLOSE_SYSTEM_DIALOGS: {
2027 if (mView != null) {
2028 mView.onCloseSystemDialogs((String)msg.obj);
2029 }
2030 } break;
Chris Tate91e9bb32010-10-12 12:58:43 -07002031 case DISPATCH_DRAG_EVENT:
2032 case DISPATCH_DRAG_LOCATION_EVENT: {
Christopher Tatea53146c2010-09-07 11:57:52 -07002033 handleDragEvent((DragEvent)msg.obj);
2034 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
2036 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002037
Jeff Brown3915bb82010-11-05 15:02:16 -07002038 private void startInputEvent(InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002039 if (mFinishedCallback != null) {
2040 Slog.w(TAG, "Received a new input event from the input queue but there is "
2041 + "already an unfinished input event in progress.");
2042 }
2043
2044 mFinishedCallback = finishedCallback;
2045 }
2046
Jeff Brown3915bb82010-11-05 15:02:16 -07002047 private void finishInputEvent(boolean handled) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002048 if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished");
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002049
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002050 if (mFinishedCallback != null) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002051 mFinishedCallback.finished(handled);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002052 mFinishedCallback = null;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002053 } else {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002054 Slog.w(TAG, "Attempted to tell the input queue that the current input event "
2055 + "is finished but there is no input event actually in progress.");
Jeff Brown46b9ac02010-04-22 18:58:52 -07002056 }
2057 }
2058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 /**
2060 * Something in the current window tells us we need to change the touch mode. For
2061 * example, we are not in touch mode, and the user touches the screen.
2062 *
2063 * If the touch mode has changed, tell the window manager, and handle it locally.
2064 *
2065 * @param inTouchMode Whether we want to be in touch mode.
2066 * @return True if the touch mode changed and focus changed was changed as a result
2067 */
2068 boolean ensureTouchMode(boolean inTouchMode) {
2069 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
2070 + "touch mode is " + mAttachInfo.mInTouchMode);
2071 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2072
2073 // tell the window manager
2074 try {
2075 sWindowSession.setInTouchMode(inTouchMode);
2076 } catch (RemoteException e) {
2077 throw new RuntimeException(e);
2078 }
2079
2080 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07002081 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083
2084 /**
2085 * Ensure that the touch mode for this window is set, and if it is changing,
2086 * take the appropriate action.
2087 * @param inTouchMode Whether we want to be in touch mode.
2088 * @return True if the touch mode changed and focus changed was changed as a result
2089 */
Romain Guy2d4cff62010-04-09 15:39:00 -07002090 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
2092 + "touch mode is " + mAttachInfo.mInTouchMode);
2093
2094 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
2095
2096 mAttachInfo.mInTouchMode = inTouchMode;
2097 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
2098
Romain Guy2d4cff62010-04-09 15:39:00 -07002099 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002100 }
2101
2102 private boolean enterTouchMode() {
2103 if (mView != null) {
2104 if (mView.hasFocus()) {
2105 // note: not relying on mFocusedView here because this could
2106 // be when the window is first being added, and mFocused isn't
2107 // set yet.
2108 final View focused = mView.findFocus();
2109 if (focused != null && !focused.isFocusableInTouchMode()) {
2110
2111 final ViewGroup ancestorToTakeFocus =
2112 findAncestorToTakeFocusInTouchMode(focused);
2113 if (ancestorToTakeFocus != null) {
2114 // there is an ancestor that wants focus after its descendants that
2115 // is focusable in touch mode.. give it focus
2116 return ancestorToTakeFocus.requestFocus();
2117 } else {
2118 // nothing appropriate to have focus in touch mode, clear it out
2119 mView.unFocus();
2120 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
2121 mFocusedView = null;
2122 return true;
2123 }
2124 }
2125 }
2126 }
2127 return false;
2128 }
2129
2130
2131 /**
2132 * Find an ancestor of focused that wants focus after its descendants and is
2133 * focusable in touch mode.
2134 * @param focused The currently focused view.
2135 * @return An appropriate view, or null if no such view exists.
2136 */
2137 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
2138 ViewParent parent = focused.getParent();
2139 while (parent instanceof ViewGroup) {
2140 final ViewGroup vgParent = (ViewGroup) parent;
2141 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
2142 && vgParent.isFocusableInTouchMode()) {
2143 return vgParent;
2144 }
2145 if (vgParent.isRootNamespace()) {
2146 return null;
2147 } else {
2148 parent = vgParent.getParent();
2149 }
2150 }
2151 return null;
2152 }
2153
2154 private boolean leaveTouchMode() {
2155 if (mView != null) {
2156 if (mView.hasFocus()) {
2157 // i learned the hard way to not trust mFocusedView :)
2158 mFocusedView = mView.findFocus();
2159 if (!(mFocusedView instanceof ViewGroup)) {
2160 // some view has focus, let it keep it
2161 return false;
2162 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2163 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2164 // some view group has focus, and doesn't prefer its children
2165 // over itself for focus, so let them keep it.
2166 return false;
2167 }
2168 }
2169
2170 // find the best view to give focus to in this brave new non-touch-mode
2171 // world
2172 final View focused = focusSearch(null, View.FOCUS_DOWN);
2173 if (focused != null) {
2174 return focused.requestFocus(View.FOCUS_DOWN);
2175 }
2176 }
2177 return false;
2178 }
2179
Jeff Brown3915bb82010-11-05 15:02:16 -07002180 private void deliverPointerEvent(MotionEvent event, boolean sendDone) {
2181 // If there is no view, then the event will not be handled.
2182 if (mView == null || !mAdded) {
2183 finishPointerEvent(event, sendDone, false);
2184 return;
2185 }
2186
2187 // Translate the pointer event for compatibility, if needed.
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002188 if (mTranslator != null) {
2189 mTranslator.translateEventInScreenToAppWindow(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
2191
Jeff Brown3915bb82010-11-05 15:02:16 -07002192 // Enter touch mode on the down.
2193 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
2194 if (isDown) {
2195 ensureTouchMode(true);
2196 }
2197 if(Config.LOGV) {
2198 captureMotionLog("captureDispatchPointer", event);
2199 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002200
Jeff Brown3915bb82010-11-05 15:02:16 -07002201 // Offset the scroll position.
2202 if (mCurScrollY != 0) {
2203 event.offsetLocation(0, mCurScrollY);
2204 }
2205 if (MEASURE_LATENCY) {
2206 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
2207 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002208
Jeff Brown3915bb82010-11-05 15:02:16 -07002209 // Remember the touch position for possible drag-initiation.
2210 mLastTouchPoint.x = event.getRawX();
2211 mLastTouchPoint.y = event.getRawY();
2212
2213 // Dispatch touch to view hierarchy.
2214 boolean handled = mView.dispatchTouchEvent(event);
2215 if (MEASURE_LATENCY) {
2216 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
2217 }
2218 if (handled) {
2219 finishPointerEvent(event, sendDone, true);
2220 return;
2221 }
2222
2223 // Apply edge slop and try again, if appropriate.
2224 final int edgeFlags = event.getEdgeFlags();
2225 if (edgeFlags != 0 && mView instanceof ViewGroup) {
2226 final int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
2227 int direction = View.FOCUS_UP;
2228 int x = (int)event.getX();
2229 int y = (int)event.getY();
2230 final int[] deltas = new int[2];
2231
2232 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
2233 direction = View.FOCUS_DOWN;
2234 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2235 deltas[0] = edgeSlop;
2236 x += edgeSlop;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002237 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002238 deltas[0] = -edgeSlop;
2239 x -= edgeSlop;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002240 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002241 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
2242 direction = View.FOCUS_UP;
2243 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2244 deltas[0] = edgeSlop;
2245 x += edgeSlop;
2246 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2247 deltas[0] = -edgeSlop;
2248 x -= edgeSlop;
2249 }
2250 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2251 direction = View.FOCUS_RIGHT;
2252 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2253 direction = View.FOCUS_LEFT;
2254 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002255
Jeff Brown3915bb82010-11-05 15:02:16 -07002256 View nearest = FocusFinder.getInstance().findNearestTouchable(
2257 ((ViewGroup) mView), x, y, direction, deltas);
2258 if (nearest != null) {
2259 event.offsetLocation(deltas[0], deltas[1]);
2260 event.setEdgeFlags(0);
2261 if (mView.dispatchTouchEvent(event)) {
2262 finishPointerEvent(event, sendDone, true);
2263 return;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002264 }
2265 }
2266 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002267
2268 // Pointer event was unhandled.
2269 finishPointerEvent(event, sendDone, false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002270 }
2271
Jeff Brown3915bb82010-11-05 15:02:16 -07002272 private void finishPointerEvent(MotionEvent event, boolean sendDone, boolean handled) {
2273 event.recycle();
2274 if (sendDone) {
2275 finishInputEvent(handled);
2276 }
2277 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
2278 }
2279
2280 private void deliverTrackballEvent(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2282
Jeff Brown3915bb82010-11-05 15:02:16 -07002283 // If there is no view, then the event will not be handled.
2284 if (mView == null || !mAdded) {
2285 finishTrackballEvent(event, sendDone, false);
2286 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 }
2288
Jeff Brown3915bb82010-11-05 15:02:16 -07002289 // Deliver the trackball event to the view.
2290 if (mView.dispatchTrackballEvent(event)) {
2291 // If we reach this, we delivered a trackball event to mView and
2292 // mView consumed it. Because we will not translate the trackball
2293 // event into a key event, touch mode will not exit, so we exit
2294 // touch mode here.
2295 ensureTouchMode(false);
2296
2297 finishTrackballEvent(event, sendDone, true);
2298 mLastTrackballTime = Integer.MIN_VALUE;
2299 return;
2300 }
2301
2302 // Translate the trackball event into DPAD keys and try to deliver those.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002303 final TrackballAxis x = mTrackballAxisX;
2304 final TrackballAxis y = mTrackballAxisY;
2305
2306 long curTime = SystemClock.uptimeMillis();
Jeff Brown3915bb82010-11-05 15:02:16 -07002307 if ((mLastTrackballTime + MAX_TRACKBALL_DELAY) < curTime) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002308 // It has been too long since the last movement,
2309 // so restart at the beginning.
2310 x.reset(0);
2311 y.reset(0);
2312 mLastTrackballTime = curTime;
2313 }
2314
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002315 final int action = event.getAction();
Jeff Brown49ed71d2010-12-06 17:13:33 -08002316 final int metaState = event.getMetaState();
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002317 switch (action) {
2318 case MotionEvent.ACTION_DOWN:
2319 x.reset(2);
2320 y.reset(2);
2321 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002322 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
2323 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2324 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002325 break;
2326 case MotionEvent.ACTION_UP:
2327 x.reset(2);
2328 y.reset(2);
2329 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002330 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState,
2331 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2332 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002333 break;
2334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002335
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002336 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2337 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2338 + " move=" + event.getX()
2339 + " / Y=" + y.position + " step="
2340 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2341 + " move=" + event.getY());
2342 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2343 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002345 // Generate DPAD events based on the trackball movement.
2346 // We pick the axis that has moved the most as the direction of
2347 // the DPAD. When we generate DPAD events for one axis, then the
2348 // other axis is reset -- we don't want to perform DPAD jumps due
2349 // to slight movements in the trackball when making major movements
2350 // along the other axis.
2351 int keycode = 0;
2352 int movement = 0;
2353 float accel = 1;
2354 if (xOff > yOff) {
2355 movement = x.generate((2/event.getXPrecision()));
2356 if (movement != 0) {
2357 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2358 : KeyEvent.KEYCODE_DPAD_LEFT;
2359 accel = x.acceleration;
2360 y.reset(2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002361 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002362 } else if (yOff > 0) {
2363 movement = y.generate((2/event.getYPrecision()));
2364 if (movement != 0) {
2365 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2366 : KeyEvent.KEYCODE_DPAD_UP;
2367 accel = y.acceleration;
2368 x.reset(2);
2369 }
2370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002372 if (keycode != 0) {
2373 if (movement < 0) movement = -movement;
2374 int accelMovement = (int)(movement * accel);
2375 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2376 + " accelMovement=" + accelMovement
2377 + " accel=" + accel);
2378 if (accelMovement > movement) {
2379 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2380 + keycode);
2381 movement--;
Jeff Brown49ed71d2010-12-06 17:13:33 -08002382 int repeatCount = accelMovement - movement;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002383 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002384 KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState,
2385 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2386 InputDevice.SOURCE_KEYBOARD), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002387 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002388 while (movement > 0) {
2389 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2390 + keycode);
2391 movement--;
2392 curTime = SystemClock.uptimeMillis();
2393 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002394 KeyEvent.ACTION_DOWN, keycode, 0, metaState,
2395 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2396 InputDevice.SOURCE_KEYBOARD), false);
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002397 deliverKeyEvent(new KeyEvent(curTime, curTime,
Jeff Brown49ed71d2010-12-06 17:13:33 -08002398 KeyEvent.ACTION_UP, keycode, 0, metaState,
2399 KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK,
2400 InputDevice.SOURCE_KEYBOARD), false);
2401 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002402 mLastTrackballTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002403 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002404
2405 // Unfortunately we can't tell whether the application consumed the keys, so
2406 // we always consider the trackball event handled.
2407 finishTrackballEvent(event, sendDone, true);
2408 }
2409
2410 private void finishTrackballEvent(MotionEvent event, boolean sendDone, boolean handled) {
2411 event.recycle();
2412 if (sendDone) {
2413 finishInputEvent(handled);
2414 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002415 }
2416
2417 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002418 * Returns true if the key is used for keyboard navigation.
2419 * @param keyEvent The key event.
2420 * @return True if the key is used for keyboard navigation.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002421 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08002422 private static boolean isNavigationKey(KeyEvent keyEvent) {
2423 switch (keyEvent.getKeyCode()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002424 case KeyEvent.KEYCODE_DPAD_LEFT:
2425 case KeyEvent.KEYCODE_DPAD_RIGHT:
2426 case KeyEvent.KEYCODE_DPAD_UP:
2427 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002428 case KeyEvent.KEYCODE_DPAD_CENTER:
2429 case KeyEvent.KEYCODE_PAGE_UP:
2430 case KeyEvent.KEYCODE_PAGE_DOWN:
2431 case KeyEvent.KEYCODE_MOVE_HOME:
2432 case KeyEvent.KEYCODE_MOVE_END:
2433 case KeyEvent.KEYCODE_TAB:
2434 case KeyEvent.KEYCODE_SPACE:
2435 case KeyEvent.KEYCODE_ENTER:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002436 return true;
2437 }
2438 return false;
2439 }
2440
2441 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002442 * Returns true if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 * @param keyEvent The key event.
Jeff Brown4e6319b2010-12-13 10:36:51 -08002444 * @return True if the key is used for typing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 */
Jeff Brown4e6319b2010-12-13 10:36:51 -08002446 private static boolean isTypingKey(KeyEvent keyEvent) {
2447 return keyEvent.getUnicodeChar() > 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002448 }
2449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08002451 * 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 -08002452 * @param event The key event.
2453 * @return Whether this key event should be consumed (meaning the act of
2454 * leaving touch mode alone is considered the event).
2455 */
2456 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Jeff Brown4e6319b2010-12-13 10:36:51 -08002457 // Only relevant in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002458 if (!mAttachInfo.mInTouchMode) {
2459 return false;
2460 }
2461
Jeff Brown4e6319b2010-12-13 10:36:51 -08002462 // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP.
2463 final int action = event.getAction();
2464 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002465 return false;
2466 }
2467
Jeff Brown4e6319b2010-12-13 10:36:51 -08002468 // Don't leave touch mode if the IME told us not to.
2469 if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2470 return false;
2471 }
2472
2473 // If the key can be used for keyboard navigation then leave touch mode
2474 // and select a focused view if needed (in ensureTouchMode).
2475 // When a new focused view is selected, we consume the navigation key because
2476 // navigation doesn't make much sense unless a view already has focus so
2477 // the key's purpose is to set focus.
2478 if (isNavigationKey(event)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 return ensureTouchMode(false);
2480 }
Jeff Brown4e6319b2010-12-13 10:36:51 -08002481
2482 // If the key can be used for typing then leave touch mode
2483 // and select a focused view if needed (in ensureTouchMode).
2484 // Always allow the view to process the typing key.
2485 if (isTypingKey(event)) {
2486 ensureTouchMode(false);
2487 return false;
2488 }
2489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002490 return false;
2491 }
2492
2493 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002494 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002495 */
2496 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002497 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 if (ev == null ||
2499 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2500 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002501 }
Romain Guy8506ab42009-06-11 17:35:47 -07002502
2503 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002504 sb.append(ev.getDownTime()).append(',');
2505 sb.append(ev.getEventTime()).append(',');
2506 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002507 sb.append(ev.getX()).append(',');
2508 sb.append(ev.getY()).append(',');
2509 sb.append(ev.getPressure()).append(',');
2510 sb.append(ev.getSize()).append(',');
2511 sb.append(ev.getMetaState()).append(',');
2512 sb.append(ev.getXPrecision()).append(',');
2513 sb.append(ev.getYPrecision()).append(',');
2514 sb.append(ev.getDeviceId()).append(',');
2515 sb.append(ev.getEdgeFlags());
2516 Log.d(TAG, sb.toString());
2517 }
2518 /**
2519 * log motion events
2520 */
2521 private static void captureKeyLog(String subTag, KeyEvent ev) {
2522 //check dynamic switch
2523 if (ev == null ||
2524 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2525 return;
2526 }
2527 StringBuilder sb = new StringBuilder(subTag + ": ");
2528 sb.append(ev.getDownTime()).append(',');
2529 sb.append(ev.getEventTime()).append(',');
2530 sb.append(ev.getAction()).append(',');
2531 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002532 sb.append(ev.getRepeatCount()).append(',');
2533 sb.append(ev.getMetaState()).append(',');
2534 sb.append(ev.getDeviceId()).append(',');
2535 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002536 Log.d(TAG, sb.toString());
2537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538
2539 int enqueuePendingEvent(Object event, boolean sendDone) {
2540 int seq = mPendingEventSeq+1;
2541 if (seq < 0) seq = 0;
2542 mPendingEventSeq = seq;
2543 mPendingEvents.put(seq, event);
2544 return sendDone ? seq : -seq;
2545 }
2546
2547 Object retrievePendingEvent(int seq) {
2548 if (seq < 0) seq = -seq;
2549 Object event = mPendingEvents.get(seq);
2550 if (event != null) {
2551 mPendingEvents.remove(seq);
2552 }
2553 return event;
2554 }
Romain Guy8506ab42009-06-11 17:35:47 -07002555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
Jeff Brown3915bb82010-11-05 15:02:16 -07002557 // If there is no view, then the event will not be handled.
2558 if (mView == null || !mAdded) {
2559 finishKeyEvent(event, sendDone, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002560 return;
2561 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002562
2563 if (LOCAL_LOGV) Log.v(TAG, "Dispatching key " + event + " to " + mView);
2564
2565 // Perform predispatching before the IME.
2566 if (mView.dispatchKeyEventPreIme(event)) {
2567 finishKeyEvent(event, sendDone, true);
2568 return;
2569 }
2570
2571 // Dispatch to the IME before propagating down the view hierarchy.
2572 // The IME will eventually call back into handleFinishedEvent.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 if (mLastWasImTarget) {
2574 InputMethodManager imm = InputMethodManager.peekInstance();
Jeff Brown3915bb82010-11-05 15:02:16 -07002575 if (imm != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 int seq = enqueuePendingEvent(event, sendDone);
2577 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2578 + seq + " event=" + event);
Jeff Brown3915bb82010-11-05 15:02:16 -07002579 imm.dispatchKeyEvent(mView.getContext(), seq, event, mInputMethodCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 return;
2581 }
2582 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002583
2584 // Not dispatching to IME, continue with post IME actions.
2585 deliverKeyEventPostIme(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002586 }
2587
Jeff Brown3915bb82010-11-05 15:02:16 -07002588 private void handleFinishedEvent(int seq, boolean handled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2590 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2591 + " handled=" + handled + " event=" + event);
2592 if (event != null) {
2593 final boolean sendDone = seq >= 0;
Jeff Brown3915bb82010-11-05 15:02:16 -07002594 if (handled) {
2595 finishKeyEvent(event, sendDone, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 } else {
Jeff Brown3915bb82010-11-05 15:02:16 -07002597 deliverKeyEventPostIme(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598 }
2599 }
2600 }
Romain Guy8506ab42009-06-11 17:35:47 -07002601
Jeff Brown3915bb82010-11-05 15:02:16 -07002602 private void deliverKeyEventPostIme(KeyEvent event, boolean sendDone) {
2603 // If the view went away, then the event will not be handled.
2604 if (mView == null || !mAdded) {
2605 finishKeyEvent(event, sendDone, false);
2606 return;
2607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002608
Jeff Brown3915bb82010-11-05 15:02:16 -07002609 // If the key's purpose is to exit touch mode then we consume it and consider it handled.
2610 if (checkForLeavingTouchModeAndConsume(event)) {
2611 finishKeyEvent(event, sendDone, true);
2612 return;
2613 }
Romain Guy8506ab42009-06-11 17:35:47 -07002614
Jeff Brown3915bb82010-11-05 15:02:16 -07002615 if (Config.LOGV) {
2616 captureKeyLog("captureDispatchKeyEvent", event);
2617 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002618
Jeff Brown90655042010-12-02 13:50:46 -08002619 // Make sure the fallback event policy sees all keys that will be delivered to the
2620 // view hierarchy.
2621 mFallbackEventHandler.preDispatchKeyEvent(event);
2622
Jeff Brown3915bb82010-11-05 15:02:16 -07002623 // Deliver the key to the view hierarchy.
2624 if (mView.dispatchKeyEvent(event)) {
2625 finishKeyEvent(event, sendDone, true);
2626 return;
2627 }
Joe Onorato86f67862010-11-05 18:57:34 -07002628
Jeff Brownc1df9072010-12-21 16:38:50 -08002629 // If the Control modifier is held, try to interpret the key as a shortcut.
2630 if (event.getAction() == KeyEvent.ACTION_UP
2631 && event.isCtrlPressed()
2632 && !KeyEvent.isModifierKey(event.getKeyCode())) {
2633 if (mView.dispatchKeyShortcutEvent(event)) {
2634 finishKeyEvent(event, sendDone, true);
2635 return;
2636 }
2637 }
2638
Jeff Brown3915bb82010-11-05 15:02:16 -07002639 // Apply the fallback event policy.
2640 if (mFallbackEventHandler.dispatchKeyEvent(event)) {
2641 finishKeyEvent(event, sendDone, true);
2642 return;
2643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644
Jeff Brown3915bb82010-11-05 15:02:16 -07002645 // Handle automatic focus changes.
2646 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2647 int direction = 0;
2648 switch (event.getKeyCode()) {
2649 case KeyEvent.KEYCODE_DPAD_LEFT:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002650 if (event.hasNoModifiers()) {
2651 direction = View.FOCUS_LEFT;
2652 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002653 break;
2654 case KeyEvent.KEYCODE_DPAD_RIGHT:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002655 if (event.hasNoModifiers()) {
2656 direction = View.FOCUS_RIGHT;
2657 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002658 break;
2659 case KeyEvent.KEYCODE_DPAD_UP:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002660 if (event.hasNoModifiers()) {
2661 direction = View.FOCUS_UP;
2662 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002663 break;
2664 case KeyEvent.KEYCODE_DPAD_DOWN:
Jeff Brown4e6319b2010-12-13 10:36:51 -08002665 if (event.hasNoModifiers()) {
2666 direction = View.FOCUS_DOWN;
2667 }
2668 break;
2669 case KeyEvent.KEYCODE_TAB:
2670 if (event.hasNoModifiers()) {
2671 direction = View.FOCUS_FORWARD;
2672 } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
2673 direction = View.FOCUS_BACKWARD;
2674 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002675 break;
2676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002677
Jeff Brown3915bb82010-11-05 15:02:16 -07002678 if (direction != 0) {
2679 View focused = mView != null ? mView.findFocus() : null;
2680 if (focused != null) {
2681 View v = focused.focusSearch(direction);
2682 if (v != null && v != focused) {
2683 // do the math the get the interesting rect
2684 // of previous focused into the coord system of
2685 // newly focused view
2686 focused.getFocusedRect(mTempRect);
2687 if (mView instanceof ViewGroup) {
2688 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2689 focused, mTempRect);
2690 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2691 v, mTempRect);
2692 }
2693 if (v.requestFocus(direction, mTempRect)) {
2694 playSoundEffect(
2695 SoundEffectConstants.getContantForFocusDirection(direction));
2696 finishKeyEvent(event, sendDone, true);
2697 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002698 }
2699 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002700
2701 // Give the focused view a last chance to handle the dpad key.
2702 if (mView.dispatchUnhandledMove(focused, direction)) {
2703 finishKeyEvent(event, sendDone, true);
2704 return;
2705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002706 }
2707 }
Jeff Brown3915bb82010-11-05 15:02:16 -07002708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709
Jeff Brown3915bb82010-11-05 15:02:16 -07002710 // Key was unhandled.
2711 finishKeyEvent(event, sendDone, false);
2712 }
2713
2714 private void finishKeyEvent(KeyEvent event, boolean sendDone, boolean handled) {
2715 if (sendDone) {
2716 finishInputEvent(handled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002717 }
2718 }
2719
Christopher Tatea53146c2010-09-07 11:57:52 -07002720 /* drag/drop */
Christopher Tate407b4e92010-11-30 17:14:08 -08002721 void setLocalDragState(Object obj) {
2722 mLocalDragState = obj;
2723 }
2724
Christopher Tatea53146c2010-09-07 11:57:52 -07002725 private void handleDragEvent(DragEvent event) {
2726 // From the root, only drag start/end/location are dispatched. entered/exited
2727 // are determined and dispatched by the viewgroup hierarchy, who then report
2728 // that back here for ultimate reporting back to the framework.
2729 if (mView != null && mAdded) {
2730 final int what = event.mAction;
2731
2732 if (what == DragEvent.ACTION_DRAG_EXITED) {
2733 // A direct EXITED event means that the window manager knows we've just crossed
2734 // a window boundary, so the current drag target within this one must have
2735 // just been exited. Send it the usual notifications and then we're done
2736 // for now.
Chris Tate9d1ab882010-11-02 15:55:39 -07002737 mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002738 } else {
2739 // Cache the drag description when the operation starts, then fill it in
2740 // on subsequent calls as a convenience
2741 if (what == DragEvent.ACTION_DRAG_STARTED) {
Chris Tate9d1ab882010-11-02 15:55:39 -07002742 mCurrentDragView = null; // Start the current-recipient tracking
Christopher Tatea53146c2010-09-07 11:57:52 -07002743 mDragDescription = event.mClipDescription;
2744 } else {
2745 event.mClipDescription = mDragDescription;
2746 }
2747
2748 // For events with a [screen] location, translate into window coordinates
2749 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
2750 mDragPoint.set(event.mX, event.mY);
2751 if (mTranslator != null) {
2752 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
2753 }
2754
2755 if (mCurScrollY != 0) {
2756 mDragPoint.offset(0, mCurScrollY);
2757 }
2758
2759 event.mX = mDragPoint.x;
2760 event.mY = mDragPoint.y;
2761 }
2762
2763 // Remember who the current drag target is pre-dispatch
2764 final View prevDragView = mCurrentDragView;
2765
2766 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07002767 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002768
2769 // If we changed apparent drag target, tell the OS about it
2770 if (prevDragView != mCurrentDragView) {
2771 try {
2772 if (prevDragView != null) {
2773 sWindowSession.dragRecipientExited(mWindow);
2774 }
2775 if (mCurrentDragView != null) {
2776 sWindowSession.dragRecipientEntered(mWindow);
2777 }
2778 } catch (RemoteException e) {
2779 Slog.e(TAG, "Unable to note drag target change");
2780 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002781 }
Chris Tated4533f12010-10-19 15:15:08 -07002782
Christopher Tate407b4e92010-11-30 17:14:08 -08002783 // Report the drop result when we're done
Chris Tated4533f12010-10-19 15:15:08 -07002784 if (what == DragEvent.ACTION_DROP) {
2785 try {
2786 Log.i(TAG, "Reporting drop result: " + result);
2787 sWindowSession.reportDropResult(mWindow, result);
2788 } catch (RemoteException e) {
2789 Log.e(TAG, "Unable to report drop result");
2790 }
2791 }
Christopher Tate407b4e92010-11-30 17:14:08 -08002792
2793 // When the drag operation ends, release any local state object
2794 // that may have been in use
2795 if (what == DragEvent.ACTION_DRAG_ENDED) {
2796 setLocalDragState(null);
2797 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002798 }
2799 }
2800 event.recycle();
2801 }
2802
Christopher Tate2c095f32010-10-04 14:13:40 -07002803 public void getLastTouchPoint(Point outLocation) {
2804 outLocation.x = (int) mLastTouchPoint.x;
2805 outLocation.y = (int) mLastTouchPoint.y;
2806 }
2807
Chris Tate9d1ab882010-11-02 15:55:39 -07002808 public void setDragFocus(View newDragTarget) {
Christopher Tatea53146c2010-09-07 11:57:52 -07002809 if (mCurrentDragView != newDragTarget) {
Chris Tate048691c2010-10-12 17:39:18 -07002810 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07002811 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002812 }
2813
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002814 private AudioManager getAudioManager() {
2815 if (mView == null) {
2816 throw new IllegalStateException("getAudioManager called when there is no mView");
2817 }
2818 if (mAudioManager == null) {
2819 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2820 }
2821 return mAudioManager;
2822 }
2823
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002824 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2825 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002826
2827 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002828 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002829 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002830 restore = true;
2831 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002832 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002833 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002834 if (params != null) {
2835 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002836 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002837 mPendingConfiguration.seq = 0;
Dianne Hackbornf123e492010-09-24 11:16:23 -07002838 //Log.d(TAG, ">>>>>> CALLING relayout");
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002839 int relayoutResult = sWindowSession.relayout(
2840 mWindow, params,
Dianne Hackborn189ee182010-12-02 21:48:53 -08002841 (int) (mView.getMeasuredWidth() * appScale + 0.5f),
2842 (int) (mView.getMeasuredHeight() * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002843 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002844 mPendingContentInsets, mPendingVisibleInsets,
2845 mPendingConfiguration, mSurface);
Dianne Hackbornf123e492010-09-24 11:16:23 -07002846 //Log.d(TAG, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002847 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002848 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002849 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002850
2851 if (mTranslator != null) {
2852 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2853 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2854 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002855 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002856 return relayoutResult;
2857 }
Romain Guy8506ab42009-06-11 17:35:47 -07002858
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002860 * {@inheritDoc}
2861 */
2862 public void playSoundEffect(int effectId) {
2863 checkThread();
2864
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002865 try {
2866 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002867
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002868 switch (effectId) {
2869 case SoundEffectConstants.CLICK:
2870 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2871 return;
2872 case SoundEffectConstants.NAVIGATION_DOWN:
2873 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2874 return;
2875 case SoundEffectConstants.NAVIGATION_LEFT:
2876 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2877 return;
2878 case SoundEffectConstants.NAVIGATION_RIGHT:
2879 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2880 return;
2881 case SoundEffectConstants.NAVIGATION_UP:
2882 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2883 return;
2884 default:
2885 throw new IllegalArgumentException("unknown effect id " + effectId +
2886 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2887 }
2888 } catch (IllegalStateException e) {
2889 // Exception thrown by getAudioManager() when mView is null
2890 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2891 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 }
2893 }
2894
2895 /**
2896 * {@inheritDoc}
2897 */
2898 public boolean performHapticFeedback(int effectId, boolean always) {
2899 try {
2900 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2901 } catch (RemoteException e) {
2902 return false;
2903 }
2904 }
2905
2906 /**
2907 * {@inheritDoc}
2908 */
2909 public View focusSearch(View focused, int direction) {
2910 checkThread();
2911 if (!(mView instanceof ViewGroup)) {
2912 return null;
2913 }
2914 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2915 }
2916
2917 public void debug() {
2918 mView.debug();
2919 }
2920
2921 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002922 if (immediate) {
2923 doDie();
2924 } else {
2925 sendEmptyMessage(DIE);
2926 }
2927 }
2928
2929 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002930 checkThread();
Jeff Brownb75fa302010-07-15 23:47:29 -07002931 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932 synchronized (this) {
2933 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07002934 destroyHardwareRenderer();
2935
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 int viewVisibility = mView.getVisibility();
2937 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2938 if (mWindowAttributesChanged || viewVisibilityChanged) {
2939 // If layout params have been changed, first give them
2940 // to the window manager to make sure it has the correct
2941 // animation info.
2942 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002943 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2944 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002945 sWindowSession.finishDrawing(mWindow);
2946 }
2947 } catch (RemoteException e) {
2948 }
2949 }
2950
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002951 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 }
2953 if (mAdded) {
2954 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002955 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002956 }
2957 }
2958 }
2959
Romain Guy29d89972010-09-22 16:10:57 -07002960 private void destroyHardwareRenderer() {
Romain Guyb051e892010-09-28 19:09:36 -07002961 if (mAttachInfo.mHardwareRenderer != null) {
2962 mAttachInfo.mHardwareRenderer.destroy(true);
2963 mAttachInfo.mHardwareRenderer = null;
Romain Guy29d89972010-09-22 16:10:57 -07002964 mAttachInfo.mHardwareAccelerated = false;
2965 }
2966 }
2967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 public void dispatchFinishedEvent(int seq, boolean handled) {
2969 Message msg = obtainMessage(FINISHED_EVENT);
2970 msg.arg1 = seq;
2971 msg.arg2 = handled ? 1 : 0;
2972 sendMessage(msg);
2973 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002976 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002977 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2978 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2979 + " visibleInsets=" + visibleInsets.toShortString()
2980 + " reportDraw=" + reportDraw);
2981 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002982 if (mTranslator != null) {
2983 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2984 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2985 w *= mTranslator.applicationInvertedScale;
2986 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002987 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002988 msg.arg1 = w;
2989 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002990 ResizedInfo ri = new ResizedInfo();
2991 ri.coveredInsets = new Rect(coveredInsets);
2992 ri.visibleInsets = new Rect(visibleInsets);
2993 ri.newConfig = newConfig;
2994 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 sendMessage(msg);
2996 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002997
Jeff Brown3915bb82010-11-05 15:02:16 -07002998 private InputQueue.FinishedCallback mFinishedCallback;
Jeff Brown46b9ac02010-04-22 18:58:52 -07002999
3000 private final InputHandler mInputHandler = new InputHandler() {
Jeff Brown3915bb82010-11-05 15:02:16 -07003001 public void handleKey(KeyEvent event, InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003002 startInputEvent(finishedCallback);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003003 dispatchKey(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003004 }
3005
Jeff Brown3915bb82010-11-05 15:02:16 -07003006 public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003007 startInputEvent(finishedCallback);
3008 dispatchMotion(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07003009 }
3010 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011
3012 public void dispatchKey(KeyEvent event) {
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003013 dispatchKey(event, false);
3014 }
3015
3016 private void dispatchKey(KeyEvent event, boolean sendDone) {
3017 //noinspection ConstantConditions
3018 if (false && event.getAction() == KeyEvent.ACTION_DOWN) {
3019 if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
Romain Guy812ccbe2010-06-01 14:07:24 -07003020 if (DBG) Log.d("keydisp", "===================================================");
3021 if (DBG) Log.d("keydisp", "Focused view Hierarchy is:");
3022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 debug();
3024
Romain Guy812ccbe2010-06-01 14:07:24 -07003025 if (DBG) Log.d("keydisp", "===================================================");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003026 }
3027 }
3028
3029 Message msg = obtainMessage(DISPATCH_KEY);
3030 msg.obj = event;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07003031 msg.arg1 = sendDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032
3033 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07003034 TAG, "sending key " + event + " to " + mView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035
3036 sendMessageAtTime(msg, event.getEventTime());
3037 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07003038
3039 public void dispatchMotion(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003040 dispatchMotion(event, false);
3041 }
3042
3043 private void dispatchMotion(MotionEvent event, boolean sendDone) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07003044 int source = event.getSource();
3045 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003046 dispatchPointer(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07003047 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003048 dispatchTrackball(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07003049 } else {
3050 // TODO
3051 Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event);
Jeff Brown93ed4e32010-09-23 13:51:48 -07003052 if (sendDone) {
Jeff Brown3915bb82010-11-05 15:02:16 -07003053 finishInputEvent(false);
Jeff Brown93ed4e32010-09-23 13:51:48 -07003054 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07003055 }
3056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003057
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003058 public void dispatchPointer(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003059 dispatchPointer(event, false);
3060 }
3061
3062 private void dispatchPointer(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003063 Message msg = obtainMessage(DISPATCH_POINTER);
3064 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07003065 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003066 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003067 }
3068
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003069 public void dispatchTrackball(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07003070 dispatchTrackball(event, false);
3071 }
3072
3073 private void dispatchTrackball(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 Message msg = obtainMessage(DISPATCH_TRACKBALL);
3075 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07003076 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003077 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07003079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003080 public void dispatchAppVisibility(boolean visible) {
3081 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
3082 msg.arg1 = visible ? 1 : 0;
3083 sendMessage(msg);
3084 }
3085
3086 public void dispatchGetNewSurface() {
3087 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
3088 sendMessage(msg);
3089 }
3090
3091 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3092 Message msg = Message.obtain();
3093 msg.what = WINDOW_FOCUS_CHANGED;
3094 msg.arg1 = hasFocus ? 1 : 0;
3095 msg.arg2 = inTouchMode ? 1 : 0;
3096 sendMessage(msg);
3097 }
3098
Dianne Hackbornffa42482009-09-23 22:20:11 -07003099 public void dispatchCloseSystemDialogs(String reason) {
3100 Message msg = Message.obtain();
3101 msg.what = CLOSE_SYSTEM_DIALOGS;
3102 msg.obj = reason;
3103 sendMessage(msg);
3104 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003105
3106 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07003107 final int what;
3108 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
3109 what = DISPATCH_DRAG_LOCATION_EVENT;
3110 removeMessages(what);
3111 } else {
3112 what = DISPATCH_DRAG_EVENT;
3113 }
Christopher Tate407b4e92010-11-30 17:14:08 -08003114 event.mLocalState = mLocalDragState; // only present when this app called startDrag()
Chris Tate91e9bb32010-10-12 12:58:43 -07003115 Message msg = obtainMessage(what, event);
Christopher Tatea53146c2010-09-07 11:57:52 -07003116 sendMessage(msg);
3117 }
3118
svetoslavganov75986cf2009-05-14 22:28:01 -07003119 /**
3120 * The window is getting focus so if there is anything focused/selected
3121 * send an {@link AccessibilityEvent} to announce that.
3122 */
3123 private void sendAccessibilityEvents() {
3124 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
3125 return;
3126 }
3127 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3128 View focusedView = mView.findFocus();
3129 if (focusedView != null && focusedView != mView) {
3130 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3131 }
3132 }
3133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003134 public boolean showContextMenuForChild(View originalView) {
3135 return false;
3136 }
3137
Adam Powell6e346362010-07-23 10:18:23 -07003138 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
3139 return null;
3140 }
3141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 public void createContextMenu(ContextMenu menu) {
3143 }
3144
3145 public void childDrawableStateChanged(View child) {
3146 }
3147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003148 void checkThread() {
3149 if (mThread != Thread.currentThread()) {
3150 throw new CalledFromWrongThreadException(
3151 "Only the original thread that created a view hierarchy can touch its views.");
3152 }
3153 }
3154
3155 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
3156 // ViewRoot never intercepts touch event, so this can be a no-op
3157 }
3158
3159 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
3160 boolean immediate) {
3161 return scrollToRectOrFocus(rectangle, immediate);
3162 }
Romain Guy8506ab42009-06-11 17:35:47 -07003163
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07003164 class TakenSurfaceHolder extends BaseSurfaceHolder {
3165 @Override
3166 public boolean onAllowLockCanvas() {
3167 return mDrawingAllowed;
3168 }
3169
3170 @Override
3171 public void onRelayoutContainer() {
3172 // Not currently interesting -- from changing between fixed and layout size.
3173 }
3174
3175 public void setFormat(int format) {
3176 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
3177 }
3178
3179 public void setType(int type) {
3180 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
3181 }
3182
3183 @Override
3184 public void onUpdateSurface() {
3185 // We take care of format and type changes on our own.
3186 throw new IllegalStateException("Shouldn't be here");
3187 }
3188
3189 public boolean isCreating() {
3190 return mIsCreating;
3191 }
3192
3193 @Override
3194 public void setFixedSize(int width, int height) {
3195 throw new UnsupportedOperationException(
3196 "Currently only support sizing from layout");
3197 }
3198
3199 public void setKeepScreenOn(boolean screenOn) {
3200 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
3201 }
3202 }
3203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 static class InputMethodCallback extends IInputMethodCallback.Stub {
3205 private WeakReference<ViewRoot> mViewRoot;
3206
3207 public InputMethodCallback(ViewRoot viewRoot) {
3208 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
3209 }
Romain Guy8506ab42009-06-11 17:35:47 -07003210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003211 public void finishedEvent(int seq, boolean handled) {
3212 final ViewRoot viewRoot = mViewRoot.get();
3213 if (viewRoot != null) {
3214 viewRoot.dispatchFinishedEvent(seq, handled);
3215 }
3216 }
3217
3218 public void sessionCreated(IInputMethodSession session) throws RemoteException {
3219 // Stub -- not for use in the client.
3220 }
3221 }
Romain Guy8506ab42009-06-11 17:35:47 -07003222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07003224 private final WeakReference<ViewRoot> mViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003225
Romain Guyfb8b7632010-08-23 21:05:08 -07003226 W(ViewRoot viewRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
3228 }
3229
Romain Guyfb8b7632010-08-23 21:05:08 -07003230 public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets,
3231 boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 final ViewRoot viewRoot = mViewRoot.get();
3233 if (viewRoot != null) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003234 viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003235 }
3236 }
3237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 public void dispatchAppVisibility(boolean visible) {
3239 final ViewRoot viewRoot = mViewRoot.get();
3240 if (viewRoot != null) {
3241 viewRoot.dispatchAppVisibility(visible);
3242 }
3243 }
3244
3245 public void dispatchGetNewSurface() {
3246 final ViewRoot viewRoot = mViewRoot.get();
3247 if (viewRoot != null) {
3248 viewRoot.dispatchGetNewSurface();
3249 }
3250 }
3251
3252 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3253 final ViewRoot viewRoot = mViewRoot.get();
3254 if (viewRoot != null) {
3255 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
3256 }
3257 }
3258
3259 private static int checkCallingPermission(String permission) {
3260 if (!Process.supportsProcesses()) {
3261 return PackageManager.PERMISSION_GRANTED;
3262 }
3263
3264 try {
3265 return ActivityManagerNative.getDefault().checkPermission(
3266 permission, Binder.getCallingPid(), Binder.getCallingUid());
3267 } catch (RemoteException e) {
3268 return PackageManager.PERMISSION_DENIED;
3269 }
3270 }
3271
3272 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3273 final ViewRoot viewRoot = mViewRoot.get();
3274 if (viewRoot != null) {
3275 final View view = viewRoot.mView;
3276 if (view != null) {
3277 if (checkCallingPermission(Manifest.permission.DUMP) !=
3278 PackageManager.PERMISSION_GRANTED) {
3279 throw new SecurityException("Insufficient permissions to invoke"
3280 + " executeCommand() from pid=" + Binder.getCallingPid()
3281 + ", uid=" + Binder.getCallingUid());
3282 }
3283
3284 OutputStream clientStream = null;
3285 try {
3286 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3287 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3288 } catch (IOException e) {
3289 e.printStackTrace();
3290 } finally {
3291 if (clientStream != null) {
3292 try {
3293 clientStream.close();
3294 } catch (IOException e) {
3295 e.printStackTrace();
3296 }
3297 }
3298 }
3299 }
3300 }
3301 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003302
Dianne Hackbornffa42482009-09-23 22:20:11 -07003303 public void closeSystemDialogs(String reason) {
3304 final ViewRoot viewRoot = mViewRoot.get();
3305 if (viewRoot != null) {
3306 viewRoot.dispatchCloseSystemDialogs(reason);
3307 }
3308 }
3309
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003310 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3311 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003312 if (sync) {
3313 try {
3314 sWindowSession.wallpaperOffsetsComplete(asBinder());
3315 } catch (RemoteException e) {
3316 }
3317 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003318 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003319
3320 public void dispatchWallpaperCommand(String action, int x, int y,
3321 int z, Bundle extras, boolean sync) {
3322 if (sync) {
3323 try {
3324 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3325 } catch (RemoteException e) {
3326 }
3327 }
3328 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003329
3330 /* Drag/drop */
3331 public void dispatchDragEvent(DragEvent event) {
3332 final ViewRoot viewRoot = mViewRoot.get();
3333 if (viewRoot != null) {
3334 viewRoot.dispatchDragEvent(event);
3335 }
3336 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 }
3338
3339 /**
3340 * Maintains state information for a single trackball axis, generating
3341 * discrete (DPAD) movements based on raw trackball motion.
3342 */
3343 static final class TrackballAxis {
3344 /**
3345 * The maximum amount of acceleration we will apply.
3346 */
3347 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003349 /**
3350 * The maximum amount of time (in milliseconds) between events in order
3351 * for us to consider the user to be doing fast trackball movements,
3352 * and thus apply an acceleration.
3353 */
3354 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003356 /**
3357 * Scaling factor to the time (in milliseconds) between events to how
3358 * much to multiple/divide the current acceleration. When movement
3359 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3360 * FAST_MOVE_TIME it divides it.
3361 */
3362 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 float position;
3365 float absPosition;
3366 float acceleration = 1;
3367 long lastMoveTime = 0;
3368 int step;
3369 int dir;
3370 int nonAccelMovement;
3371
3372 void reset(int _step) {
3373 position = 0;
3374 acceleration = 1;
3375 lastMoveTime = 0;
3376 step = _step;
3377 dir = 0;
3378 }
3379
3380 /**
3381 * Add trackball movement into the state. If the direction of movement
3382 * has been reversed, the state is reset before adding the
3383 * movement (so that you don't have to compensate for any previously
3384 * collected movement before see the result of the movement in the
3385 * new direction).
3386 *
3387 * @return Returns the absolute value of the amount of movement
3388 * collected so far.
3389 */
3390 float collect(float off, long time, String axis) {
3391 long normTime;
3392 if (off > 0) {
3393 normTime = (long)(off * FAST_MOVE_TIME);
3394 if (dir < 0) {
3395 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3396 position = 0;
3397 step = 0;
3398 acceleration = 1;
3399 lastMoveTime = 0;
3400 }
3401 dir = 1;
3402 } else if (off < 0) {
3403 normTime = (long)((-off) * FAST_MOVE_TIME);
3404 if (dir > 0) {
3405 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3406 position = 0;
3407 step = 0;
3408 acceleration = 1;
3409 lastMoveTime = 0;
3410 }
3411 dir = -1;
3412 } else {
3413 normTime = 0;
3414 }
Romain Guy8506ab42009-06-11 17:35:47 -07003415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 // The number of milliseconds between each movement that is
3417 // considered "normal" and will not result in any acceleration
3418 // or deceleration, scaled by the offset we have here.
3419 if (normTime > 0) {
3420 long delta = time - lastMoveTime;
3421 lastMoveTime = time;
3422 float acc = acceleration;
3423 if (delta < normTime) {
3424 // The user is scrolling rapidly, so increase acceleration.
3425 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3426 if (scale > 1) acc *= scale;
3427 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3428 + off + " normTime=" + normTime + " delta=" + delta
3429 + " scale=" + scale + " acc=" + acc);
3430 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3431 } else {
3432 // The user is scrolling slowly, so decrease acceleration.
3433 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3434 if (scale > 1) acc /= scale;
3435 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3436 + off + " normTime=" + normTime + " delta=" + delta
3437 + " scale=" + scale + " acc=" + acc);
3438 acceleration = acc > 1 ? acc : 1;
3439 }
3440 }
3441 position += off;
3442 return (absPosition = Math.abs(position));
3443 }
3444
3445 /**
3446 * Generate the number of discrete movement events appropriate for
3447 * the currently collected trackball movement.
3448 *
3449 * @param precision The minimum movement required to generate the
3450 * first discrete movement.
3451 *
3452 * @return Returns the number of discrete movements, either positive
3453 * or negative, or 0 if there is not enough trackball movement yet
3454 * for a discrete movement.
3455 */
3456 int generate(float precision) {
3457 int movement = 0;
3458 nonAccelMovement = 0;
3459 do {
3460 final int dir = position >= 0 ? 1 : -1;
3461 switch (step) {
3462 // If we are going to execute the first step, then we want
3463 // to do this as soon as possible instead of waiting for
3464 // a full movement, in order to make things look responsive.
3465 case 0:
3466 if (absPosition < precision) {
3467 return movement;
3468 }
3469 movement += dir;
3470 nonAccelMovement += dir;
3471 step = 1;
3472 break;
3473 // If we have generated the first movement, then we need
3474 // to wait for the second complete trackball motion before
3475 // generating the second discrete movement.
3476 case 1:
3477 if (absPosition < 2) {
3478 return movement;
3479 }
3480 movement += dir;
3481 nonAccelMovement += dir;
3482 position += dir > 0 ? -2 : 2;
3483 absPosition = Math.abs(position);
3484 step = 2;
3485 break;
3486 // After the first two, we generate discrete movements
3487 // consistently with the trackball, applying an acceleration
3488 // if the trackball is moving quickly. This is a simple
3489 // acceleration on top of what we already compute based
3490 // on how quickly the wheel is being turned, to apply
3491 // a longer increasing acceleration to continuous movement
3492 // in one direction.
3493 default:
3494 if (absPosition < 1) {
3495 return movement;
3496 }
3497 movement += dir;
3498 position += dir >= 0 ? -1 : 1;
3499 absPosition = Math.abs(position);
3500 float acc = acceleration;
3501 acc *= 1.1f;
3502 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3503 break;
3504 }
3505 } while (true);
3506 }
3507 }
3508
3509 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3510 public CalledFromWrongThreadException(String msg) {
3511 super(msg);
3512 }
3513 }
3514
3515 private SurfaceHolder mHolder = new SurfaceHolder() {
3516 // we only need a SurfaceHolder for opengl. it would be nice
3517 // to implement everything else though, especially the callback
3518 // support (opengl doesn't make use of it right now, but eventually
3519 // will).
3520 public Surface getSurface() {
3521 return mSurface;
3522 }
3523
3524 public boolean isCreating() {
3525 return false;
3526 }
3527
3528 public void addCallback(Callback callback) {
3529 }
3530
3531 public void removeCallback(Callback callback) {
3532 }
3533
3534 public void setFixedSize(int width, int height) {
3535 }
3536
3537 public void setSizeFromLayout() {
3538 }
3539
3540 public void setFormat(int format) {
3541 }
3542
3543 public void setType(int type) {
3544 }
3545
3546 public void setKeepScreenOn(boolean screenOn) {
3547 }
3548
3549 public Canvas lockCanvas() {
3550 return null;
3551 }
3552
3553 public Canvas lockCanvas(Rect dirty) {
3554 return null;
3555 }
3556
3557 public void unlockCanvasAndPost(Canvas canvas) {
3558 }
3559 public Rect getSurfaceFrame() {
3560 return null;
3561 }
3562 };
3563
3564 static RunQueue getRunQueue() {
3565 RunQueue rq = sRunQueues.get();
3566 if (rq != null) {
3567 return rq;
3568 }
3569 rq = new RunQueue();
3570 sRunQueues.set(rq);
3571 return rq;
3572 }
Romain Guy8506ab42009-06-11 17:35:47 -07003573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 /**
3575 * @hide
3576 */
3577 static final class RunQueue {
3578 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3579
3580 void post(Runnable action) {
3581 postDelayed(action, 0);
3582 }
3583
3584 void postDelayed(Runnable action, long delayMillis) {
3585 HandlerAction handlerAction = new HandlerAction();
3586 handlerAction.action = action;
3587 handlerAction.delay = delayMillis;
3588
3589 synchronized (mActions) {
3590 mActions.add(handlerAction);
3591 }
3592 }
3593
3594 void removeCallbacks(Runnable action) {
3595 final HandlerAction handlerAction = new HandlerAction();
3596 handlerAction.action = action;
3597
3598 synchronized (mActions) {
3599 final ArrayList<HandlerAction> actions = mActions;
3600
3601 while (actions.remove(handlerAction)) {
3602 // Keep going
3603 }
3604 }
3605 }
3606
3607 void executeActions(Handler handler) {
3608 synchronized (mActions) {
3609 final ArrayList<HandlerAction> actions = mActions;
3610 final int count = actions.size();
3611
3612 for (int i = 0; i < count; i++) {
3613 final HandlerAction handlerAction = actions.get(i);
3614 handler.postDelayed(handlerAction.action, handlerAction.delay);
3615 }
3616
Romain Guy15df6702009-08-17 20:17:30 -07003617 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003618 }
3619 }
3620
3621 private static class HandlerAction {
3622 Runnable action;
3623 long delay;
3624
3625 @Override
3626 public boolean equals(Object o) {
3627 if (this == o) return true;
3628 if (o == null || getClass() != o.getClass()) return false;
3629
3630 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 return !(action != null ? !action.equals(that.action) : that.action != null);
3632
3633 }
3634
3635 @Override
3636 public int hashCode() {
3637 int result = action != null ? action.hashCode() : 0;
3638 result = 31 * result + (int) (delay ^ (delay >>> 32));
3639 return result;
3640 }
3641 }
3642 }
3643
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003644 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003645}