blob: 06261bb03aafcddd2fee9d5a55918186713ee49e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Romain Guy6b7bd242010-10-06 19:49:23 -070019import android.Manifest;
20import android.app.ActivityManagerNative;
21import android.content.ClipDescription;
22import android.content.ComponentCallbacks;
23import android.content.Context;
24import android.content.pm.PackageManager;
25import android.content.res.CompatibilityInfo;
26import android.content.res.Configuration;
27import android.content.res.Resources;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.Canvas;
29import android.graphics.PixelFormat;
Christopher Tate2c095f32010-10-04 14:13:40 -070030import android.graphics.Point;
Christopher Tatea53146c2010-09-07 11:57:52 -070031import android.graphics.PointF;
Romain Guy6b7bd242010-10-06 19:49:23 -070032import android.graphics.PorterDuff;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Rect;
34import android.graphics.Region;
Romain Guy6b7bd242010-10-06 19:49:23 -070035import android.media.AudioManager;
36import android.os.Binder;
37import android.os.Bundle;
38import android.os.Debug;
39import android.os.Handler;
40import android.os.LatencyTimer;
41import android.os.Looper;
42import android.os.Message;
43import android.os.ParcelFileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Process;
Romain Guy6b7bd242010-10-06 19:49:23 -070045import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.SystemClock;
48import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.util.AndroidRuntimeException;
50import android.util.Config;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -070051import android.util.DisplayMetrics;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.EventLog;
Romain Guy6b7bd242010-10-06 19:49:23 -070053import android.util.Log;
Chet Haase949dbf72010-08-11 18:41:06 -070054import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.view.accessibility.AccessibilityEvent;
58import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.inputmethod.InputConnection;
60import android.view.inputmethod.InputMethodManager;
61import android.widget.Scroller;
Romain Guy6b7bd242010-10-06 19:49:23 -070062import com.android.internal.view.BaseSurfaceHolder;
63import com.android.internal.view.IInputMethodCallback;
64import com.android.internal.view.IInputMethodSession;
65import com.android.internal.view.RootViewSurfaceTaker;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import java.io.IOException;
68import java.io.OutputStream;
Romain Guy6b7bd242010-10-06 19:49:23 -070069import java.lang.ref.WeakReference;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import java.util.ArrayList;
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072/**
73 * The top of a view hierarchy, implementing the needed protocol between View
74 * and the WindowManager. This is for the most part an internal implementation
75 * detail of {@link WindowManagerImpl}.
76 *
77 * {@hide}
78 */
Romain Guy812ccbe2010-06-01 14:07:24 -070079@SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"})
80public final class ViewRoot extends Handler implements ViewParent, View.AttachInfo.Callbacks {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 private static final String TAG = "ViewRoot";
82 private static final boolean DBG = false;
Mike Reedfd716532009-10-12 14:42:56 -040083 private static final boolean SHOW_FPS = false;
Romain Guy812ccbe2010-06-01 14:07:24 -070084 private static final boolean LOCAL_LOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /** @noinspection PointlessBooleanExpression*/
86 private static final boolean DEBUG_DRAW = false || LOCAL_LOGV;
87 private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV;
Christopher Tatefa9e7c02010-05-06 12:07:10 -070088 private static final boolean DEBUG_INPUT = true || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV;
90 private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV;
91 private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV;
92 private static final boolean DEBUG_IMF = false || LOCAL_LOGV;
Dianne Hackborn694f79b2010-03-17 19:44:59 -070093 private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 private static final boolean WATCH_POINTER = false;
95
Michael Chan53071d62009-05-13 17:29:48 -070096 private static final boolean MEASURE_LATENCY = false;
97 private static LatencyTimer lt;
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
100 * Maximum time we allow the user to roll the trackball enough to generate
101 * a key event, before resetting the counters.
102 */
103 static final int MAX_TRACKBALL_DELAY = 250;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 static IWindowSession sWindowSession;
106
107 static final Object mStaticInit = new Object();
108 static boolean mInitialized = false;
109
110 static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>();
111
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800112 static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList<Runnable>();
113 static boolean sFirstDrawComplete = false;
114
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800115 static final ArrayList<ComponentCallbacks> sConfigCallbacks
116 = new ArrayList<ComponentCallbacks>();
117
Romain Guy8506ab42009-06-11 17:35:47 -0700118 private static int sDrawTime;
Romain Guy13922e02009-05-12 17:56:14 -0700119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 long mLastTrackballTime = 0;
121 final TrackballAxis mTrackballAxisX = new TrackballAxis();
122 final TrackballAxis mTrackballAxisY = new TrackballAxis();
123
124 final int[] mTmpLocation = new int[2];
Romain Guy8506ab42009-06-11 17:35:47 -0700125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 final InputMethodCallback mInputMethodCallback;
127 final SparseArray<Object> mPendingEvents = new SparseArray<Object>();
128 int mPendingEventSeq = 0;
Romain Guy8506ab42009-06-11 17:35:47 -0700129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final Thread mThread;
131
132 final WindowLeaked mLocation;
133
134 final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams();
135
136 final W mWindow;
137
138 View mView;
139 View mFocusedView;
140 View mRealFocusedView; // this is not set to null in touch mode
141 int mViewVisibility;
142 boolean mAppVisible = true;
143
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700144 SurfaceHolder.Callback2 mSurfaceHolderCallback;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700145 BaseSurfaceHolder mSurfaceHolder;
146 boolean mIsCreating;
147 boolean mDrawingAllowed;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 final Region mTransparentRegion;
150 final Region mPreviousTransparentRegion;
151
152 int mWidth;
153 int mHeight;
154 Rect mDirty; // will be a graphics.Region soon
Romain Guybb93d552009-03-24 21:04:15 -0700155 boolean mIsAnimating;
Romain Guy8506ab42009-06-11 17:35:47 -0700156
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700157 CompatibilityInfo.Translator mTranslator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 final View.AttachInfo mAttachInfo;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700160 InputChannel mInputChannel;
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700161 InputQueue.Callback mInputQueueCallback;
162 InputQueue mInputQueue;
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 final Rect mTempRect; // used in the transaction to not thrash the heap.
165 final Rect mVisRect; // used to retrieve visible rect of focused view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166
167 boolean mTraversalScheduled;
168 boolean mWillDrawSoon;
169 boolean mLayoutRequested;
170 boolean mFirst;
171 boolean mReportNextDraw;
172 boolean mFullRedrawNeeded;
173 boolean mNewSurfaceNeeded;
174 boolean mHasHadWindowFocus;
175 boolean mLastWasImTarget;
176
177 boolean mWindowAttributesChanged = false;
178
179 // These can be accessed by any thread, must be protected with a lock.
Mathias Agopian5583dc62009-07-09 16:28:11 -0700180 // Surface can never be reassigned or cleared (use Surface.clear()).
181 private final Surface mSurface = new Surface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182
183 boolean mAdded;
184 boolean mAddedTouchMode;
185
186 /*package*/ int mAddNesting;
187
188 // These are accessed by multiple threads.
189 final Rect mWinFrame; // frame given by window manager.
190
191 final Rect mPendingVisibleInsets = new Rect();
192 final Rect mPendingContentInsets = new Rect();
193 final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets
194 = new ViewTreeObserver.InternalInsetsInfo();
195
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700196 final Configuration mLastConfiguration = new Configuration();
197 final Configuration mPendingConfiguration = new Configuration();
198
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800199 class ResizedInfo {
200 Rect coveredInsets;
201 Rect visibleInsets;
202 Configuration newConfig;
203 }
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 boolean mScrollMayChange;
206 int mSoftInputMode;
207 View mLastScrolledFocus;
208 int mScrollY;
209 int mCurScrollY;
210 Scroller mScroller;
Romain Guy8506ab42009-06-11 17:35:47 -0700211
Romain Guy8506ab42009-06-11 17:35:47 -0700212 final ViewConfiguration mViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
Christopher Tatea53146c2010-09-07 11:57:52 -0700214 /* Drag/drop */
215 ClipDescription mDragDescription;
216 View mCurrentDragView;
217 final PointF mDragPoint = new PointF();
Christopher Tate2c095f32010-10-04 14:13:40 -0700218 final PointF mLastTouchPoint = new PointF();
Christopher Tatea53146c2010-09-07 11:57:52 -0700219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 /**
221 * see {@link #playSoundEffect(int)}
222 */
223 AudioManager mAudioManager;
224
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700225 private final int mDensity;
Adam Powellb08013c2010-09-16 16:28:11 -0700226
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700227 public static IWindowSession getWindowSession(Looper mainLooper) {
228 synchronized (mStaticInit) {
229 if (!mInitialized) {
230 try {
231 InputMethodManager imm = InputMethodManager.getInstance(mainLooper);
232 sWindowSession = IWindowManager.Stub.asInterface(
233 ServiceManager.getService("window"))
234 .openSession(imm.getClient(), imm.getInputContext());
235 mInitialized = true;
236 } catch (RemoteException e) {
237 }
238 }
239 return sWindowSession;
240 }
241 }
242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 public ViewRoot(Context context) {
244 super();
245
Romain Guy812ccbe2010-06-01 14:07:24 -0700246 if (MEASURE_LATENCY) {
247 if (lt == null) {
248 lt = new LatencyTimer(100, 1000);
249 }
Michael Chan53071d62009-05-13 17:29:48 -0700250 }
251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 // Initialize the statics when this class is first instantiated. This is
253 // done here instead of in the static block because Zygote does not
254 // allow the spawning of threads.
Dianne Hackborn4c62fc02009-08-08 20:40:27 -0700255 getWindowSession(context.getMainLooper());
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 mThread = Thread.currentThread();
258 mLocation = new WindowLeaked(null);
259 mLocation.fillInStackTrace();
260 mWidth = -1;
261 mHeight = -1;
262 mDirty = new Rect();
263 mTempRect = new Rect();
264 mVisRect = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mWinFrame = new Rect();
Romain Guyfb8b7632010-08-23 21:05:08 -0700266 mWindow = new W(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 mInputMethodCallback = new InputMethodCallback(this);
268 mViewVisibility = View.GONE;
269 mTransparentRegion = new Region();
270 mPreviousTransparentRegion = new Region();
271 mFirst = true; // true for the first time the view is added
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mAdded = false;
273 mAttachInfo = new View.AttachInfo(sWindowSession, mWindow, this, this);
274 mViewConfiguration = ViewConfiguration.get(context);
Dianne Hackborn11ea3342009-07-22 21:48:55 -0700275 mDensity = context.getResources().getDisplayMetrics().densityDpi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
Dianne Hackborn2a9094d2010-02-03 19:20:09 -0800278 public static void addFirstDrawHandler(Runnable callback) {
279 synchronized (sFirstDrawHandlers) {
280 if (!sFirstDrawComplete) {
281 sFirstDrawHandlers.add(callback);
282 }
283 }
284 }
285
Dianne Hackborne36d6e22010-02-17 19:46:25 -0800286 public static void addConfigCallback(ComponentCallbacks callback) {
287 synchronized (sConfigCallbacks) {
288 sConfigCallbacks.add(callback);
289 }
290 }
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 // FIXME for perf testing only
293 private boolean mProfile = false;
294
295 /**
296 * Call this to profile the next traversal call.
297 * FIXME for perf testing only. Remove eventually
298 */
299 public void profile() {
300 mProfile = true;
301 }
302
303 /**
304 * Indicates whether we are in touch mode. Calling this method triggers an IPC
305 * call and should be avoided whenever possible.
306 *
307 * @return True, if the device is in touch mode, false otherwise.
308 *
309 * @hide
310 */
311 static boolean isInTouchMode() {
312 if (mInitialized) {
313 try {
314 return sWindowSession.getInTouchMode();
315 } catch (RemoteException e) {
316 }
317 }
318 return false;
319 }
320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 /**
322 * We have one child
323 */
Romain Guye4d01122010-06-16 18:44:05 -0700324 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 synchronized (this) {
326 if (mView == null) {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700327 mView = view;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700328 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700329 attrs = mWindowAttributes;
Romain Guye4d01122010-06-16 18:44:05 -0700330
Romain Guy529b60a2010-08-03 18:05:47 -0700331 enableHardwareAcceleration(attrs);
Romain Guye4d01122010-06-16 18:44:05 -0700332
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700333 if (view instanceof RootViewSurfaceTaker) {
334 mSurfaceHolderCallback =
335 ((RootViewSurfaceTaker)view).willYouTakeTheSurface();
336 if (mSurfaceHolderCallback != null) {
337 mSurfaceHolder = new TakenSurfaceHolder();
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700338 mSurfaceHolder.setFormat(PixelFormat.UNKNOWN);
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700339 }
340 }
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700341 Resources resources = mView.getContext().getResources();
342 CompatibilityInfo compatibilityInfo = resources.getCompatibilityInfo();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700343 mTranslator = compatibilityInfo.getTranslator();
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700344
345 if (mTranslator != null || !compatibilityInfo.supportsScreen()) {
Mitsuru Oshima240f8a72009-07-22 20:39:14 -0700346 mSurface.setCompatibleDisplayMetrics(resources.getDisplayMetrics(),
347 mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700348 }
349
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700350 boolean restore = false;
Romain Guy35b38ce2009-10-07 13:38:55 -0700351 if (mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -0700352 restore = true;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700353 attrs.backup();
354 mTranslator.translateWindowLayout(attrs);
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700355 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700356 if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs);
357
Mitsuru Oshima1ecf5d22009-07-06 17:20:38 -0700358 if (!compatibilityInfo.supportsScreen()) {
359 attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
360 }
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 mSoftInputMode = attrs.softInputMode;
363 mWindowAttributesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 mAttachInfo.mRootView = view;
Romain Guy35b38ce2009-10-07 13:38:55 -0700365 mAttachInfo.mScalingRequired = mTranslator != null;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700366 mAttachInfo.mApplicationScale =
367 mTranslator == null ? 1.0f : mTranslator.applicationScale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 if (panelParentView != null) {
369 mAttachInfo.mPanelParentWindowToken
370 = panelParentView.getApplicationWindowToken();
371 }
372 mAdded = true;
373 int res; /* = WindowManagerImpl.ADD_OKAY; */
Romain Guy8506ab42009-06-11 17:35:47 -0700374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 // Schedule the first layout -before- adding to the window
376 // manager, to make sure we do the relayout before receiving
377 // any other events from the system.
378 requestLayout();
Jeff Brown46b9ac02010-04-22 18:58:52 -0700379 mInputChannel = new InputChannel();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 try {
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700381 res = sWindowSession.add(mWindow, mWindowAttributes,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700382 getHostVisibility(), mAttachInfo.mContentInsets,
383 mInputChannel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 } catch (RemoteException e) {
385 mAdded = false;
386 mView = null;
387 mAttachInfo.mRootView = null;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700388 mInputChannel = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 unscheduleTraversals();
390 throw new RuntimeException("Adding window failed", e);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700391 } finally {
392 if (restore) {
393 attrs.restore();
394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700396
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700397 if (mTranslator != null) {
398 mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700399 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800400 mPendingContentInsets.set(mAttachInfo.mContentInsets);
401 mPendingVisibleInsets.set(0, 0, 0, 0);
Jeff Brownc5ed5912010-07-14 18:48:53 -0700402 if (Config.LOGV) Log.v(TAG, "Added window " + mWindow);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 if (res < WindowManagerImpl.ADD_OKAY) {
404 mView = null;
405 mAttachInfo.mRootView = null;
406 mAdded = false;
407 unscheduleTraversals();
408 switch (res) {
409 case WindowManagerImpl.ADD_BAD_APP_TOKEN:
410 case WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN:
411 throw new WindowManagerImpl.BadTokenException(
412 "Unable to add window -- token " + attrs.token
413 + " is not valid; is your activity running?");
414 case WindowManagerImpl.ADD_NOT_APP_TOKEN:
415 throw new WindowManagerImpl.BadTokenException(
416 "Unable to add window -- token " + attrs.token
417 + " is not for an application");
418 case WindowManagerImpl.ADD_APP_EXITING:
419 throw new WindowManagerImpl.BadTokenException(
420 "Unable to add window -- app for token " + attrs.token
421 + " is exiting");
422 case WindowManagerImpl.ADD_DUPLICATE_ADD:
423 throw new WindowManagerImpl.BadTokenException(
424 "Unable to add window -- window " + mWindow
425 + " has already been added");
426 case WindowManagerImpl.ADD_STARTING_NOT_NEEDED:
427 // Silently ignore -- we would have just removed it
428 // right away, anyway.
429 return;
430 case WindowManagerImpl.ADD_MULTIPLE_SINGLETON:
431 throw new WindowManagerImpl.BadTokenException(
432 "Unable to add window " + mWindow +
433 " -- another window of this type already exists");
434 case WindowManagerImpl.ADD_PERMISSION_DENIED:
435 throw new WindowManagerImpl.BadTokenException(
436 "Unable to add window " + mWindow +
437 " -- permission denied for this window type");
438 }
439 throw new RuntimeException(
440 "Unable to add window -- unknown error code " + res);
441 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700442
Jeff Brown00fa7bd2010-07-02 15:37:36 -0700443 if (view instanceof RootViewSurfaceTaker) {
444 mInputQueueCallback =
445 ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue();
446 }
447 if (mInputQueueCallback != null) {
448 mInputQueue = new InputQueue(mInputChannel);
449 mInputQueueCallback.onInputQueueCreated(mInputQueue);
450 } else {
451 InputQueue.registerInputChannel(mInputChannel, mInputHandler,
452 Looper.myQueue());
Jeff Brown46b9ac02010-04-22 18:58:52 -0700453 }
454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 view.assignParent(this);
456 mAddedTouchMode = (res&WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE) != 0;
457 mAppVisible = (res&WindowManagerImpl.ADD_FLAG_APP_VISIBLE) != 0;
458 }
459 }
460 }
461
Romain Guy529b60a2010-08-03 18:05:47 -0700462 private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
Romain Guye4d01122010-06-16 18:44:05 -0700463 // Only enable hardware acceleration if we are not in the system process
464 // The window manager creates ViewRoots to display animated preview windows
465 // of launching apps and we don't want those to be hardware accelerated
Romain Guy52339202010-09-03 16:04:46 -0700466 if (!HardwareRenderer.sRendererDisabled) {
Romain Guye4d01122010-06-16 18:44:05 -0700467 // Try to enable hardware acceleration if requested
Romain Guy529b60a2010-08-03 18:05:47 -0700468 if (attrs != null &&
469 (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
Romain Guye4d01122010-06-16 18:44:05 -0700470 final boolean translucent = attrs.format != PixelFormat.OPAQUE;
Romain Guyb051e892010-09-28 19:09:36 -0700471 if (mAttachInfo.mHardwareRenderer != null) {
472 mAttachInfo.mHardwareRenderer.destroy(true);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700473 }
Romain Guyb051e892010-09-28 19:09:36 -0700474 mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent);
Romain Guy53ca03d2010-10-08 18:55:27 -0700475 mAttachInfo.mHardwareAccelerated = mAttachInfo.mHardwareRenderer != null;
Romain Guye4d01122010-06-16 18:44:05 -0700476 }
477 }
478 }
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 public View getView() {
481 return mView;
482 }
483
484 final WindowLeaked getLocation() {
485 return mLocation;
486 }
487
488 void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
489 synchronized (this) {
The Android Open Source Project10592532009-03-18 17:39:46 -0700490 int oldSoftInputMode = mWindowAttributes.softInputMode;
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700491 // preserve compatible window flag if exists.
492 int compatibleWindowFlag =
493 mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 mWindowAttributes.copyFrom(attrs);
Mitsuru Oshima5a2b91d2009-07-16 16:30:02 -0700495 mWindowAttributes.flags |= compatibleWindowFlag;
496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 if (newView) {
498 mSoftInputMode = attrs.softInputMode;
499 requestLayout();
500 }
The Android Open Source Project10592532009-03-18 17:39:46 -0700501 // Don't lose the mode we last auto-computed.
502 if ((attrs.softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
503 == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
504 mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode
505 & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST)
506 | (oldSoftInputMode
507 & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST);
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 mWindowAttributesChanged = true;
510 scheduleTraversals();
511 }
512 }
513
514 void handleAppVisibility(boolean visible) {
515 if (mAppVisible != visible) {
516 mAppVisible = visible;
517 scheduleTraversals();
518 }
519 }
520
521 void handleGetNewSurface() {
522 mNewSurfaceNeeded = true;
523 mFullRedrawNeeded = true;
524 scheduleTraversals();
525 }
526
527 /**
528 * {@inheritDoc}
529 */
530 public void requestLayout() {
531 checkThread();
532 mLayoutRequested = true;
533 scheduleTraversals();
534 }
535
536 /**
537 * {@inheritDoc}
538 */
539 public boolean isLayoutRequested() {
540 return mLayoutRequested;
541 }
542
543 public void invalidateChild(View child, Rect dirty) {
544 checkThread();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700545 if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty);
546 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -0700548 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700549 if (mCurScrollY != 0) {
Romain Guy1e095972009-07-07 11:22:45 -0700550 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700551 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700552 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -0700553 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700554 }
Romain Guy1e095972009-07-07 11:22:45 -0700555 if (mAttachInfo.mScalingRequired) {
556 dirty.inset(-1, -1);
557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 }
559 mDirty.union(dirty);
560 if (!mWillDrawSoon) {
561 scheduleTraversals();
562 }
563 }
Romain Guy0d9275e2010-10-26 14:22:30 -0700564
565 void invalidate() {
566 mDirty.set(0, 0, mWidth, mHeight);
567 scheduleTraversals();
568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569
570 public ViewParent getParent() {
571 return null;
572 }
573
574 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
575 invalidateChild(null, dirty);
576 return null;
577 }
578
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700579 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (child != mView) {
581 throw new RuntimeException("child is not mine, honest!");
582 }
583 // Note: don't apply scroll offset, because we want to know its
584 // visibility in the virtual canvas being given to the view hierarchy.
585 return r.intersect(0, 0, mWidth, mHeight);
586 }
587
588 public void bringChildToFront(View child) {
589 }
590
591 public void scheduleTraversals() {
592 if (!mTraversalScheduled) {
593 mTraversalScheduled = true;
594 sendEmptyMessage(DO_TRAVERSAL);
595 }
596 }
597
598 public void unscheduleTraversals() {
599 if (mTraversalScheduled) {
600 mTraversalScheduled = false;
601 removeMessages(DO_TRAVERSAL);
602 }
603 }
604
605 int getHostVisibility() {
606 return mAppVisible ? mView.getVisibility() : View.GONE;
607 }
Romain Guy8506ab42009-06-11 17:35:47 -0700608
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 private void performTraversals() {
610 // cache mView since it is used so much below...
611 final View host = mView;
612
613 if (DBG) {
614 System.out.println("======================================");
615 System.out.println("performTraversals");
616 host.debug();
617 }
618
619 if (host == null || !mAdded)
620 return;
621
622 mTraversalScheduled = false;
623 mWillDrawSoon = true;
624 boolean windowResizesToFitContent = false;
625 boolean fullRedrawNeeded = mFullRedrawNeeded;
626 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700627 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 WindowManager.LayoutParams lp = mWindowAttributes;
629
630 int desiredWindowWidth;
631 int desiredWindowHeight;
632 int childWidthMeasureSpec;
633 int childHeightMeasureSpec;
634
635 final View.AttachInfo attachInfo = mAttachInfo;
636
637 final int viewVisibility = getHostVisibility();
638 boolean viewVisibilityChanged = mViewVisibility != viewVisibility
639 || mNewSurfaceNeeded;
640
641 WindowManager.LayoutParams params = null;
642 if (mWindowAttributesChanged) {
643 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700644 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 params = lp;
646 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700647 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800648 if (mFirst) {
649 fullRedrawNeeded = true;
650 mLayoutRequested = true;
651
Romain Guy8506ab42009-06-11 17:35:47 -0700652 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700653 mView.getContext().getResources().getDisplayMetrics();
654 desiredWindowWidth = packageMetrics.widthPixels;
655 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656
657 // For the very first time, tell the view hierarchy that it
658 // is attached to the window. Note that at this point the surface
659 // object is not initialized to its backing store, but soon it
660 // will be (assuming the window is visible).
661 attachInfo.mSurface = mSurface;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700662 attachInfo.mTranslucentWindow = PixelFormat.formatHasAlpha(lp.format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 attachInfo.mHasWindowFocus = false;
664 attachInfo.mWindowVisibility = viewVisibility;
665 attachInfo.mRecomputeGlobalAttributes = false;
666 attachInfo.mKeepScreenOn = false;
667 viewVisibilityChanged = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700668 mLastConfiguration.setTo(host.getResources().getConfiguration());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 host.dispatchAttachedToWindow(attachInfo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -0700671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700673 desiredWindowWidth = frame.width();
674 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700676 if (DEBUG_ORIENTATION) Log.v(TAG,
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700677 "View " + host + " resized to: " + frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 fullRedrawNeeded = true;
679 mLayoutRequested = true;
680 windowResizesToFitContent = true;
681 }
682 }
683
684 if (viewVisibilityChanged) {
685 attachInfo.mWindowVisibility = viewVisibility;
686 host.dispatchWindowVisibilityChanged(viewVisibility);
687 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
Romain Guyb051e892010-09-28 19:09:36 -0700688 if (mAttachInfo.mHardwareRenderer != null) {
689 mAttachInfo.mHardwareRenderer.destroy(false);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692 if (viewVisibility == View.GONE) {
693 // After making a window gone, we will count it as being
694 // shown for the first time the next time it gets focus.
695 mHasHadWindowFocus = false;
696 }
697 }
698
699 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -0700700
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800701 if (mLayoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -0700702 // Execute enqueued actions on every layout in case a view that was detached
703 // enqueued an action after being detached
704 getRunQueue().executeActions(attachInfo.mHandler);
705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (mFirst) {
707 host.fitSystemWindows(mAttachInfo.mContentInsets);
708 // make sure touch mode code executes by setting cached value
709 // to opposite of the added touch mode.
710 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -0700711 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 } else {
713 if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) {
714 mAttachInfo.mContentInsets.set(mPendingContentInsets);
715 host.fitSystemWindows(mAttachInfo.mContentInsets);
716 insetsChanged = true;
717 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
718 + mAttachInfo.mContentInsets);
719 }
720 if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) {
721 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
722 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
723 + mAttachInfo.mVisibleInsets);
724 }
725 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
726 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
727 windowResizesToFitContent = true;
728
Romain Guy8506ab42009-06-11 17:35:47 -0700729 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700730 mView.getContext().getResources().getDisplayMetrics();
731 desiredWindowWidth = packageMetrics.widthPixels;
732 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734 }
735
736 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
737 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
738
739 // Ask host how big it wants to be
Jeff Brownc5ed5912010-07-14 18:48:53 -0700740 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 "Measuring " + host + " in display " + desiredWindowWidth
742 + "x" + desiredWindowHeight + "...");
743 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
744
745 if (DBG) {
746 System.out.println("======================================");
747 System.out.println("performTraversals -- after measure");
748 host.debug();
749 }
750 }
751
752 if (attachInfo.mRecomputeGlobalAttributes) {
753 //Log.i(TAG, "Computing screen on!");
754 attachInfo.mRecomputeGlobalAttributes = false;
755 boolean oldVal = attachInfo.mKeepScreenOn;
756 attachInfo.mKeepScreenOn = false;
757 host.dispatchCollectViewAttributes(0);
758 if (attachInfo.mKeepScreenOn != oldVal) {
759 params = lp;
760 //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn);
761 }
762 }
763
764 if (mFirst || attachInfo.mViewVisibilityChanged) {
765 attachInfo.mViewVisibilityChanged = false;
766 int resizeMode = mSoftInputMode &
767 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
768 // If we are in auto resize mode, then we need to determine
769 // what mode to use now.
770 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
771 final int N = attachInfo.mScrollContainers.size();
772 for (int i=0; i<N; i++) {
773 if (attachInfo.mScrollContainers.get(i).isShown()) {
774 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
775 }
776 }
777 if (resizeMode == 0) {
778 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
779 }
780 if ((lp.softInputMode &
781 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
782 lp.softInputMode = (lp.softInputMode &
783 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
784 resizeMode;
785 params = lp;
786 }
787 }
788 }
Romain Guy8506ab42009-06-11 17:35:47 -0700789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
791 if (!PixelFormat.formatHasAlpha(params.format)) {
792 params.format = PixelFormat.TRANSLUCENT;
793 }
794 }
795
796 boolean windowShouldResize = mLayoutRequested && windowResizesToFitContent
Romain Guy2e4f4262010-04-06 11:07:52 -0700797 && ((mWidth != host.mMeasuredWidth || mHeight != host.mMeasuredHeight)
798 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
799 frame.width() < desiredWindowWidth && frame.width() != mWidth)
800 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
801 frame.height() < desiredWindowHeight && frame.height() != mHeight));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802
803 final boolean computesInternalInsets =
804 attachInfo.mTreeObserver.hasComputeInternalInsetsListeners();
Romain Guy812ccbe2010-06-01 14:07:24 -0700805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 boolean insetsPending = false;
807 int relayoutResult = 0;
Romain Guy812ccbe2010-06-01 14:07:24 -0700808
809 if (mFirst || windowShouldResize || insetsChanged ||
810 viewVisibilityChanged || params != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811
812 if (viewVisibility == View.VISIBLE) {
813 // If this window is giving internal insets to the window
814 // manager, and it is being added or changing its visibility,
815 // then we want to first give the window manager "fake"
816 // insets to cause it to effectively ignore the content of
817 // the window during layout. This avoids it briefly causing
818 // other windows to resize/move based on the raw frame of the
819 // window, waiting until we can finish laying out this window
820 // and get back to the window manager with the ultimately
821 // computed insets.
Romain Guy812ccbe2010-06-01 14:07:24 -0700822 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823 }
824
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700825 if (mSurfaceHolder != null) {
826 mSurfaceHolder.mSurfaceLock.lock();
827 mDrawingAllowed = true;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700828 }
Romain Guy812ccbe2010-06-01 14:07:24 -0700829
Romain Guyc361da82010-10-25 15:29:10 -0700830 boolean hwInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800831 boolean contentInsetsChanged = false;
Romain Guy13922e02009-05-12 17:56:14 -0700832 boolean visibleInsetsChanged;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700833 boolean hadSurface = mSurface.isValid();
Romain Guy812ccbe2010-06-01 14:07:24 -0700834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 int fl = 0;
837 if (params != null) {
838 fl = params.flags;
839 if (attachInfo.mKeepScreenOn) {
840 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
841 }
842 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700843 if (DEBUG_LAYOUT) {
844 Log.i(TAG, "host=w:" + host.mMeasuredWidth + ", h:" +
845 host.mMeasuredHeight + ", params=" + params);
846 }
847 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
848
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 if (params != null) {
850 params.flags = fl;
851 }
852
853 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString()
854 + " content=" + mPendingContentInsets.toShortString()
855 + " visible=" + mPendingVisibleInsets.toShortString()
856 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -0700857
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700858 if (mPendingConfiguration.seq != 0) {
859 if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: "
860 + mPendingConfiguration);
861 updateConfiguration(mPendingConfiguration, !mFirst);
862 mPendingConfiguration.seq = 0;
863 }
864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 contentInsetsChanged = !mPendingContentInsets.equals(
866 mAttachInfo.mContentInsets);
867 visibleInsetsChanged = !mPendingVisibleInsets.equals(
868 mAttachInfo.mVisibleInsets);
869 if (contentInsetsChanged) {
870 mAttachInfo.mContentInsets.set(mPendingContentInsets);
871 host.fitSystemWindows(mAttachInfo.mContentInsets);
872 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
873 + mAttachInfo.mContentInsets);
874 }
875 if (visibleInsetsChanged) {
876 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
877 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
878 + mAttachInfo.mVisibleInsets);
879 }
880
881 if (!hadSurface) {
882 if (mSurface.isValid()) {
883 // If we are creating a new surface, then we need to
884 // completely redraw it. Also, when we get to the
885 // point of drawing it we will hold off and schedule
886 // a new traversal instead. This is so we can tell the
887 // window manager about all of the windows being displayed
888 // before actually drawing them, so it can display then
889 // all at once.
890 newSurface = true;
891 fullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -0700892 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -0700893
Romain Guyb051e892010-09-28 19:09:36 -0700894 if (mAttachInfo.mHardwareRenderer != null) {
Romain Guyc361da82010-10-25 15:29:10 -0700895 hwInitialized = mAttachInfo.mHardwareRenderer.initialize(mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
897 }
898 } else if (!mSurface.isValid()) {
899 // If the surface has been removed, then reset the scroll
900 // positions.
901 mLastScrolledFocus = null;
902 mScrollY = mCurScrollY = 0;
903 if (mScroller != null) {
904 mScroller.abortAnimation();
905 }
906 }
907 } catch (RemoteException e) {
908 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 if (DEBUG_ORIENTATION) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -0700911 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912
913 attachInfo.mWindowLeft = frame.left;
914 attachInfo.mWindowTop = frame.top;
915
916 // !!FIXME!! This next section handles the case where we did not get the
917 // window size we asked for. We should avoid this by getting a maximum size from
918 // the window session beforehand.
919 mWidth = frame.width();
920 mHeight = frame.height();
921
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700922 if (mSurfaceHolder != null) {
923 // The app owns the surface; tell it about what is going on.
924 if (mSurface.isValid()) {
925 // XXX .copyFrom() doesn't work!
926 //mSurfaceHolder.mSurface.copyFrom(mSurface);
927 mSurfaceHolder.mSurface = mSurface;
928 }
929 mSurfaceHolder.mSurfaceLock.unlock();
930 if (mSurface.isValid()) {
931 if (!hadSurface) {
932 mSurfaceHolder.ungetCallbacks();
933
934 mIsCreating = true;
935 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
936 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
937 if (callbacks != null) {
938 for (SurfaceHolder.Callback c : callbacks) {
939 c.surfaceCreated(mSurfaceHolder);
940 }
941 }
942 surfaceChanged = true;
Romain Guyc361da82010-10-25 15:29:10 -0700943
944 if (mAttachInfo.mHardwareRenderer != null) {
945 // This will bail out early if already initialized
946 mAttachInfo.mHardwareRenderer.initialize(mHolder);
947 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700948 }
949 if (surfaceChanged) {
950 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
951 lp.format, mWidth, mHeight);
952 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
953 if (callbacks != null) {
954 for (SurfaceHolder.Callback c : callbacks) {
955 c.surfaceChanged(mSurfaceHolder, lp.format,
956 mWidth, mHeight);
957 }
958 }
959 }
960 mIsCreating = false;
961 } else if (hadSurface) {
962 mSurfaceHolder.ungetCallbacks();
963 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
964 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
965 if (callbacks != null) {
966 for (SurfaceHolder.Callback c : callbacks) {
967 c.surfaceDestroyed(mSurfaceHolder);
968 }
969 }
970 mSurfaceHolder.mSurfaceLock.lock();
971 // Make surface invalid.
972 //mSurfaceHolder.mSurface.copyFrom(mSurface);
973 mSurfaceHolder.mSurface = new Surface();
974 mSurfaceHolder.mSurfaceLock.unlock();
975 }
976 }
Romain Guy53389bd2010-09-07 17:16:32 -0700977
Romain Guyc361da82010-10-25 15:29:10 -0700978 if (hwInitialized || (windowShouldResize && mAttachInfo.mHardwareRenderer != null)) {
Romain Guyb051e892010-09-28 19:09:36 -0700979 mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800980 }
981
982 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Romain Guy2d4cff62010-04-09 15:39:00 -0700983 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 if (focusChangedDueToTouchMode || mWidth != host.mMeasuredWidth
985 || mHeight != host.mMeasuredHeight || contentInsetsChanged) {
986 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
987 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
988
989 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
990 + mWidth + " measuredWidth=" + host.mMeasuredWidth
991 + " mHeight=" + mHeight
992 + " measuredHeight" + host.mMeasuredHeight
993 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -0700994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 // Ask host how big it wants to be
996 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
997
998 // Implementation of weights from WindowManager.LayoutParams
999 // We just grow the dimensions as needed and re-measure if
1000 // needs be
1001 int width = host.mMeasuredWidth;
1002 int height = host.mMeasuredHeight;
1003 boolean measureAgain = false;
1004
1005 if (lp.horizontalWeight > 0.0f) {
1006 width += (int) ((mWidth - width) * lp.horizontalWeight);
1007 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1008 MeasureSpec.EXACTLY);
1009 measureAgain = true;
1010 }
1011 if (lp.verticalWeight > 0.0f) {
1012 height += (int) ((mHeight - height) * lp.verticalWeight);
1013 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1014 MeasureSpec.EXACTLY);
1015 measureAgain = true;
1016 }
1017
1018 if (measureAgain) {
1019 if (DEBUG_LAYOUT) Log.v(TAG,
1020 "And hey let's measure once more: width=" + width
1021 + " height=" + height);
1022 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1023 }
1024
1025 mLayoutRequested = true;
1026 }
1027 }
1028
1029 final boolean didLayout = mLayoutRequested;
1030 boolean triggerGlobalLayoutListener = didLayout
1031 || attachInfo.mRecomputeGlobalAttributes;
1032 if (didLayout) {
1033 mLayoutRequested = false;
1034 mScrollMayChange = true;
1035 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001036 TAG, "Laying out " + host + " to (" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 host.mMeasuredWidth + ", " + host.mMeasuredHeight + ")");
Romain Guy13922e02009-05-12 17:56:14 -07001038 long startTime = 0L;
Romain Guy5429e1d2010-09-07 12:38:00 -07001039 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 startTime = SystemClock.elapsedRealtime();
1041 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
1043
Romain Guy13922e02009-05-12 17:56:14 -07001044 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1045 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
1046 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
1047 + "please refer to the logs with the tag "
1048 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1049 }
1050 }
1051
Romain Guy5429e1d2010-09-07 12:38:00 -07001052 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1054 }
1055
1056 // By this point all views have been sized and positionned
1057 // We can compute the transparent area
1058
1059 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1060 // start out transparent
1061 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1062 host.getLocationInWindow(mTmpLocation);
1063 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1064 mTmpLocation[0] + host.mRight - host.mLeft,
1065 mTmpLocation[1] + host.mBottom - host.mTop);
1066
1067 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001068 if (mTranslator != null) {
1069 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1070 }
1071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1073 mPreviousTransparentRegion.set(mTransparentRegion);
1074 // reconfigure window manager
1075 try {
1076 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1077 } catch (RemoteException e) {
1078 }
1079 }
1080 }
1081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 if (DBG) {
1083 System.out.println("======================================");
1084 System.out.println("performTraversals -- after setFrame");
1085 host.debug();
1086 }
1087 }
1088
1089 if (triggerGlobalLayoutListener) {
1090 attachInfo.mRecomputeGlobalAttributes = false;
1091 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1092 }
1093
1094 if (computesInternalInsets) {
1095 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1096 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1097 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1098 givenContent.left = givenContent.top = givenContent.right
1099 = givenContent.bottom = givenVisible.left = givenVisible.top
1100 = givenVisible.right = givenVisible.bottom = 0;
1101 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001102 Rect contentInsets = insets.contentInsets;
1103 Rect visibleInsets = insets.visibleInsets;
1104 if (mTranslator != null) {
1105 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1106 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1109 mLastGivenInsets.set(insets);
1110 try {
1111 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001112 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 } catch (RemoteException e) {
1114 }
1115 }
1116 }
Romain Guy8506ab42009-06-11 17:35:47 -07001117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 if (mFirst) {
1119 // handle first focus request
1120 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1121 + mView.hasFocus());
1122 if (mView != null) {
1123 if (!mView.hasFocus()) {
1124 mView.requestFocus(View.FOCUS_FORWARD);
1125 mFocusedView = mRealFocusedView = mView.findFocus();
1126 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1127 + mFocusedView);
1128 } else {
1129 mRealFocusedView = mView.findFocus();
1130 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1131 + mRealFocusedView);
1132 }
1133 }
1134 }
1135
1136 mFirst = false;
1137 mWillDrawSoon = false;
1138 mNewSurfaceNeeded = false;
1139 mViewVisibility = viewVisibility;
1140
1141 if (mAttachInfo.mHasWindowFocus) {
1142 final boolean imTarget = WindowManager.LayoutParams
1143 .mayUseInputMethod(mWindowAttributes.flags);
1144 if (imTarget != mLastWasImTarget) {
1145 mLastWasImTarget = imTarget;
1146 InputMethodManager imm = InputMethodManager.peekInstance();
1147 if (imm != null && imTarget) {
1148 imm.startGettingWindowFocus(mView);
1149 imm.onWindowFocus(mView, mView.findFocus(),
1150 mWindowAttributes.softInputMode,
1151 !mHasHadWindowFocus, mWindowAttributes.flags);
1152 }
1153 }
1154 }
Romain Guy8506ab42009-06-11 17:35:47 -07001155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1157
1158 if (!cancelDraw && !newSurface) {
1159 mFullRedrawNeeded = false;
1160 draw(fullRedrawNeeded);
1161
1162 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1163 || mReportNextDraw) {
1164 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001165 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 }
1167 mReportNextDraw = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001168 if (mSurfaceHolder != null && mSurface.isValid()) {
1169 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
1170 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1171 if (callbacks != null) {
1172 for (SurfaceHolder.Callback c : callbacks) {
1173 if (c instanceof SurfaceHolder.Callback2) {
1174 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
1175 mSurfaceHolder);
1176 }
1177 }
1178 }
1179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 try {
1181 sWindowSession.finishDrawing(mWindow);
1182 } catch (RemoteException e) {
1183 }
1184 }
1185 } else {
1186 // We were supposed to report when we are done drawing. Since we canceled the
1187 // draw, remember it here.
1188 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1189 mReportNextDraw = true;
1190 }
1191 if (fullRedrawNeeded) {
1192 mFullRedrawNeeded = true;
1193 }
1194 // Try again
1195 scheduleTraversals();
1196 }
1197 }
1198
1199 public void requestTransparentRegion(View child) {
1200 // the test below should not fail unless someone is messing with us
1201 checkThread();
1202 if (mView == child) {
1203 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1204 // Need to make sure we re-evaluate the window attributes next
1205 // time around, to ensure the window has the correct format.
1206 mWindowAttributesChanged = true;
1207 }
1208 }
1209
1210 /**
1211 * Figures out the measure spec for the root view in a window based on it's
1212 * layout params.
1213 *
1214 * @param windowSize
1215 * The available width or height of the window
1216 *
1217 * @param rootDimension
1218 * The layout params for one dimension (width or height) of the
1219 * window.
1220 *
1221 * @return The measure spec to use to measure the root view.
1222 */
1223 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1224 int measureSpec;
1225 switch (rootDimension) {
1226
Romain Guy980a9382010-01-08 15:06:28 -08001227 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 // Window can't resize. Force root view to be windowSize.
1229 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1230 break;
1231 case ViewGroup.LayoutParams.WRAP_CONTENT:
1232 // Window can resize. Set max size for root view.
1233 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1234 break;
1235 default:
1236 // Window wants to be an exact size. Force root view to be that size.
1237 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1238 break;
1239 }
1240 return measureSpec;
1241 }
1242
1243 private void draw(boolean fullRedrawNeeded) {
1244 Surface surface = mSurface;
1245 if (surface == null || !surface.isValid()) {
1246 return;
1247 }
1248
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001249 if (!sFirstDrawComplete) {
1250 synchronized (sFirstDrawHandlers) {
1251 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07001252 final int count = sFirstDrawHandlers.size();
1253 for (int i = 0; i< count; i++) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001254 post(sFirstDrawHandlers.get(i));
1255 }
1256 }
1257 }
1258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 scrollToRectOrFocus(null, false);
1260
1261 if (mAttachInfo.mViewScrollChanged) {
1262 mAttachInfo.mViewScrollChanged = false;
1263 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1264 }
Romain Guy8506ab42009-06-11 17:35:47 -07001265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 int yoff;
Romain Guy5bcdff42009-05-14 21:27:18 -07001267 final boolean scrolling = mScroller != null && mScroller.computeScrollOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 if (scrolling) {
1269 yoff = mScroller.getCurrY();
1270 } else {
1271 yoff = mScrollY;
1272 }
1273 if (mCurScrollY != yoff) {
1274 mCurScrollY = yoff;
1275 fullRedrawNeeded = true;
1276 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001277 float appScale = mAttachInfo.mApplicationScale;
1278 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279
1280 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001281 if (mSurfaceHolder != null) {
1282 // The app owns the surface, we won't draw.
1283 dirty.setEmpty();
1284 return;
1285 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07001286
1287 if (fullRedrawNeeded) {
1288 mAttachInfo.mIgnoreDirtyState = true;
1289 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1290 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001291
Romain Guyb051e892010-09-28 19:09:36 -07001292 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guyfd507262010-10-10 15:42:49 -07001293 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy101e2ae2010-10-11 12:41:21 -07001294 mIsAnimating = false;
Romain Guyfd507262010-10-10 15:42:49 -07001295 dirty.setEmpty();
Romain Guy101e2ae2010-10-11 12:41:21 -07001296 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, yoff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 if (scrolling) {
1300 mFullRedrawNeeded = true;
1301 scheduleTraversals();
1302 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 return;
1305 }
1306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001308 Log.v(TAG, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 + mWindowAttributes.getTitle()
1310 + ": dirty={" + dirty.left + "," + dirty.top
1311 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001312 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1313 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 }
1315
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001316 if (!dirty.isEmpty() || mIsAnimating) {
1317 Canvas canvas;
1318 try {
1319 int left = dirty.left;
1320 int top = dirty.top;
1321 int right = dirty.right;
1322 int bottom = dirty.bottom;
1323 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001324
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001325 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1326 bottom != dirty.bottom) {
1327 mAttachInfo.mIgnoreDirtyState = true;
1328 }
1329
1330 // TODO: Do this in native
1331 canvas.setDensity(mDensity);
1332 } catch (Surface.OutOfResourcesException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001333 Log.e(TAG, "OutOfResourcesException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001334 // TODO: we should ask the window manager to do something!
1335 // for now we just do nothing
1336 return;
1337 } catch (IllegalArgumentException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001338 Log.e(TAG, "IllegalArgumentException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001339 // TODO: we should ask the window manager to do something!
1340 // for now we just do nothing
1341 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001342 }
1343
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001344 try {
1345 if (!dirty.isEmpty() || mIsAnimating) {
1346 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001348 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001349 Log.v(TAG, "Surface " + surface + " drawing to bitmap w="
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001350 + canvas.getWidth() + ", h=" + canvas.getHeight());
1351 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353
Romain Guy5429e1d2010-09-07 12:38:00 -07001354 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001355 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001357
1358 // If this bitmap's format includes an alpha channel, we
1359 // need to clear it before drawing so that the child will
1360 // properly re-composite its drawing on a transparent
1361 // background. This automatically respects the clip/dirty region
1362 // or
1363 // If we are applying an offset, we need to clear the area
1364 // where the offset doesn't appear to avoid having garbage
1365 // left in the blank areas.
1366 if (!canvas.isOpaque() || yoff != 0) {
1367 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1368 }
1369
1370 dirty.setEmpty();
1371 mIsAnimating = false;
1372 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1373 mView.mPrivateFlags |= View.DRAWN;
1374
1375 if (DEBUG_DRAW) {
1376 Context cxt = mView.getContext();
1377 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1378 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1379 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1380 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001381 try {
1382 canvas.translate(0, -yoff);
1383 if (mTranslator != null) {
1384 mTranslator.translateCanvas(canvas);
1385 }
1386 canvas.setScreenDensity(scalingRequired
1387 ? DisplayMetrics.DENSITY_DEVICE : 0);
1388 mView.draw(canvas);
1389 } finally {
1390 mAttachInfo.mIgnoreDirtyState = false;
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001391 }
1392
1393 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1394 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1395 }
1396
Romain Guy5429e1d2010-09-07 12:38:00 -07001397 if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001398 int now = (int)SystemClock.elapsedRealtime();
1399 if (sDrawTime != 0) {
1400 nativeShowFPS(canvas, now - sDrawTime);
1401 }
1402 sDrawTime = now;
1403 }
1404
Romain Guy5429e1d2010-09-07 12:38:00 -07001405 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001406 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
1409
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001410 } finally {
1411 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414
1415 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001416 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
Romain Guy8506ab42009-06-11 17:35:47 -07001418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 if (scrolling) {
1420 mFullRedrawNeeded = true;
1421 scheduleTraversals();
1422 }
1423 }
1424
1425 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1426 final View.AttachInfo attachInfo = mAttachInfo;
1427 final Rect ci = attachInfo.mContentInsets;
1428 final Rect vi = attachInfo.mVisibleInsets;
1429 int scrollY = 0;
1430 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 if (vi.left > ci.left || vi.top > ci.top
1433 || vi.right > ci.right || vi.bottom > ci.bottom) {
1434 // We'll assume that we aren't going to change the scroll
1435 // offset, since we want to avoid that unless it is actually
1436 // going to make the focus visible... otherwise we scroll
1437 // all over the place.
1438 scrollY = mScrollY;
1439 // We can be called for two different situations: during a draw,
1440 // to update the scroll position if the focus has changed (in which
1441 // case 'rectangle' is null), or in response to a
1442 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1443 // is non-null and we just want to scroll to whatever that
1444 // rectangle is).
1445 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001446
1447 // When in touch mode, focus points to the previously focused view,
1448 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001449 // line checks whether the view is still in our hierarchy.
1450 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001451 mRealFocusedView = null;
1452 return false;
1453 }
1454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001455 if (focus != mLastScrolledFocus) {
1456 // If the focus has changed, then ignore any requests to scroll
1457 // to a rectangle; first we want to make sure the entire focus
1458 // view is visible.
1459 rectangle = null;
1460 }
1461 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1462 + " rectangle=" + rectangle + " ci=" + ci
1463 + " vi=" + vi);
1464 if (focus == mLastScrolledFocus && !mScrollMayChange
1465 && rectangle == null) {
1466 // Optimization: if the focus hasn't changed since last
1467 // time, and no layout has happened, then just leave things
1468 // as they are.
1469 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1470 + mScrollY + " vi=" + vi.toShortString());
1471 } else if (focus != null) {
1472 // We need to determine if the currently focused view is
1473 // within the visible part of the window and, if not, apply
1474 // a pan so it can be seen.
1475 mLastScrolledFocus = focus;
1476 mScrollMayChange = false;
1477 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1478 // Try to find the rectangle from the focus view.
1479 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1480 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1481 + mView.getWidth() + " h=" + mView.getHeight()
1482 + " ci=" + ci.toShortString()
1483 + " vi=" + vi.toShortString());
1484 if (rectangle == null) {
1485 focus.getFocusedRect(mTempRect);
1486 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1487 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001488 if (mView instanceof ViewGroup) {
1489 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1490 focus, mTempRect);
1491 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001492 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1493 "Focus in window: focusRect="
1494 + mTempRect.toShortString()
1495 + " visRect=" + mVisRect.toShortString());
1496 } else {
1497 mTempRect.set(rectangle);
1498 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1499 "Request scroll to rect: "
1500 + mTempRect.toShortString()
1501 + " visRect=" + mVisRect.toShortString());
1502 }
1503 if (mTempRect.intersect(mVisRect)) {
1504 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1505 "Focus window visible rect: "
1506 + mTempRect.toShortString());
1507 if (mTempRect.height() >
1508 (mView.getHeight()-vi.top-vi.bottom)) {
1509 // If the focus simply is not going to fit, then
1510 // best is probably just to leave things as-is.
1511 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1512 "Too tall; leaving scrollY=" + scrollY);
1513 } else if ((mTempRect.top-scrollY) < vi.top) {
1514 scrollY -= vi.top - (mTempRect.top-scrollY);
1515 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1516 "Top covered; scrollY=" + scrollY);
1517 } else if ((mTempRect.bottom-scrollY)
1518 > (mView.getHeight()-vi.bottom)) {
1519 scrollY += (mTempRect.bottom-scrollY)
1520 - (mView.getHeight()-vi.bottom);
1521 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1522 "Bottom covered; scrollY=" + scrollY);
1523 }
1524 handled = true;
1525 }
1526 }
1527 }
1528 }
Romain Guy8506ab42009-06-11 17:35:47 -07001529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001530 if (scrollY != mScrollY) {
1531 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1532 + mScrollY + " , new=" + scrollY);
1533 if (!immediate) {
1534 if (mScroller == null) {
1535 mScroller = new Scroller(mView.getContext());
1536 }
1537 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1538 } else if (mScroller != null) {
1539 mScroller.abortAnimation();
1540 }
1541 mScrollY = scrollY;
1542 }
Romain Guy8506ab42009-06-11 17:35:47 -07001543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 return handled;
1545 }
Romain Guy8506ab42009-06-11 17:35:47 -07001546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 public void requestChildFocus(View child, View focused) {
1548 checkThread();
1549 if (mFocusedView != focused) {
1550 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1551 scheduleTraversals();
1552 }
1553 mFocusedView = mRealFocusedView = focused;
1554 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1555 + mFocusedView);
1556 }
1557
1558 public void clearChildFocus(View child) {
1559 checkThread();
1560
1561 View oldFocus = mFocusedView;
1562
1563 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1564 mFocusedView = mRealFocusedView = null;
1565 if (mView != null && !mView.hasFocus()) {
1566 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1567 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1568 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1569 }
1570 } else if (oldFocus != null) {
1571 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1572 }
1573 }
1574
1575
1576 public void focusableViewAvailable(View v) {
1577 checkThread();
1578
1579 if (mView != null && !mView.hasFocus()) {
1580 v.requestFocus();
1581 } else {
1582 // the one case where will transfer focus away from the current one
1583 // is if the current view is a view group that prefers to give focus
1584 // to its children first AND the view is a descendant of it.
1585 mFocusedView = mView.findFocus();
1586 boolean descendantsHaveDibsOnFocus =
1587 (mFocusedView instanceof ViewGroup) &&
1588 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1589 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1590 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1591 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1592 v.requestFocus();
1593 }
1594 }
1595 }
1596
1597 public void recomputeViewAttributes(View child) {
1598 checkThread();
1599 if (mView == child) {
1600 mAttachInfo.mRecomputeGlobalAttributes = true;
1601 if (!mWillDrawSoon) {
1602 scheduleTraversals();
1603 }
1604 }
1605 }
1606
1607 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001608 if (mView != null) {
1609 mView.dispatchDetachedFromWindow();
1610 }
1611
1612 mView = null;
1613 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001614 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615
Romain Guy29d89972010-09-22 16:10:57 -07001616 destroyHardwareRenderer();
Romain Guy4caa4ed2010-08-25 14:46:24 -07001617
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001618 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001620 if (mInputChannel != null) {
1621 if (mInputQueueCallback != null) {
1622 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
1623 mInputQueueCallback = null;
1624 } else {
1625 InputQueue.unregisterInputChannel(mInputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001626 }
1627 }
1628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 try {
1630 sWindowSession.remove(mWindow);
1631 } catch (RemoteException e) {
1632 }
Jeff Brown349703e2010-06-22 01:27:15 -07001633
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001634 // Dispose the input channel after removing the window so the Window Manager
1635 // doesn't interpret the input channel being closed as an abnormal termination.
1636 if (mInputChannel != null) {
1637 mInputChannel.dispose();
1638 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 }
Romain Guy8506ab42009-06-11 17:35:47 -07001641
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001642 void updateConfiguration(Configuration config, boolean force) {
1643 if (DEBUG_CONFIGURATION) Log.v(TAG,
1644 "Applying new config to window "
1645 + mWindowAttributes.getTitle()
1646 + ": " + config);
1647 synchronized (sConfigCallbacks) {
1648 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1649 sConfigCallbacks.get(i).onConfigurationChanged(config);
1650 }
1651 }
1652 if (mView != null) {
1653 // At this point the resources have been updated to
1654 // have the most recent config, whatever that is. Use
1655 // the on in them which may be newer.
1656 if (mView != null) {
1657 config = mView.getResources().getConfiguration();
1658 }
1659 if (force || mLastConfiguration.diff(config) != 0) {
1660 mLastConfiguration.setTo(config);
1661 mView.dispatchConfigurationChanged(config);
1662 }
1663 }
1664 }
1665
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001666 /**
1667 * Return true if child is an ancestor of parent, (or equal to the parent).
1668 */
1669 private static boolean isViewDescendantOf(View child, View parent) {
1670 if (child == parent) {
1671 return true;
1672 }
1673
1674 final ViewParent theParent = child.getParent();
1675 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1676 }
1677
Romain Guycdb86672010-03-18 18:54:50 -07001678 private static void forceLayout(View view) {
1679 view.forceLayout();
1680 if (view instanceof ViewGroup) {
1681 ViewGroup group = (ViewGroup) view;
1682 final int count = group.getChildCount();
1683 for (int i = 0; i < count; i++) {
1684 forceLayout(group.getChildAt(i));
1685 }
1686 }
1687 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688
1689 public final static int DO_TRAVERSAL = 1000;
1690 public final static int DIE = 1001;
1691 public final static int RESIZED = 1002;
1692 public final static int RESIZED_REPORT = 1003;
1693 public final static int WINDOW_FOCUS_CHANGED = 1004;
1694 public final static int DISPATCH_KEY = 1005;
1695 public final static int DISPATCH_POINTER = 1006;
1696 public final static int DISPATCH_TRACKBALL = 1007;
1697 public final static int DISPATCH_APP_VISIBILITY = 1008;
1698 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1699 public final static int FINISHED_EVENT = 1010;
1700 public final static int DISPATCH_KEY_FROM_IME = 1011;
1701 public final static int FINISH_INPUT_CONNECTION = 1012;
1702 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001703 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Christopher Tatea53146c2010-09-07 11:57:52 -07001704 public final static int DISPATCH_DRAG_EVENT = 1015;
Chris Tate91e9bb32010-10-12 12:58:43 -07001705 public final static int DISPATCH_DRAG_LOCATION_EVENT = 1016;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706
1707 @Override
1708 public void handleMessage(Message msg) {
1709 switch (msg.what) {
1710 case View.AttachInfo.INVALIDATE_MSG:
1711 ((View) msg.obj).invalidate();
1712 break;
1713 case View.AttachInfo.INVALIDATE_RECT_MSG:
1714 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1715 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1716 info.release();
1717 break;
1718 case DO_TRAVERSAL:
1719 if (mProfile) {
1720 Debug.startMethodTracing("ViewRoot");
1721 }
1722
1723 performTraversals();
1724
1725 if (mProfile) {
1726 Debug.stopMethodTracing();
1727 mProfile = false;
1728 }
1729 break;
1730 case FINISHED_EVENT:
1731 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1732 break;
1733 case DISPATCH_KEY:
1734 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001735 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 + msg.obj + " to " + mView);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001737 deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001738 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001739 case DISPATCH_POINTER: {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001740 MotionEvent event = (MotionEvent) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 try {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001742 deliverPointerEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 } finally {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001744 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001745 if (msg.arg1 != 0) {
1746 finishInputEvent();
1747 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001750 } break;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001751 case DISPATCH_TRACKBALL: {
1752 MotionEvent event = (MotionEvent) msg.obj;
1753 try {
1754 deliverTrackballEvent(event);
1755 } finally {
1756 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001757 if (msg.arg1 != 0) {
1758 finishInputEvent();
1759 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001760 }
1761 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 case DISPATCH_APP_VISIBILITY:
1763 handleAppVisibility(msg.arg1 != 0);
1764 break;
1765 case DISPATCH_GET_NEW_SURFACE:
1766 handleGetNewSurface();
1767 break;
1768 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001769 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001772 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001773 && mPendingVisibleInsets.equals(ri.visibleInsets)
1774 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 break;
1776 }
1777 // fall through...
1778 case RESIZED_REPORT:
1779 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001780 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1781 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001782 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001784 mWinFrame.left = 0;
1785 mWinFrame.right = msg.arg1;
1786 mWinFrame.top = 0;
1787 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001788 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1789 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 if (msg.what == RESIZED_REPORT) {
1791 mReportNextDraw = true;
1792 }
Romain Guycdb86672010-03-18 18:54:50 -07001793
1794 if (mView != null) {
1795 forceLayout(mView);
1796 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001797 requestLayout();
1798 }
1799 break;
1800 case WINDOW_FOCUS_CHANGED: {
1801 if (mAdded) {
1802 boolean hasWindowFocus = msg.arg1 != 0;
1803 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1804 if (hasWindowFocus) {
1805 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001806 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807
Romain Guyc361da82010-10-25 15:29:10 -07001808 if (mAttachInfo.mHardwareRenderer != null &&
1809 mSurface != null && mSurface.isValid()) {
Romain Guyb051e892010-09-28 19:09:36 -07001810 mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
1811 mAttachInfo, mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 }
1813 }
Romain Guy8506ab42009-06-11 17:35:47 -07001814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 mLastWasImTarget = WindowManager.LayoutParams
1816 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 InputMethodManager imm = InputMethodManager.peekInstance();
1819 if (mView != null) {
1820 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1821 imm.startGettingWindowFocus(mView);
1822 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001823 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 mView.dispatchWindowFocusChanged(hasWindowFocus);
1825 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 // Note: must be done after the focus change callbacks,
1828 // so all of the view state is set up correctly.
1829 if (hasWindowFocus) {
1830 if (imm != null && mLastWasImTarget) {
1831 imm.onWindowFocus(mView, mView.findFocus(),
1832 mWindowAttributes.softInputMode,
1833 !mHasHadWindowFocus, mWindowAttributes.flags);
1834 }
1835 // Clear the forward bit. We can just do this directly, since
1836 // the window manager doesn't care about it.
1837 mWindowAttributes.softInputMode &=
1838 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1839 ((WindowManager.LayoutParams)mView.getLayoutParams())
1840 .softInputMode &=
1841 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1842 mHasHadWindowFocus = true;
1843 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001844
1845 if (hasWindowFocus && mView != null) {
1846 sendAccessibilityEvents();
1847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
1849 } break;
1850 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07001851 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001853 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001855 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07001857 KeyEvent event = (KeyEvent)msg.obj;
1858 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
1859 // The IME is trying to say this event is from the
1860 // system! Bad bad bad!
Romain Guy812ccbe2010-06-01 14:07:24 -07001861 event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project10592532009-03-18 17:39:46 -07001862 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 deliverKeyEventToViewHierarchy((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07001864 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 case FINISH_INPUT_CONNECTION: {
1866 InputMethodManager imm = InputMethodManager.peekInstance();
1867 if (imm != null) {
1868 imm.reportFinishInputConnection((InputConnection)msg.obj);
1869 }
1870 } break;
1871 case CHECK_FOCUS: {
1872 InputMethodManager imm = InputMethodManager.peekInstance();
1873 if (imm != null) {
1874 imm.checkFocus();
1875 }
1876 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001877 case CLOSE_SYSTEM_DIALOGS: {
1878 if (mView != null) {
1879 mView.onCloseSystemDialogs((String)msg.obj);
1880 }
1881 } break;
Chris Tate91e9bb32010-10-12 12:58:43 -07001882 case DISPATCH_DRAG_EVENT:
1883 case DISPATCH_DRAG_LOCATION_EVENT: {
Christopher Tatea53146c2010-09-07 11:57:52 -07001884 handleDragEvent((DragEvent)msg.obj);
1885 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
1887 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001888
Jeff Brown93ed4e32010-09-23 13:51:48 -07001889 private void startInputEvent(Runnable finishedCallback) {
1890 if (mFinishedCallback != null) {
1891 Slog.w(TAG, "Received a new input event from the input queue but there is "
1892 + "already an unfinished input event in progress.");
1893 }
1894
1895 mFinishedCallback = finishedCallback;
1896 }
1897
1898 private void finishInputEvent() {
1899 if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished");
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001900
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001901 if (mFinishedCallback != null) {
1902 mFinishedCallback.run();
1903 mFinishedCallback = null;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001904 } else {
Jeff Brown93ed4e32010-09-23 13:51:48 -07001905 Slog.w(TAG, "Attempted to tell the input queue that the current input event "
1906 + "is finished but there is no input event actually in progress.");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001907 }
1908 }
1909
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001910 /**
1911 * Something in the current window tells us we need to change the touch mode. For
1912 * example, we are not in touch mode, and the user touches the screen.
1913 *
1914 * If the touch mode has changed, tell the window manager, and handle it locally.
1915 *
1916 * @param inTouchMode Whether we want to be in touch mode.
1917 * @return True if the touch mode changed and focus changed was changed as a result
1918 */
1919 boolean ensureTouchMode(boolean inTouchMode) {
1920 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
1921 + "touch mode is " + mAttachInfo.mInTouchMode);
1922 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1923
1924 // tell the window manager
1925 try {
1926 sWindowSession.setInTouchMode(inTouchMode);
1927 } catch (RemoteException e) {
1928 throw new RuntimeException(e);
1929 }
1930
1931 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07001932 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934
1935 /**
1936 * Ensure that the touch mode for this window is set, and if it is changing,
1937 * take the appropriate action.
1938 * @param inTouchMode Whether we want to be in touch mode.
1939 * @return True if the touch mode changed and focus changed was changed as a result
1940 */
Romain Guy2d4cff62010-04-09 15:39:00 -07001941 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
1943 + "touch mode is " + mAttachInfo.mInTouchMode);
1944
1945 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1946
1947 mAttachInfo.mInTouchMode = inTouchMode;
1948 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
1949
Romain Guy2d4cff62010-04-09 15:39:00 -07001950 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001951 }
1952
1953 private boolean enterTouchMode() {
1954 if (mView != null) {
1955 if (mView.hasFocus()) {
1956 // note: not relying on mFocusedView here because this could
1957 // be when the window is first being added, and mFocused isn't
1958 // set yet.
1959 final View focused = mView.findFocus();
1960 if (focused != null && !focused.isFocusableInTouchMode()) {
1961
1962 final ViewGroup ancestorToTakeFocus =
1963 findAncestorToTakeFocusInTouchMode(focused);
1964 if (ancestorToTakeFocus != null) {
1965 // there is an ancestor that wants focus after its descendants that
1966 // is focusable in touch mode.. give it focus
1967 return ancestorToTakeFocus.requestFocus();
1968 } else {
1969 // nothing appropriate to have focus in touch mode, clear it out
1970 mView.unFocus();
1971 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
1972 mFocusedView = null;
1973 return true;
1974 }
1975 }
1976 }
1977 }
1978 return false;
1979 }
1980
1981
1982 /**
1983 * Find an ancestor of focused that wants focus after its descendants and is
1984 * focusable in touch mode.
1985 * @param focused The currently focused view.
1986 * @return An appropriate view, or null if no such view exists.
1987 */
1988 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
1989 ViewParent parent = focused.getParent();
1990 while (parent instanceof ViewGroup) {
1991 final ViewGroup vgParent = (ViewGroup) parent;
1992 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
1993 && vgParent.isFocusableInTouchMode()) {
1994 return vgParent;
1995 }
1996 if (vgParent.isRootNamespace()) {
1997 return null;
1998 } else {
1999 parent = vgParent.getParent();
2000 }
2001 }
2002 return null;
2003 }
2004
2005 private boolean leaveTouchMode() {
2006 if (mView != null) {
2007 if (mView.hasFocus()) {
2008 // i learned the hard way to not trust mFocusedView :)
2009 mFocusedView = mView.findFocus();
2010 if (!(mFocusedView instanceof ViewGroup)) {
2011 // some view has focus, let it keep it
2012 return false;
2013 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2014 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2015 // some view group has focus, and doesn't prefer its children
2016 // over itself for focus, so let them keep it.
2017 return false;
2018 }
2019 }
2020
2021 // find the best view to give focus to in this brave new non-touch-mode
2022 // world
2023 final View focused = focusSearch(null, View.FOCUS_DOWN);
2024 if (focused != null) {
2025 return focused.requestFocus(View.FOCUS_DOWN);
2026 }
2027 }
2028 return false;
2029 }
2030
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002031 private void deliverPointerEvent(MotionEvent event) {
2032 if (mTranslator != null) {
2033 mTranslator.translateEventInScreenToAppWindow(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002035
2036 boolean handled;
2037 if (mView != null && mAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002039 // enter touch mode on the down
2040 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
2041 if (isDown) {
2042 ensureTouchMode(true);
2043 }
2044 if(Config.LOGV) {
2045 captureMotionLog("captureDispatchPointer", event);
2046 }
2047 if (mCurScrollY != 0) {
2048 event.offsetLocation(0, mCurScrollY);
2049 }
2050 if (MEASURE_LATENCY) {
2051 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
2052 }
Christopher Tate2c095f32010-10-04 14:13:40 -07002053 // cache for possible drag-initiation
2054 mLastTouchPoint.x = event.getRawX();
2055 mLastTouchPoint.y = event.getRawY();
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002056 handled = mView.dispatchTouchEvent(event);
2057 if (MEASURE_LATENCY) {
2058 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
2059 }
2060 if (!handled && isDown) {
2061 int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
2062
2063 final int edgeFlags = event.getEdgeFlags();
2064 int direction = View.FOCUS_UP;
2065 int x = (int)event.getX();
2066 int y = (int)event.getY();
2067 final int[] deltas = new int[2];
2068
2069 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
2070 direction = View.FOCUS_DOWN;
2071 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2072 deltas[0] = edgeSlop;
2073 x += edgeSlop;
2074 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2075 deltas[0] = -edgeSlop;
2076 x -= edgeSlop;
2077 }
2078 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
2079 direction = View.FOCUS_UP;
2080 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2081 deltas[0] = edgeSlop;
2082 x += edgeSlop;
2083 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2084 deltas[0] = -edgeSlop;
2085 x -= edgeSlop;
2086 }
2087 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2088 direction = View.FOCUS_RIGHT;
2089 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2090 direction = View.FOCUS_LEFT;
2091 }
2092
2093 if (edgeFlags != 0 && mView instanceof ViewGroup) {
2094 View nearest = FocusFinder.getInstance().findNearestTouchable(
2095 ((ViewGroup) mView), x, y, direction, deltas);
2096 if (nearest != null) {
2097 event.offsetLocation(deltas[0], deltas[1]);
2098 event.setEdgeFlags(0);
2099 mView.dispatchTouchEvent(event);
2100 }
2101 }
2102 }
2103 }
2104 }
2105
2106 private void deliverTrackballEvent(MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2108
2109 boolean handled = false;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002110 if (mView != null && mAdded) {
2111 handled = mView.dispatchTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 if (handled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 // If we reach this, we delivered a trackball event to mView and
2114 // mView consumed it. Because we will not translate the trackball
2115 // event into a key event, touch mode will not exit, so we exit
2116 // touch mode here.
2117 ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 return;
2119 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002120
2121 // Otherwise we could do something here, like changing the focus
2122 // or something?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 }
2124
2125 final TrackballAxis x = mTrackballAxisX;
2126 final TrackballAxis y = mTrackballAxisY;
2127
2128 long curTime = SystemClock.uptimeMillis();
2129 if ((mLastTrackballTime+MAX_TRACKBALL_DELAY) < curTime) {
2130 // It has been too long since the last movement,
2131 // so restart at the beginning.
2132 x.reset(0);
2133 y.reset(0);
2134 mLastTrackballTime = curTime;
2135 }
2136
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002137 final int action = event.getAction();
2138 final int metastate = event.getMetaState();
2139 switch (action) {
2140 case MotionEvent.ACTION_DOWN:
2141 x.reset(2);
2142 y.reset(2);
2143 deliverKeyEvent(new KeyEvent(curTime, curTime,
2144 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER,
2145 0, metastate), false);
2146 break;
2147 case MotionEvent.ACTION_UP:
2148 x.reset(2);
2149 y.reset(2);
2150 deliverKeyEvent(new KeyEvent(curTime, curTime,
2151 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER,
2152 0, metastate), false);
2153 break;
2154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002156 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2157 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2158 + " move=" + event.getX()
2159 + " / Y=" + y.position + " step="
2160 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2161 + " move=" + event.getY());
2162 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2163 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002164
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002165 // Generate DPAD events based on the trackball movement.
2166 // We pick the axis that has moved the most as the direction of
2167 // the DPAD. When we generate DPAD events for one axis, then the
2168 // other axis is reset -- we don't want to perform DPAD jumps due
2169 // to slight movements in the trackball when making major movements
2170 // along the other axis.
2171 int keycode = 0;
2172 int movement = 0;
2173 float accel = 1;
2174 if (xOff > yOff) {
2175 movement = x.generate((2/event.getXPrecision()));
2176 if (movement != 0) {
2177 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2178 : KeyEvent.KEYCODE_DPAD_LEFT;
2179 accel = x.acceleration;
2180 y.reset(2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002181 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002182 } else if (yOff > 0) {
2183 movement = y.generate((2/event.getYPrecision()));
2184 if (movement != 0) {
2185 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2186 : KeyEvent.KEYCODE_DPAD_UP;
2187 accel = y.acceleration;
2188 x.reset(2);
2189 }
2190 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002191
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002192 if (keycode != 0) {
2193 if (movement < 0) movement = -movement;
2194 int accelMovement = (int)(movement * accel);
2195 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2196 + " accelMovement=" + accelMovement
2197 + " accel=" + accel);
2198 if (accelMovement > movement) {
2199 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2200 + keycode);
2201 movement--;
2202 deliverKeyEvent(new KeyEvent(curTime, curTime,
2203 KeyEvent.ACTION_MULTIPLE, keycode,
2204 accelMovement-movement, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002205 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002206 while (movement > 0) {
2207 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2208 + keycode);
2209 movement--;
2210 curTime = SystemClock.uptimeMillis();
2211 deliverKeyEvent(new KeyEvent(curTime, curTime,
2212 KeyEvent.ACTION_DOWN, keycode, 0, event.getMetaState()), false);
2213 deliverKeyEvent(new KeyEvent(curTime, curTime,
2214 KeyEvent.ACTION_UP, keycode, 0, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002216 mLastTrackballTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 }
2218 }
2219
2220 /**
2221 * @param keyCode The key code
2222 * @return True if the key is directional.
2223 */
2224 static boolean isDirectional(int keyCode) {
2225 switch (keyCode) {
2226 case KeyEvent.KEYCODE_DPAD_LEFT:
2227 case KeyEvent.KEYCODE_DPAD_RIGHT:
2228 case KeyEvent.KEYCODE_DPAD_UP:
2229 case KeyEvent.KEYCODE_DPAD_DOWN:
2230 return true;
2231 }
2232 return false;
2233 }
2234
2235 /**
2236 * Returns true if this key is a keyboard key.
2237 * @param keyEvent The key event.
2238 * @return whether this key is a keyboard key.
2239 */
2240 private static boolean isKeyboardKey(KeyEvent keyEvent) {
2241 final int convertedKey = keyEvent.getUnicodeChar();
2242 return convertedKey > 0;
2243 }
2244
2245
2246
2247 /**
2248 * See if the key event means we should leave touch mode (and leave touch
2249 * mode if so).
2250 * @param event The key event.
2251 * @return Whether this key event should be consumed (meaning the act of
2252 * leaving touch mode alone is considered the event).
2253 */
2254 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Adam Powell51a6bee2010-03-15 14:07:28 -07002255 final int action = event.getAction();
2256 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002257 return false;
2258 }
2259 if ((event.getFlags()&KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2260 return false;
2261 }
2262
2263 // only relevant if we are in touch mode
2264 if (!mAttachInfo.mInTouchMode) {
2265 return false;
2266 }
2267
2268 // if something like an edit text has focus and the user is typing,
2269 // leave touch mode
2270 //
2271 // note: the condition of not being a keyboard key is kind of a hacky
2272 // approximation of whether we think the focused view will want the
2273 // key; if we knew for sure whether the focused view would consume
2274 // the event, that would be better.
2275 if (isKeyboardKey(event) && mView != null && mView.hasFocus()) {
2276 mFocusedView = mView.findFocus();
2277 if ((mFocusedView instanceof ViewGroup)
2278 && ((ViewGroup) mFocusedView).getDescendantFocusability() ==
2279 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2280 // something has focus, but is holding it weakly as a container
2281 return false;
2282 }
2283 if (ensureTouchMode(false)) {
2284 throw new IllegalStateException("should not have changed focus "
2285 + "when leaving touch mode while a view has focus.");
2286 }
2287 return false;
2288 }
2289
2290 if (isDirectional(event.getKeyCode())) {
2291 // no view has focus, so we leave touch mode (and find something
2292 // to give focus to). the event is consumed if we were able to
2293 // find something to give focus to.
2294 return ensureTouchMode(false);
2295 }
2296 return false;
2297 }
2298
2299 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002300 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301 */
2302 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002303 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002304 if (ev == null ||
2305 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2306 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002307 }
Romain Guy8506ab42009-06-11 17:35:47 -07002308
2309 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 sb.append(ev.getDownTime()).append(',');
2311 sb.append(ev.getEventTime()).append(',');
2312 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002313 sb.append(ev.getX()).append(',');
2314 sb.append(ev.getY()).append(',');
2315 sb.append(ev.getPressure()).append(',');
2316 sb.append(ev.getSize()).append(',');
2317 sb.append(ev.getMetaState()).append(',');
2318 sb.append(ev.getXPrecision()).append(',');
2319 sb.append(ev.getYPrecision()).append(',');
2320 sb.append(ev.getDeviceId()).append(',');
2321 sb.append(ev.getEdgeFlags());
2322 Log.d(TAG, sb.toString());
2323 }
2324 /**
2325 * log motion events
2326 */
2327 private static void captureKeyLog(String subTag, KeyEvent ev) {
2328 //check dynamic switch
2329 if (ev == null ||
2330 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2331 return;
2332 }
2333 StringBuilder sb = new StringBuilder(subTag + ": ");
2334 sb.append(ev.getDownTime()).append(',');
2335 sb.append(ev.getEventTime()).append(',');
2336 sb.append(ev.getAction()).append(',');
2337 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002338 sb.append(ev.getRepeatCount()).append(',');
2339 sb.append(ev.getMetaState()).append(',');
2340 sb.append(ev.getDeviceId()).append(',');
2341 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002342 Log.d(TAG, sb.toString());
2343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002344
2345 int enqueuePendingEvent(Object event, boolean sendDone) {
2346 int seq = mPendingEventSeq+1;
2347 if (seq < 0) seq = 0;
2348 mPendingEventSeq = seq;
2349 mPendingEvents.put(seq, event);
2350 return sendDone ? seq : -seq;
2351 }
2352
2353 Object retrievePendingEvent(int seq) {
2354 if (seq < 0) seq = -seq;
2355 Object event = mPendingEvents.get(seq);
2356 if (event != null) {
2357 mPendingEvents.remove(seq);
2358 }
2359 return event;
2360 }
Romain Guy8506ab42009-06-11 17:35:47 -07002361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
2363 // If mView is null, we just consume the key event because it doesn't
2364 // make sense to do anything else with it.
Romain Guy812ccbe2010-06-01 14:07:24 -07002365 boolean handled = mView == null || mView.dispatchKeyEventPreIme(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 if (handled) {
2367 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002368 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002369 }
2370 return;
2371 }
2372 // If it is possible for this window to interact with the input
2373 // method window, then we want to first dispatch our key events
2374 // to the input method.
2375 if (mLastWasImTarget) {
2376 InputMethodManager imm = InputMethodManager.peekInstance();
2377 if (imm != null && mView != null) {
2378 int seq = enqueuePendingEvent(event, sendDone);
2379 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2380 + seq + " event=" + event);
2381 imm.dispatchKeyEvent(mView.getContext(), seq, event,
2382 mInputMethodCallback);
2383 return;
2384 }
2385 }
2386 deliverKeyEventToViewHierarchy(event, sendDone);
2387 }
2388
2389 void handleFinishedEvent(int seq, boolean handled) {
2390 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2391 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2392 + " handled=" + handled + " event=" + event);
2393 if (event != null) {
2394 final boolean sendDone = seq >= 0;
2395 if (!handled) {
2396 deliverKeyEventToViewHierarchy(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 } else if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002398 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002399 } else {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002400 Log.w(TAG, "handleFinishedEvent(seq=" + seq
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002401 + " handled=" + handled + " ev=" + event
2402 + ") neither delivering nor finishing key");
2403 }
2404 }
2405 }
Romain Guy8506ab42009-06-11 17:35:47 -07002406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002407 private void deliverKeyEventToViewHierarchy(KeyEvent event, boolean sendDone) {
2408 try {
2409 if (mView != null && mAdded) {
2410 final int action = event.getAction();
2411 boolean isDown = (action == KeyEvent.ACTION_DOWN);
2412
2413 if (checkForLeavingTouchModeAndConsume(event)) {
2414 return;
Romain Guy8506ab42009-06-11 17:35:47 -07002415 }
2416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002417 if (Config.LOGV) {
2418 captureKeyLog("captureDispatchKeyEvent", event);
2419 }
2420 boolean keyHandled = mView.dispatchKeyEvent(event);
2421
2422 if (!keyHandled && isDown) {
2423 int direction = 0;
2424 switch (event.getKeyCode()) {
2425 case KeyEvent.KEYCODE_DPAD_LEFT:
2426 direction = View.FOCUS_LEFT;
2427 break;
2428 case KeyEvent.KEYCODE_DPAD_RIGHT:
2429 direction = View.FOCUS_RIGHT;
2430 break;
2431 case KeyEvent.KEYCODE_DPAD_UP:
2432 direction = View.FOCUS_UP;
2433 break;
2434 case KeyEvent.KEYCODE_DPAD_DOWN:
2435 direction = View.FOCUS_DOWN;
2436 break;
2437 }
2438
2439 if (direction != 0) {
2440
2441 View focused = mView != null ? mView.findFocus() : null;
2442 if (focused != null) {
2443 View v = focused.focusSearch(direction);
2444 boolean focusPassed = false;
2445 if (v != null && v != focused) {
2446 // do the math the get the interesting rect
2447 // of previous focused into the coord system of
2448 // newly focused view
2449 focused.getFocusedRect(mTempRect);
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07002450 if (mView instanceof ViewGroup) {
2451 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2452 focused, mTempRect);
2453 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2454 v, mTempRect);
2455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002456 focusPassed = v.requestFocus(direction, mTempRect);
2457 }
2458
2459 if (!focusPassed) {
2460 mView.dispatchUnhandledMove(focused, direction);
2461 } else {
2462 playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
2463 }
2464 }
2465 }
2466 }
2467 }
2468
2469 } finally {
2470 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002471 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002472 }
2473 // Let the exception fall through -- the looper will catch
2474 // it and take care of the bad app for us.
2475 }
2476 }
2477
Christopher Tatea53146c2010-09-07 11:57:52 -07002478 /* drag/drop */
2479 private void handleDragEvent(DragEvent event) {
2480 // From the root, only drag start/end/location are dispatched. entered/exited
2481 // are determined and dispatched by the viewgroup hierarchy, who then report
2482 // that back here for ultimate reporting back to the framework.
2483 if (mView != null && mAdded) {
2484 final int what = event.mAction;
2485
2486 if (what == DragEvent.ACTION_DRAG_EXITED) {
2487 // A direct EXITED event means that the window manager knows we've just crossed
2488 // a window boundary, so the current drag target within this one must have
2489 // just been exited. Send it the usual notifications and then we're done
2490 // for now.
2491 setDragFocus(event, null);
2492 } else {
2493 // Cache the drag description when the operation starts, then fill it in
2494 // on subsequent calls as a convenience
2495 if (what == DragEvent.ACTION_DRAG_STARTED) {
2496 mDragDescription = event.mClipDescription;
2497 } else {
2498 event.mClipDescription = mDragDescription;
2499 }
2500
2501 // For events with a [screen] location, translate into window coordinates
2502 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
2503 mDragPoint.set(event.mX, event.mY);
2504 if (mTranslator != null) {
2505 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
2506 }
2507
2508 if (mCurScrollY != 0) {
2509 mDragPoint.offset(0, mCurScrollY);
2510 }
2511
2512 event.mX = mDragPoint.x;
2513 event.mY = mDragPoint.y;
2514 }
2515
2516 // Remember who the current drag target is pre-dispatch
2517 final View prevDragView = mCurrentDragView;
2518
2519 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07002520 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002521
2522 // If we changed apparent drag target, tell the OS about it
2523 if (prevDragView != mCurrentDragView) {
2524 try {
2525 if (prevDragView != null) {
2526 sWindowSession.dragRecipientExited(mWindow);
2527 }
2528 if (mCurrentDragView != null) {
2529 sWindowSession.dragRecipientEntered(mWindow);
2530 }
2531 } catch (RemoteException e) {
2532 Slog.e(TAG, "Unable to note drag target change");
2533 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002534 }
Chris Tated4533f12010-10-19 15:15:08 -07002535
2536 // Report the drop result if necessary
2537 if (what == DragEvent.ACTION_DROP) {
2538 try {
2539 Log.i(TAG, "Reporting drop result: " + result);
2540 sWindowSession.reportDropResult(mWindow, result);
2541 } catch (RemoteException e) {
2542 Log.e(TAG, "Unable to report drop result");
2543 }
2544 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002545 }
2546 }
2547 event.recycle();
2548 }
2549
Christopher Tate2c095f32010-10-04 14:13:40 -07002550 public void getLastTouchPoint(Point outLocation) {
2551 outLocation.x = (int) mLastTouchPoint.x;
2552 outLocation.y = (int) mLastTouchPoint.y;
2553 }
2554
Christopher Tatea53146c2010-09-07 11:57:52 -07002555 public void setDragFocus(DragEvent event, View newDragTarget) {
2556 final int action = event.mAction;
2557 // If we've dragged off of a view, send it the EXITED message
2558 if (mCurrentDragView != newDragTarget) {
2559 if (mCurrentDragView != null) {
2560 event.mAction = DragEvent.ACTION_DRAG_EXITED;
2561 mCurrentDragView.dispatchDragEvent(event);
2562 }
Chris Tate048691c2010-10-12 17:39:18 -07002563 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07002564 }
2565 // If we've dragged over a new view, send it the ENTERED message
2566 if (newDragTarget != null) {
2567 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
2568 newDragTarget.dispatchDragEvent(event);
2569 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002570 event.mAction = action; // restore the event's original state
2571 }
2572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002573 private AudioManager getAudioManager() {
2574 if (mView == null) {
2575 throw new IllegalStateException("getAudioManager called when there is no mView");
2576 }
2577 if (mAudioManager == null) {
2578 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2579 }
2580 return mAudioManager;
2581 }
2582
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002583 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2584 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002585
2586 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002587 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002588 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002589 restore = true;
2590 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002591 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002592 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002593 if (params != null) {
2594 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002595 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002596 mPendingConfiguration.seq = 0;
Dianne Hackbornf123e492010-09-24 11:16:23 -07002597 //Log.d(TAG, ">>>>>> CALLING relayout");
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002598 int relayoutResult = sWindowSession.relayout(
2599 mWindow, params,
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002600 (int) (mView.mMeasuredWidth * appScale + 0.5f),
2601 (int) (mView.mMeasuredHeight * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002602 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002603 mPendingContentInsets, mPendingVisibleInsets,
2604 mPendingConfiguration, mSurface);
Dianne Hackbornf123e492010-09-24 11:16:23 -07002605 //Log.d(TAG, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002606 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002607 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002608 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002609
2610 if (mTranslator != null) {
2611 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2612 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2613 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002614 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002615 return relayoutResult;
2616 }
Romain Guy8506ab42009-06-11 17:35:47 -07002617
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002618 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619 * {@inheritDoc}
2620 */
2621 public void playSoundEffect(int effectId) {
2622 checkThread();
2623
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002624 try {
2625 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002627 switch (effectId) {
2628 case SoundEffectConstants.CLICK:
2629 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2630 return;
2631 case SoundEffectConstants.NAVIGATION_DOWN:
2632 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2633 return;
2634 case SoundEffectConstants.NAVIGATION_LEFT:
2635 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2636 return;
2637 case SoundEffectConstants.NAVIGATION_RIGHT:
2638 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2639 return;
2640 case SoundEffectConstants.NAVIGATION_UP:
2641 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2642 return;
2643 default:
2644 throw new IllegalArgumentException("unknown effect id " + effectId +
2645 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2646 }
2647 } catch (IllegalStateException e) {
2648 // Exception thrown by getAudioManager() when mView is null
2649 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2650 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002651 }
2652 }
2653
2654 /**
2655 * {@inheritDoc}
2656 */
2657 public boolean performHapticFeedback(int effectId, boolean always) {
2658 try {
2659 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2660 } catch (RemoteException e) {
2661 return false;
2662 }
2663 }
2664
2665 /**
2666 * {@inheritDoc}
2667 */
2668 public View focusSearch(View focused, int direction) {
2669 checkThread();
2670 if (!(mView instanceof ViewGroup)) {
2671 return null;
2672 }
2673 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2674 }
2675
2676 public void debug() {
2677 mView.debug();
2678 }
2679
2680 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002681 if (immediate) {
2682 doDie();
2683 } else {
2684 sendEmptyMessage(DIE);
2685 }
2686 }
2687
2688 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002689 checkThread();
Jeff Brownb75fa302010-07-15 23:47:29 -07002690 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 synchronized (this) {
2692 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07002693 destroyHardwareRenderer();
2694
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002695 int viewVisibility = mView.getVisibility();
2696 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2697 if (mWindowAttributesChanged || viewVisibilityChanged) {
2698 // If layout params have been changed, first give them
2699 // to the window manager to make sure it has the correct
2700 // animation info.
2701 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002702 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2703 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 sWindowSession.finishDrawing(mWindow);
2705 }
2706 } catch (RemoteException e) {
2707 }
2708 }
2709
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002710 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002711 }
2712 if (mAdded) {
2713 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002714 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 }
2716 }
2717 }
2718
Romain Guy29d89972010-09-22 16:10:57 -07002719 private void destroyHardwareRenderer() {
Romain Guyb051e892010-09-28 19:09:36 -07002720 if (mAttachInfo.mHardwareRenderer != null) {
2721 mAttachInfo.mHardwareRenderer.destroy(true);
2722 mAttachInfo.mHardwareRenderer = null;
Romain Guy29d89972010-09-22 16:10:57 -07002723 mAttachInfo.mHardwareAccelerated = false;
2724 }
2725 }
2726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002727 public void dispatchFinishedEvent(int seq, boolean handled) {
2728 Message msg = obtainMessage(FINISHED_EVENT);
2729 msg.arg1 = seq;
2730 msg.arg2 = handled ? 1 : 0;
2731 sendMessage(msg);
2732 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002735 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2737 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2738 + " visibleInsets=" + visibleInsets.toShortString()
2739 + " reportDraw=" + reportDraw);
2740 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002741 if (mTranslator != null) {
2742 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2743 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2744 w *= mTranslator.applicationInvertedScale;
2745 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002746 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002747 msg.arg1 = w;
2748 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002749 ResizedInfo ri = new ResizedInfo();
2750 ri.coveredInsets = new Rect(coveredInsets);
2751 ri.visibleInsets = new Rect(visibleInsets);
2752 ri.newConfig = newConfig;
2753 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002754 sendMessage(msg);
2755 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002756
2757 private Runnable mFinishedCallback;
2758
2759 private final InputHandler mInputHandler = new InputHandler() {
2760 public void handleKey(KeyEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002761 startInputEvent(finishedCallback);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002762 dispatchKey(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002763 }
2764
Jeff Brownc5ed5912010-07-14 18:48:53 -07002765 public void handleMotion(MotionEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002766 startInputEvent(finishedCallback);
2767 dispatchMotion(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002768 }
2769 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002770
2771 public void dispatchKey(KeyEvent event) {
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002772 dispatchKey(event, false);
2773 }
2774
2775 private void dispatchKey(KeyEvent event, boolean sendDone) {
2776 //noinspection ConstantConditions
2777 if (false && event.getAction() == KeyEvent.ACTION_DOWN) {
2778 if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
Romain Guy812ccbe2010-06-01 14:07:24 -07002779 if (DBG) Log.d("keydisp", "===================================================");
2780 if (DBG) Log.d("keydisp", "Focused view Hierarchy is:");
2781
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002782 debug();
2783
Romain Guy812ccbe2010-06-01 14:07:24 -07002784 if (DBG) Log.d("keydisp", "===================================================");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002785 }
2786 }
2787
2788 Message msg = obtainMessage(DISPATCH_KEY);
2789 msg.obj = event;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002790 msg.arg1 = sendDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002791
2792 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07002793 TAG, "sending key " + event + " to " + mView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794
2795 sendMessageAtTime(msg, event.getEventTime());
2796 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002797
2798 public void dispatchMotion(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002799 dispatchMotion(event, false);
2800 }
2801
2802 private void dispatchMotion(MotionEvent event, boolean sendDone) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002803 int source = event.getSource();
2804 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002805 dispatchPointer(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002806 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002807 dispatchTrackball(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002808 } else {
2809 // TODO
2810 Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event);
Jeff Brown93ed4e32010-09-23 13:51:48 -07002811 if (sendDone) {
2812 finishInputEvent();
2813 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002814 }
2815 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002816
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002817 public void dispatchPointer(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002818 dispatchPointer(event, false);
2819 }
2820
2821 private void dispatchPointer(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002822 Message msg = obtainMessage(DISPATCH_POINTER);
2823 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002824 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002825 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 }
2827
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002828 public void dispatchTrackball(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002829 dispatchTrackball(event, false);
2830 }
2831
2832 private void dispatchTrackball(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2834 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002835 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002836 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002837 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002839 public void dispatchAppVisibility(boolean visible) {
2840 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
2841 msg.arg1 = visible ? 1 : 0;
2842 sendMessage(msg);
2843 }
2844
2845 public void dispatchGetNewSurface() {
2846 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
2847 sendMessage(msg);
2848 }
2849
2850 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2851 Message msg = Message.obtain();
2852 msg.what = WINDOW_FOCUS_CHANGED;
2853 msg.arg1 = hasFocus ? 1 : 0;
2854 msg.arg2 = inTouchMode ? 1 : 0;
2855 sendMessage(msg);
2856 }
2857
Dianne Hackbornffa42482009-09-23 22:20:11 -07002858 public void dispatchCloseSystemDialogs(String reason) {
2859 Message msg = Message.obtain();
2860 msg.what = CLOSE_SYSTEM_DIALOGS;
2861 msg.obj = reason;
2862 sendMessage(msg);
2863 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002864
2865 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07002866 final int what;
2867 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
2868 what = DISPATCH_DRAG_LOCATION_EVENT;
2869 removeMessages(what);
2870 } else {
2871 what = DISPATCH_DRAG_EVENT;
2872 }
2873 Message msg = obtainMessage(what, event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002874 sendMessage(msg);
2875 }
2876
svetoslavganov75986cf2009-05-14 22:28:01 -07002877 /**
2878 * The window is getting focus so if there is anything focused/selected
2879 * send an {@link AccessibilityEvent} to announce that.
2880 */
2881 private void sendAccessibilityEvents() {
2882 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
2883 return;
2884 }
2885 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2886 View focusedView = mView.findFocus();
2887 if (focusedView != null && focusedView != mView) {
2888 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2889 }
2890 }
2891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 public boolean showContextMenuForChild(View originalView) {
2893 return false;
2894 }
2895
Adam Powell6e346362010-07-23 10:18:23 -07002896 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
2897 return null;
2898 }
2899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 public void createContextMenu(ContextMenu menu) {
2901 }
2902
2903 public void childDrawableStateChanged(View child) {
2904 }
2905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002906 void checkThread() {
2907 if (mThread != Thread.currentThread()) {
2908 throw new CalledFromWrongThreadException(
2909 "Only the original thread that created a view hierarchy can touch its views.");
2910 }
2911 }
2912
2913 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2914 // ViewRoot never intercepts touch event, so this can be a no-op
2915 }
2916
2917 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
2918 boolean immediate) {
2919 return scrollToRectOrFocus(rectangle, immediate);
2920 }
Romain Guy8506ab42009-06-11 17:35:47 -07002921
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002922 class TakenSurfaceHolder extends BaseSurfaceHolder {
2923 @Override
2924 public boolean onAllowLockCanvas() {
2925 return mDrawingAllowed;
2926 }
2927
2928 @Override
2929 public void onRelayoutContainer() {
2930 // Not currently interesting -- from changing between fixed and layout size.
2931 }
2932
2933 public void setFormat(int format) {
2934 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
2935 }
2936
2937 public void setType(int type) {
2938 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
2939 }
2940
2941 @Override
2942 public void onUpdateSurface() {
2943 // We take care of format and type changes on our own.
2944 throw new IllegalStateException("Shouldn't be here");
2945 }
2946
2947 public boolean isCreating() {
2948 return mIsCreating;
2949 }
2950
2951 @Override
2952 public void setFixedSize(int width, int height) {
2953 throw new UnsupportedOperationException(
2954 "Currently only support sizing from layout");
2955 }
2956
2957 public void setKeepScreenOn(boolean screenOn) {
2958 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
2959 }
2960 }
2961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002962 static class InputMethodCallback extends IInputMethodCallback.Stub {
2963 private WeakReference<ViewRoot> mViewRoot;
2964
2965 public InputMethodCallback(ViewRoot viewRoot) {
2966 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2967 }
Romain Guy8506ab42009-06-11 17:35:47 -07002968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 public void finishedEvent(int seq, boolean handled) {
2970 final ViewRoot viewRoot = mViewRoot.get();
2971 if (viewRoot != null) {
2972 viewRoot.dispatchFinishedEvent(seq, handled);
2973 }
2974 }
2975
2976 public void sessionCreated(IInputMethodSession session) throws RemoteException {
2977 // Stub -- not for use in the client.
2978 }
2979 }
Romain Guy8506ab42009-06-11 17:35:47 -07002980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002981 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002982 private final WeakReference<ViewRoot> mViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002983
Romain Guyfb8b7632010-08-23 21:05:08 -07002984 W(ViewRoot viewRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2986 }
2987
Romain Guyfb8b7632010-08-23 21:05:08 -07002988 public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets,
2989 boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 final ViewRoot viewRoot = mViewRoot.get();
2991 if (viewRoot != null) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002992 viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002993 }
2994 }
2995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002996 public void dispatchAppVisibility(boolean visible) {
2997 final ViewRoot viewRoot = mViewRoot.get();
2998 if (viewRoot != null) {
2999 viewRoot.dispatchAppVisibility(visible);
3000 }
3001 }
3002
3003 public void dispatchGetNewSurface() {
3004 final ViewRoot viewRoot = mViewRoot.get();
3005 if (viewRoot != null) {
3006 viewRoot.dispatchGetNewSurface();
3007 }
3008 }
3009
3010 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3011 final ViewRoot viewRoot = mViewRoot.get();
3012 if (viewRoot != null) {
3013 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
3014 }
3015 }
3016
3017 private static int checkCallingPermission(String permission) {
3018 if (!Process.supportsProcesses()) {
3019 return PackageManager.PERMISSION_GRANTED;
3020 }
3021
3022 try {
3023 return ActivityManagerNative.getDefault().checkPermission(
3024 permission, Binder.getCallingPid(), Binder.getCallingUid());
3025 } catch (RemoteException e) {
3026 return PackageManager.PERMISSION_DENIED;
3027 }
3028 }
3029
3030 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3031 final ViewRoot viewRoot = mViewRoot.get();
3032 if (viewRoot != null) {
3033 final View view = viewRoot.mView;
3034 if (view != null) {
3035 if (checkCallingPermission(Manifest.permission.DUMP) !=
3036 PackageManager.PERMISSION_GRANTED) {
3037 throw new SecurityException("Insufficient permissions to invoke"
3038 + " executeCommand() from pid=" + Binder.getCallingPid()
3039 + ", uid=" + Binder.getCallingUid());
3040 }
3041
3042 OutputStream clientStream = null;
3043 try {
3044 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3045 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3046 } catch (IOException e) {
3047 e.printStackTrace();
3048 } finally {
3049 if (clientStream != null) {
3050 try {
3051 clientStream.close();
3052 } catch (IOException e) {
3053 e.printStackTrace();
3054 }
3055 }
3056 }
3057 }
3058 }
3059 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003060
Dianne Hackbornffa42482009-09-23 22:20:11 -07003061 public void closeSystemDialogs(String reason) {
3062 final ViewRoot viewRoot = mViewRoot.get();
3063 if (viewRoot != null) {
3064 viewRoot.dispatchCloseSystemDialogs(reason);
3065 }
3066 }
3067
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003068 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3069 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003070 if (sync) {
3071 try {
3072 sWindowSession.wallpaperOffsetsComplete(asBinder());
3073 } catch (RemoteException e) {
3074 }
3075 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003076 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003077
3078 public void dispatchWallpaperCommand(String action, int x, int y,
3079 int z, Bundle extras, boolean sync) {
3080 if (sync) {
3081 try {
3082 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3083 } catch (RemoteException e) {
3084 }
3085 }
3086 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003087
3088 /* Drag/drop */
3089 public void dispatchDragEvent(DragEvent event) {
3090 final ViewRoot viewRoot = mViewRoot.get();
3091 if (viewRoot != null) {
3092 viewRoot.dispatchDragEvent(event);
3093 }
3094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003095 }
3096
3097 /**
3098 * Maintains state information for a single trackball axis, generating
3099 * discrete (DPAD) movements based on raw trackball motion.
3100 */
3101 static final class TrackballAxis {
3102 /**
3103 * The maximum amount of acceleration we will apply.
3104 */
3105 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 /**
3108 * The maximum amount of time (in milliseconds) between events in order
3109 * for us to consider the user to be doing fast trackball movements,
3110 * and thus apply an acceleration.
3111 */
3112 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 /**
3115 * Scaling factor to the time (in milliseconds) between events to how
3116 * much to multiple/divide the current acceleration. When movement
3117 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3118 * FAST_MOVE_TIME it divides it.
3119 */
3120 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 float position;
3123 float absPosition;
3124 float acceleration = 1;
3125 long lastMoveTime = 0;
3126 int step;
3127 int dir;
3128 int nonAccelMovement;
3129
3130 void reset(int _step) {
3131 position = 0;
3132 acceleration = 1;
3133 lastMoveTime = 0;
3134 step = _step;
3135 dir = 0;
3136 }
3137
3138 /**
3139 * Add trackball movement into the state. If the direction of movement
3140 * has been reversed, the state is reset before adding the
3141 * movement (so that you don't have to compensate for any previously
3142 * collected movement before see the result of the movement in the
3143 * new direction).
3144 *
3145 * @return Returns the absolute value of the amount of movement
3146 * collected so far.
3147 */
3148 float collect(float off, long time, String axis) {
3149 long normTime;
3150 if (off > 0) {
3151 normTime = (long)(off * FAST_MOVE_TIME);
3152 if (dir < 0) {
3153 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3154 position = 0;
3155 step = 0;
3156 acceleration = 1;
3157 lastMoveTime = 0;
3158 }
3159 dir = 1;
3160 } else if (off < 0) {
3161 normTime = (long)((-off) * FAST_MOVE_TIME);
3162 if (dir > 0) {
3163 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3164 position = 0;
3165 step = 0;
3166 acceleration = 1;
3167 lastMoveTime = 0;
3168 }
3169 dir = -1;
3170 } else {
3171 normTime = 0;
3172 }
Romain Guy8506ab42009-06-11 17:35:47 -07003173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174 // The number of milliseconds between each movement that is
3175 // considered "normal" and will not result in any acceleration
3176 // or deceleration, scaled by the offset we have here.
3177 if (normTime > 0) {
3178 long delta = time - lastMoveTime;
3179 lastMoveTime = time;
3180 float acc = acceleration;
3181 if (delta < normTime) {
3182 // The user is scrolling rapidly, so increase acceleration.
3183 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3184 if (scale > 1) acc *= scale;
3185 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3186 + off + " normTime=" + normTime + " delta=" + delta
3187 + " scale=" + scale + " acc=" + acc);
3188 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3189 } else {
3190 // The user is scrolling slowly, so decrease acceleration.
3191 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3192 if (scale > 1) acc /= scale;
3193 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3194 + off + " normTime=" + normTime + " delta=" + delta
3195 + " scale=" + scale + " acc=" + acc);
3196 acceleration = acc > 1 ? acc : 1;
3197 }
3198 }
3199 position += off;
3200 return (absPosition = Math.abs(position));
3201 }
3202
3203 /**
3204 * Generate the number of discrete movement events appropriate for
3205 * the currently collected trackball movement.
3206 *
3207 * @param precision The minimum movement required to generate the
3208 * first discrete movement.
3209 *
3210 * @return Returns the number of discrete movements, either positive
3211 * or negative, or 0 if there is not enough trackball movement yet
3212 * for a discrete movement.
3213 */
3214 int generate(float precision) {
3215 int movement = 0;
3216 nonAccelMovement = 0;
3217 do {
3218 final int dir = position >= 0 ? 1 : -1;
3219 switch (step) {
3220 // If we are going to execute the first step, then we want
3221 // to do this as soon as possible instead of waiting for
3222 // a full movement, in order to make things look responsive.
3223 case 0:
3224 if (absPosition < precision) {
3225 return movement;
3226 }
3227 movement += dir;
3228 nonAccelMovement += dir;
3229 step = 1;
3230 break;
3231 // If we have generated the first movement, then we need
3232 // to wait for the second complete trackball motion before
3233 // generating the second discrete movement.
3234 case 1:
3235 if (absPosition < 2) {
3236 return movement;
3237 }
3238 movement += dir;
3239 nonAccelMovement += dir;
3240 position += dir > 0 ? -2 : 2;
3241 absPosition = Math.abs(position);
3242 step = 2;
3243 break;
3244 // After the first two, we generate discrete movements
3245 // consistently with the trackball, applying an acceleration
3246 // if the trackball is moving quickly. This is a simple
3247 // acceleration on top of what we already compute based
3248 // on how quickly the wheel is being turned, to apply
3249 // a longer increasing acceleration to continuous movement
3250 // in one direction.
3251 default:
3252 if (absPosition < 1) {
3253 return movement;
3254 }
3255 movement += dir;
3256 position += dir >= 0 ? -1 : 1;
3257 absPosition = Math.abs(position);
3258 float acc = acceleration;
3259 acc *= 1.1f;
3260 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3261 break;
3262 }
3263 } while (true);
3264 }
3265 }
3266
3267 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3268 public CalledFromWrongThreadException(String msg) {
3269 super(msg);
3270 }
3271 }
3272
3273 private SurfaceHolder mHolder = new SurfaceHolder() {
3274 // we only need a SurfaceHolder for opengl. it would be nice
3275 // to implement everything else though, especially the callback
3276 // support (opengl doesn't make use of it right now, but eventually
3277 // will).
3278 public Surface getSurface() {
3279 return mSurface;
3280 }
3281
3282 public boolean isCreating() {
3283 return false;
3284 }
3285
3286 public void addCallback(Callback callback) {
3287 }
3288
3289 public void removeCallback(Callback callback) {
3290 }
3291
3292 public void setFixedSize(int width, int height) {
3293 }
3294
3295 public void setSizeFromLayout() {
3296 }
3297
3298 public void setFormat(int format) {
3299 }
3300
3301 public void setType(int type) {
3302 }
3303
3304 public void setKeepScreenOn(boolean screenOn) {
3305 }
3306
3307 public Canvas lockCanvas() {
3308 return null;
3309 }
3310
3311 public Canvas lockCanvas(Rect dirty) {
3312 return null;
3313 }
3314
3315 public void unlockCanvasAndPost(Canvas canvas) {
3316 }
3317 public Rect getSurfaceFrame() {
3318 return null;
3319 }
3320 };
3321
3322 static RunQueue getRunQueue() {
3323 RunQueue rq = sRunQueues.get();
3324 if (rq != null) {
3325 return rq;
3326 }
3327 rq = new RunQueue();
3328 sRunQueues.set(rq);
3329 return rq;
3330 }
Romain Guy8506ab42009-06-11 17:35:47 -07003331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003332 /**
3333 * @hide
3334 */
3335 static final class RunQueue {
3336 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3337
3338 void post(Runnable action) {
3339 postDelayed(action, 0);
3340 }
3341
3342 void postDelayed(Runnable action, long delayMillis) {
3343 HandlerAction handlerAction = new HandlerAction();
3344 handlerAction.action = action;
3345 handlerAction.delay = delayMillis;
3346
3347 synchronized (mActions) {
3348 mActions.add(handlerAction);
3349 }
3350 }
3351
3352 void removeCallbacks(Runnable action) {
3353 final HandlerAction handlerAction = new HandlerAction();
3354 handlerAction.action = action;
3355
3356 synchronized (mActions) {
3357 final ArrayList<HandlerAction> actions = mActions;
3358
3359 while (actions.remove(handlerAction)) {
3360 // Keep going
3361 }
3362 }
3363 }
3364
3365 void executeActions(Handler handler) {
3366 synchronized (mActions) {
3367 final ArrayList<HandlerAction> actions = mActions;
3368 final int count = actions.size();
3369
3370 for (int i = 0; i < count; i++) {
3371 final HandlerAction handlerAction = actions.get(i);
3372 handler.postDelayed(handlerAction.action, handlerAction.delay);
3373 }
3374
Romain Guy15df6702009-08-17 20:17:30 -07003375 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 }
3377 }
3378
3379 private static class HandlerAction {
3380 Runnable action;
3381 long delay;
3382
3383 @Override
3384 public boolean equals(Object o) {
3385 if (this == o) return true;
3386 if (o == null || getClass() != o.getClass()) return false;
3387
3388 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 return !(action != null ? !action.equals(that.action) : that.action != null);
3390
3391 }
3392
3393 @Override
3394 public int hashCode() {
3395 int result = action != null ? action.hashCode() : 0;
3396 result = 31 * result + (int) (delay ^ (delay >>> 32));
3397 return result;
3398 }
3399 }
3400 }
3401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003402 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003403}