blob: 22a7773fd1e1fcfb9e905942c6ebd48b919dd881 [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);
Chet Haase70d4ba12010-10-06 09:46:45 -0700546 if (dirty == null) {
547 // Fast invalidation for GL-enabled applications; GL must redraw everything
548 invalidate();
549 return;
550 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700551 if (mCurScrollY != 0 || mTranslator != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 mTempRect.set(dirty);
Romain Guy1e095972009-07-07 11:22:45 -0700553 dirty = mTempRect;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700554 if (mCurScrollY != 0) {
Romain Guy1e095972009-07-07 11:22:45 -0700555 dirty.offset(0, -mCurScrollY);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700556 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700557 if (mTranslator != null) {
Romain Guy1e095972009-07-07 11:22:45 -0700558 mTranslator.translateRectInAppWindowToScreen(dirty);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700559 }
Romain Guy1e095972009-07-07 11:22:45 -0700560 if (mAttachInfo.mScalingRequired) {
561 dirty.inset(-1, -1);
562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564 mDirty.union(dirty);
565 if (!mWillDrawSoon) {
566 scheduleTraversals();
567 }
568 }
Romain Guy0d9275e2010-10-26 14:22:30 -0700569
570 void invalidate() {
571 mDirty.set(0, 0, mWidth, mHeight);
572 scheduleTraversals();
573 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574
575 public ViewParent getParent() {
576 return null;
577 }
578
579 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
580 invalidateChild(null, dirty);
581 return null;
582 }
583
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700584 public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 if (child != mView) {
586 throw new RuntimeException("child is not mine, honest!");
587 }
588 // Note: don't apply scroll offset, because we want to know its
589 // visibility in the virtual canvas being given to the view hierarchy.
590 return r.intersect(0, 0, mWidth, mHeight);
591 }
592
593 public void bringChildToFront(View child) {
594 }
595
596 public void scheduleTraversals() {
597 if (!mTraversalScheduled) {
598 mTraversalScheduled = true;
599 sendEmptyMessage(DO_TRAVERSAL);
600 }
601 }
602
603 public void unscheduleTraversals() {
604 if (mTraversalScheduled) {
605 mTraversalScheduled = false;
606 removeMessages(DO_TRAVERSAL);
607 }
608 }
609
610 int getHostVisibility() {
611 return mAppVisible ? mView.getVisibility() : View.GONE;
612 }
Romain Guy8506ab42009-06-11 17:35:47 -0700613
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 private void performTraversals() {
615 // cache mView since it is used so much below...
616 final View host = mView;
617
618 if (DBG) {
619 System.out.println("======================================");
620 System.out.println("performTraversals");
621 host.debug();
622 }
623
624 if (host == null || !mAdded)
625 return;
626
627 mTraversalScheduled = false;
628 mWillDrawSoon = true;
629 boolean windowResizesToFitContent = false;
630 boolean fullRedrawNeeded = mFullRedrawNeeded;
631 boolean newSurface = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700632 boolean surfaceChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800633 WindowManager.LayoutParams lp = mWindowAttributes;
634
635 int desiredWindowWidth;
636 int desiredWindowHeight;
637 int childWidthMeasureSpec;
638 int childHeightMeasureSpec;
639
640 final View.AttachInfo attachInfo = mAttachInfo;
641
642 final int viewVisibility = getHostVisibility();
643 boolean viewVisibilityChanged = mViewVisibility != viewVisibility
644 || mNewSurfaceNeeded;
645
646 WindowManager.LayoutParams params = null;
647 if (mWindowAttributesChanged) {
648 mWindowAttributesChanged = false;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700649 surfaceChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 params = lp;
651 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700652 Rect frame = mWinFrame;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 if (mFirst) {
654 fullRedrawNeeded = true;
655 mLayoutRequested = true;
656
Romain Guy8506ab42009-06-11 17:35:47 -0700657 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700658 mView.getContext().getResources().getDisplayMetrics();
659 desiredWindowWidth = packageMetrics.widthPixels;
660 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800661
662 // For the very first time, tell the view hierarchy that it
663 // is attached to the window. Note that at this point the surface
664 // object is not initialized to its backing store, but soon it
665 // will be (assuming the window is visible).
666 attachInfo.mSurface = mSurface;
Dianne Hackborn289b9b62010-07-09 11:44:11 -0700667 attachInfo.mTranslucentWindow = PixelFormat.formatHasAlpha(lp.format);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 attachInfo.mHasWindowFocus = false;
669 attachInfo.mWindowVisibility = viewVisibility;
670 attachInfo.mRecomputeGlobalAttributes = false;
671 attachInfo.mKeepScreenOn = false;
672 viewVisibilityChanged = false;
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700673 mLastConfiguration.setTo(host.getResources().getConfiguration());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 host.dispatchAttachedToWindow(attachInfo, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800675 //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn);
svetoslavganov75986cf2009-05-14 22:28:01 -0700676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 } else {
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700678 desiredWindowWidth = frame.width();
679 desiredWindowHeight = frame.height();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) {
Jeff Brownc5ed5912010-07-14 18:48:53 -0700681 if (DEBUG_ORIENTATION) Log.v(TAG,
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700682 "View " + host + " resized to: " + frame);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 fullRedrawNeeded = true;
684 mLayoutRequested = true;
685 windowResizesToFitContent = true;
686 }
687 }
688
689 if (viewVisibilityChanged) {
690 attachInfo.mWindowVisibility = viewVisibility;
691 host.dispatchWindowVisibilityChanged(viewVisibility);
692 if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
Romain Guyb051e892010-09-28 19:09:36 -0700693 if (mAttachInfo.mHardwareRenderer != null) {
694 mAttachInfo.mHardwareRenderer.destroy(false);
Romain Guy4caa4ed2010-08-25 14:46:24 -0700695 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 }
697 if (viewVisibility == View.GONE) {
698 // After making a window gone, we will count it as being
699 // shown for the first time the next time it gets focus.
700 mHasHadWindowFocus = false;
701 }
702 }
703
704 boolean insetsChanged = false;
Romain Guy8506ab42009-06-11 17:35:47 -0700705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (mLayoutRequested) {
Romain Guy15df6702009-08-17 20:17:30 -0700707 // Execute enqueued actions on every layout in case a view that was detached
708 // enqueued an action after being detached
709 getRunQueue().executeActions(attachInfo.mHandler);
710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 if (mFirst) {
712 host.fitSystemWindows(mAttachInfo.mContentInsets);
713 // make sure touch mode code executes by setting cached value
714 // to opposite of the added touch mode.
715 mAttachInfo.mInTouchMode = !mAddedTouchMode;
Romain Guy2d4cff62010-04-09 15:39:00 -0700716 ensureTouchModeLocally(mAddedTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 } else {
718 if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) {
719 mAttachInfo.mContentInsets.set(mPendingContentInsets);
720 host.fitSystemWindows(mAttachInfo.mContentInsets);
721 insetsChanged = true;
722 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
723 + mAttachInfo.mContentInsets);
724 }
725 if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) {
726 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
727 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
728 + mAttachInfo.mVisibleInsets);
729 }
730 if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT
731 || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
732 windowResizesToFitContent = true;
733
Romain Guy8506ab42009-06-11 17:35:47 -0700734 DisplayMetrics packageMetrics =
Mitsuru Oshima9189cab2009-06-03 11:19:12 -0700735 mView.getContext().getResources().getDisplayMetrics();
736 desiredWindowWidth = packageMetrics.widthPixels;
737 desiredWindowHeight = packageMetrics.heightPixels;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739 }
740
741 childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
742 childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
743
744 // Ask host how big it wants to be
Jeff Brownc5ed5912010-07-14 18:48:53 -0700745 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(TAG,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 "Measuring " + host + " in display " + desiredWindowWidth
747 + "x" + desiredWindowHeight + "...");
748 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
749
750 if (DBG) {
751 System.out.println("======================================");
752 System.out.println("performTraversals -- after measure");
753 host.debug();
754 }
755 }
756
757 if (attachInfo.mRecomputeGlobalAttributes) {
758 //Log.i(TAG, "Computing screen on!");
759 attachInfo.mRecomputeGlobalAttributes = false;
760 boolean oldVal = attachInfo.mKeepScreenOn;
761 attachInfo.mKeepScreenOn = false;
762 host.dispatchCollectViewAttributes(0);
763 if (attachInfo.mKeepScreenOn != oldVal) {
764 params = lp;
765 //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn);
766 }
767 }
768
769 if (mFirst || attachInfo.mViewVisibilityChanged) {
770 attachInfo.mViewVisibilityChanged = false;
771 int resizeMode = mSoftInputMode &
772 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
773 // If we are in auto resize mode, then we need to determine
774 // what mode to use now.
775 if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) {
776 final int N = attachInfo.mScrollContainers.size();
777 for (int i=0; i<N; i++) {
778 if (attachInfo.mScrollContainers.get(i).isShown()) {
779 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
780 }
781 }
782 if (resizeMode == 0) {
783 resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
784 }
785 if ((lp.softInputMode &
786 WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) {
787 lp.softInputMode = (lp.softInputMode &
788 ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
789 resizeMode;
790 params = lp;
791 }
792 }
793 }
Romain Guy8506ab42009-06-11 17:35:47 -0700794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800795 if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
796 if (!PixelFormat.formatHasAlpha(params.format)) {
797 params.format = PixelFormat.TRANSLUCENT;
798 }
799 }
800
801 boolean windowShouldResize = mLayoutRequested && windowResizesToFitContent
Romain Guy2e4f4262010-04-06 11:07:52 -0700802 && ((mWidth != host.mMeasuredWidth || mHeight != host.mMeasuredHeight)
803 || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT &&
804 frame.width() < desiredWindowWidth && frame.width() != mWidth)
805 || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT &&
806 frame.height() < desiredWindowHeight && frame.height() != mHeight));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807
808 final boolean computesInternalInsets =
809 attachInfo.mTreeObserver.hasComputeInternalInsetsListeners();
Romain Guy812ccbe2010-06-01 14:07:24 -0700810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 boolean insetsPending = false;
812 int relayoutResult = 0;
Romain Guy812ccbe2010-06-01 14:07:24 -0700813
814 if (mFirst || windowShouldResize || insetsChanged ||
815 viewVisibilityChanged || params != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816
817 if (viewVisibility == View.VISIBLE) {
818 // If this window is giving internal insets to the window
819 // manager, and it is being added or changing its visibility,
820 // then we want to first give the window manager "fake"
821 // insets to cause it to effectively ignore the content of
822 // the window during layout. This avoids it briefly causing
823 // other windows to resize/move based on the raw frame of the
824 // window, waiting until we can finish laying out this window
825 // and get back to the window manager with the ultimately
826 // computed insets.
Romain Guy812ccbe2010-06-01 14:07:24 -0700827 insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 }
829
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700830 if (mSurfaceHolder != null) {
831 mSurfaceHolder.mSurfaceLock.lock();
832 mDrawingAllowed = true;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700833 }
Romain Guy812ccbe2010-06-01 14:07:24 -0700834
Romain Guyc361da82010-10-25 15:29:10 -0700835 boolean hwInitialized = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836 boolean contentInsetsChanged = false;
Romain Guy13922e02009-05-12 17:56:14 -0700837 boolean visibleInsetsChanged;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700838 boolean hadSurface = mSurface.isValid();
Romain Guy812ccbe2010-06-01 14:07:24 -0700839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 try {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 int fl = 0;
842 if (params != null) {
843 fl = params.flags;
844 if (attachInfo.mKeepScreenOn) {
845 params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
846 }
847 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700848 if (DEBUG_LAYOUT) {
849 Log.i(TAG, "host=w:" + host.mMeasuredWidth + ", h:" +
850 host.mMeasuredHeight + ", params=" + params);
851 }
852 relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 if (params != null) {
855 params.flags = fl;
856 }
857
858 if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString()
859 + " content=" + mPendingContentInsets.toShortString()
860 + " visible=" + mPendingVisibleInsets.toShortString()
861 + " surface=" + mSurface);
Romain Guy8506ab42009-06-11 17:35:47 -0700862
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700863 if (mPendingConfiguration.seq != 0) {
864 if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: "
865 + mPendingConfiguration);
866 updateConfiguration(mPendingConfiguration, !mFirst);
867 mPendingConfiguration.seq = 0;
868 }
869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 contentInsetsChanged = !mPendingContentInsets.equals(
871 mAttachInfo.mContentInsets);
872 visibleInsetsChanged = !mPendingVisibleInsets.equals(
873 mAttachInfo.mVisibleInsets);
874 if (contentInsetsChanged) {
875 mAttachInfo.mContentInsets.set(mPendingContentInsets);
876 host.fitSystemWindows(mAttachInfo.mContentInsets);
877 if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: "
878 + mAttachInfo.mContentInsets);
879 }
880 if (visibleInsetsChanged) {
881 mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets);
882 if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: "
883 + mAttachInfo.mVisibleInsets);
884 }
885
886 if (!hadSurface) {
887 if (mSurface.isValid()) {
888 // If we are creating a new surface, then we need to
889 // completely redraw it. Also, when we get to the
890 // point of drawing it we will hold off and schedule
891 // a new traversal instead. This is so we can tell the
892 // window manager about all of the windows being displayed
893 // before actually drawing them, so it can display then
894 // all at once.
895 newSurface = true;
896 fullRedrawNeeded = true;
Jack Palevich61a6e682009-10-09 17:37:50 -0700897 mPreviousTransparentRegion.setEmpty();
Romain Guy8506ab42009-06-11 17:35:47 -0700898
Romain Guyb051e892010-09-28 19:09:36 -0700899 if (mAttachInfo.mHardwareRenderer != null) {
Romain Guyc361da82010-10-25 15:29:10 -0700900 hwInitialized = mAttachInfo.mHardwareRenderer.initialize(mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 }
902 }
903 } else if (!mSurface.isValid()) {
904 // If the surface has been removed, then reset the scroll
905 // positions.
906 mLastScrolledFocus = null;
907 mScrollY = mCurScrollY = 0;
908 if (mScroller != null) {
909 mScroller.abortAnimation();
910 }
911 }
912 } catch (RemoteException e) {
913 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 if (DEBUG_ORIENTATION) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -0700916 TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
918 attachInfo.mWindowLeft = frame.left;
919 attachInfo.mWindowTop = frame.top;
920
921 // !!FIXME!! This next section handles the case where we did not get the
922 // window size we asked for. We should avoid this by getting a maximum size from
923 // the window session beforehand.
924 mWidth = frame.width();
925 mHeight = frame.height();
926
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700927 if (mSurfaceHolder != null) {
928 // The app owns the surface; tell it about what is going on.
929 if (mSurface.isValid()) {
930 // XXX .copyFrom() doesn't work!
931 //mSurfaceHolder.mSurface.copyFrom(mSurface);
932 mSurfaceHolder.mSurface = mSurface;
933 }
934 mSurfaceHolder.mSurfaceLock.unlock();
935 if (mSurface.isValid()) {
936 if (!hadSurface) {
937 mSurfaceHolder.ungetCallbacks();
938
939 mIsCreating = true;
940 mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder);
941 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
942 if (callbacks != null) {
943 for (SurfaceHolder.Callback c : callbacks) {
944 c.surfaceCreated(mSurfaceHolder);
945 }
946 }
947 surfaceChanged = true;
Romain Guyc361da82010-10-25 15:29:10 -0700948
949 if (mAttachInfo.mHardwareRenderer != null) {
950 // This will bail out early if already initialized
951 mAttachInfo.mHardwareRenderer.initialize(mHolder);
952 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -0700953 }
954 if (surfaceChanged) {
955 mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder,
956 lp.format, mWidth, mHeight);
957 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
958 if (callbacks != null) {
959 for (SurfaceHolder.Callback c : callbacks) {
960 c.surfaceChanged(mSurfaceHolder, lp.format,
961 mWidth, mHeight);
962 }
963 }
964 }
965 mIsCreating = false;
966 } else if (hadSurface) {
967 mSurfaceHolder.ungetCallbacks();
968 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
969 mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder);
970 if (callbacks != null) {
971 for (SurfaceHolder.Callback c : callbacks) {
972 c.surfaceDestroyed(mSurfaceHolder);
973 }
974 }
975 mSurfaceHolder.mSurfaceLock.lock();
976 // Make surface invalid.
977 //mSurfaceHolder.mSurface.copyFrom(mSurface);
978 mSurfaceHolder.mSurface = new Surface();
979 mSurfaceHolder.mSurfaceLock.unlock();
980 }
981 }
Romain Guy53389bd2010-09-07 17:16:32 -0700982
Romain Guyc361da82010-10-25 15:29:10 -0700983 if (hwInitialized || (windowShouldResize && mAttachInfo.mHardwareRenderer != null)) {
Romain Guyb051e892010-09-28 19:09:36 -0700984 mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986
987 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
Romain Guy2d4cff62010-04-09 15:39:00 -0700988 (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800989 if (focusChangedDueToTouchMode || mWidth != host.mMeasuredWidth
990 || mHeight != host.mMeasuredHeight || contentInsetsChanged) {
991 childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
992 childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
993
994 if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth="
995 + mWidth + " measuredWidth=" + host.mMeasuredWidth
996 + " mHeight=" + mHeight
997 + " measuredHeight" + host.mMeasuredHeight
998 + " coveredInsetsChanged=" + contentInsetsChanged);
Romain Guy8506ab42009-06-11 17:35:47 -0700999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 // Ask host how big it wants to be
1001 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1002
1003 // Implementation of weights from WindowManager.LayoutParams
1004 // We just grow the dimensions as needed and re-measure if
1005 // needs be
1006 int width = host.mMeasuredWidth;
1007 int height = host.mMeasuredHeight;
1008 boolean measureAgain = false;
1009
1010 if (lp.horizontalWeight > 0.0f) {
1011 width += (int) ((mWidth - width) * lp.horizontalWeight);
1012 childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width,
1013 MeasureSpec.EXACTLY);
1014 measureAgain = true;
1015 }
1016 if (lp.verticalWeight > 0.0f) {
1017 height += (int) ((mHeight - height) * lp.verticalWeight);
1018 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height,
1019 MeasureSpec.EXACTLY);
1020 measureAgain = true;
1021 }
1022
1023 if (measureAgain) {
1024 if (DEBUG_LAYOUT) Log.v(TAG,
1025 "And hey let's measure once more: width=" + width
1026 + " height=" + height);
1027 host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
1028 }
1029
1030 mLayoutRequested = true;
1031 }
1032 }
1033
1034 final boolean didLayout = mLayoutRequested;
1035 boolean triggerGlobalLayoutListener = didLayout
1036 || attachInfo.mRecomputeGlobalAttributes;
1037 if (didLayout) {
1038 mLayoutRequested = false;
1039 mScrollMayChange = true;
1040 if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001041 TAG, "Laying out " + host + " to (" +
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 host.mMeasuredWidth + ", " + host.mMeasuredHeight + ")");
Romain Guy13922e02009-05-12 17:56:14 -07001043 long startTime = 0L;
Romain Guy5429e1d2010-09-07 12:38:00 -07001044 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 startTime = SystemClock.elapsedRealtime();
1046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
1048
Romain Guy13922e02009-05-12 17:56:14 -07001049 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1050 if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) {
1051 throw new IllegalStateException("The view hierarchy is an inconsistent state,"
1052 + "please refer to the logs with the tag "
1053 + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation.");
1054 }
1055 }
1056
Romain Guy5429e1d2010-09-07 12:38:00 -07001057 if (ViewDebug.DEBUG_PROFILE_LAYOUT) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime);
1059 }
1060
1061 // By this point all views have been sized and positionned
1062 // We can compute the transparent area
1063
1064 if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) {
1065 // start out transparent
1066 // TODO: AVOID THAT CALL BY CACHING THE RESULT?
1067 host.getLocationInWindow(mTmpLocation);
1068 mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1],
1069 mTmpLocation[0] + host.mRight - host.mLeft,
1070 mTmpLocation[1] + host.mBottom - host.mTop);
1071
1072 host.gatherTransparentRegion(mTransparentRegion);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001073 if (mTranslator != null) {
1074 mTranslator.translateRegionInWindowToScreen(mTransparentRegion);
1075 }
1076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 if (!mTransparentRegion.equals(mPreviousTransparentRegion)) {
1078 mPreviousTransparentRegion.set(mTransparentRegion);
1079 // reconfigure window manager
1080 try {
1081 sWindowSession.setTransparentRegion(mWindow, mTransparentRegion);
1082 } catch (RemoteException e) {
1083 }
1084 }
1085 }
1086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 if (DBG) {
1088 System.out.println("======================================");
1089 System.out.println("performTraversals -- after setFrame");
1090 host.debug();
1091 }
1092 }
1093
1094 if (triggerGlobalLayoutListener) {
1095 attachInfo.mRecomputeGlobalAttributes = false;
1096 attachInfo.mTreeObserver.dispatchOnGlobalLayout();
1097 }
1098
1099 if (computesInternalInsets) {
1100 ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets;
1101 final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets;
1102 final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets;
1103 givenContent.left = givenContent.top = givenContent.right
1104 = givenContent.bottom = givenVisible.left = givenVisible.top
1105 = givenVisible.right = givenVisible.bottom = 0;
1106 attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001107 Rect contentInsets = insets.contentInsets;
1108 Rect visibleInsets = insets.visibleInsets;
1109 if (mTranslator != null) {
1110 contentInsets = mTranslator.getTranslatedContentInsets(contentInsets);
1111 visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001113 if (insetsPending || !mLastGivenInsets.equals(insets)) {
1114 mLastGivenInsets.set(insets);
1115 try {
1116 sWindowSession.setInsets(mWindow, insets.mTouchableInsets,
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001117 contentInsets, visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 } catch (RemoteException e) {
1119 }
1120 }
1121 }
Romain Guy8506ab42009-06-11 17:35:47 -07001122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 if (mFirst) {
1124 // handle first focus request
1125 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()="
1126 + mView.hasFocus());
1127 if (mView != null) {
1128 if (!mView.hasFocus()) {
1129 mView.requestFocus(View.FOCUS_FORWARD);
1130 mFocusedView = mRealFocusedView = mView.findFocus();
1131 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view="
1132 + mFocusedView);
1133 } else {
1134 mRealFocusedView = mView.findFocus();
1135 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view="
1136 + mRealFocusedView);
1137 }
1138 }
1139 }
1140
1141 mFirst = false;
1142 mWillDrawSoon = false;
1143 mNewSurfaceNeeded = false;
1144 mViewVisibility = viewVisibility;
1145
1146 if (mAttachInfo.mHasWindowFocus) {
1147 final boolean imTarget = WindowManager.LayoutParams
1148 .mayUseInputMethod(mWindowAttributes.flags);
1149 if (imTarget != mLastWasImTarget) {
1150 mLastWasImTarget = imTarget;
1151 InputMethodManager imm = InputMethodManager.peekInstance();
1152 if (imm != null && imTarget) {
1153 imm.startGettingWindowFocus(mView);
1154 imm.onWindowFocus(mView, mView.findFocus(),
1155 mWindowAttributes.softInputMode,
1156 !mHasHadWindowFocus, mWindowAttributes.flags);
1157 }
1158 }
1159 }
Romain Guy8506ab42009-06-11 17:35:47 -07001160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw();
1162
1163 if (!cancelDraw && !newSurface) {
1164 mFullRedrawNeeded = false;
1165 draw(fullRedrawNeeded);
1166
1167 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0
1168 || mReportNextDraw) {
1169 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001170 Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001171 }
1172 mReportNextDraw = false;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -07001173 if (mSurfaceHolder != null && mSurface.isValid()) {
1174 mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder);
1175 SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
1176 if (callbacks != null) {
1177 for (SurfaceHolder.Callback c : callbacks) {
1178 if (c instanceof SurfaceHolder.Callback2) {
1179 ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded(
1180 mSurfaceHolder);
1181 }
1182 }
1183 }
1184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 try {
1186 sWindowSession.finishDrawing(mWindow);
1187 } catch (RemoteException e) {
1188 }
1189 }
1190 } else {
1191 // We were supposed to report when we are done drawing. Since we canceled the
1192 // draw, remember it here.
1193 if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
1194 mReportNextDraw = true;
1195 }
1196 if (fullRedrawNeeded) {
1197 mFullRedrawNeeded = true;
1198 }
1199 // Try again
1200 scheduleTraversals();
1201 }
1202 }
1203
1204 public void requestTransparentRegion(View child) {
1205 // the test below should not fail unless someone is messing with us
1206 checkThread();
1207 if (mView == child) {
1208 mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS;
1209 // Need to make sure we re-evaluate the window attributes next
1210 // time around, to ensure the window has the correct format.
1211 mWindowAttributesChanged = true;
1212 }
1213 }
1214
1215 /**
1216 * Figures out the measure spec for the root view in a window based on it's
1217 * layout params.
1218 *
1219 * @param windowSize
1220 * The available width or height of the window
1221 *
1222 * @param rootDimension
1223 * The layout params for one dimension (width or height) of the
1224 * window.
1225 *
1226 * @return The measure spec to use to measure the root view.
1227 */
1228 private int getRootMeasureSpec(int windowSize, int rootDimension) {
1229 int measureSpec;
1230 switch (rootDimension) {
1231
Romain Guy980a9382010-01-08 15:06:28 -08001232 case ViewGroup.LayoutParams.MATCH_PARENT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 // Window can't resize. Force root view to be windowSize.
1234 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
1235 break;
1236 case ViewGroup.LayoutParams.WRAP_CONTENT:
1237 // Window can resize. Set max size for root view.
1238 measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
1239 break;
1240 default:
1241 // Window wants to be an exact size. Force root view to be that size.
1242 measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
1243 break;
1244 }
1245 return measureSpec;
1246 }
1247
1248 private void draw(boolean fullRedrawNeeded) {
1249 Surface surface = mSurface;
1250 if (surface == null || !surface.isValid()) {
1251 return;
1252 }
1253
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001254 if (!sFirstDrawComplete) {
1255 synchronized (sFirstDrawHandlers) {
1256 sFirstDrawComplete = true;
Romain Guy812ccbe2010-06-01 14:07:24 -07001257 final int count = sFirstDrawHandlers.size();
1258 for (int i = 0; i< count; i++) {
Dianne Hackborn2a9094d2010-02-03 19:20:09 -08001259 post(sFirstDrawHandlers.get(i));
1260 }
1261 }
1262 }
1263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 scrollToRectOrFocus(null, false);
1265
1266 if (mAttachInfo.mViewScrollChanged) {
1267 mAttachInfo.mViewScrollChanged = false;
1268 mAttachInfo.mTreeObserver.dispatchOnScrollChanged();
1269 }
Romain Guy8506ab42009-06-11 17:35:47 -07001270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 int yoff;
Romain Guy5bcdff42009-05-14 21:27:18 -07001272 final boolean scrolling = mScroller != null && mScroller.computeScrollOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 if (scrolling) {
1274 yoff = mScroller.getCurrY();
1275 } else {
1276 yoff = mScrollY;
1277 }
1278 if (mCurScrollY != yoff) {
1279 mCurScrollY = yoff;
1280 fullRedrawNeeded = true;
1281 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001282 float appScale = mAttachInfo.mApplicationScale;
1283 boolean scalingRequired = mAttachInfo.mScalingRequired;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284
1285 Rect dirty = mDirty;
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001286 if (mSurfaceHolder != null) {
1287 // The app owns the surface, we won't draw.
1288 dirty.setEmpty();
1289 return;
1290 }
Romain Guy58ef7fb2010-09-13 12:52:37 -07001291
1292 if (fullRedrawNeeded) {
1293 mAttachInfo.mIgnoreDirtyState = true;
1294 dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f));
1295 }
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07001296
Romain Guyb051e892010-09-28 19:09:36 -07001297 if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guyfd507262010-10-10 15:42:49 -07001298 if (!dirty.isEmpty() || mIsAnimating) {
Romain Guy101e2ae2010-10-11 12:41:21 -07001299 mIsAnimating = false;
Romain Guyfd507262010-10-10 15:42:49 -07001300 dirty.setEmpty();
Romain Guy101e2ae2010-10-11 12:41:21 -07001301 mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, yoff);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001304 if (scrolling) {
1305 mFullRedrawNeeded = true;
1306 scheduleTraversals();
1307 }
Romain Guy812ccbe2010-06-01 14:07:24 -07001308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 return;
1310 }
1311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001313 Log.v(TAG, "Draw " + mView + "/"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 + mWindowAttributes.getTitle()
1315 + ": dirty={" + dirty.left + "," + dirty.top
1316 + "," + dirty.right + "," + dirty.bottom + "} surface="
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07001317 + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" +
1318 appScale + ", width=" + mWidth + ", height=" + mHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 }
1320
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001321 if (!dirty.isEmpty() || mIsAnimating) {
1322 Canvas canvas;
1323 try {
1324 int left = dirty.left;
1325 int top = dirty.top;
1326 int right = dirty.right;
1327 int bottom = dirty.bottom;
1328 canvas = surface.lockCanvas(dirty);
Romain Guy5bcdff42009-05-14 21:27:18 -07001329
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001330 if (left != dirty.left || top != dirty.top || right != dirty.right ||
1331 bottom != dirty.bottom) {
1332 mAttachInfo.mIgnoreDirtyState = true;
1333 }
1334
1335 // TODO: Do this in native
1336 canvas.setDensity(mDensity);
1337 } catch (Surface.OutOfResourcesException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001338 Log.e(TAG, "OutOfResourcesException 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;
1342 } catch (IllegalArgumentException e) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001343 Log.e(TAG, "IllegalArgumentException locking surface", e);
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001344 // TODO: we should ask the window manager to do something!
1345 // for now we just do nothing
1346 return;
Romain Guy5bcdff42009-05-14 21:27:18 -07001347 }
1348
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001349 try {
1350 if (!dirty.isEmpty() || mIsAnimating) {
1351 long startTime = 0L;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001353 if (DEBUG_ORIENTATION || DEBUG_DRAW) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001354 Log.v(TAG, "Surface " + surface + " drawing to bitmap w="
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001355 + canvas.getWidth() + ", h=" + canvas.getHeight());
1356 //canvas.drawARGB(255, 255, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07001357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
Romain Guy5429e1d2010-09-07 12:38:00 -07001359 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001360 startTime = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001361 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001362
1363 // If this bitmap's format includes an alpha channel, we
1364 // need to clear it before drawing so that the child will
1365 // properly re-composite its drawing on a transparent
1366 // background. This automatically respects the clip/dirty region
1367 // or
1368 // If we are applying an offset, we need to clear the area
1369 // where the offset doesn't appear to avoid having garbage
1370 // left in the blank areas.
1371 if (!canvas.isOpaque() || yoff != 0) {
1372 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
1373 }
1374
1375 dirty.setEmpty();
1376 mIsAnimating = false;
1377 mAttachInfo.mDrawingTime = SystemClock.uptimeMillis();
1378 mView.mPrivateFlags |= View.DRAWN;
1379
1380 if (DEBUG_DRAW) {
1381 Context cxt = mView.getContext();
1382 Log.i(TAG, "Drawing: package:" + cxt.getPackageName() +
1383 ", metrics=" + cxt.getResources().getDisplayMetrics() +
1384 ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo());
1385 }
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001386 try {
1387 canvas.translate(0, -yoff);
1388 if (mTranslator != null) {
1389 mTranslator.translateCanvas(canvas);
1390 }
1391 canvas.setScreenDensity(scalingRequired
1392 ? DisplayMetrics.DENSITY_DEVICE : 0);
1393 mView.draw(canvas);
1394 } finally {
1395 mAttachInfo.mIgnoreDirtyState = false;
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001396 }
1397
1398 if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) {
1399 mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING);
1400 }
1401
Romain Guy5429e1d2010-09-07 12:38:00 -07001402 if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001403 int now = (int)SystemClock.elapsedRealtime();
1404 if (sDrawTime != 0) {
1405 nativeShowFPS(canvas, now - sDrawTime);
1406 }
1407 sDrawTime = now;
1408 }
1409
Romain Guy5429e1d2010-09-07 12:38:00 -07001410 if (ViewDebug.DEBUG_PROFILE_DRAWING) {
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001411 EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime);
1412 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001413 }
1414
Mathias Agopiana62b09a2010-04-21 18:36:53 -07001415 } finally {
1416 surface.unlockCanvasAndPost(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 }
1419
1420 if (LOCAL_LOGV) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07001421 Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
Romain Guy8506ab42009-06-11 17:35:47 -07001423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 if (scrolling) {
1425 mFullRedrawNeeded = true;
1426 scheduleTraversals();
1427 }
1428 }
1429
1430 boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) {
1431 final View.AttachInfo attachInfo = mAttachInfo;
1432 final Rect ci = attachInfo.mContentInsets;
1433 final Rect vi = attachInfo.mVisibleInsets;
1434 int scrollY = 0;
1435 boolean handled = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001436
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001437 if (vi.left > ci.left || vi.top > ci.top
1438 || vi.right > ci.right || vi.bottom > ci.bottom) {
1439 // We'll assume that we aren't going to change the scroll
1440 // offset, since we want to avoid that unless it is actually
1441 // going to make the focus visible... otherwise we scroll
1442 // all over the place.
1443 scrollY = mScrollY;
1444 // We can be called for two different situations: during a draw,
1445 // to update the scroll position if the focus has changed (in which
1446 // case 'rectangle' is null), or in response to a
1447 // requestChildRectangleOnScreen() call (in which case 'rectangle'
1448 // is non-null and we just want to scroll to whatever that
1449 // rectangle is).
1450 View focus = mRealFocusedView;
Romain Guye8b16522009-07-14 13:06:42 -07001451
1452 // When in touch mode, focus points to the previously focused view,
1453 // which may have been removed from the view hierarchy. The following
Joe Onoratob71193b2009-11-24 18:34:42 -05001454 // line checks whether the view is still in our hierarchy.
1455 if (focus == null || focus.mAttachInfo != mAttachInfo) {
Romain Guye8b16522009-07-14 13:06:42 -07001456 mRealFocusedView = null;
1457 return false;
1458 }
1459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460 if (focus != mLastScrolledFocus) {
1461 // If the focus has changed, then ignore any requests to scroll
1462 // to a rectangle; first we want to make sure the entire focus
1463 // view is visible.
1464 rectangle = null;
1465 }
1466 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus
1467 + " rectangle=" + rectangle + " ci=" + ci
1468 + " vi=" + vi);
1469 if (focus == mLastScrolledFocus && !mScrollMayChange
1470 && rectangle == null) {
1471 // Optimization: if the focus hasn't changed since last
1472 // time, and no layout has happened, then just leave things
1473 // as they are.
1474 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y="
1475 + mScrollY + " vi=" + vi.toShortString());
1476 } else if (focus != null) {
1477 // We need to determine if the currently focused view is
1478 // within the visible part of the window and, if not, apply
1479 // a pan so it can be seen.
1480 mLastScrolledFocus = focus;
1481 mScrollMayChange = false;
1482 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?");
1483 // Try to find the rectangle from the focus view.
1484 if (focus.getGlobalVisibleRect(mVisRect, null)) {
1485 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w="
1486 + mView.getWidth() + " h=" + mView.getHeight()
1487 + " ci=" + ci.toShortString()
1488 + " vi=" + vi.toShortString());
1489 if (rectangle == null) {
1490 focus.getFocusedRect(mTempRect);
1491 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus
1492 + ": focusRect=" + mTempRect.toShortString());
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07001493 if (mView instanceof ViewGroup) {
1494 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
1495 focus, mTempRect);
1496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1498 "Focus in window: focusRect="
1499 + mTempRect.toShortString()
1500 + " visRect=" + mVisRect.toShortString());
1501 } else {
1502 mTempRect.set(rectangle);
1503 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1504 "Request scroll to rect: "
1505 + mTempRect.toShortString()
1506 + " visRect=" + mVisRect.toShortString());
1507 }
1508 if (mTempRect.intersect(mVisRect)) {
1509 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1510 "Focus window visible rect: "
1511 + mTempRect.toShortString());
1512 if (mTempRect.height() >
1513 (mView.getHeight()-vi.top-vi.bottom)) {
1514 // If the focus simply is not going to fit, then
1515 // best is probably just to leave things as-is.
1516 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1517 "Too tall; leaving scrollY=" + scrollY);
1518 } else if ((mTempRect.top-scrollY) < vi.top) {
1519 scrollY -= vi.top - (mTempRect.top-scrollY);
1520 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1521 "Top covered; scrollY=" + scrollY);
1522 } else if ((mTempRect.bottom-scrollY)
1523 > (mView.getHeight()-vi.bottom)) {
1524 scrollY += (mTempRect.bottom-scrollY)
1525 - (mView.getHeight()-vi.bottom);
1526 if (DEBUG_INPUT_RESIZE) Log.v(TAG,
1527 "Bottom covered; scrollY=" + scrollY);
1528 }
1529 handled = true;
1530 }
1531 }
1532 }
1533 }
Romain Guy8506ab42009-06-11 17:35:47 -07001534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 if (scrollY != mScrollY) {
1536 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old="
1537 + mScrollY + " , new=" + scrollY);
1538 if (!immediate) {
1539 if (mScroller == null) {
1540 mScroller = new Scroller(mView.getContext());
1541 }
1542 mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY);
1543 } else if (mScroller != null) {
1544 mScroller.abortAnimation();
1545 }
1546 mScrollY = scrollY;
1547 }
Romain Guy8506ab42009-06-11 17:35:47 -07001548
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 return handled;
1550 }
Romain Guy8506ab42009-06-11 17:35:47 -07001551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 public void requestChildFocus(View child, View focused) {
1553 checkThread();
1554 if (mFocusedView != focused) {
1555 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused);
1556 scheduleTraversals();
1557 }
1558 mFocusedView = mRealFocusedView = focused;
1559 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now "
1560 + mFocusedView);
1561 }
1562
1563 public void clearChildFocus(View child) {
1564 checkThread();
1565
1566 View oldFocus = mFocusedView;
1567
1568 if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus");
1569 mFocusedView = mRealFocusedView = null;
1570 if (mView != null && !mView.hasFocus()) {
1571 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1572 if (!mView.requestFocus(View.FOCUS_FORWARD)) {
1573 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1574 }
1575 } else if (oldFocus != null) {
1576 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null);
1577 }
1578 }
1579
1580
1581 public void focusableViewAvailable(View v) {
1582 checkThread();
1583
1584 if (mView != null && !mView.hasFocus()) {
1585 v.requestFocus();
1586 } else {
1587 // the one case where will transfer focus away from the current one
1588 // is if the current view is a view group that prefers to give focus
1589 // to its children first AND the view is a descendant of it.
1590 mFocusedView = mView.findFocus();
1591 boolean descendantsHaveDibsOnFocus =
1592 (mFocusedView instanceof ViewGroup) &&
1593 (((ViewGroup) mFocusedView).getDescendantFocusability() ==
1594 ViewGroup.FOCUS_AFTER_DESCENDANTS);
1595 if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) {
1596 // If a view gets the focus, the listener will be invoked from requestChildFocus()
1597 v.requestFocus();
1598 }
1599 }
1600 }
1601
1602 public void recomputeViewAttributes(View child) {
1603 checkThread();
1604 if (mView == child) {
1605 mAttachInfo.mRecomputeGlobalAttributes = true;
1606 if (!mWillDrawSoon) {
1607 scheduleTraversals();
1608 }
1609 }
1610 }
1611
1612 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 if (mView != null) {
1614 mView.dispatchDetachedFromWindow();
1615 }
1616
1617 mView = null;
1618 mAttachInfo.mRootView = null;
Mathias Agopian5583dc62009-07-09 16:28:11 -07001619 mAttachInfo.mSurface = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620
Romain Guy29d89972010-09-22 16:10:57 -07001621 destroyHardwareRenderer();
Romain Guy4caa4ed2010-08-25 14:46:24 -07001622
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07001623 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001625 if (mInputChannel != null) {
1626 if (mInputQueueCallback != null) {
1627 mInputQueueCallback.onInputQueueDestroyed(mInputQueue);
1628 mInputQueueCallback = null;
1629 } else {
1630 InputQueue.unregisterInputChannel(mInputChannel);
Jeff Brown46b9ac02010-04-22 18:58:52 -07001631 }
1632 }
1633
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 try {
1635 sWindowSession.remove(mWindow);
1636 } catch (RemoteException e) {
1637 }
Jeff Brown349703e2010-06-22 01:27:15 -07001638
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001639 // Dispose the input channel after removing the window so the Window Manager
1640 // doesn't interpret the input channel being closed as an abnormal termination.
1641 if (mInputChannel != null) {
1642 mInputChannel.dispose();
1643 mInputChannel = null;
Jeff Brown349703e2010-06-22 01:27:15 -07001644 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645 }
Romain Guy8506ab42009-06-11 17:35:47 -07001646
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001647 void updateConfiguration(Configuration config, boolean force) {
1648 if (DEBUG_CONFIGURATION) Log.v(TAG,
1649 "Applying new config to window "
1650 + mWindowAttributes.getTitle()
1651 + ": " + config);
1652 synchronized (sConfigCallbacks) {
1653 for (int i=sConfigCallbacks.size()-1; i>=0; i--) {
1654 sConfigCallbacks.get(i).onConfigurationChanged(config);
1655 }
1656 }
1657 if (mView != null) {
1658 // At this point the resources have been updated to
1659 // have the most recent config, whatever that is. Use
1660 // the on in them which may be newer.
1661 if (mView != null) {
1662 config = mView.getResources().getConfiguration();
1663 }
1664 if (force || mLastConfiguration.diff(config) != 0) {
1665 mLastConfiguration.setTo(config);
1666 mView.dispatchConfigurationChanged(config);
1667 }
1668 }
1669 }
1670
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 /**
1672 * Return true if child is an ancestor of parent, (or equal to the parent).
1673 */
1674 private static boolean isViewDescendantOf(View child, View parent) {
1675 if (child == parent) {
1676 return true;
1677 }
1678
1679 final ViewParent theParent = child.getParent();
1680 return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent);
1681 }
1682
Romain Guycdb86672010-03-18 18:54:50 -07001683 private static void forceLayout(View view) {
1684 view.forceLayout();
1685 if (view instanceof ViewGroup) {
1686 ViewGroup group = (ViewGroup) view;
1687 final int count = group.getChildCount();
1688 for (int i = 0; i < count; i++) {
1689 forceLayout(group.getChildAt(i));
1690 }
1691 }
1692 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001693
1694 public final static int DO_TRAVERSAL = 1000;
1695 public final static int DIE = 1001;
1696 public final static int RESIZED = 1002;
1697 public final static int RESIZED_REPORT = 1003;
1698 public final static int WINDOW_FOCUS_CHANGED = 1004;
1699 public final static int DISPATCH_KEY = 1005;
1700 public final static int DISPATCH_POINTER = 1006;
1701 public final static int DISPATCH_TRACKBALL = 1007;
1702 public final static int DISPATCH_APP_VISIBILITY = 1008;
1703 public final static int DISPATCH_GET_NEW_SURFACE = 1009;
1704 public final static int FINISHED_EVENT = 1010;
1705 public final static int DISPATCH_KEY_FROM_IME = 1011;
1706 public final static int FINISH_INPUT_CONNECTION = 1012;
1707 public final static int CHECK_FOCUS = 1013;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001708 public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Christopher Tatea53146c2010-09-07 11:57:52 -07001709 public final static int DISPATCH_DRAG_EVENT = 1015;
Chris Tate91e9bb32010-10-12 12:58:43 -07001710 public final static int DISPATCH_DRAG_LOCATION_EVENT = 1016;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001711
1712 @Override
1713 public void handleMessage(Message msg) {
1714 switch (msg.what) {
1715 case View.AttachInfo.INVALIDATE_MSG:
1716 ((View) msg.obj).invalidate();
1717 break;
1718 case View.AttachInfo.INVALIDATE_RECT_MSG:
1719 final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj;
1720 info.target.invalidate(info.left, info.top, info.right, info.bottom);
1721 info.release();
1722 break;
1723 case DO_TRAVERSAL:
1724 if (mProfile) {
1725 Debug.startMethodTracing("ViewRoot");
1726 }
1727
1728 performTraversals();
1729
1730 if (mProfile) {
1731 Debug.stopMethodTracing();
1732 mProfile = false;
1733 }
1734 break;
1735 case FINISHED_EVENT:
1736 handleFinishedEvent(msg.arg1, msg.arg2 != 0);
1737 break;
1738 case DISPATCH_KEY:
1739 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001740 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 + msg.obj + " to " + mView);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001742 deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001744 case DISPATCH_POINTER: {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001745 MotionEvent event = (MotionEvent) msg.obj;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 try {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001747 deliverPointerEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001748 } finally {
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001749 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001750 if (msg.arg1 != 0) {
1751 finishInputEvent();
1752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001754 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001755 } break;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001756 case DISPATCH_TRACKBALL: {
1757 MotionEvent event = (MotionEvent) msg.obj;
1758 try {
1759 deliverTrackballEvent(event);
1760 } finally {
1761 event.recycle();
Jeff Brown93ed4e32010-09-23 13:51:48 -07001762 if (msg.arg1 != 0) {
1763 finishInputEvent();
1764 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001765 }
1766 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001767 case DISPATCH_APP_VISIBILITY:
1768 handleAppVisibility(msg.arg1 != 0);
1769 break;
1770 case DISPATCH_GET_NEW_SURFACE:
1771 handleGetNewSurface();
1772 break;
1773 case RESIZED:
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001774 ResizedInfo ri = (ResizedInfo)msg.obj;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07001775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001777 && mPendingContentInsets.equals(ri.coveredInsets)
Dianne Hackbornd49258f2010-03-26 00:44:29 -07001778 && mPendingVisibleInsets.equals(ri.visibleInsets)
1779 && ((ResizedInfo)msg.obj).newConfig == null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 break;
1781 }
1782 // fall through...
1783 case RESIZED_REPORT:
1784 if (mAdded) {
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001785 Configuration config = ((ResizedInfo)msg.obj).newConfig;
1786 if (config != null) {
Dianne Hackborn694f79b2010-03-17 19:44:59 -07001787 updateConfiguration(config, false);
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 mWinFrame.left = 0;
1790 mWinFrame.right = msg.arg1;
1791 mWinFrame.top = 0;
1792 mWinFrame.bottom = msg.arg2;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08001793 mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets);
1794 mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 if (msg.what == RESIZED_REPORT) {
1796 mReportNextDraw = true;
1797 }
Romain Guycdb86672010-03-18 18:54:50 -07001798
1799 if (mView != null) {
1800 forceLayout(mView);
1801 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 requestLayout();
1803 }
1804 break;
1805 case WINDOW_FOCUS_CHANGED: {
1806 if (mAdded) {
1807 boolean hasWindowFocus = msg.arg1 != 0;
1808 mAttachInfo.mHasWindowFocus = hasWindowFocus;
1809 if (hasWindowFocus) {
1810 boolean inTouchMode = msg.arg2 != 0;
Romain Guy2d4cff62010-04-09 15:39:00 -07001811 ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812
Romain Guyc361da82010-10-25 15:29:10 -07001813 if (mAttachInfo.mHardwareRenderer != null &&
1814 mSurface != null && mSurface.isValid()) {
Romain Guyb051e892010-09-28 19:09:36 -07001815 mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight,
1816 mAttachInfo, mHolder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 }
1818 }
Romain Guy8506ab42009-06-11 17:35:47 -07001819
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 mLastWasImTarget = WindowManager.LayoutParams
1821 .mayUseInputMethod(mWindowAttributes.flags);
Romain Guy8506ab42009-06-11 17:35:47 -07001822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001823 InputMethodManager imm = InputMethodManager.peekInstance();
1824 if (mView != null) {
1825 if (hasWindowFocus && imm != null && mLastWasImTarget) {
1826 imm.startGettingWindowFocus(mView);
1827 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07001828 mAttachInfo.mKeyDispatchState.reset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 mView.dispatchWindowFocusChanged(hasWindowFocus);
1830 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 // Note: must be done after the focus change callbacks,
1833 // so all of the view state is set up correctly.
1834 if (hasWindowFocus) {
1835 if (imm != null && mLastWasImTarget) {
1836 imm.onWindowFocus(mView, mView.findFocus(),
1837 mWindowAttributes.softInputMode,
1838 !mHasHadWindowFocus, mWindowAttributes.flags);
1839 }
1840 // Clear the forward bit. We can just do this directly, since
1841 // the window manager doesn't care about it.
1842 mWindowAttributes.softInputMode &=
1843 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1844 ((WindowManager.LayoutParams)mView.getLayoutParams())
1845 .softInputMode &=
1846 ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
1847 mHasHadWindowFocus = true;
1848 }
svetoslavganov75986cf2009-05-14 22:28:01 -07001849
1850 if (hasWindowFocus && mView != null) {
1851 sendAccessibilityEvents();
1852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854 } break;
1855 case DIE:
Dianne Hackborn94d69142009-09-28 22:14:42 -07001856 doDie();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 break;
The Android Open Source Project10592532009-03-18 17:39:46 -07001858 case DISPATCH_KEY_FROM_IME: {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07001860 TAG, "Dispatching key "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001861 + msg.obj + " from IME to " + mView);
The Android Open Source Project10592532009-03-18 17:39:46 -07001862 KeyEvent event = (KeyEvent)msg.obj;
1863 if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) {
1864 // The IME is trying to say this event is from the
1865 // system! Bad bad bad!
Romain Guy812ccbe2010-06-01 14:07:24 -07001866 event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM);
The Android Open Source Project10592532009-03-18 17:39:46 -07001867 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 deliverKeyEventToViewHierarchy((KeyEvent)msg.obj, false);
The Android Open Source Project10592532009-03-18 17:39:46 -07001869 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 case FINISH_INPUT_CONNECTION: {
1871 InputMethodManager imm = InputMethodManager.peekInstance();
1872 if (imm != null) {
1873 imm.reportFinishInputConnection((InputConnection)msg.obj);
1874 }
1875 } break;
1876 case CHECK_FOCUS: {
1877 InputMethodManager imm = InputMethodManager.peekInstance();
1878 if (imm != null) {
1879 imm.checkFocus();
1880 }
1881 } break;
Dianne Hackbornffa42482009-09-23 22:20:11 -07001882 case CLOSE_SYSTEM_DIALOGS: {
1883 if (mView != null) {
1884 mView.onCloseSystemDialogs((String)msg.obj);
1885 }
1886 } break;
Chris Tate91e9bb32010-10-12 12:58:43 -07001887 case DISPATCH_DRAG_EVENT:
1888 case DISPATCH_DRAG_LOCATION_EVENT: {
Christopher Tatea53146c2010-09-07 11:57:52 -07001889 handleDragEvent((DragEvent)msg.obj);
1890 } break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 }
1892 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07001893
Jeff Brown93ed4e32010-09-23 13:51:48 -07001894 private void startInputEvent(Runnable finishedCallback) {
1895 if (mFinishedCallback != null) {
1896 Slog.w(TAG, "Received a new input event from the input queue but there is "
1897 + "already an unfinished input event in progress.");
1898 }
1899
1900 mFinishedCallback = finishedCallback;
1901 }
1902
1903 private void finishInputEvent() {
1904 if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished");
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001905
Jeff Brown00fa7bd2010-07-02 15:37:36 -07001906 if (mFinishedCallback != null) {
1907 mFinishedCallback.run();
1908 mFinishedCallback = null;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07001909 } else {
Jeff Brown93ed4e32010-09-23 13:51:48 -07001910 Slog.w(TAG, "Attempted to tell the input queue that the current input event "
1911 + "is finished but there is no input event actually in progress.");
Jeff Brown46b9ac02010-04-22 18:58:52 -07001912 }
1913 }
1914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001915 /**
1916 * Something in the current window tells us we need to change the touch mode. For
1917 * example, we are not in touch mode, and the user touches the screen.
1918 *
1919 * If the touch mode has changed, tell the window manager, and handle it locally.
1920 *
1921 * @param inTouchMode Whether we want to be in touch mode.
1922 * @return True if the touch mode changed and focus changed was changed as a result
1923 */
1924 boolean ensureTouchMode(boolean inTouchMode) {
1925 if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current "
1926 + "touch mode is " + mAttachInfo.mInTouchMode);
1927 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1928
1929 // tell the window manager
1930 try {
1931 sWindowSession.setInTouchMode(inTouchMode);
1932 } catch (RemoteException e) {
1933 throw new RuntimeException(e);
1934 }
1935
1936 // handle the change
Romain Guy2d4cff62010-04-09 15:39:00 -07001937 return ensureTouchModeLocally(inTouchMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939
1940 /**
1941 * Ensure that the touch mode for this window is set, and if it is changing,
1942 * take the appropriate action.
1943 * @param inTouchMode Whether we want to be in touch mode.
1944 * @return True if the touch mode changed and focus changed was changed as a result
1945 */
Romain Guy2d4cff62010-04-09 15:39:00 -07001946 private boolean ensureTouchModeLocally(boolean inTouchMode) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current "
1948 + "touch mode is " + mAttachInfo.mInTouchMode);
1949
1950 if (mAttachInfo.mInTouchMode == inTouchMode) return false;
1951
1952 mAttachInfo.mInTouchMode = inTouchMode;
1953 mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode);
1954
Romain Guy2d4cff62010-04-09 15:39:00 -07001955 return (inTouchMode) ? enterTouchMode() : leaveTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
1957
1958 private boolean enterTouchMode() {
1959 if (mView != null) {
1960 if (mView.hasFocus()) {
1961 // note: not relying on mFocusedView here because this could
1962 // be when the window is first being added, and mFocused isn't
1963 // set yet.
1964 final View focused = mView.findFocus();
1965 if (focused != null && !focused.isFocusableInTouchMode()) {
1966
1967 final ViewGroup ancestorToTakeFocus =
1968 findAncestorToTakeFocusInTouchMode(focused);
1969 if (ancestorToTakeFocus != null) {
1970 // there is an ancestor that wants focus after its descendants that
1971 // is focusable in touch mode.. give it focus
1972 return ancestorToTakeFocus.requestFocus();
1973 } else {
1974 // nothing appropriate to have focus in touch mode, clear it out
1975 mView.unFocus();
1976 mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null);
1977 mFocusedView = null;
1978 return true;
1979 }
1980 }
1981 }
1982 }
1983 return false;
1984 }
1985
1986
1987 /**
1988 * Find an ancestor of focused that wants focus after its descendants and is
1989 * focusable in touch mode.
1990 * @param focused The currently focused view.
1991 * @return An appropriate view, or null if no such view exists.
1992 */
1993 private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
1994 ViewParent parent = focused.getParent();
1995 while (parent instanceof ViewGroup) {
1996 final ViewGroup vgParent = (ViewGroup) parent;
1997 if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS
1998 && vgParent.isFocusableInTouchMode()) {
1999 return vgParent;
2000 }
2001 if (vgParent.isRootNamespace()) {
2002 return null;
2003 } else {
2004 parent = vgParent.getParent();
2005 }
2006 }
2007 return null;
2008 }
2009
2010 private boolean leaveTouchMode() {
2011 if (mView != null) {
2012 if (mView.hasFocus()) {
2013 // i learned the hard way to not trust mFocusedView :)
2014 mFocusedView = mView.findFocus();
2015 if (!(mFocusedView instanceof ViewGroup)) {
2016 // some view has focus, let it keep it
2017 return false;
2018 } else if (((ViewGroup)mFocusedView).getDescendantFocusability() !=
2019 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2020 // some view group has focus, and doesn't prefer its children
2021 // over itself for focus, so let them keep it.
2022 return false;
2023 }
2024 }
2025
2026 // find the best view to give focus to in this brave new non-touch-mode
2027 // world
2028 final View focused = focusSearch(null, View.FOCUS_DOWN);
2029 if (focused != null) {
2030 return focused.requestFocus(View.FOCUS_DOWN);
2031 }
2032 }
2033 return false;
2034 }
2035
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002036 private void deliverPointerEvent(MotionEvent event) {
2037 if (mTranslator != null) {
2038 mTranslator.translateEventInScreenToAppWindow(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002039 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002040
2041 boolean handled;
2042 if (mView != null && mAdded) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002043
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002044 // enter touch mode on the down
2045 boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN;
2046 if (isDown) {
2047 ensureTouchMode(true);
2048 }
2049 if(Config.LOGV) {
2050 captureMotionLog("captureDispatchPointer", event);
2051 }
2052 if (mCurScrollY != 0) {
2053 event.offsetLocation(0, mCurScrollY);
2054 }
2055 if (MEASURE_LATENCY) {
2056 lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano());
2057 }
Christopher Tate2c095f32010-10-04 14:13:40 -07002058 // cache for possible drag-initiation
2059 mLastTouchPoint.x = event.getRawX();
2060 mLastTouchPoint.y = event.getRawY();
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002061 handled = mView.dispatchTouchEvent(event);
2062 if (MEASURE_LATENCY) {
2063 lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano());
2064 }
2065 if (!handled && isDown) {
2066 int edgeSlop = mViewConfiguration.getScaledEdgeSlop();
2067
2068 final int edgeFlags = event.getEdgeFlags();
2069 int direction = View.FOCUS_UP;
2070 int x = (int)event.getX();
2071 int y = (int)event.getY();
2072 final int[] deltas = new int[2];
2073
2074 if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) {
2075 direction = View.FOCUS_DOWN;
2076 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2077 deltas[0] = edgeSlop;
2078 x += edgeSlop;
2079 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2080 deltas[0] = -edgeSlop;
2081 x -= edgeSlop;
2082 }
2083 } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) {
2084 direction = View.FOCUS_UP;
2085 if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2086 deltas[0] = edgeSlop;
2087 x += edgeSlop;
2088 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2089 deltas[0] = -edgeSlop;
2090 x -= edgeSlop;
2091 }
2092 } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) {
2093 direction = View.FOCUS_RIGHT;
2094 } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) {
2095 direction = View.FOCUS_LEFT;
2096 }
2097
2098 if (edgeFlags != 0 && mView instanceof ViewGroup) {
2099 View nearest = FocusFinder.getInstance().findNearestTouchable(
2100 ((ViewGroup) mView), x, y, direction, deltas);
2101 if (nearest != null) {
2102 event.offsetLocation(deltas[0], deltas[1]);
2103 event.setEdgeFlags(0);
2104 mView.dispatchTouchEvent(event);
2105 }
2106 }
2107 }
2108 }
2109 }
2110
2111 private void deliverTrackballEvent(MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event);
2113
2114 boolean handled = false;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002115 if (mView != null && mAdded) {
2116 handled = mView.dispatchTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 if (handled) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002118 // If we reach this, we delivered a trackball event to mView and
2119 // mView consumed it. Because we will not translate the trackball
2120 // event into a key event, touch mode will not exit, so we exit
2121 // touch mode here.
2122 ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002123 return;
2124 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002125
2126 // Otherwise we could do something here, like changing the focus
2127 // or something?
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 }
2129
2130 final TrackballAxis x = mTrackballAxisX;
2131 final TrackballAxis y = mTrackballAxisY;
2132
2133 long curTime = SystemClock.uptimeMillis();
2134 if ((mLastTrackballTime+MAX_TRACKBALL_DELAY) < curTime) {
2135 // It has been too long since the last movement,
2136 // so restart at the beginning.
2137 x.reset(0);
2138 y.reset(0);
2139 mLastTrackballTime = curTime;
2140 }
2141
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002142 final int action = event.getAction();
2143 final int metastate = event.getMetaState();
2144 switch (action) {
2145 case MotionEvent.ACTION_DOWN:
2146 x.reset(2);
2147 y.reset(2);
2148 deliverKeyEvent(new KeyEvent(curTime, curTime,
2149 KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER,
2150 0, metastate), false);
2151 break;
2152 case MotionEvent.ACTION_UP:
2153 x.reset(2);
2154 y.reset(2);
2155 deliverKeyEvent(new KeyEvent(curTime, curTime,
2156 KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER,
2157 0, metastate), false);
2158 break;
2159 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002161 if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step="
2162 + x.step + " dir=" + x.dir + " acc=" + x.acceleration
2163 + " move=" + event.getX()
2164 + " / Y=" + y.position + " step="
2165 + y.step + " dir=" + y.dir + " acc=" + y.acceleration
2166 + " move=" + event.getY());
2167 final float xOff = x.collect(event.getX(), event.getEventTime(), "X");
2168 final float yOff = y.collect(event.getY(), event.getEventTime(), "Y");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002170 // Generate DPAD events based on the trackball movement.
2171 // We pick the axis that has moved the most as the direction of
2172 // the DPAD. When we generate DPAD events for one axis, then the
2173 // other axis is reset -- we don't want to perform DPAD jumps due
2174 // to slight movements in the trackball when making major movements
2175 // along the other axis.
2176 int keycode = 0;
2177 int movement = 0;
2178 float accel = 1;
2179 if (xOff > yOff) {
2180 movement = x.generate((2/event.getXPrecision()));
2181 if (movement != 0) {
2182 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT
2183 : KeyEvent.KEYCODE_DPAD_LEFT;
2184 accel = x.acceleration;
2185 y.reset(2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002187 } else if (yOff > 0) {
2188 movement = y.generate((2/event.getYPrecision()));
2189 if (movement != 0) {
2190 keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN
2191 : KeyEvent.KEYCODE_DPAD_UP;
2192 accel = y.acceleration;
2193 x.reset(2);
2194 }
2195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002196
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002197 if (keycode != 0) {
2198 if (movement < 0) movement = -movement;
2199 int accelMovement = (int)(movement * accel);
2200 if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement
2201 + " accelMovement=" + accelMovement
2202 + " accel=" + accel);
2203 if (accelMovement > movement) {
2204 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2205 + keycode);
2206 movement--;
2207 deliverKeyEvent(new KeyEvent(curTime, curTime,
2208 KeyEvent.ACTION_MULTIPLE, keycode,
2209 accelMovement-movement, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002210 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002211 while (movement > 0) {
2212 if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: "
2213 + keycode);
2214 movement--;
2215 curTime = SystemClock.uptimeMillis();
2216 deliverKeyEvent(new KeyEvent(curTime, curTime,
2217 KeyEvent.ACTION_DOWN, keycode, 0, event.getMetaState()), false);
2218 deliverKeyEvent(new KeyEvent(curTime, curTime,
2219 KeyEvent.ACTION_UP, keycode, 0, metastate), false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002221 mLastTrackballTime = curTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 }
2223 }
2224
2225 /**
2226 * @param keyCode The key code
2227 * @return True if the key is directional.
2228 */
2229 static boolean isDirectional(int keyCode) {
2230 switch (keyCode) {
2231 case KeyEvent.KEYCODE_DPAD_LEFT:
2232 case KeyEvent.KEYCODE_DPAD_RIGHT:
2233 case KeyEvent.KEYCODE_DPAD_UP:
2234 case KeyEvent.KEYCODE_DPAD_DOWN:
2235 return true;
2236 }
2237 return false;
2238 }
2239
2240 /**
2241 * Returns true if this key is a keyboard key.
2242 * @param keyEvent The key event.
2243 * @return whether this key is a keyboard key.
2244 */
2245 private static boolean isKeyboardKey(KeyEvent keyEvent) {
2246 final int convertedKey = keyEvent.getUnicodeChar();
2247 return convertedKey > 0;
2248 }
2249
2250
2251
2252 /**
2253 * See if the key event means we should leave touch mode (and leave touch
2254 * mode if so).
2255 * @param event The key event.
2256 * @return Whether this key event should be consumed (meaning the act of
2257 * leaving touch mode alone is considered the event).
2258 */
2259 private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) {
Adam Powell51a6bee2010-03-15 14:07:28 -07002260 final int action = event.getAction();
2261 if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002262 return false;
2263 }
2264 if ((event.getFlags()&KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) {
2265 return false;
2266 }
2267
2268 // only relevant if we are in touch mode
2269 if (!mAttachInfo.mInTouchMode) {
2270 return false;
2271 }
2272
2273 // if something like an edit text has focus and the user is typing,
2274 // leave touch mode
2275 //
2276 // note: the condition of not being a keyboard key is kind of a hacky
2277 // approximation of whether we think the focused view will want the
2278 // key; if we knew for sure whether the focused view would consume
2279 // the event, that would be better.
2280 if (isKeyboardKey(event) && mView != null && mView.hasFocus()) {
2281 mFocusedView = mView.findFocus();
2282 if ((mFocusedView instanceof ViewGroup)
2283 && ((ViewGroup) mFocusedView).getDescendantFocusability() ==
2284 ViewGroup.FOCUS_AFTER_DESCENDANTS) {
2285 // something has focus, but is holding it weakly as a container
2286 return false;
2287 }
2288 if (ensureTouchMode(false)) {
2289 throw new IllegalStateException("should not have changed focus "
2290 + "when leaving touch mode while a view has focus.");
2291 }
2292 return false;
2293 }
2294
2295 if (isDirectional(event.getKeyCode())) {
2296 // no view has focus, so we leave touch mode (and find something
2297 // to give focus to). the event is consumed if we were able to
2298 // find something to give focus to.
2299 return ensureTouchMode(false);
2300 }
2301 return false;
2302 }
2303
2304 /**
Romain Guy8506ab42009-06-11 17:35:47 -07002305 * log motion events
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002306 */
2307 private static void captureMotionLog(String subTag, MotionEvent ev) {
Romain Guy8506ab42009-06-11 17:35:47 -07002308 //check dynamic switch
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002309 if (ev == null ||
2310 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2311 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002312 }
Romain Guy8506ab42009-06-11 17:35:47 -07002313
2314 StringBuilder sb = new StringBuilder(subTag + ": ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002315 sb.append(ev.getDownTime()).append(',');
2316 sb.append(ev.getEventTime()).append(',');
2317 sb.append(ev.getAction()).append(',');
Romain Guy8506ab42009-06-11 17:35:47 -07002318 sb.append(ev.getX()).append(',');
2319 sb.append(ev.getY()).append(',');
2320 sb.append(ev.getPressure()).append(',');
2321 sb.append(ev.getSize()).append(',');
2322 sb.append(ev.getMetaState()).append(',');
2323 sb.append(ev.getXPrecision()).append(',');
2324 sb.append(ev.getYPrecision()).append(',');
2325 sb.append(ev.getDeviceId()).append(',');
2326 sb.append(ev.getEdgeFlags());
2327 Log.d(TAG, sb.toString());
2328 }
2329 /**
2330 * log motion events
2331 */
2332 private static void captureKeyLog(String subTag, KeyEvent ev) {
2333 //check dynamic switch
2334 if (ev == null ||
2335 SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) {
2336 return;
2337 }
2338 StringBuilder sb = new StringBuilder(subTag + ": ");
2339 sb.append(ev.getDownTime()).append(',');
2340 sb.append(ev.getEventTime()).append(',');
2341 sb.append(ev.getAction()).append(',');
2342 sb.append(ev.getKeyCode()).append(',');
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002343 sb.append(ev.getRepeatCount()).append(',');
2344 sb.append(ev.getMetaState()).append(',');
2345 sb.append(ev.getDeviceId()).append(',');
2346 sb.append(ev.getScanCode());
Romain Guy8506ab42009-06-11 17:35:47 -07002347 Log.d(TAG, sb.toString());
2348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002349
2350 int enqueuePendingEvent(Object event, boolean sendDone) {
2351 int seq = mPendingEventSeq+1;
2352 if (seq < 0) seq = 0;
2353 mPendingEventSeq = seq;
2354 mPendingEvents.put(seq, event);
2355 return sendDone ? seq : -seq;
2356 }
2357
2358 Object retrievePendingEvent(int seq) {
2359 if (seq < 0) seq = -seq;
2360 Object event = mPendingEvents.get(seq);
2361 if (event != null) {
2362 mPendingEvents.remove(seq);
2363 }
2364 return event;
2365 }
Romain Guy8506ab42009-06-11 17:35:47 -07002366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 private void deliverKeyEvent(KeyEvent event, boolean sendDone) {
2368 // If mView is null, we just consume the key event because it doesn't
2369 // make sense to do anything else with it.
Romain Guy812ccbe2010-06-01 14:07:24 -07002370 boolean handled = mView == null || mView.dispatchKeyEventPreIme(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 if (handled) {
2372 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002373 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 }
2375 return;
2376 }
2377 // If it is possible for this window to interact with the input
2378 // method window, then we want to first dispatch our key events
2379 // to the input method.
2380 if (mLastWasImTarget) {
2381 InputMethodManager imm = InputMethodManager.peekInstance();
2382 if (imm != null && mView != null) {
2383 int seq = enqueuePendingEvent(event, sendDone);
2384 if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="
2385 + seq + " event=" + event);
2386 imm.dispatchKeyEvent(mView.getContext(), seq, event,
2387 mInputMethodCallback);
2388 return;
2389 }
2390 }
2391 deliverKeyEventToViewHierarchy(event, sendDone);
2392 }
2393
2394 void handleFinishedEvent(int seq, boolean handled) {
2395 final KeyEvent event = (KeyEvent)retrievePendingEvent(seq);
2396 if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq
2397 + " handled=" + handled + " event=" + event);
2398 if (event != null) {
2399 final boolean sendDone = seq >= 0;
2400 if (!handled) {
2401 deliverKeyEventToViewHierarchy(event, sendDone);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002402 } else if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002403 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 } else {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002405 Log.w(TAG, "handleFinishedEvent(seq=" + seq
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 + " handled=" + handled + " ev=" + event
2407 + ") neither delivering nor finishing key");
2408 }
2409 }
2410 }
Romain Guy8506ab42009-06-11 17:35:47 -07002411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002412 private void deliverKeyEventToViewHierarchy(KeyEvent event, boolean sendDone) {
2413 try {
2414 if (mView != null && mAdded) {
2415 final int action = event.getAction();
2416 boolean isDown = (action == KeyEvent.ACTION_DOWN);
2417
2418 if (checkForLeavingTouchModeAndConsume(event)) {
2419 return;
Romain Guy8506ab42009-06-11 17:35:47 -07002420 }
2421
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002422 if (Config.LOGV) {
2423 captureKeyLog("captureDispatchKeyEvent", event);
2424 }
2425 boolean keyHandled = mView.dispatchKeyEvent(event);
2426
2427 if (!keyHandled && isDown) {
2428 int direction = 0;
2429 switch (event.getKeyCode()) {
2430 case KeyEvent.KEYCODE_DPAD_LEFT:
2431 direction = View.FOCUS_LEFT;
2432 break;
2433 case KeyEvent.KEYCODE_DPAD_RIGHT:
2434 direction = View.FOCUS_RIGHT;
2435 break;
2436 case KeyEvent.KEYCODE_DPAD_UP:
2437 direction = View.FOCUS_UP;
2438 break;
2439 case KeyEvent.KEYCODE_DPAD_DOWN:
2440 direction = View.FOCUS_DOWN;
2441 break;
2442 }
2443
2444 if (direction != 0) {
2445
2446 View focused = mView != null ? mView.findFocus() : null;
2447 if (focused != null) {
2448 View v = focused.focusSearch(direction);
2449 boolean focusPassed = false;
2450 if (v != null && v != focused) {
2451 // do the math the get the interesting rect
2452 // of previous focused into the coord system of
2453 // newly focused view
2454 focused.getFocusedRect(mTempRect);
Dianne Hackborn1c6a8942010-03-23 16:34:20 -07002455 if (mView instanceof ViewGroup) {
2456 ((ViewGroup) mView).offsetDescendantRectToMyCoords(
2457 focused, mTempRect);
2458 ((ViewGroup) mView).offsetRectIntoDescendantCoords(
2459 v, mTempRect);
2460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002461 focusPassed = v.requestFocus(direction, mTempRect);
2462 }
2463
2464 if (!focusPassed) {
2465 mView.dispatchUnhandledMove(focused, direction);
2466 } else {
2467 playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
2468 }
2469 }
2470 }
2471 }
2472 }
2473
2474 } finally {
2475 if (sendDone) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002476 finishInputEvent();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002477 }
2478 // Let the exception fall through -- the looper will catch
2479 // it and take care of the bad app for us.
2480 }
2481 }
2482
Christopher Tatea53146c2010-09-07 11:57:52 -07002483 /* drag/drop */
2484 private void handleDragEvent(DragEvent event) {
2485 // From the root, only drag start/end/location are dispatched. entered/exited
2486 // are determined and dispatched by the viewgroup hierarchy, who then report
2487 // that back here for ultimate reporting back to the framework.
2488 if (mView != null && mAdded) {
2489 final int what = event.mAction;
2490
2491 if (what == DragEvent.ACTION_DRAG_EXITED) {
2492 // A direct EXITED event means that the window manager knows we've just crossed
2493 // a window boundary, so the current drag target within this one must have
2494 // just been exited. Send it the usual notifications and then we're done
2495 // for now.
2496 setDragFocus(event, null);
2497 } else {
2498 // Cache the drag description when the operation starts, then fill it in
2499 // on subsequent calls as a convenience
2500 if (what == DragEvent.ACTION_DRAG_STARTED) {
2501 mDragDescription = event.mClipDescription;
2502 } else {
2503 event.mClipDescription = mDragDescription;
2504 }
2505
2506 // For events with a [screen] location, translate into window coordinates
2507 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
2508 mDragPoint.set(event.mX, event.mY);
2509 if (mTranslator != null) {
2510 mTranslator.translatePointInScreenToAppWindow(mDragPoint);
2511 }
2512
2513 if (mCurScrollY != 0) {
2514 mDragPoint.offset(0, mCurScrollY);
2515 }
2516
2517 event.mX = mDragPoint.x;
2518 event.mY = mDragPoint.y;
2519 }
2520
2521 // Remember who the current drag target is pre-dispatch
2522 final View prevDragView = mCurrentDragView;
2523
2524 // Now dispatch the drag/drop event
Chris Tated4533f12010-10-19 15:15:08 -07002525 boolean result = mView.dispatchDragEvent(event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002526
2527 // If we changed apparent drag target, tell the OS about it
2528 if (prevDragView != mCurrentDragView) {
2529 try {
2530 if (prevDragView != null) {
2531 sWindowSession.dragRecipientExited(mWindow);
2532 }
2533 if (mCurrentDragView != null) {
2534 sWindowSession.dragRecipientEntered(mWindow);
2535 }
2536 } catch (RemoteException e) {
2537 Slog.e(TAG, "Unable to note drag target change");
2538 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002539 }
Chris Tated4533f12010-10-19 15:15:08 -07002540
2541 // Report the drop result if necessary
2542 if (what == DragEvent.ACTION_DROP) {
2543 try {
2544 Log.i(TAG, "Reporting drop result: " + result);
2545 sWindowSession.reportDropResult(mWindow, result);
2546 } catch (RemoteException e) {
2547 Log.e(TAG, "Unable to report drop result");
2548 }
2549 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002550 }
2551 }
2552 event.recycle();
2553 }
2554
Christopher Tate2c095f32010-10-04 14:13:40 -07002555 public void getLastTouchPoint(Point outLocation) {
2556 outLocation.x = (int) mLastTouchPoint.x;
2557 outLocation.y = (int) mLastTouchPoint.y;
2558 }
2559
Christopher Tatea53146c2010-09-07 11:57:52 -07002560 public void setDragFocus(DragEvent event, View newDragTarget) {
2561 final int action = event.mAction;
2562 // If we've dragged off of a view, send it the EXITED message
2563 if (mCurrentDragView != newDragTarget) {
2564 if (mCurrentDragView != null) {
2565 event.mAction = DragEvent.ACTION_DRAG_EXITED;
2566 mCurrentDragView.dispatchDragEvent(event);
2567 }
Chris Tate048691c2010-10-12 17:39:18 -07002568 mCurrentDragView = newDragTarget;
Christopher Tatea53146c2010-09-07 11:57:52 -07002569 }
2570 // If we've dragged over a new view, send it the ENTERED message
2571 if (newDragTarget != null) {
2572 event.mAction = DragEvent.ACTION_DRAG_ENTERED;
2573 newDragTarget.dispatchDragEvent(event);
2574 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002575 event.mAction = action; // restore the event's original state
2576 }
2577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002578 private AudioManager getAudioManager() {
2579 if (mView == null) {
2580 throw new IllegalStateException("getAudioManager called when there is no mView");
2581 }
2582 if (mAudioManager == null) {
2583 mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE);
2584 }
2585 return mAudioManager;
2586 }
2587
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002588 private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
2589 boolean insetsPending) throws RemoteException {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002590
2591 float appScale = mAttachInfo.mApplicationScale;
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002592 boolean restore = false;
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002593 if (params != null && mTranslator != null) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002594 restore = true;
2595 params.backup();
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002596 mTranslator.translateWindowLayout(params);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002597 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002598 if (params != null) {
2599 if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002600 }
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002601 mPendingConfiguration.seq = 0;
Dianne Hackbornf123e492010-09-24 11:16:23 -07002602 //Log.d(TAG, ">>>>>> CALLING relayout");
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002603 int relayoutResult = sWindowSession.relayout(
2604 mWindow, params,
Mitsuru Oshima61324e52009-07-21 15:40:36 -07002605 (int) (mView.mMeasuredWidth * appScale + 0.5f),
2606 (int) (mView.mMeasuredHeight * appScale + 0.5f),
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002607 viewVisibility, insetsPending, mWinFrame,
Dianne Hackborn694f79b2010-03-17 19:44:59 -07002608 mPendingContentInsets, mPendingVisibleInsets,
2609 mPendingConfiguration, mSurface);
Dianne Hackbornf123e492010-09-24 11:16:23 -07002610 //Log.d(TAG, "<<<<<< BACK FROM relayout");
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002611 if (restore) {
Mitsuru Oshimae5fb3282009-06-09 21:16:08 -07002612 params.restore();
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002613 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002614
2615 if (mTranslator != null) {
2616 mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
2617 mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
2618 mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002619 }
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002620 return relayoutResult;
2621 }
Romain Guy8506ab42009-06-11 17:35:47 -07002622
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002623 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 * {@inheritDoc}
2625 */
2626 public void playSoundEffect(int effectId) {
2627 checkThread();
2628
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002629 try {
2630 final AudioManager audioManager = getAudioManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631
Jean-Michel Trivi13b18fd2010-05-05 09:18:15 -07002632 switch (effectId) {
2633 case SoundEffectConstants.CLICK:
2634 audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
2635 return;
2636 case SoundEffectConstants.NAVIGATION_DOWN:
2637 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN);
2638 return;
2639 case SoundEffectConstants.NAVIGATION_LEFT:
2640 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT);
2641 return;
2642 case SoundEffectConstants.NAVIGATION_RIGHT:
2643 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT);
2644 return;
2645 case SoundEffectConstants.NAVIGATION_UP:
2646 audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP);
2647 return;
2648 default:
2649 throw new IllegalArgumentException("unknown effect id " + effectId +
2650 " not defined in " + SoundEffectConstants.class.getCanonicalName());
2651 }
2652 } catch (IllegalStateException e) {
2653 // Exception thrown by getAudioManager() when mView is null
2654 Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e);
2655 e.printStackTrace();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002656 }
2657 }
2658
2659 /**
2660 * {@inheritDoc}
2661 */
2662 public boolean performHapticFeedback(int effectId, boolean always) {
2663 try {
2664 return sWindowSession.performHapticFeedback(mWindow, effectId, always);
2665 } catch (RemoteException e) {
2666 return false;
2667 }
2668 }
2669
2670 /**
2671 * {@inheritDoc}
2672 */
2673 public View focusSearch(View focused, int direction) {
2674 checkThread();
2675 if (!(mView instanceof ViewGroup)) {
2676 return null;
2677 }
2678 return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction);
2679 }
2680
2681 public void debug() {
2682 mView.debug();
2683 }
2684
2685 public void die(boolean immediate) {
Dianne Hackborn94d69142009-09-28 22:14:42 -07002686 if (immediate) {
2687 doDie();
2688 } else {
2689 sendEmptyMessage(DIE);
2690 }
2691 }
2692
2693 void doDie() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002694 checkThread();
Jeff Brownb75fa302010-07-15 23:47:29 -07002695 if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002696 synchronized (this) {
2697 if (mAdded && !mFirst) {
Romain Guy29d89972010-09-22 16:10:57 -07002698 destroyHardwareRenderer();
2699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700 int viewVisibility = mView.getVisibility();
2701 boolean viewVisibilityChanged = mViewVisibility != viewVisibility;
2702 if (mWindowAttributesChanged || viewVisibilityChanged) {
2703 // If layout params have been changed, first give them
2704 // to the window manager to make sure it has the correct
2705 // animation info.
2706 try {
Mitsuru Oshima8169dae2009-04-28 18:12:09 -07002707 if ((relayoutWindow(mWindowAttributes, viewVisibility, false)
2708 & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002709 sWindowSession.finishDrawing(mWindow);
2710 }
2711 } catch (RemoteException e) {
2712 }
2713 }
2714
Dianne Hackborn0586a1b2009-09-06 21:08:27 -07002715 mSurface.release();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002716 }
2717 if (mAdded) {
2718 mAdded = false;
Dianne Hackborn94d69142009-09-28 22:14:42 -07002719 dispatchDetachedFromWindow();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 }
2721 }
2722 }
2723
Romain Guy29d89972010-09-22 16:10:57 -07002724 private void destroyHardwareRenderer() {
Romain Guyb051e892010-09-28 19:09:36 -07002725 if (mAttachInfo.mHardwareRenderer != null) {
2726 mAttachInfo.mHardwareRenderer.destroy(true);
2727 mAttachInfo.mHardwareRenderer = null;
Romain Guy29d89972010-09-22 16:10:57 -07002728 mAttachInfo.mHardwareAccelerated = false;
2729 }
2730 }
2731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 public void dispatchFinishedEvent(int seq, boolean handled) {
2733 Message msg = obtainMessage(FINISHED_EVENT);
2734 msg.arg1 = seq;
2735 msg.arg2 = handled ? 1 : 0;
2736 sendMessage(msg);
2737 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -07002738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002739 public void dispatchResized(int w, int h, Rect coveredInsets,
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002740 Rect visibleInsets, boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w
2742 + " h=" + h + " coveredInsets=" + coveredInsets.toShortString()
2743 + " visibleInsets=" + visibleInsets.toShortString()
2744 + " reportDraw=" + reportDraw);
2745 Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED);
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002746 if (mTranslator != null) {
2747 mTranslator.translateRectInScreenToAppWindow(coveredInsets);
2748 mTranslator.translateRectInScreenToAppWindow(visibleInsets);
2749 w *= mTranslator.applicationInvertedScale;
2750 h *= mTranslator.applicationInvertedScale;
Mitsuru Oshima9189cab2009-06-03 11:19:12 -07002751 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -07002752 msg.arg1 = w;
2753 msg.arg2 = h;
Dianne Hackborne36d6e22010-02-17 19:46:25 -08002754 ResizedInfo ri = new ResizedInfo();
2755 ri.coveredInsets = new Rect(coveredInsets);
2756 ri.visibleInsets = new Rect(visibleInsets);
2757 ri.newConfig = newConfig;
2758 msg.obj = ri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002759 sendMessage(msg);
2760 }
Jeff Brown46b9ac02010-04-22 18:58:52 -07002761
2762 private Runnable mFinishedCallback;
2763
2764 private final InputHandler mInputHandler = new InputHandler() {
2765 public void handleKey(KeyEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002766 startInputEvent(finishedCallback);
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002767 dispatchKey(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002768 }
2769
Jeff Brownc5ed5912010-07-14 18:48:53 -07002770 public void handleMotion(MotionEvent event, Runnable finishedCallback) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002771 startInputEvent(finishedCallback);
2772 dispatchMotion(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -07002773 }
2774 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002775
2776 public void dispatchKey(KeyEvent event) {
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002777 dispatchKey(event, false);
2778 }
2779
2780 private void dispatchKey(KeyEvent event, boolean sendDone) {
2781 //noinspection ConstantConditions
2782 if (false && event.getAction() == KeyEvent.ACTION_DOWN) {
2783 if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) {
Romain Guy812ccbe2010-06-01 14:07:24 -07002784 if (DBG) Log.d("keydisp", "===================================================");
2785 if (DBG) Log.d("keydisp", "Focused view Hierarchy is:");
2786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002787 debug();
2788
Romain Guy812ccbe2010-06-01 14:07:24 -07002789 if (DBG) Log.d("keydisp", "===================================================");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002790 }
2791 }
2792
2793 Message msg = obtainMessage(DISPATCH_KEY);
2794 msg.obj = event;
Jeff Brown92ff1dd2010-08-11 16:16:06 -07002795 msg.arg1 = sendDone ? 1 : 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002796
2797 if (LOCAL_LOGV) Log.v(
Jeff Brownc5ed5912010-07-14 18:48:53 -07002798 TAG, "sending key " + event + " to " + mView);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002799
2800 sendMessageAtTime(msg, event.getEventTime());
2801 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002802
2803 public void dispatchMotion(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002804 dispatchMotion(event, false);
2805 }
2806
2807 private void dispatchMotion(MotionEvent event, boolean sendDone) {
Jeff Brownc5ed5912010-07-14 18:48:53 -07002808 int source = event.getSource();
2809 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002810 dispatchPointer(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002811 } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002812 dispatchTrackball(event, sendDone);
Jeff Brownc5ed5912010-07-14 18:48:53 -07002813 } else {
2814 // TODO
2815 Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event);
Jeff Brown93ed4e32010-09-23 13:51:48 -07002816 if (sendDone) {
2817 finishInputEvent();
2818 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07002819 }
2820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002822 public void dispatchPointer(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002823 dispatchPointer(event, false);
2824 }
2825
2826 private void dispatchPointer(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 Message msg = obtainMessage(DISPATCH_POINTER);
2828 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002829 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002830 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002831 }
2832
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002833 public void dispatchTrackball(MotionEvent event) {
Jeff Brown93ed4e32010-09-23 13:51:48 -07002834 dispatchTrackball(event, false);
2835 }
2836
2837 private void dispatchTrackball(MotionEvent event, boolean sendDone) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002838 Message msg = obtainMessage(DISPATCH_TRACKBALL);
2839 msg.obj = event;
Jeff Brown93ed4e32010-09-23 13:51:48 -07002840 msg.arg1 = sendDone ? 1 : 0;
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002841 sendMessageAtTime(msg, event.getEventTime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 }
Jeff Brown00fa7bd2010-07-02 15:37:36 -07002843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002844 public void dispatchAppVisibility(boolean visible) {
2845 Message msg = obtainMessage(DISPATCH_APP_VISIBILITY);
2846 msg.arg1 = visible ? 1 : 0;
2847 sendMessage(msg);
2848 }
2849
2850 public void dispatchGetNewSurface() {
2851 Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE);
2852 sendMessage(msg);
2853 }
2854
2855 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
2856 Message msg = Message.obtain();
2857 msg.what = WINDOW_FOCUS_CHANGED;
2858 msg.arg1 = hasFocus ? 1 : 0;
2859 msg.arg2 = inTouchMode ? 1 : 0;
2860 sendMessage(msg);
2861 }
2862
Dianne Hackbornffa42482009-09-23 22:20:11 -07002863 public void dispatchCloseSystemDialogs(String reason) {
2864 Message msg = Message.obtain();
2865 msg.what = CLOSE_SYSTEM_DIALOGS;
2866 msg.obj = reason;
2867 sendMessage(msg);
2868 }
Christopher Tatea53146c2010-09-07 11:57:52 -07002869
2870 public void dispatchDragEvent(DragEvent event) {
Chris Tate91e9bb32010-10-12 12:58:43 -07002871 final int what;
2872 if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) {
2873 what = DISPATCH_DRAG_LOCATION_EVENT;
2874 removeMessages(what);
2875 } else {
2876 what = DISPATCH_DRAG_EVENT;
2877 }
2878 Message msg = obtainMessage(what, event);
Christopher Tatea53146c2010-09-07 11:57:52 -07002879 sendMessage(msg);
2880 }
2881
svetoslavganov75986cf2009-05-14 22:28:01 -07002882 /**
2883 * The window is getting focus so if there is anything focused/selected
2884 * send an {@link AccessibilityEvent} to announce that.
2885 */
2886 private void sendAccessibilityEvents() {
2887 if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) {
2888 return;
2889 }
2890 mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2891 View focusedView = mView.findFocus();
2892 if (focusedView != null && focusedView != mView) {
2893 focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2894 }
2895 }
2896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002897 public boolean showContextMenuForChild(View originalView) {
2898 return false;
2899 }
2900
Adam Powell6e346362010-07-23 10:18:23 -07002901 public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) {
2902 return null;
2903 }
2904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002905 public void createContextMenu(ContextMenu menu) {
2906 }
2907
2908 public void childDrawableStateChanged(View child) {
2909 }
2910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 void checkThread() {
2912 if (mThread != Thread.currentThread()) {
2913 throw new CalledFromWrongThreadException(
2914 "Only the original thread that created a view hierarchy can touch its views.");
2915 }
2916 }
2917
2918 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
2919 // ViewRoot never intercepts touch event, so this can be a no-op
2920 }
2921
2922 public boolean requestChildRectangleOnScreen(View child, Rect rectangle,
2923 boolean immediate) {
2924 return scrollToRectOrFocus(rectangle, immediate);
2925 }
Romain Guy8506ab42009-06-11 17:35:47 -07002926
Dianne Hackborndc8a7f62010-05-10 11:29:34 -07002927 class TakenSurfaceHolder extends BaseSurfaceHolder {
2928 @Override
2929 public boolean onAllowLockCanvas() {
2930 return mDrawingAllowed;
2931 }
2932
2933 @Override
2934 public void onRelayoutContainer() {
2935 // Not currently interesting -- from changing between fixed and layout size.
2936 }
2937
2938 public void setFormat(int format) {
2939 ((RootViewSurfaceTaker)mView).setSurfaceFormat(format);
2940 }
2941
2942 public void setType(int type) {
2943 ((RootViewSurfaceTaker)mView).setSurfaceType(type);
2944 }
2945
2946 @Override
2947 public void onUpdateSurface() {
2948 // We take care of format and type changes on our own.
2949 throw new IllegalStateException("Shouldn't be here");
2950 }
2951
2952 public boolean isCreating() {
2953 return mIsCreating;
2954 }
2955
2956 @Override
2957 public void setFixedSize(int width, int height) {
2958 throw new UnsupportedOperationException(
2959 "Currently only support sizing from layout");
2960 }
2961
2962 public void setKeepScreenOn(boolean screenOn) {
2963 ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn);
2964 }
2965 }
2966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002967 static class InputMethodCallback extends IInputMethodCallback.Stub {
2968 private WeakReference<ViewRoot> mViewRoot;
2969
2970 public InputMethodCallback(ViewRoot viewRoot) {
2971 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2972 }
Romain Guy8506ab42009-06-11 17:35:47 -07002973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 public void finishedEvent(int seq, boolean handled) {
2975 final ViewRoot viewRoot = mViewRoot.get();
2976 if (viewRoot != null) {
2977 viewRoot.dispatchFinishedEvent(seq, handled);
2978 }
2979 }
2980
2981 public void sessionCreated(IInputMethodSession session) throws RemoteException {
2982 // Stub -- not for use in the client.
2983 }
2984 }
Romain Guy8506ab42009-06-11 17:35:47 -07002985
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002986 static class W extends IWindow.Stub {
The Android Open Source Projectba87e3e2009-03-13 13:04:22 -07002987 private final WeakReference<ViewRoot> mViewRoot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988
Romain Guyfb8b7632010-08-23 21:05:08 -07002989 W(ViewRoot viewRoot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002990 mViewRoot = new WeakReference<ViewRoot>(viewRoot);
2991 }
2992
Romain Guyfb8b7632010-08-23 21:05:08 -07002993 public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets,
2994 boolean reportDraw, Configuration newConfig) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002995 final ViewRoot viewRoot = mViewRoot.get();
2996 if (viewRoot != null) {
Romain Guyfb8b7632010-08-23 21:05:08 -07002997 viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002998 }
2999 }
3000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003001 public void dispatchAppVisibility(boolean visible) {
3002 final ViewRoot viewRoot = mViewRoot.get();
3003 if (viewRoot != null) {
3004 viewRoot.dispatchAppVisibility(visible);
3005 }
3006 }
3007
3008 public void dispatchGetNewSurface() {
3009 final ViewRoot viewRoot = mViewRoot.get();
3010 if (viewRoot != null) {
3011 viewRoot.dispatchGetNewSurface();
3012 }
3013 }
3014
3015 public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) {
3016 final ViewRoot viewRoot = mViewRoot.get();
3017 if (viewRoot != null) {
3018 viewRoot.windowFocusChanged(hasFocus, inTouchMode);
3019 }
3020 }
3021
3022 private static int checkCallingPermission(String permission) {
3023 if (!Process.supportsProcesses()) {
3024 return PackageManager.PERMISSION_GRANTED;
3025 }
3026
3027 try {
3028 return ActivityManagerNative.getDefault().checkPermission(
3029 permission, Binder.getCallingPid(), Binder.getCallingUid());
3030 } catch (RemoteException e) {
3031 return PackageManager.PERMISSION_DENIED;
3032 }
3033 }
3034
3035 public void executeCommand(String command, String parameters, ParcelFileDescriptor out) {
3036 final ViewRoot viewRoot = mViewRoot.get();
3037 if (viewRoot != null) {
3038 final View view = viewRoot.mView;
3039 if (view != null) {
3040 if (checkCallingPermission(Manifest.permission.DUMP) !=
3041 PackageManager.PERMISSION_GRANTED) {
3042 throw new SecurityException("Insufficient permissions to invoke"
3043 + " executeCommand() from pid=" + Binder.getCallingPid()
3044 + ", uid=" + Binder.getCallingUid());
3045 }
3046
3047 OutputStream clientStream = null;
3048 try {
3049 clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out);
3050 ViewDebug.dispatchCommand(view, command, parameters, clientStream);
3051 } catch (IOException e) {
3052 e.printStackTrace();
3053 } finally {
3054 if (clientStream != null) {
3055 try {
3056 clientStream.close();
3057 } catch (IOException e) {
3058 e.printStackTrace();
3059 }
3060 }
3061 }
3062 }
3063 }
3064 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003065
Dianne Hackbornffa42482009-09-23 22:20:11 -07003066 public void closeSystemDialogs(String reason) {
3067 final ViewRoot viewRoot = mViewRoot.get();
3068 if (viewRoot != null) {
3069 viewRoot.dispatchCloseSystemDialogs(reason);
3070 }
3071 }
3072
Marco Nelissenbf6956b2009-11-09 15:21:13 -08003073 public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep,
3074 boolean sync) {
Dianne Hackborn19382ac2009-09-11 21:13:37 -07003075 if (sync) {
3076 try {
3077 sWindowSession.wallpaperOffsetsComplete(asBinder());
3078 } catch (RemoteException e) {
3079 }
3080 }
Dianne Hackborn72c82ab2009-08-11 21:13:54 -07003081 }
Dianne Hackborn75804932009-10-20 20:15:20 -07003082
3083 public void dispatchWallpaperCommand(String action, int x, int y,
3084 int z, Bundle extras, boolean sync) {
3085 if (sync) {
3086 try {
3087 sWindowSession.wallpaperCommandComplete(asBinder(), null);
3088 } catch (RemoteException e) {
3089 }
3090 }
3091 }
Christopher Tatea53146c2010-09-07 11:57:52 -07003092
3093 /* Drag/drop */
3094 public void dispatchDragEvent(DragEvent event) {
3095 final ViewRoot viewRoot = mViewRoot.get();
3096 if (viewRoot != null) {
3097 viewRoot.dispatchDragEvent(event);
3098 }
3099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003100 }
3101
3102 /**
3103 * Maintains state information for a single trackball axis, generating
3104 * discrete (DPAD) movements based on raw trackball motion.
3105 */
3106 static final class TrackballAxis {
3107 /**
3108 * The maximum amount of acceleration we will apply.
3109 */
3110 static final float MAX_ACCELERATION = 20;
Romain Guy8506ab42009-06-11 17:35:47 -07003111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 /**
3113 * The maximum amount of time (in milliseconds) between events in order
3114 * for us to consider the user to be doing fast trackball movements,
3115 * and thus apply an acceleration.
3116 */
3117 static final long FAST_MOVE_TIME = 150;
Romain Guy8506ab42009-06-11 17:35:47 -07003118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003119 /**
3120 * Scaling factor to the time (in milliseconds) between events to how
3121 * much to multiple/divide the current acceleration. When movement
3122 * is < FAST_MOVE_TIME this multiplies the acceleration; when >
3123 * FAST_MOVE_TIME it divides it.
3124 */
3125 static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40);
Romain Guy8506ab42009-06-11 17:35:47 -07003126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 float position;
3128 float absPosition;
3129 float acceleration = 1;
3130 long lastMoveTime = 0;
3131 int step;
3132 int dir;
3133 int nonAccelMovement;
3134
3135 void reset(int _step) {
3136 position = 0;
3137 acceleration = 1;
3138 lastMoveTime = 0;
3139 step = _step;
3140 dir = 0;
3141 }
3142
3143 /**
3144 * Add trackball movement into the state. If the direction of movement
3145 * has been reversed, the state is reset before adding the
3146 * movement (so that you don't have to compensate for any previously
3147 * collected movement before see the result of the movement in the
3148 * new direction).
3149 *
3150 * @return Returns the absolute value of the amount of movement
3151 * collected so far.
3152 */
3153 float collect(float off, long time, String axis) {
3154 long normTime;
3155 if (off > 0) {
3156 normTime = (long)(off * FAST_MOVE_TIME);
3157 if (dir < 0) {
3158 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!");
3159 position = 0;
3160 step = 0;
3161 acceleration = 1;
3162 lastMoveTime = 0;
3163 }
3164 dir = 1;
3165 } else if (off < 0) {
3166 normTime = (long)((-off) * FAST_MOVE_TIME);
3167 if (dir > 0) {
3168 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!");
3169 position = 0;
3170 step = 0;
3171 acceleration = 1;
3172 lastMoveTime = 0;
3173 }
3174 dir = -1;
3175 } else {
3176 normTime = 0;
3177 }
Romain Guy8506ab42009-06-11 17:35:47 -07003178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003179 // The number of milliseconds between each movement that is
3180 // considered "normal" and will not result in any acceleration
3181 // or deceleration, scaled by the offset we have here.
3182 if (normTime > 0) {
3183 long delta = time - lastMoveTime;
3184 lastMoveTime = time;
3185 float acc = acceleration;
3186 if (delta < normTime) {
3187 // The user is scrolling rapidly, so increase acceleration.
3188 float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR;
3189 if (scale > 1) acc *= scale;
3190 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off="
3191 + off + " normTime=" + normTime + " delta=" + delta
3192 + " scale=" + scale + " acc=" + acc);
3193 acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION;
3194 } else {
3195 // The user is scrolling slowly, so decrease acceleration.
3196 float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR;
3197 if (scale > 1) acc /= scale;
3198 if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off="
3199 + off + " normTime=" + normTime + " delta=" + delta
3200 + " scale=" + scale + " acc=" + acc);
3201 acceleration = acc > 1 ? acc : 1;
3202 }
3203 }
3204 position += off;
3205 return (absPosition = Math.abs(position));
3206 }
3207
3208 /**
3209 * Generate the number of discrete movement events appropriate for
3210 * the currently collected trackball movement.
3211 *
3212 * @param precision The minimum movement required to generate the
3213 * first discrete movement.
3214 *
3215 * @return Returns the number of discrete movements, either positive
3216 * or negative, or 0 if there is not enough trackball movement yet
3217 * for a discrete movement.
3218 */
3219 int generate(float precision) {
3220 int movement = 0;
3221 nonAccelMovement = 0;
3222 do {
3223 final int dir = position >= 0 ? 1 : -1;
3224 switch (step) {
3225 // If we are going to execute the first step, then we want
3226 // to do this as soon as possible instead of waiting for
3227 // a full movement, in order to make things look responsive.
3228 case 0:
3229 if (absPosition < precision) {
3230 return movement;
3231 }
3232 movement += dir;
3233 nonAccelMovement += dir;
3234 step = 1;
3235 break;
3236 // If we have generated the first movement, then we need
3237 // to wait for the second complete trackball motion before
3238 // generating the second discrete movement.
3239 case 1:
3240 if (absPosition < 2) {
3241 return movement;
3242 }
3243 movement += dir;
3244 nonAccelMovement += dir;
3245 position += dir > 0 ? -2 : 2;
3246 absPosition = Math.abs(position);
3247 step = 2;
3248 break;
3249 // After the first two, we generate discrete movements
3250 // consistently with the trackball, applying an acceleration
3251 // if the trackball is moving quickly. This is a simple
3252 // acceleration on top of what we already compute based
3253 // on how quickly the wheel is being turned, to apply
3254 // a longer increasing acceleration to continuous movement
3255 // in one direction.
3256 default:
3257 if (absPosition < 1) {
3258 return movement;
3259 }
3260 movement += dir;
3261 position += dir >= 0 ? -1 : 1;
3262 absPosition = Math.abs(position);
3263 float acc = acceleration;
3264 acc *= 1.1f;
3265 acceleration = acc < MAX_ACCELERATION ? acc : acceleration;
3266 break;
3267 }
3268 } while (true);
3269 }
3270 }
3271
3272 public static final class CalledFromWrongThreadException extends AndroidRuntimeException {
3273 public CalledFromWrongThreadException(String msg) {
3274 super(msg);
3275 }
3276 }
3277
3278 private SurfaceHolder mHolder = new SurfaceHolder() {
3279 // we only need a SurfaceHolder for opengl. it would be nice
3280 // to implement everything else though, especially the callback
3281 // support (opengl doesn't make use of it right now, but eventually
3282 // will).
3283 public Surface getSurface() {
3284 return mSurface;
3285 }
3286
3287 public boolean isCreating() {
3288 return false;
3289 }
3290
3291 public void addCallback(Callback callback) {
3292 }
3293
3294 public void removeCallback(Callback callback) {
3295 }
3296
3297 public void setFixedSize(int width, int height) {
3298 }
3299
3300 public void setSizeFromLayout() {
3301 }
3302
3303 public void setFormat(int format) {
3304 }
3305
3306 public void setType(int type) {
3307 }
3308
3309 public void setKeepScreenOn(boolean screenOn) {
3310 }
3311
3312 public Canvas lockCanvas() {
3313 return null;
3314 }
3315
3316 public Canvas lockCanvas(Rect dirty) {
3317 return null;
3318 }
3319
3320 public void unlockCanvasAndPost(Canvas canvas) {
3321 }
3322 public Rect getSurfaceFrame() {
3323 return null;
3324 }
3325 };
3326
3327 static RunQueue getRunQueue() {
3328 RunQueue rq = sRunQueues.get();
3329 if (rq != null) {
3330 return rq;
3331 }
3332 rq = new RunQueue();
3333 sRunQueues.set(rq);
3334 return rq;
3335 }
Romain Guy8506ab42009-06-11 17:35:47 -07003336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003337 /**
3338 * @hide
3339 */
3340 static final class RunQueue {
3341 private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();
3342
3343 void post(Runnable action) {
3344 postDelayed(action, 0);
3345 }
3346
3347 void postDelayed(Runnable action, long delayMillis) {
3348 HandlerAction handlerAction = new HandlerAction();
3349 handlerAction.action = action;
3350 handlerAction.delay = delayMillis;
3351
3352 synchronized (mActions) {
3353 mActions.add(handlerAction);
3354 }
3355 }
3356
3357 void removeCallbacks(Runnable action) {
3358 final HandlerAction handlerAction = new HandlerAction();
3359 handlerAction.action = action;
3360
3361 synchronized (mActions) {
3362 final ArrayList<HandlerAction> actions = mActions;
3363
3364 while (actions.remove(handlerAction)) {
3365 // Keep going
3366 }
3367 }
3368 }
3369
3370 void executeActions(Handler handler) {
3371 synchronized (mActions) {
3372 final ArrayList<HandlerAction> actions = mActions;
3373 final int count = actions.size();
3374
3375 for (int i = 0; i < count; i++) {
3376 final HandlerAction handlerAction = actions.get(i);
3377 handler.postDelayed(handlerAction.action, handlerAction.delay);
3378 }
3379
Romain Guy15df6702009-08-17 20:17:30 -07003380 actions.clear();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 }
3382 }
3383
3384 private static class HandlerAction {
3385 Runnable action;
3386 long delay;
3387
3388 @Override
3389 public boolean equals(Object o) {
3390 if (this == o) return true;
3391 if (o == null || getClass() != o.getClass()) return false;
3392
3393 HandlerAction that = (HandlerAction) o;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 return !(action != null ? !action.equals(that.action) : that.action != null);
3395
3396 }
3397
3398 @Override
3399 public int hashCode() {
3400 int result = action != null ? action.hashCode() : 0;
3401 result = 31 * result + (int) (delay ^ (delay >>> 32));
3402 return result;
3403 }
3404 }
3405 }
3406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 private static native void nativeShowFPS(Canvas canvas, int durationMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003408}