The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 19 | import android.Manifest; |
| 20 | import android.app.ActivityManagerNative; |
| 21 | import android.content.ClipDescription; |
| 22 | import android.content.ComponentCallbacks; |
| 23 | import android.content.Context; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.res.CompatibilityInfo; |
| 26 | import android.content.res.Configuration; |
| 27 | import android.content.res.Resources; |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 28 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.graphics.Canvas; |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 30 | import android.graphics.Paint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.graphics.PixelFormat; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 32 | import android.graphics.Point; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 33 | import android.graphics.PointF; |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 34 | import android.graphics.PorterDuff; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.graphics.Rect; |
| 36 | import android.graphics.Region; |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 37 | import android.media.AudioManager; |
| 38 | import android.os.Binder; |
| 39 | import android.os.Bundle; |
| 40 | import android.os.Debug; |
| 41 | import android.os.Handler; |
| 42 | import android.os.LatencyTimer; |
| 43 | import android.os.Looper; |
| 44 | import android.os.Message; |
| 45 | import android.os.ParcelFileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Process; |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 47 | import android.os.RemoteException; |
| 48 | import android.os.ServiceManager; |
| 49 | import android.os.SystemClock; |
| 50 | import android.os.SystemProperties; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.util.AndroidRuntimeException; |
| 52 | import android.util.Config; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 53 | import android.util.DisplayMetrics; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.util.EventLog; |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 55 | import android.util.Log; |
Chet Haase | 949dbf7 | 2010-08-11 18:41:06 -0700 | [diff] [blame] | 56 | import android.util.Slog; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.util.SparseArray; |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 58 | import android.util.TypedValue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.view.View.MeasureSpec; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 60 | import android.view.accessibility.AccessibilityEvent; |
| 61 | import android.view.accessibility.AccessibilityManager; |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 62 | import android.view.animation.AccelerateDecelerateInterpolator; |
| 63 | import android.view.animation.Interpolator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.view.inputmethod.InputConnection; |
| 65 | import android.view.inputmethod.InputMethodManager; |
| 66 | import android.widget.Scroller; |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 67 | import com.android.internal.policy.PolicyManager; |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 68 | import com.android.internal.view.BaseSurfaceHolder; |
| 69 | import com.android.internal.view.IInputMethodCallback; |
| 70 | import com.android.internal.view.IInputMethodSession; |
| 71 | import com.android.internal.view.RootViewSurfaceTaker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import java.io.IOException; |
| 74 | import java.io.OutputStream; |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 75 | import java.lang.ref.WeakReference; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import java.util.ArrayList; |
| 77 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | /** |
| 79 | * The top of a view hierarchy, implementing the needed protocol between View |
| 80 | * and the WindowManager. This is for the most part an internal implementation |
| 81 | * detail of {@link WindowManagerImpl}. |
| 82 | * |
| 83 | * {@hide} |
| 84 | */ |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 85 | @SuppressWarnings({"EmptyCatchBlock", "PointlessBooleanExpression"}) |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 86 | public final class ViewRoot extends Handler implements ViewParent, |
| 87 | View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | private static final String TAG = "ViewRoot"; |
| 89 | private static final boolean DBG = false; |
Mike Reed | fd71653 | 2009-10-12 14:42:56 -0400 | [diff] [blame] | 90 | private static final boolean SHOW_FPS = false; |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 91 | private static final boolean LOCAL_LOGV = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | /** @noinspection PointlessBooleanExpression*/ |
| 93 | private static final boolean DEBUG_DRAW = false || LOCAL_LOGV; |
| 94 | private static final boolean DEBUG_LAYOUT = false || LOCAL_LOGV; |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 95 | private static final boolean DEBUG_DIALOG = false || LOCAL_LOGV; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private static final boolean DEBUG_INPUT_RESIZE = false || LOCAL_LOGV; |
| 97 | private static final boolean DEBUG_ORIENTATION = false || LOCAL_LOGV; |
| 98 | private static final boolean DEBUG_TRACKBALL = false || LOCAL_LOGV; |
| 99 | private static final boolean DEBUG_IMF = false || LOCAL_LOGV; |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 100 | private static final boolean DEBUG_CONFIGURATION = false || LOCAL_LOGV; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | private static final boolean WATCH_POINTER = false; |
| 102 | |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 103 | private static final boolean MEASURE_LATENCY = false; |
| 104 | private static LatencyTimer lt; |
| 105 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Maximum time we allow the user to roll the trackball enough to generate |
| 108 | * a key event, before resetting the counters. |
| 109 | */ |
| 110 | static final int MAX_TRACKBALL_DELAY = 250; |
| 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | static IWindowSession sWindowSession; |
| 113 | |
| 114 | static final Object mStaticInit = new Object(); |
| 115 | static boolean mInitialized = false; |
| 116 | |
| 117 | static final ThreadLocal<RunQueue> sRunQueues = new ThreadLocal<RunQueue>(); |
| 118 | |
Dianne Hackborn | 2a9094d | 2010-02-03 19:20:09 -0800 | [diff] [blame] | 119 | static final ArrayList<Runnable> sFirstDrawHandlers = new ArrayList<Runnable>(); |
| 120 | static boolean sFirstDrawComplete = false; |
| 121 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 122 | static final ArrayList<ComponentCallbacks> sConfigCallbacks |
| 123 | = new ArrayList<ComponentCallbacks>(); |
| 124 | |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 125 | private static int sDrawTime; |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | long mLastTrackballTime = 0; |
| 128 | final TrackballAxis mTrackballAxisX = new TrackballAxis(); |
| 129 | final TrackballAxis mTrackballAxisY = new TrackballAxis(); |
| 130 | |
| 131 | final int[] mTmpLocation = new int[2]; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 132 | |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 133 | final TypedValue mTmpValue = new TypedValue(); |
| 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | final InputMethodCallback mInputMethodCallback; |
| 136 | final SparseArray<Object> mPendingEvents = new SparseArray<Object>(); |
| 137 | int mPendingEventSeq = 0; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 138 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | final Thread mThread; |
| 140 | |
| 141 | final WindowLeaked mLocation; |
| 142 | |
| 143 | final WindowManager.LayoutParams mWindowAttributes = new WindowManager.LayoutParams(); |
| 144 | |
| 145 | final W mWindow; |
| 146 | |
| 147 | View mView; |
| 148 | View mFocusedView; |
| 149 | View mRealFocusedView; // this is not set to null in touch mode |
| 150 | int mViewVisibility; |
| 151 | boolean mAppVisible = true; |
| 152 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 153 | SurfaceHolder.Callback2 mSurfaceHolderCallback; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 154 | BaseSurfaceHolder mSurfaceHolder; |
| 155 | boolean mIsCreating; |
| 156 | boolean mDrawingAllowed; |
| 157 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | final Region mTransparentRegion; |
| 159 | final Region mPreviousTransparentRegion; |
| 160 | |
| 161 | int mWidth; |
| 162 | int mHeight; |
| 163 | Rect mDirty; // will be a graphics.Region soon |
Romain Guy | bb93d55 | 2009-03-24 21:04:15 -0700 | [diff] [blame] | 164 | boolean mIsAnimating; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 165 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 166 | CompatibilityInfo.Translator mTranslator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
| 168 | final View.AttachInfo mAttachInfo; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 169 | InputChannel mInputChannel; |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 170 | InputQueue.Callback mInputQueueCallback; |
| 171 | InputQueue mInputQueue; |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 172 | FallbackEventHandler mFallbackEventHandler; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 173 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | final Rect mTempRect; // used in the transaction to not thrash the heap. |
| 175 | final Rect mVisRect; // used to retrieve visible rect of focused view. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
| 177 | boolean mTraversalScheduled; |
| 178 | boolean mWillDrawSoon; |
| 179 | boolean mLayoutRequested; |
| 180 | boolean mFirst; |
| 181 | boolean mReportNextDraw; |
| 182 | boolean mFullRedrawNeeded; |
| 183 | boolean mNewSurfaceNeeded; |
| 184 | boolean mHasHadWindowFocus; |
| 185 | boolean mLastWasImTarget; |
| 186 | |
| 187 | boolean mWindowAttributesChanged = false; |
| 188 | |
| 189 | // These can be accessed by any thread, must be protected with a lock. |
Mathias Agopian | 5583dc6 | 2009-07-09 16:28:11 -0700 | [diff] [blame] | 190 | // Surface can never be reassigned or cleared (use Surface.clear()). |
| 191 | private final Surface mSurface = new Surface(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
| 193 | boolean mAdded; |
| 194 | boolean mAddedTouchMode; |
| 195 | |
| 196 | /*package*/ int mAddNesting; |
| 197 | |
| 198 | // These are accessed by multiple threads. |
| 199 | final Rect mWinFrame; // frame given by window manager. |
| 200 | |
| 201 | final Rect mPendingVisibleInsets = new Rect(); |
| 202 | final Rect mPendingContentInsets = new Rect(); |
| 203 | final ViewTreeObserver.InternalInsetsInfo mLastGivenInsets |
| 204 | = new ViewTreeObserver.InternalInsetsInfo(); |
| 205 | |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 206 | final Configuration mLastConfiguration = new Configuration(); |
| 207 | final Configuration mPendingConfiguration = new Configuration(); |
| 208 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 209 | class ResizedInfo { |
| 210 | Rect coveredInsets; |
| 211 | Rect visibleInsets; |
| 212 | Configuration newConfig; |
| 213 | } |
| 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | boolean mScrollMayChange; |
| 216 | int mSoftInputMode; |
| 217 | View mLastScrolledFocus; |
| 218 | int mScrollY; |
| 219 | int mCurScrollY; |
| 220 | Scroller mScroller; |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 221 | Bitmap mResizeBitmap; |
| 222 | long mResizeBitmapStartTime; |
| 223 | int mResizeBitmapDuration; |
| 224 | static final Interpolator mResizeInterpolator = new AccelerateDecelerateInterpolator(); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 225 | |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 226 | final ViewConfiguration mViewConfiguration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 228 | /* Drag/drop */ |
| 229 | ClipDescription mDragDescription; |
| 230 | View mCurrentDragView; |
Christopher Tate | 407b4e9 | 2010-11-30 17:14:08 -0800 | [diff] [blame] | 231 | Object mLocalDragState; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 232 | final PointF mDragPoint = new PointF(); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 233 | final PointF mLastTouchPoint = new PointF(); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | /** |
| 236 | * see {@link #playSoundEffect(int)} |
| 237 | */ |
| 238 | AudioManager mAudioManager; |
| 239 | |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 240 | private final int mDensity; |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 241 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 242 | public static IWindowSession getWindowSession(Looper mainLooper) { |
| 243 | synchronized (mStaticInit) { |
| 244 | if (!mInitialized) { |
| 245 | try { |
| 246 | InputMethodManager imm = InputMethodManager.getInstance(mainLooper); |
| 247 | sWindowSession = IWindowManager.Stub.asInterface( |
| 248 | ServiceManager.getService("window")) |
| 249 | .openSession(imm.getClient(), imm.getInputContext()); |
| 250 | mInitialized = true; |
| 251 | } catch (RemoteException e) { |
| 252 | } |
| 253 | } |
| 254 | return sWindowSession; |
| 255 | } |
| 256 | } |
| 257 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | public ViewRoot(Context context) { |
| 259 | super(); |
| 260 | |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 261 | if (MEASURE_LATENCY) { |
| 262 | if (lt == null) { |
| 263 | lt = new LatencyTimer(100, 1000); |
| 264 | } |
Michael Chan | 53071d6 | 2009-05-13 17:29:48 -0700 | [diff] [blame] | 265 | } |
| 266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | // Initialize the statics when this class is first instantiated. This is |
| 268 | // done here instead of in the static block because Zygote does not |
| 269 | // allow the spawning of threads. |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 270 | getWindowSession(context.getMainLooper()); |
| 271 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | mThread = Thread.currentThread(); |
| 273 | mLocation = new WindowLeaked(null); |
| 274 | mLocation.fillInStackTrace(); |
| 275 | mWidth = -1; |
| 276 | mHeight = -1; |
| 277 | mDirty = new Rect(); |
| 278 | mTempRect = new Rect(); |
| 279 | mVisRect = new Rect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | mWinFrame = new Rect(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 281 | mWindow = new W(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | mInputMethodCallback = new InputMethodCallback(this); |
| 283 | mViewVisibility = View.GONE; |
| 284 | mTransparentRegion = new Region(); |
| 285 | mPreviousTransparentRegion = new Region(); |
| 286 | mFirst = true; // true for the first time the view is added |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | mAdded = false; |
| 288 | mAttachInfo = new View.AttachInfo(sWindowSession, mWindow, this, this); |
| 289 | mViewConfiguration = ViewConfiguration.get(context); |
Dianne Hackborn | 11ea334 | 2009-07-22 21:48:55 -0700 | [diff] [blame] | 290 | mDensity = context.getResources().getDisplayMetrics().densityDpi; |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 291 | mFallbackEventHandler = PolicyManager.makeNewFallbackEventHandler(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Dianne Hackborn | 2a9094d | 2010-02-03 19:20:09 -0800 | [diff] [blame] | 294 | public static void addFirstDrawHandler(Runnable callback) { |
| 295 | synchronized (sFirstDrawHandlers) { |
| 296 | if (!sFirstDrawComplete) { |
| 297 | sFirstDrawHandlers.add(callback); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 302 | public static void addConfigCallback(ComponentCallbacks callback) { |
| 303 | synchronized (sConfigCallbacks) { |
| 304 | sConfigCallbacks.add(callback); |
| 305 | } |
| 306 | } |
| 307 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | // FIXME for perf testing only |
| 309 | private boolean mProfile = false; |
| 310 | |
| 311 | /** |
| 312 | * Call this to profile the next traversal call. |
| 313 | * FIXME for perf testing only. Remove eventually |
| 314 | */ |
| 315 | public void profile() { |
| 316 | mProfile = true; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Indicates whether we are in touch mode. Calling this method triggers an IPC |
| 321 | * call and should be avoided whenever possible. |
| 322 | * |
| 323 | * @return True, if the device is in touch mode, false otherwise. |
| 324 | * |
| 325 | * @hide |
| 326 | */ |
| 327 | static boolean isInTouchMode() { |
| 328 | if (mInitialized) { |
| 329 | try { |
| 330 | return sWindowSession.getInTouchMode(); |
| 331 | } catch (RemoteException e) { |
| 332 | } |
| 333 | } |
| 334 | return false; |
| 335 | } |
| 336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | /** |
| 338 | * We have one child |
| 339 | */ |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 340 | public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | synchronized (this) { |
| 342 | if (mView == null) { |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 343 | mView = view; |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 344 | mFallbackEventHandler.setView(view); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 345 | mWindowAttributes.copyFrom(attrs); |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 346 | attrs = mWindowAttributes; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 347 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 348 | if (view instanceof RootViewSurfaceTaker) { |
| 349 | mSurfaceHolderCallback = |
| 350 | ((RootViewSurfaceTaker)view).willYouTakeTheSurface(); |
| 351 | if (mSurfaceHolderCallback != null) { |
| 352 | mSurfaceHolder = new TakenSurfaceHolder(); |
Dianne Hackborn | 289b9b6 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 353 | mSurfaceHolder.setFormat(PixelFormat.UNKNOWN); |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 354 | } |
| 355 | } |
Romain Guy | 1aec9a2 | 2011-01-05 09:37:12 -0800 | [diff] [blame^] | 356 | |
| 357 | // If the application owns the surface, don't enable hardware acceleration |
| 358 | if (mSurfaceHolder == null) { |
| 359 | enableHardwareAcceleration(attrs); |
| 360 | } |
| 361 | |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 362 | Resources resources = mView.getContext().getResources(); |
| 363 | CompatibilityInfo compatibilityInfo = resources.getCompatibilityInfo(); |
Mitsuru Oshima | 589cebe | 2009-07-22 20:38:58 -0700 | [diff] [blame] | 364 | mTranslator = compatibilityInfo.getTranslator(); |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 365 | |
| 366 | if (mTranslator != null || !compatibilityInfo.supportsScreen()) { |
Mitsuru Oshima | 240f8a7 | 2009-07-22 20:39:14 -0700 | [diff] [blame] | 367 | mSurface.setCompatibleDisplayMetrics(resources.getDisplayMetrics(), |
| 368 | mTranslator); |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 371 | boolean restore = false; |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 372 | if (mTranslator != null) { |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 373 | restore = true; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 374 | attrs.backup(); |
| 375 | mTranslator.translateWindowLayout(attrs); |
Mitsuru Oshima | 3d91492 | 2009-05-13 22:29:15 -0700 | [diff] [blame] | 376 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 377 | if (DEBUG_LAYOUT) Log.d(TAG, "WindowLayout in setView:" + attrs); |
| 378 | |
Mitsuru Oshima | 1ecf5d2 | 2009-07-06 17:20:38 -0700 | [diff] [blame] | 379 | if (!compatibilityInfo.supportsScreen()) { |
| 380 | attrs.flags |= WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW; |
| 381 | } |
| 382 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | mSoftInputMode = attrs.softInputMode; |
| 384 | mWindowAttributesChanged = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | mAttachInfo.mRootView = view; |
Romain Guy | 35b38ce | 2009-10-07 13:38:55 -0700 | [diff] [blame] | 386 | mAttachInfo.mScalingRequired = mTranslator != null; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 387 | mAttachInfo.mApplicationScale = |
| 388 | mTranslator == null ? 1.0f : mTranslator.applicationScale; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | if (panelParentView != null) { |
| 390 | mAttachInfo.mPanelParentWindowToken |
| 391 | = panelParentView.getApplicationWindowToken(); |
| 392 | } |
| 393 | mAdded = true; |
| 394 | int res; /* = WindowManagerImpl.ADD_OKAY; */ |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 395 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | // Schedule the first layout -before- adding to the window |
| 397 | // manager, to make sure we do the relayout before receiving |
| 398 | // any other events from the system. |
| 399 | requestLayout(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 400 | mInputChannel = new InputChannel(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | try { |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 402 | res = sWindowSession.add(mWindow, mWindowAttributes, |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 403 | getHostVisibility(), mAttachInfo.mContentInsets, |
| 404 | mInputChannel); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } catch (RemoteException e) { |
| 406 | mAdded = false; |
| 407 | mView = null; |
| 408 | mAttachInfo.mRootView = null; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 409 | mInputChannel = null; |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 410 | mFallbackEventHandler.setView(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | unscheduleTraversals(); |
| 412 | throw new RuntimeException("Adding window failed", e); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 413 | } finally { |
| 414 | if (restore) { |
| 415 | attrs.restore(); |
| 416 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 418 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 419 | if (mTranslator != null) { |
| 420 | mTranslator.translateRectInScreenToAppWindow(mAttachInfo.mContentInsets); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 421 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | mPendingContentInsets.set(mAttachInfo.mContentInsets); |
| 423 | mPendingVisibleInsets.set(0, 0, 0, 0); |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 424 | if (DEBUG_LAYOUT) Log.v(TAG, "Added window " + mWindow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | if (res < WindowManagerImpl.ADD_OKAY) { |
| 426 | mView = null; |
| 427 | mAttachInfo.mRootView = null; |
| 428 | mAdded = false; |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 429 | mFallbackEventHandler.setView(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | unscheduleTraversals(); |
| 431 | switch (res) { |
| 432 | case WindowManagerImpl.ADD_BAD_APP_TOKEN: |
| 433 | case WindowManagerImpl.ADD_BAD_SUBWINDOW_TOKEN: |
| 434 | throw new WindowManagerImpl.BadTokenException( |
| 435 | "Unable to add window -- token " + attrs.token |
| 436 | + " is not valid; is your activity running?"); |
| 437 | case WindowManagerImpl.ADD_NOT_APP_TOKEN: |
| 438 | throw new WindowManagerImpl.BadTokenException( |
| 439 | "Unable to add window -- token " + attrs.token |
| 440 | + " is not for an application"); |
| 441 | case WindowManagerImpl.ADD_APP_EXITING: |
| 442 | throw new WindowManagerImpl.BadTokenException( |
| 443 | "Unable to add window -- app for token " + attrs.token |
| 444 | + " is exiting"); |
| 445 | case WindowManagerImpl.ADD_DUPLICATE_ADD: |
| 446 | throw new WindowManagerImpl.BadTokenException( |
| 447 | "Unable to add window -- window " + mWindow |
| 448 | + " has already been added"); |
| 449 | case WindowManagerImpl.ADD_STARTING_NOT_NEEDED: |
| 450 | // Silently ignore -- we would have just removed it |
| 451 | // right away, anyway. |
| 452 | return; |
| 453 | case WindowManagerImpl.ADD_MULTIPLE_SINGLETON: |
| 454 | throw new WindowManagerImpl.BadTokenException( |
| 455 | "Unable to add window " + mWindow + |
| 456 | " -- another window of this type already exists"); |
| 457 | case WindowManagerImpl.ADD_PERMISSION_DENIED: |
| 458 | throw new WindowManagerImpl.BadTokenException( |
| 459 | "Unable to add window " + mWindow + |
| 460 | " -- permission denied for this window type"); |
| 461 | } |
| 462 | throw new RuntimeException( |
| 463 | "Unable to add window -- unknown error code " + res); |
| 464 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 465 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 466 | if (view instanceof RootViewSurfaceTaker) { |
| 467 | mInputQueueCallback = |
| 468 | ((RootViewSurfaceTaker)view).willYouTakeTheInputQueue(); |
| 469 | } |
| 470 | if (mInputQueueCallback != null) { |
| 471 | mInputQueue = new InputQueue(mInputChannel); |
| 472 | mInputQueueCallback.onInputQueueCreated(mInputQueue); |
| 473 | } else { |
| 474 | InputQueue.registerInputChannel(mInputChannel, mInputHandler, |
| 475 | Looper.myQueue()); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 476 | } |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | view.assignParent(this); |
| 479 | mAddedTouchMode = (res&WindowManagerImpl.ADD_FLAG_IN_TOUCH_MODE) != 0; |
| 480 | mAppVisible = (res&WindowManagerImpl.ADD_FLAG_APP_VISIBLE) != 0; |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
Romain Guy | 529b60a | 2010-08-03 18:05:47 -0700 | [diff] [blame] | 485 | private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) { |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 486 | mAttachInfo.mHardwareAccelerated = false; |
| 487 | mAttachInfo.mHardwareAccelerationRequested = false; |
| 488 | |
| 489 | // Try to enable hardware acceleration if requested |
| 490 | if (attrs != null && |
| 491 | (attrs.flags & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) { |
| 492 | // Only enable hardware acceleration if we are not in the system process |
| 493 | // The window manager creates ViewRoots to display animated preview windows |
| 494 | // of launching apps and we don't want those to be hardware accelerated |
| 495 | if (!HardwareRenderer.sRendererDisabled) { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 496 | final boolean translucent = attrs.format != PixelFormat.OPAQUE; |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 497 | if (mAttachInfo.mHardwareRenderer != null) { |
| 498 | mAttachInfo.mHardwareRenderer.destroy(true); |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 499 | } |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 500 | mAttachInfo.mHardwareRenderer = HardwareRenderer.createGlRenderer(2, translucent); |
Dianne Hackborn | 7eec10e | 2010-11-12 18:03:47 -0800 | [diff] [blame] | 501 | mAttachInfo.mHardwareAccelerated = mAttachInfo.mHardwareAccelerationRequested |
| 502 | = mAttachInfo.mHardwareRenderer != null; |
| 503 | } else if (HardwareRenderer.isAvailable()) { |
| 504 | mAttachInfo.mHardwareAccelerationRequested = true; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | public View getView() { |
| 510 | return mView; |
| 511 | } |
| 512 | |
| 513 | final WindowLeaked getLocation() { |
| 514 | return mLocation; |
| 515 | } |
| 516 | |
| 517 | void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) { |
| 518 | synchronized (this) { |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 519 | int oldSoftInputMode = mWindowAttributes.softInputMode; |
Mitsuru Oshima | 5a2b91d | 2009-07-16 16:30:02 -0700 | [diff] [blame] | 520 | // preserve compatible window flag if exists. |
| 521 | int compatibleWindowFlag = |
| 522 | mWindowAttributes.flags & WindowManager.LayoutParams.FLAG_COMPATIBLE_WINDOW; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | mWindowAttributes.copyFrom(attrs); |
Mitsuru Oshima | 5a2b91d | 2009-07-16 16:30:02 -0700 | [diff] [blame] | 524 | mWindowAttributes.flags |= compatibleWindowFlag; |
| 525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | if (newView) { |
| 527 | mSoftInputMode = attrs.softInputMode; |
| 528 | requestLayout(); |
| 529 | } |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 530 | // Don't lose the mode we last auto-computed. |
| 531 | if ((attrs.softInputMode&WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 532 | == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) { |
| 533 | mWindowAttributes.softInputMode = (mWindowAttributes.softInputMode |
| 534 | & ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) |
| 535 | | (oldSoftInputMode |
| 536 | & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST); |
| 537 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 538 | mWindowAttributesChanged = true; |
| 539 | scheduleTraversals(); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void handleAppVisibility(boolean visible) { |
| 544 | if (mAppVisible != visible) { |
| 545 | mAppVisible = visible; |
| 546 | scheduleTraversals(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void handleGetNewSurface() { |
| 551 | mNewSurfaceNeeded = true; |
| 552 | mFullRedrawNeeded = true; |
| 553 | scheduleTraversals(); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * {@inheritDoc} |
| 558 | */ |
| 559 | public void requestLayout() { |
| 560 | checkThread(); |
| 561 | mLayoutRequested = true; |
| 562 | scheduleTraversals(); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * {@inheritDoc} |
| 567 | */ |
| 568 | public boolean isLayoutRequested() { |
| 569 | return mLayoutRequested; |
| 570 | } |
| 571 | |
| 572 | public void invalidateChild(View child, Rect dirty) { |
| 573 | checkThread(); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 574 | if (DEBUG_DRAW) Log.v(TAG, "Invalidate child: " + dirty); |
Chet Haase | 70d4ba1 | 2010-10-06 09:46:45 -0700 | [diff] [blame] | 575 | if (dirty == null) { |
| 576 | // Fast invalidation for GL-enabled applications; GL must redraw everything |
| 577 | invalidate(); |
| 578 | return; |
| 579 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 580 | if (mCurScrollY != 0 || mTranslator != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | mTempRect.set(dirty); |
Romain Guy | 1e09597 | 2009-07-07 11:22:45 -0700 | [diff] [blame] | 582 | dirty = mTempRect; |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 583 | if (mCurScrollY != 0) { |
Romain Guy | 1e09597 | 2009-07-07 11:22:45 -0700 | [diff] [blame] | 584 | dirty.offset(0, -mCurScrollY); |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 585 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 586 | if (mTranslator != null) { |
Romain Guy | 1e09597 | 2009-07-07 11:22:45 -0700 | [diff] [blame] | 587 | mTranslator.translateRectInAppWindowToScreen(dirty); |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 588 | } |
Romain Guy | 1e09597 | 2009-07-07 11:22:45 -0700 | [diff] [blame] | 589 | if (mAttachInfo.mScalingRequired) { |
| 590 | dirty.inset(-1, -1); |
| 591 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | } |
Romain Guy | 7d69594 | 2010-12-01 17:22:29 -0800 | [diff] [blame] | 593 | if (!mDirty.isEmpty()) { |
| 594 | mAttachInfo.mIgnoreDirtyState = true; |
| 595 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 596 | mDirty.union(dirty); |
| 597 | if (!mWillDrawSoon) { |
| 598 | scheduleTraversals(); |
| 599 | } |
| 600 | } |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 601 | |
| 602 | void invalidate() { |
| 603 | mDirty.set(0, 0, mWidth, mHeight); |
| 604 | scheduleTraversals(); |
| 605 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 606 | |
| 607 | public ViewParent getParent() { |
| 608 | return null; |
| 609 | } |
| 610 | |
| 611 | public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) { |
| 612 | invalidateChild(null, dirty); |
| 613 | return null; |
| 614 | } |
| 615 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 616 | public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | if (child != mView) { |
| 618 | throw new RuntimeException("child is not mine, honest!"); |
| 619 | } |
| 620 | // Note: don't apply scroll offset, because we want to know its |
| 621 | // visibility in the virtual canvas being given to the view hierarchy. |
| 622 | return r.intersect(0, 0, mWidth, mHeight); |
| 623 | } |
| 624 | |
| 625 | public void bringChildToFront(View child) { |
| 626 | } |
| 627 | |
| 628 | public void scheduleTraversals() { |
| 629 | if (!mTraversalScheduled) { |
| 630 | mTraversalScheduled = true; |
| 631 | sendEmptyMessage(DO_TRAVERSAL); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | public void unscheduleTraversals() { |
| 636 | if (mTraversalScheduled) { |
| 637 | mTraversalScheduled = false; |
| 638 | removeMessages(DO_TRAVERSAL); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | int getHostVisibility() { |
| 643 | return mAppVisible ? mView.getVisibility() : View.GONE; |
| 644 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 645 | |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 646 | void disposeResizeBitmap() { |
| 647 | if (mResizeBitmap != null) { |
| 648 | mResizeBitmap.recycle(); |
| 649 | mResizeBitmap = null; |
| 650 | } |
| 651 | } |
| 652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | private void performTraversals() { |
| 654 | // cache mView since it is used so much below... |
| 655 | final View host = mView; |
| 656 | |
| 657 | if (DBG) { |
| 658 | System.out.println("======================================"); |
| 659 | System.out.println("performTraversals"); |
| 660 | host.debug(); |
| 661 | } |
| 662 | |
| 663 | if (host == null || !mAdded) |
| 664 | return; |
| 665 | |
| 666 | mTraversalScheduled = false; |
| 667 | mWillDrawSoon = true; |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 668 | boolean windowSizeMayChange = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 669 | boolean fullRedrawNeeded = mFullRedrawNeeded; |
| 670 | boolean newSurface = false; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 671 | boolean surfaceChanged = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | WindowManager.LayoutParams lp = mWindowAttributes; |
| 673 | |
| 674 | int desiredWindowWidth; |
| 675 | int desiredWindowHeight; |
| 676 | int childWidthMeasureSpec; |
| 677 | int childHeightMeasureSpec; |
| 678 | |
| 679 | final View.AttachInfo attachInfo = mAttachInfo; |
| 680 | |
| 681 | final int viewVisibility = getHostVisibility(); |
| 682 | boolean viewVisibilityChanged = mViewVisibility != viewVisibility |
| 683 | || mNewSurfaceNeeded; |
| 684 | |
| 685 | WindowManager.LayoutParams params = null; |
| 686 | if (mWindowAttributesChanged) { |
| 687 | mWindowAttributesChanged = false; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 688 | surfaceChanged = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 689 | params = lp; |
| 690 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 691 | Rect frame = mWinFrame; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | if (mFirst) { |
| 693 | fullRedrawNeeded = true; |
| 694 | mLayoutRequested = true; |
| 695 | |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 696 | DisplayMetrics packageMetrics = |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 697 | mView.getContext().getResources().getDisplayMetrics(); |
| 698 | desiredWindowWidth = packageMetrics.widthPixels; |
| 699 | desiredWindowHeight = packageMetrics.heightPixels; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 700 | |
| 701 | // For the very first time, tell the view hierarchy that it |
| 702 | // is attached to the window. Note that at this point the surface |
| 703 | // object is not initialized to its backing store, but soon it |
| 704 | // will be (assuming the window is visible). |
| 705 | attachInfo.mSurface = mSurface; |
Adam Powell | 26153a3 | 2010-11-08 15:22:27 -0800 | [diff] [blame] | 706 | attachInfo.mUse32BitDrawingCache = PixelFormat.formatHasAlpha(lp.format) || |
| 707 | lp.format == PixelFormat.RGBX_8888; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | attachInfo.mHasWindowFocus = false; |
| 709 | attachInfo.mWindowVisibility = viewVisibility; |
| 710 | attachInfo.mRecomputeGlobalAttributes = false; |
| 711 | attachInfo.mKeepScreenOn = false; |
| 712 | viewVisibilityChanged = false; |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 713 | mLastConfiguration.setTo(host.getResources().getConfiguration()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | host.dispatchAttachedToWindow(attachInfo, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | //Log.i(TAG, "Screen on initialized: " + attachInfo.mKeepScreenOn); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | } else { |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 718 | desiredWindowWidth = frame.width(); |
| 719 | desiredWindowHeight = frame.height(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | if (desiredWindowWidth != mWidth || desiredWindowHeight != mHeight) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 721 | if (DEBUG_ORIENTATION) Log.v(TAG, |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 722 | "View " + host + " resized to: " + frame); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | fullRedrawNeeded = true; |
| 724 | mLayoutRequested = true; |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 725 | windowSizeMayChange = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
| 729 | if (viewVisibilityChanged) { |
| 730 | attachInfo.mWindowVisibility = viewVisibility; |
| 731 | host.dispatchWindowVisibilityChanged(viewVisibility); |
| 732 | if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 733 | if (mAttachInfo.mHardwareRenderer != null) { |
| 734 | mAttachInfo.mHardwareRenderer.destroy(false); |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 735 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | } |
| 737 | if (viewVisibility == View.GONE) { |
| 738 | // After making a window gone, we will count it as being |
| 739 | // shown for the first time the next time it gets focus. |
| 740 | mHasHadWindowFocus = false; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | boolean insetsChanged = false; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 745 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | if (mLayoutRequested) { |
Romain Guy | 15df670 | 2009-08-17 20:17:30 -0700 | [diff] [blame] | 747 | // Execute enqueued actions on every layout in case a view that was detached |
| 748 | // enqueued an action after being detached |
| 749 | getRunQueue().executeActions(attachInfo.mHandler); |
| 750 | |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 751 | final Resources res = mView.getContext().getResources(); |
| 752 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | if (mFirst) { |
| 754 | host.fitSystemWindows(mAttachInfo.mContentInsets); |
| 755 | // make sure touch mode code executes by setting cached value |
| 756 | // to opposite of the added touch mode. |
| 757 | mAttachInfo.mInTouchMode = !mAddedTouchMode; |
Romain Guy | 2d4cff6 | 2010-04-09 15:39:00 -0700 | [diff] [blame] | 758 | ensureTouchModeLocally(mAddedTouchMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 759 | } else { |
| 760 | if (!mAttachInfo.mContentInsets.equals(mPendingContentInsets)) { |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 761 | if (mWidth > 0 && mHeight > 0 && |
| 762 | mSurface != null && mSurface.isValid() && |
| 763 | mAttachInfo.mHardwareRenderer != null && |
| 764 | mAttachInfo.mHardwareRenderer.isEnabled() && |
| 765 | lp != null && !PixelFormat.formatHasAlpha(lp.format)) { |
| 766 | |
| 767 | disposeResizeBitmap(); |
| 768 | |
| 769 | boolean completed = false; |
| 770 | try { |
| 771 | mResizeBitmap = Bitmap.createBitmap(mWidth, mHeight, |
| 772 | Bitmap.Config.ARGB_8888); |
| 773 | mResizeBitmap.setHasAlpha(false); |
| 774 | Canvas canvas = new Canvas(mResizeBitmap); |
| 775 | int yoff; |
| 776 | final boolean scrolling = mScroller != null |
| 777 | && mScroller.computeScrollOffset(); |
| 778 | if (scrolling) { |
| 779 | yoff = mScroller.getCurrY(); |
| 780 | mScroller.abortAnimation(); |
| 781 | } else { |
| 782 | yoff = mScrollY; |
| 783 | } |
| 784 | canvas.translate(0, -yoff); |
| 785 | if (mTranslator != null) { |
| 786 | mTranslator.translateCanvas(canvas); |
| 787 | } |
| 788 | canvas.setScreenDensity(mAttachInfo.mScalingRequired |
| 789 | ? DisplayMetrics.DENSITY_DEVICE : 0); |
| 790 | mView.draw(canvas); |
| 791 | mResizeBitmapStartTime = SystemClock.uptimeMillis(); |
| 792 | mResizeBitmapDuration = mView.getResources().getInteger( |
| 793 | com.android.internal.R.integer.config_mediumAnimTime); |
| 794 | completed = true; |
| 795 | } catch (OutOfMemoryError e) { |
| 796 | Log.w(TAG, "Not enough memory for content change anim buffer", e); |
| 797 | } finally { |
| 798 | if (!completed) { |
| 799 | mResizeBitmap = null; |
| 800 | } |
| 801 | } |
| 802 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | mAttachInfo.mContentInsets.set(mPendingContentInsets); |
| 804 | host.fitSystemWindows(mAttachInfo.mContentInsets); |
| 805 | insetsChanged = true; |
| 806 | if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: " |
| 807 | + mAttachInfo.mContentInsets); |
| 808 | } |
| 809 | if (!mAttachInfo.mVisibleInsets.equals(mPendingVisibleInsets)) { |
| 810 | mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets); |
| 811 | if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: " |
| 812 | + mAttachInfo.mVisibleInsets); |
| 813 | } |
| 814 | if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT |
| 815 | || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) { |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 816 | windowSizeMayChange = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 817 | |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 818 | DisplayMetrics packageMetrics = res.getDisplayMetrics(); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 819 | desiredWindowWidth = packageMetrics.widthPixels; |
| 820 | desiredWindowHeight = packageMetrics.heightPixels; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | } |
| 822 | } |
| 823 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | // Ask host how big it wants to be |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 825 | if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v(TAG, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | "Measuring " + host + " in display " + desiredWindowWidth |
| 827 | + "x" + desiredWindowHeight + "..."); |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 828 | |
| 829 | boolean goodMeasure = false; |
| 830 | if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT |
| 831 | || lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) { |
| 832 | // On large screens, we don't want to allow dialogs to just |
| 833 | // stretch to fill the entire width of the screen to display |
| 834 | // one line of text. First try doing the layout at a smaller |
| 835 | // size to see if it will fit. |
| 836 | final DisplayMetrics packageMetrics = res.getDisplayMetrics(); |
| 837 | res.getValue(com.android.internal.R.dimen.config_prefDialogWidth, mTmpValue, true); |
| 838 | int baseSize = 0; |
| 839 | if (mTmpValue.type == TypedValue.TYPE_DIMENSION) { |
| 840 | baseSize = (int)mTmpValue.getDimension(packageMetrics); |
| 841 | } |
| 842 | if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": baseSize=" + baseSize); |
Dianne Hackborn | 7d3a5bc | 2010-11-29 22:52:12 -0800 | [diff] [blame] | 843 | if (baseSize != 0 && desiredWindowWidth > baseSize) { |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 844 | childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width); |
| 845 | childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height); |
| 846 | host.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 847 | if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": measured (" |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 848 | + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")"); |
| 849 | if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) { |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 850 | goodMeasure = true; |
| 851 | } else { |
| 852 | // Didn't fit in that size... try expanding a bit. |
| 853 | baseSize = (baseSize+desiredWindowWidth)/2; |
| 854 | if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": next baseSize=" |
| 855 | + baseSize); |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 856 | childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width); |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 857 | host.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 858 | if (DEBUG_DIALOG) Log.v(TAG, "Window " + mView + ": measured (" |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 859 | + host.getMeasuredWidth() + "," + host.getMeasuredHeight() + ")"); |
| 860 | if ((host.getMeasuredWidthAndState()&View.MEASURED_STATE_TOO_SMALL) == 0) { |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 861 | if (DEBUG_DIALOG) Log.v(TAG, "Good!"); |
| 862 | goodMeasure = true; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | if (!goodMeasure) { |
| 869 | childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width); |
| 870 | childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height); |
| 871 | host.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Adam Powell | aa0b92c | 2010-12-13 22:38:53 -0800 | [diff] [blame] | 872 | if (mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) { |
| 873 | windowSizeMayChange = true; |
| 874 | } |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 875 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | |
| 877 | if (DBG) { |
| 878 | System.out.println("======================================"); |
| 879 | System.out.println("performTraversals -- after measure"); |
| 880 | host.debug(); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | if (attachInfo.mRecomputeGlobalAttributes) { |
| 885 | //Log.i(TAG, "Computing screen on!"); |
| 886 | attachInfo.mRecomputeGlobalAttributes = false; |
| 887 | boolean oldVal = attachInfo.mKeepScreenOn; |
| 888 | attachInfo.mKeepScreenOn = false; |
| 889 | host.dispatchCollectViewAttributes(0); |
| 890 | if (attachInfo.mKeepScreenOn != oldVal) { |
| 891 | params = lp; |
| 892 | //Log.i(TAG, "Keep screen on changed: " + attachInfo.mKeepScreenOn); |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | if (mFirst || attachInfo.mViewVisibilityChanged) { |
| 897 | attachInfo.mViewVisibilityChanged = false; |
| 898 | int resizeMode = mSoftInputMode & |
| 899 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST; |
| 900 | // If we are in auto resize mode, then we need to determine |
| 901 | // what mode to use now. |
| 902 | if (resizeMode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED) { |
| 903 | final int N = attachInfo.mScrollContainers.size(); |
| 904 | for (int i=0; i<N; i++) { |
| 905 | if (attachInfo.mScrollContainers.get(i).isShown()) { |
| 906 | resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
| 907 | } |
| 908 | } |
| 909 | if (resizeMode == 0) { |
| 910 | resizeMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN; |
| 911 | } |
| 912 | if ((lp.softInputMode & |
| 913 | WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) != resizeMode) { |
| 914 | lp.softInputMode = (lp.softInputMode & |
| 915 | ~WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST) | |
| 916 | resizeMode; |
| 917 | params = lp; |
| 918 | } |
| 919 | } |
| 920 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 921 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | if (params != null && (host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) { |
| 923 | if (!PixelFormat.formatHasAlpha(params.format)) { |
| 924 | params.format = PixelFormat.TRANSLUCENT; |
| 925 | } |
| 926 | } |
| 927 | |
Dianne Hackborn | 711e62a | 2010-11-29 16:38:22 -0800 | [diff] [blame] | 928 | boolean windowShouldResize = mLayoutRequested && windowSizeMayChange |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 929 | && ((mWidth != host.getMeasuredWidth() || mHeight != host.getMeasuredHeight()) |
Romain Guy | 2e4f426 | 2010-04-06 11:07:52 -0700 | [diff] [blame] | 930 | || (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT && |
| 931 | frame.width() < desiredWindowWidth && frame.width() != mWidth) |
| 932 | || (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT && |
| 933 | frame.height() < desiredWindowHeight && frame.height() != mHeight)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | |
| 935 | final boolean computesInternalInsets = |
| 936 | attachInfo.mTreeObserver.hasComputeInternalInsetsListeners(); |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 937 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | boolean insetsPending = false; |
| 939 | int relayoutResult = 0; |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 940 | |
| 941 | if (mFirst || windowShouldResize || insetsChanged || |
| 942 | viewVisibilityChanged || params != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | |
| 944 | if (viewVisibility == View.VISIBLE) { |
| 945 | // If this window is giving internal insets to the window |
| 946 | // manager, and it is being added or changing its visibility, |
| 947 | // then we want to first give the window manager "fake" |
| 948 | // insets to cause it to effectively ignore the content of |
| 949 | // the window during layout. This avoids it briefly causing |
| 950 | // other windows to resize/move based on the raw frame of the |
| 951 | // window, waiting until we can finish laying out this window |
| 952 | // and get back to the window manager with the ultimately |
| 953 | // computed insets. |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 954 | insetsPending = computesInternalInsets && (mFirst || viewVisibilityChanged); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | } |
| 956 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 957 | if (mSurfaceHolder != null) { |
| 958 | mSurfaceHolder.mSurfaceLock.lock(); |
| 959 | mDrawingAllowed = true; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 960 | } |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 961 | |
Romain Guy | c361da8 | 2010-10-25 15:29:10 -0700 | [diff] [blame] | 962 | boolean hwInitialized = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | boolean contentInsetsChanged = false; |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 964 | boolean visibleInsetsChanged; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 965 | boolean hadSurface = mSurface.isValid(); |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 966 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 967 | try { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | int fl = 0; |
| 969 | if (params != null) { |
| 970 | fl = params.flags; |
| 971 | if (attachInfo.mKeepScreenOn) { |
| 972 | params.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; |
| 973 | } |
| 974 | } |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 975 | if (DEBUG_LAYOUT) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 976 | Log.i(TAG, "host=w:" + host.getMeasuredWidth() + ", h:" + |
| 977 | host.getMeasuredHeight() + ", params=" + params); |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 978 | } |
| 979 | relayoutResult = relayoutWindow(params, viewVisibility, insetsPending); |
| 980 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 981 | if (params != null) { |
| 982 | params.flags = fl; |
| 983 | } |
| 984 | |
| 985 | if (DEBUG_LAYOUT) Log.v(TAG, "relayout: frame=" + frame.toShortString() |
| 986 | + " content=" + mPendingContentInsets.toShortString() |
| 987 | + " visible=" + mPendingVisibleInsets.toShortString() |
| 988 | + " surface=" + mSurface); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 989 | |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 990 | if (mPendingConfiguration.seq != 0) { |
| 991 | if (DEBUG_CONFIGURATION) Log.v(TAG, "Visible with new config: " |
| 992 | + mPendingConfiguration); |
| 993 | updateConfiguration(mPendingConfiguration, !mFirst); |
| 994 | mPendingConfiguration.seq = 0; |
| 995 | } |
| 996 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | contentInsetsChanged = !mPendingContentInsets.equals( |
| 998 | mAttachInfo.mContentInsets); |
| 999 | visibleInsetsChanged = !mPendingVisibleInsets.equals( |
| 1000 | mAttachInfo.mVisibleInsets); |
| 1001 | if (contentInsetsChanged) { |
| 1002 | mAttachInfo.mContentInsets.set(mPendingContentInsets); |
| 1003 | host.fitSystemWindows(mAttachInfo.mContentInsets); |
| 1004 | if (DEBUG_LAYOUT) Log.v(TAG, "Content insets changing to: " |
| 1005 | + mAttachInfo.mContentInsets); |
| 1006 | } |
| 1007 | if (visibleInsetsChanged) { |
| 1008 | mAttachInfo.mVisibleInsets.set(mPendingVisibleInsets); |
| 1009 | if (DEBUG_LAYOUT) Log.v(TAG, "Visible insets changing to: " |
| 1010 | + mAttachInfo.mVisibleInsets); |
| 1011 | } |
| 1012 | |
| 1013 | if (!hadSurface) { |
| 1014 | if (mSurface.isValid()) { |
| 1015 | // If we are creating a new surface, then we need to |
| 1016 | // completely redraw it. Also, when we get to the |
| 1017 | // point of drawing it we will hold off and schedule |
| 1018 | // a new traversal instead. This is so we can tell the |
| 1019 | // window manager about all of the windows being displayed |
| 1020 | // before actually drawing them, so it can display then |
| 1021 | // all at once. |
| 1022 | newSurface = true; |
| 1023 | fullRedrawNeeded = true; |
Jack Palevich | 61a6e68 | 2009-10-09 17:37:50 -0700 | [diff] [blame] | 1024 | mPreviousTransparentRegion.setEmpty(); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1025 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1026 | if (mAttachInfo.mHardwareRenderer != null) { |
Romain Guy | c361da8 | 2010-10-25 15:29:10 -0700 | [diff] [blame] | 1027 | hwInitialized = mAttachInfo.mHardwareRenderer.initialize(mHolder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1028 | } |
| 1029 | } |
| 1030 | } else if (!mSurface.isValid()) { |
| 1031 | // If the surface has been removed, then reset the scroll |
| 1032 | // positions. |
| 1033 | mLastScrolledFocus = null; |
| 1034 | mScrollY = mCurScrollY = 0; |
| 1035 | if (mScroller != null) { |
| 1036 | mScroller.abortAnimation(); |
| 1037 | } |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1038 | disposeResizeBitmap(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | } |
| 1040 | } catch (RemoteException e) { |
| 1041 | } |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 1042 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | if (DEBUG_ORIENTATION) Log.v( |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1044 | TAG, "Relayout returned: frame=" + frame + ", surface=" + mSurface); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | |
| 1046 | attachInfo.mWindowLeft = frame.left; |
| 1047 | attachInfo.mWindowTop = frame.top; |
| 1048 | |
| 1049 | // !!FIXME!! This next section handles the case where we did not get the |
| 1050 | // window size we asked for. We should avoid this by getting a maximum size from |
| 1051 | // the window session beforehand. |
| 1052 | mWidth = frame.width(); |
| 1053 | mHeight = frame.height(); |
| 1054 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 1055 | if (mSurfaceHolder != null) { |
| 1056 | // The app owns the surface; tell it about what is going on. |
| 1057 | if (mSurface.isValid()) { |
| 1058 | // XXX .copyFrom() doesn't work! |
| 1059 | //mSurfaceHolder.mSurface.copyFrom(mSurface); |
| 1060 | mSurfaceHolder.mSurface = mSurface; |
| 1061 | } |
| 1062 | mSurfaceHolder.mSurfaceLock.unlock(); |
| 1063 | if (mSurface.isValid()) { |
| 1064 | if (!hadSurface) { |
| 1065 | mSurfaceHolder.ungetCallbacks(); |
| 1066 | |
| 1067 | mIsCreating = true; |
| 1068 | mSurfaceHolderCallback.surfaceCreated(mSurfaceHolder); |
| 1069 | SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); |
| 1070 | if (callbacks != null) { |
| 1071 | for (SurfaceHolder.Callback c : callbacks) { |
| 1072 | c.surfaceCreated(mSurfaceHolder); |
| 1073 | } |
| 1074 | } |
| 1075 | surfaceChanged = true; |
Romain Guy | c361da8 | 2010-10-25 15:29:10 -0700 | [diff] [blame] | 1076 | |
| 1077 | if (mAttachInfo.mHardwareRenderer != null) { |
| 1078 | // This will bail out early if already initialized |
| 1079 | mAttachInfo.mHardwareRenderer.initialize(mHolder); |
| 1080 | } |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 1081 | } |
| 1082 | if (surfaceChanged) { |
| 1083 | mSurfaceHolderCallback.surfaceChanged(mSurfaceHolder, |
| 1084 | lp.format, mWidth, mHeight); |
| 1085 | SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); |
| 1086 | if (callbacks != null) { |
| 1087 | for (SurfaceHolder.Callback c : callbacks) { |
| 1088 | c.surfaceChanged(mSurfaceHolder, lp.format, |
| 1089 | mWidth, mHeight); |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | mIsCreating = false; |
| 1094 | } else if (hadSurface) { |
| 1095 | mSurfaceHolder.ungetCallbacks(); |
| 1096 | SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); |
| 1097 | mSurfaceHolderCallback.surfaceDestroyed(mSurfaceHolder); |
| 1098 | if (callbacks != null) { |
| 1099 | for (SurfaceHolder.Callback c : callbacks) { |
| 1100 | c.surfaceDestroyed(mSurfaceHolder); |
| 1101 | } |
| 1102 | } |
| 1103 | mSurfaceHolder.mSurfaceLock.lock(); |
| 1104 | // Make surface invalid. |
| 1105 | //mSurfaceHolder.mSurface.copyFrom(mSurface); |
| 1106 | mSurfaceHolder.mSurface = new Surface(); |
| 1107 | mSurfaceHolder.mSurfaceLock.unlock(); |
| 1108 | } |
| 1109 | } |
Romain Guy | 53389bd | 2010-09-07 17:16:32 -0700 | [diff] [blame] | 1110 | |
Romain Guy | 6b5108b | 2011-01-04 16:11:10 -0800 | [diff] [blame] | 1111 | if (hwInitialized || ((windowShouldResize || params != null) && |
Romain Guy | dbf78bd | 2010-12-07 17:04:03 -0800 | [diff] [blame] | 1112 | mAttachInfo.mHardwareRenderer != null && |
| 1113 | mAttachInfo.mHardwareRenderer.isEnabled())) { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1114 | mAttachInfo.mHardwareRenderer.setup(mWidth, mHeight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | boolean focusChangedDueToTouchMode = ensureTouchModeLocally( |
Romain Guy | 2d4cff6 | 2010-04-09 15:39:00 -0700 | [diff] [blame] | 1118 | (relayoutResult&WindowManagerImpl.RELAYOUT_IN_TOUCH_MODE) != 0); |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1119 | if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth() |
| 1120 | || mHeight != host.getMeasuredHeight() || contentInsetsChanged) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1121 | childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width); |
| 1122 | childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height); |
| 1123 | |
| 1124 | if (DEBUG_LAYOUT) Log.v(TAG, "Ooops, something changed! mWidth=" |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1125 | + mWidth + " measuredWidth=" + host.getMeasuredWidth() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1126 | + " mHeight=" + mHeight |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1127 | + " measuredHeight" + host.getMeasuredHeight() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | + " coveredInsetsChanged=" + contentInsetsChanged); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1129 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | // Ask host how big it wants to be |
| 1131 | host.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 1132 | |
| 1133 | // Implementation of weights from WindowManager.LayoutParams |
| 1134 | // We just grow the dimensions as needed and re-measure if |
| 1135 | // needs be |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1136 | int width = host.getMeasuredWidth(); |
| 1137 | int height = host.getMeasuredHeight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1138 | boolean measureAgain = false; |
| 1139 | |
| 1140 | if (lp.horizontalWeight > 0.0f) { |
| 1141 | width += (int) ((mWidth - width) * lp.horizontalWeight); |
| 1142 | childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(width, |
| 1143 | MeasureSpec.EXACTLY); |
| 1144 | measureAgain = true; |
| 1145 | } |
| 1146 | if (lp.verticalWeight > 0.0f) { |
| 1147 | height += (int) ((mHeight - height) * lp.verticalWeight); |
| 1148 | childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, |
| 1149 | MeasureSpec.EXACTLY); |
| 1150 | measureAgain = true; |
| 1151 | } |
| 1152 | |
| 1153 | if (measureAgain) { |
| 1154 | if (DEBUG_LAYOUT) Log.v(TAG, |
| 1155 | "And hey let's measure once more: width=" + width |
| 1156 | + " height=" + height); |
| 1157 | host.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 1158 | } |
| 1159 | |
| 1160 | mLayoutRequested = true; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | final boolean didLayout = mLayoutRequested; |
| 1165 | boolean triggerGlobalLayoutListener = didLayout |
| 1166 | || attachInfo.mRecomputeGlobalAttributes; |
| 1167 | if (didLayout) { |
| 1168 | mLayoutRequested = false; |
| 1169 | mScrollMayChange = true; |
| 1170 | if (DEBUG_ORIENTATION || DEBUG_LAYOUT) Log.v( |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1171 | TAG, "Laying out " + host + " to (" + |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1172 | host.getMeasuredWidth() + ", " + host.getMeasuredHeight() + ")"); |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 1173 | long startTime = 0L; |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 1174 | if (ViewDebug.DEBUG_PROFILE_LAYOUT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | startTime = SystemClock.elapsedRealtime(); |
| 1176 | } |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1177 | host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 1179 | if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) { |
| 1180 | if (!host.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_LAYOUT)) { |
| 1181 | throw new IllegalStateException("The view hierarchy is an inconsistent state," |
| 1182 | + "please refer to the logs with the tag " |
| 1183 | + ViewDebug.CONSISTENCY_LOG_TAG + " for more infomation."); |
| 1184 | } |
| 1185 | } |
| 1186 | |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 1187 | if (ViewDebug.DEBUG_PROFILE_LAYOUT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | EventLog.writeEvent(60001, SystemClock.elapsedRealtime() - startTime); |
| 1189 | } |
| 1190 | |
| 1191 | // By this point all views have been sized and positionned |
| 1192 | // We can compute the transparent area |
| 1193 | |
| 1194 | if ((host.mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) != 0) { |
| 1195 | // start out transparent |
| 1196 | // TODO: AVOID THAT CALL BY CACHING THE RESULT? |
| 1197 | host.getLocationInWindow(mTmpLocation); |
| 1198 | mTransparentRegion.set(mTmpLocation[0], mTmpLocation[1], |
| 1199 | mTmpLocation[0] + host.mRight - host.mLeft, |
| 1200 | mTmpLocation[1] + host.mBottom - host.mTop); |
| 1201 | |
| 1202 | host.gatherTransparentRegion(mTransparentRegion); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 1203 | if (mTranslator != null) { |
| 1204 | mTranslator.translateRegionInWindowToScreen(mTransparentRegion); |
| 1205 | } |
| 1206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | if (!mTransparentRegion.equals(mPreviousTransparentRegion)) { |
| 1208 | mPreviousTransparentRegion.set(mTransparentRegion); |
| 1209 | // reconfigure window manager |
| 1210 | try { |
| 1211 | sWindowSession.setTransparentRegion(mWindow, mTransparentRegion); |
| 1212 | } catch (RemoteException e) { |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | if (DBG) { |
| 1218 | System.out.println("======================================"); |
| 1219 | System.out.println("performTraversals -- after setFrame"); |
| 1220 | host.debug(); |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | if (triggerGlobalLayoutListener) { |
| 1225 | attachInfo.mRecomputeGlobalAttributes = false; |
| 1226 | attachInfo.mTreeObserver.dispatchOnGlobalLayout(); |
| 1227 | } |
| 1228 | |
| 1229 | if (computesInternalInsets) { |
| 1230 | ViewTreeObserver.InternalInsetsInfo insets = attachInfo.mGivenInternalInsets; |
| 1231 | final Rect givenContent = attachInfo.mGivenInternalInsets.contentInsets; |
| 1232 | final Rect givenVisible = attachInfo.mGivenInternalInsets.visibleInsets; |
| 1233 | givenContent.left = givenContent.top = givenContent.right |
| 1234 | = givenContent.bottom = givenVisible.left = givenVisible.top |
| 1235 | = givenVisible.right = givenVisible.bottom = 0; |
| 1236 | attachInfo.mTreeObserver.dispatchOnComputeInternalInsets(insets); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 1237 | Rect contentInsets = insets.contentInsets; |
| 1238 | Rect visibleInsets = insets.visibleInsets; |
| 1239 | if (mTranslator != null) { |
| 1240 | contentInsets = mTranslator.getTranslatedContentInsets(contentInsets); |
| 1241 | visibleInsets = mTranslator.getTranslatedVisbileInsets(visibleInsets); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 1242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | if (insetsPending || !mLastGivenInsets.equals(insets)) { |
| 1244 | mLastGivenInsets.set(insets); |
| 1245 | try { |
| 1246 | sWindowSession.setInsets(mWindow, insets.mTouchableInsets, |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 1247 | contentInsets, visibleInsets); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | } catch (RemoteException e) { |
| 1249 | } |
| 1250 | } |
| 1251 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | if (mFirst) { |
| 1254 | // handle first focus request |
| 1255 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: mView.hasFocus()=" |
| 1256 | + mView.hasFocus()); |
| 1257 | if (mView != null) { |
| 1258 | if (!mView.hasFocus()) { |
| 1259 | mView.requestFocus(View.FOCUS_FORWARD); |
| 1260 | mFocusedView = mRealFocusedView = mView.findFocus(); |
| 1261 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: requested focused view=" |
| 1262 | + mFocusedView); |
| 1263 | } else { |
| 1264 | mRealFocusedView = mView.findFocus(); |
| 1265 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "First: existing focused view=" |
| 1266 | + mRealFocusedView); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | mFirst = false; |
| 1272 | mWillDrawSoon = false; |
| 1273 | mNewSurfaceNeeded = false; |
| 1274 | mViewVisibility = viewVisibility; |
| 1275 | |
| 1276 | if (mAttachInfo.mHasWindowFocus) { |
| 1277 | final boolean imTarget = WindowManager.LayoutParams |
| 1278 | .mayUseInputMethod(mWindowAttributes.flags); |
| 1279 | if (imTarget != mLastWasImTarget) { |
| 1280 | mLastWasImTarget = imTarget; |
| 1281 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1282 | if (imm != null && imTarget) { |
| 1283 | imm.startGettingWindowFocus(mView); |
| 1284 | imm.onWindowFocus(mView, mView.findFocus(), |
| 1285 | mWindowAttributes.softInputMode, |
| 1286 | !mHasHadWindowFocus, mWindowAttributes.flags); |
| 1287 | } |
| 1288 | } |
| 1289 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1290 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | boolean cancelDraw = attachInfo.mTreeObserver.dispatchOnPreDraw(); |
| 1292 | |
| 1293 | if (!cancelDraw && !newSurface) { |
| 1294 | mFullRedrawNeeded = false; |
| 1295 | draw(fullRedrawNeeded); |
| 1296 | |
| 1297 | if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0 |
| 1298 | || mReportNextDraw) { |
| 1299 | if (LOCAL_LOGV) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1300 | Log.v(TAG, "FINISHED DRAWING: " + mWindowAttributes.getTitle()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1301 | } |
| 1302 | mReportNextDraw = false; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1303 | if (mSurfaceHolder != null && mSurface.isValid()) { |
| 1304 | mSurfaceHolderCallback.surfaceRedrawNeeded(mSurfaceHolder); |
| 1305 | SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks(); |
| 1306 | if (callbacks != null) { |
| 1307 | for (SurfaceHolder.Callback c : callbacks) { |
| 1308 | if (c instanceof SurfaceHolder.Callback2) { |
| 1309 | ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded( |
| 1310 | mSurfaceHolder); |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | try { |
| 1316 | sWindowSession.finishDrawing(mWindow); |
| 1317 | } catch (RemoteException e) { |
| 1318 | } |
| 1319 | } |
| 1320 | } else { |
| 1321 | // We were supposed to report when we are done drawing. Since we canceled the |
| 1322 | // draw, remember it here. |
| 1323 | if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) { |
| 1324 | mReportNextDraw = true; |
| 1325 | } |
| 1326 | if (fullRedrawNeeded) { |
| 1327 | mFullRedrawNeeded = true; |
| 1328 | } |
| 1329 | // Try again |
| 1330 | scheduleTraversals(); |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | public void requestTransparentRegion(View child) { |
| 1335 | // the test below should not fail unless someone is messing with us |
| 1336 | checkThread(); |
| 1337 | if (mView == child) { |
| 1338 | mView.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS; |
| 1339 | // Need to make sure we re-evaluate the window attributes next |
| 1340 | // time around, to ensure the window has the correct format. |
| 1341 | mWindowAttributesChanged = true; |
Mathias Agopian | 1bd80ad | 2010-11-04 17:13:39 -0700 | [diff] [blame] | 1342 | requestLayout(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | /** |
| 1347 | * Figures out the measure spec for the root view in a window based on it's |
| 1348 | * layout params. |
| 1349 | * |
| 1350 | * @param windowSize |
| 1351 | * The available width or height of the window |
| 1352 | * |
| 1353 | * @param rootDimension |
| 1354 | * The layout params for one dimension (width or height) of the |
| 1355 | * window. |
| 1356 | * |
| 1357 | * @return The measure spec to use to measure the root view. |
| 1358 | */ |
| 1359 | private int getRootMeasureSpec(int windowSize, int rootDimension) { |
| 1360 | int measureSpec; |
| 1361 | switch (rootDimension) { |
| 1362 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 1363 | case ViewGroup.LayoutParams.MATCH_PARENT: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | // Window can't resize. Force root view to be windowSize. |
| 1365 | measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY); |
| 1366 | break; |
| 1367 | case ViewGroup.LayoutParams.WRAP_CONTENT: |
| 1368 | // Window can resize. Set max size for root view. |
| 1369 | measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST); |
| 1370 | break; |
| 1371 | default: |
| 1372 | // Window wants to be an exact size. Force root view to be that size. |
| 1373 | measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY); |
| 1374 | break; |
| 1375 | } |
| 1376 | return measureSpec; |
| 1377 | } |
| 1378 | |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1379 | int mHardwareYOffset; |
| 1380 | int mResizeAlpha; |
| 1381 | final Paint mResizePaint = new Paint(); |
| 1382 | |
| 1383 | public void onHardwarePreDraw(Canvas canvas) { |
| 1384 | canvas.translate(0, -mHardwareYOffset); |
| 1385 | } |
| 1386 | |
| 1387 | public void onHardwarePostDraw(Canvas canvas) { |
| 1388 | if (mResizeBitmap != null) { |
| 1389 | canvas.translate(0, mHardwareYOffset); |
| 1390 | mResizePaint.setAlpha(mResizeAlpha); |
| 1391 | canvas.drawBitmap(mResizeBitmap, 0, 0, mResizePaint); |
| 1392 | } |
| 1393 | } |
| 1394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | private void draw(boolean fullRedrawNeeded) { |
| 1396 | Surface surface = mSurface; |
| 1397 | if (surface == null || !surface.isValid()) { |
| 1398 | return; |
| 1399 | } |
| 1400 | |
Dianne Hackborn | 2a9094d | 2010-02-03 19:20:09 -0800 | [diff] [blame] | 1401 | if (!sFirstDrawComplete) { |
| 1402 | synchronized (sFirstDrawHandlers) { |
| 1403 | sFirstDrawComplete = true; |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 1404 | final int count = sFirstDrawHandlers.size(); |
| 1405 | for (int i = 0; i< count; i++) { |
Dianne Hackborn | 2a9094d | 2010-02-03 19:20:09 -0800 | [diff] [blame] | 1406 | post(sFirstDrawHandlers.get(i)); |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | scrollToRectOrFocus(null, false); |
| 1412 | |
| 1413 | if (mAttachInfo.mViewScrollChanged) { |
| 1414 | mAttachInfo.mViewScrollChanged = false; |
| 1415 | mAttachInfo.mTreeObserver.dispatchOnScrollChanged(); |
| 1416 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1417 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | int yoff; |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1419 | boolean animating = mScroller != null && mScroller.computeScrollOffset(); |
| 1420 | if (animating) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1421 | yoff = mScroller.getCurrY(); |
| 1422 | } else { |
| 1423 | yoff = mScrollY; |
| 1424 | } |
| 1425 | if (mCurScrollY != yoff) { |
| 1426 | mCurScrollY = yoff; |
| 1427 | fullRedrawNeeded = true; |
| 1428 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 1429 | float appScale = mAttachInfo.mApplicationScale; |
| 1430 | boolean scalingRequired = mAttachInfo.mScalingRequired; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1431 | |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1432 | int resizeAlpha = 0; |
| 1433 | if (mResizeBitmap != null) { |
| 1434 | long deltaTime = SystemClock.uptimeMillis() - mResizeBitmapStartTime; |
| 1435 | if (deltaTime < mResizeBitmapDuration) { |
| 1436 | float amt = deltaTime/(float)mResizeBitmapDuration; |
| 1437 | amt = mResizeInterpolator.getInterpolation(amt); |
| 1438 | animating = true; |
| 1439 | resizeAlpha = 255 - (int)(amt*255); |
| 1440 | } else { |
| 1441 | disposeResizeBitmap(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 | Rect dirty = mDirty; |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 1446 | if (mSurfaceHolder != null) { |
| 1447 | // The app owns the surface, we won't draw. |
| 1448 | dirty.setEmpty(); |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1449 | if (animating) { |
| 1450 | if (mScroller != null) { |
| 1451 | mScroller.abortAnimation(); |
| 1452 | } |
| 1453 | disposeResizeBitmap(); |
| 1454 | } |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 1455 | return; |
| 1456 | } |
Romain Guy | 58ef7fb | 2010-09-13 12:52:37 -0700 | [diff] [blame] | 1457 | |
| 1458 | if (fullRedrawNeeded) { |
| 1459 | mAttachInfo.mIgnoreDirtyState = true; |
| 1460 | dirty.union(0, 0, (int) (mWidth * appScale + 0.5f), (int) (mHeight * appScale + 0.5f)); |
| 1461 | } |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 1462 | |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1463 | if (mAttachInfo.mHardwareRenderer != null && mAttachInfo.mHardwareRenderer.isEnabled()) { |
Romain Guy | fd50726 | 2010-10-10 15:42:49 -0700 | [diff] [blame] | 1464 | if (!dirty.isEmpty() || mIsAnimating) { |
Romain Guy | 101e2ae | 2010-10-11 12:41:21 -0700 | [diff] [blame] | 1465 | mIsAnimating = false; |
Romain Guy | fd50726 | 2010-10-10 15:42:49 -0700 | [diff] [blame] | 1466 | dirty.setEmpty(); |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1467 | mHardwareYOffset = yoff; |
| 1468 | mResizeAlpha = resizeAlpha; |
| 1469 | mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | } |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 1471 | |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1472 | if (animating) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1473 | mFullRedrawNeeded = true; |
| 1474 | scheduleTraversals(); |
| 1475 | } |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 1476 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1477 | return; |
| 1478 | } |
| 1479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | if (DEBUG_ORIENTATION || DEBUG_DRAW) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1481 | Log.v(TAG, "Draw " + mView + "/" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | + mWindowAttributes.getTitle() |
| 1483 | + ": dirty={" + dirty.left + "," + dirty.top |
| 1484 | + "," + dirty.right + "," + dirty.bottom + "} surface=" |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 1485 | + surface + " surface.isValid()=" + surface.isValid() + ", appScale:" + |
| 1486 | appScale + ", width=" + mWidth + ", height=" + mHeight); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | } |
| 1488 | |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1489 | if (!dirty.isEmpty() || mIsAnimating) { |
| 1490 | Canvas canvas; |
| 1491 | try { |
| 1492 | int left = dirty.left; |
| 1493 | int top = dirty.top; |
| 1494 | int right = dirty.right; |
| 1495 | int bottom = dirty.bottom; |
| 1496 | canvas = surface.lockCanvas(dirty); |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 1497 | |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1498 | if (left != dirty.left || top != dirty.top || right != dirty.right || |
| 1499 | bottom != dirty.bottom) { |
| 1500 | mAttachInfo.mIgnoreDirtyState = true; |
| 1501 | } |
| 1502 | |
| 1503 | // TODO: Do this in native |
| 1504 | canvas.setDensity(mDensity); |
| 1505 | } catch (Surface.OutOfResourcesException e) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1506 | Log.e(TAG, "OutOfResourcesException locking surface", e); |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1507 | // TODO: we should ask the window manager to do something! |
| 1508 | // for now we just do nothing |
| 1509 | return; |
| 1510 | } catch (IllegalArgumentException e) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1511 | Log.e(TAG, "IllegalArgumentException locking surface", e); |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1512 | // TODO: we should ask the window manager to do something! |
| 1513 | // for now we just do nothing |
| 1514 | return; |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1517 | try { |
| 1518 | if (!dirty.isEmpty() || mIsAnimating) { |
| 1519 | long startTime = 0L; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1521 | if (DEBUG_ORIENTATION || DEBUG_DRAW) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1522 | Log.v(TAG, "Surface " + surface + " drawing to bitmap w=" |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1523 | + canvas.getWidth() + ", h=" + canvas.getHeight()); |
| 1524 | //canvas.drawARGB(255, 255, 0, 0); |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 1525 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1526 | |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 1527 | if (ViewDebug.DEBUG_PROFILE_DRAWING) { |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1528 | startTime = SystemClock.elapsedRealtime(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | } |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1530 | |
| 1531 | // If this bitmap's format includes an alpha channel, we |
| 1532 | // need to clear it before drawing so that the child will |
| 1533 | // properly re-composite its drawing on a transparent |
| 1534 | // background. This automatically respects the clip/dirty region |
| 1535 | // or |
| 1536 | // If we are applying an offset, we need to clear the area |
| 1537 | // where the offset doesn't appear to avoid having garbage |
| 1538 | // left in the blank areas. |
| 1539 | if (!canvas.isOpaque() || yoff != 0) { |
| 1540 | canvas.drawColor(0, PorterDuff.Mode.CLEAR); |
| 1541 | } |
| 1542 | |
| 1543 | dirty.setEmpty(); |
| 1544 | mIsAnimating = false; |
| 1545 | mAttachInfo.mDrawingTime = SystemClock.uptimeMillis(); |
| 1546 | mView.mPrivateFlags |= View.DRAWN; |
| 1547 | |
| 1548 | if (DEBUG_DRAW) { |
| 1549 | Context cxt = mView.getContext(); |
| 1550 | Log.i(TAG, "Drawing: package:" + cxt.getPackageName() + |
| 1551 | ", metrics=" + cxt.getResources().getDisplayMetrics() + |
| 1552 | ", compatibilityInfo=" + cxt.getResources().getCompatibilityInfo()); |
| 1553 | } |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1554 | try { |
| 1555 | canvas.translate(0, -yoff); |
| 1556 | if (mTranslator != null) { |
| 1557 | mTranslator.translateCanvas(canvas); |
| 1558 | } |
| 1559 | canvas.setScreenDensity(scalingRequired |
| 1560 | ? DisplayMetrics.DENSITY_DEVICE : 0); |
| 1561 | mView.draw(canvas); |
| 1562 | } finally { |
| 1563 | mAttachInfo.mIgnoreDirtyState = false; |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | if (Config.DEBUG && ViewDebug.consistencyCheckEnabled) { |
| 1567 | mView.dispatchConsistencyCheck(ViewDebug.CONSISTENCY_DRAWING); |
| 1568 | } |
| 1569 | |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 1570 | if (SHOW_FPS || ViewDebug.DEBUG_SHOW_FPS) { |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1571 | int now = (int)SystemClock.elapsedRealtime(); |
| 1572 | if (sDrawTime != 0) { |
| 1573 | nativeShowFPS(canvas, now - sDrawTime); |
| 1574 | } |
| 1575 | sDrawTime = now; |
| 1576 | } |
| 1577 | |
Romain Guy | 5429e1d | 2010-09-07 12:38:00 -0700 | [diff] [blame] | 1578 | if (ViewDebug.DEBUG_PROFILE_DRAWING) { |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1579 | EventLog.writeEvent(60000, SystemClock.elapsedRealtime() - startTime); |
| 1580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
Mathias Agopian | a62b09a | 2010-04-21 18:36:53 -0700 | [diff] [blame] | 1583 | } finally { |
| 1584 | surface.unlockCanvasAndPost(canvas); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1585 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | if (LOCAL_LOGV) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1589 | Log.v(TAG, "Surface " + surface + " unlockCanvasAndPost"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1590 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1591 | |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1592 | if (animating) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1593 | mFullRedrawNeeded = true; |
| 1594 | scheduleTraversals(); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | boolean scrollToRectOrFocus(Rect rectangle, boolean immediate) { |
| 1599 | final View.AttachInfo attachInfo = mAttachInfo; |
| 1600 | final Rect ci = attachInfo.mContentInsets; |
| 1601 | final Rect vi = attachInfo.mVisibleInsets; |
| 1602 | int scrollY = 0; |
| 1603 | boolean handled = false; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1604 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | if (vi.left > ci.left || vi.top > ci.top |
| 1606 | || vi.right > ci.right || vi.bottom > ci.bottom) { |
| 1607 | // We'll assume that we aren't going to change the scroll |
| 1608 | // offset, since we want to avoid that unless it is actually |
| 1609 | // going to make the focus visible... otherwise we scroll |
| 1610 | // all over the place. |
| 1611 | scrollY = mScrollY; |
| 1612 | // We can be called for two different situations: during a draw, |
| 1613 | // to update the scroll position if the focus has changed (in which |
| 1614 | // case 'rectangle' is null), or in response to a |
| 1615 | // requestChildRectangleOnScreen() call (in which case 'rectangle' |
| 1616 | // is non-null and we just want to scroll to whatever that |
| 1617 | // rectangle is). |
| 1618 | View focus = mRealFocusedView; |
Romain Guy | e8b1652 | 2009-07-14 13:06:42 -0700 | [diff] [blame] | 1619 | |
| 1620 | // When in touch mode, focus points to the previously focused view, |
| 1621 | // which may have been removed from the view hierarchy. The following |
Joe Onorato | b71193b | 2009-11-24 18:34:42 -0500 | [diff] [blame] | 1622 | // line checks whether the view is still in our hierarchy. |
| 1623 | if (focus == null || focus.mAttachInfo != mAttachInfo) { |
Romain Guy | e8b1652 | 2009-07-14 13:06:42 -0700 | [diff] [blame] | 1624 | mRealFocusedView = null; |
| 1625 | return false; |
| 1626 | } |
| 1627 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | if (focus != mLastScrolledFocus) { |
| 1629 | // If the focus has changed, then ignore any requests to scroll |
| 1630 | // to a rectangle; first we want to make sure the entire focus |
| 1631 | // view is visible. |
| 1632 | rectangle = null; |
| 1633 | } |
| 1634 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Eval scroll: focus=" + focus |
| 1635 | + " rectangle=" + rectangle + " ci=" + ci |
| 1636 | + " vi=" + vi); |
| 1637 | if (focus == mLastScrolledFocus && !mScrollMayChange |
| 1638 | && rectangle == null) { |
| 1639 | // Optimization: if the focus hasn't changed since last |
| 1640 | // time, and no layout has happened, then just leave things |
| 1641 | // as they are. |
| 1642 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Keeping scroll y=" |
| 1643 | + mScrollY + " vi=" + vi.toShortString()); |
| 1644 | } else if (focus != null) { |
| 1645 | // We need to determine if the currently focused view is |
| 1646 | // within the visible part of the window and, if not, apply |
| 1647 | // a pan so it can be seen. |
| 1648 | mLastScrolledFocus = focus; |
| 1649 | mScrollMayChange = false; |
| 1650 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Need to scroll?"); |
| 1651 | // Try to find the rectangle from the focus view. |
| 1652 | if (focus.getGlobalVisibleRect(mVisRect, null)) { |
| 1653 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Root w=" |
| 1654 | + mView.getWidth() + " h=" + mView.getHeight() |
| 1655 | + " ci=" + ci.toShortString() |
| 1656 | + " vi=" + vi.toShortString()); |
| 1657 | if (rectangle == null) { |
| 1658 | focus.getFocusedRect(mTempRect); |
| 1659 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Focus " + focus |
| 1660 | + ": focusRect=" + mTempRect.toShortString()); |
Dianne Hackborn | 1c6a894 | 2010-03-23 16:34:20 -0700 | [diff] [blame] | 1661 | if (mView instanceof ViewGroup) { |
| 1662 | ((ViewGroup) mView).offsetDescendantRectToMyCoords( |
| 1663 | focus, mTempRect); |
| 1664 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, |
| 1666 | "Focus in window: focusRect=" |
| 1667 | + mTempRect.toShortString() |
| 1668 | + " visRect=" + mVisRect.toShortString()); |
| 1669 | } else { |
| 1670 | mTempRect.set(rectangle); |
| 1671 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, |
| 1672 | "Request scroll to rect: " |
| 1673 | + mTempRect.toShortString() |
| 1674 | + " visRect=" + mVisRect.toShortString()); |
| 1675 | } |
| 1676 | if (mTempRect.intersect(mVisRect)) { |
| 1677 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, |
| 1678 | "Focus window visible rect: " |
| 1679 | + mTempRect.toShortString()); |
| 1680 | if (mTempRect.height() > |
| 1681 | (mView.getHeight()-vi.top-vi.bottom)) { |
| 1682 | // If the focus simply is not going to fit, then |
| 1683 | // best is probably just to leave things as-is. |
| 1684 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, |
| 1685 | "Too tall; leaving scrollY=" + scrollY); |
| 1686 | } else if ((mTempRect.top-scrollY) < vi.top) { |
| 1687 | scrollY -= vi.top - (mTempRect.top-scrollY); |
| 1688 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, |
| 1689 | "Top covered; scrollY=" + scrollY); |
| 1690 | } else if ((mTempRect.bottom-scrollY) |
| 1691 | > (mView.getHeight()-vi.bottom)) { |
| 1692 | scrollY += (mTempRect.bottom-scrollY) |
| 1693 | - (mView.getHeight()-vi.bottom); |
| 1694 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, |
| 1695 | "Bottom covered; scrollY=" + scrollY); |
| 1696 | } |
| 1697 | handled = true; |
| 1698 | } |
| 1699 | } |
| 1700 | } |
| 1701 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | if (scrollY != mScrollY) { |
| 1704 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Pan scroll changed: old=" |
| 1705 | + mScrollY + " , new=" + scrollY); |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 1706 | if (!immediate && mResizeBitmap == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1707 | if (mScroller == null) { |
| 1708 | mScroller = new Scroller(mView.getContext()); |
| 1709 | } |
| 1710 | mScroller.startScroll(0, mScrollY, 0, scrollY-mScrollY); |
| 1711 | } else if (mScroller != null) { |
| 1712 | mScroller.abortAnimation(); |
| 1713 | } |
| 1714 | mScrollY = scrollY; |
| 1715 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1716 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | return handled; |
| 1718 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | public void requestChildFocus(View child, View focused) { |
| 1721 | checkThread(); |
| 1722 | if (mFocusedView != focused) { |
| 1723 | mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(mFocusedView, focused); |
| 1724 | scheduleTraversals(); |
| 1725 | } |
| 1726 | mFocusedView = mRealFocusedView = focused; |
| 1727 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Request child focus: focus now " |
| 1728 | + mFocusedView); |
| 1729 | } |
| 1730 | |
| 1731 | public void clearChildFocus(View child) { |
| 1732 | checkThread(); |
| 1733 | |
| 1734 | View oldFocus = mFocusedView; |
| 1735 | |
| 1736 | if (DEBUG_INPUT_RESIZE) Log.v(TAG, "Clearing child focus"); |
| 1737 | mFocusedView = mRealFocusedView = null; |
| 1738 | if (mView != null && !mView.hasFocus()) { |
| 1739 | // If a view gets the focus, the listener will be invoked from requestChildFocus() |
| 1740 | if (!mView.requestFocus(View.FOCUS_FORWARD)) { |
| 1741 | mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null); |
| 1742 | } |
| 1743 | } else if (oldFocus != null) { |
| 1744 | mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, null); |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | |
| 1749 | public void focusableViewAvailable(View v) { |
| 1750 | checkThread(); |
| 1751 | |
| 1752 | if (mView != null && !mView.hasFocus()) { |
| 1753 | v.requestFocus(); |
| 1754 | } else { |
| 1755 | // the one case where will transfer focus away from the current one |
| 1756 | // is if the current view is a view group that prefers to give focus |
| 1757 | // to its children first AND the view is a descendant of it. |
| 1758 | mFocusedView = mView.findFocus(); |
| 1759 | boolean descendantsHaveDibsOnFocus = |
| 1760 | (mFocusedView instanceof ViewGroup) && |
| 1761 | (((ViewGroup) mFocusedView).getDescendantFocusability() == |
| 1762 | ViewGroup.FOCUS_AFTER_DESCENDANTS); |
| 1763 | if (descendantsHaveDibsOnFocus && isViewDescendantOf(v, mFocusedView)) { |
| 1764 | // If a view gets the focus, the listener will be invoked from requestChildFocus() |
| 1765 | v.requestFocus(); |
| 1766 | } |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | public void recomputeViewAttributes(View child) { |
| 1771 | checkThread(); |
| 1772 | if (mView == child) { |
| 1773 | mAttachInfo.mRecomputeGlobalAttributes = true; |
| 1774 | if (!mWillDrawSoon) { |
| 1775 | scheduleTraversals(); |
| 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | void dispatchDetachedFromWindow() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | if (mView != null) { |
| 1782 | mView.dispatchDetachedFromWindow(); |
| 1783 | } |
| 1784 | |
| 1785 | mView = null; |
| 1786 | mAttachInfo.mRootView = null; |
Mathias Agopian | 5583dc6 | 2009-07-09 16:28:11 -0700 | [diff] [blame] | 1787 | mAttachInfo.mSurface = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1788 | |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 1789 | destroyHardwareRenderer(); |
Romain Guy | 4caa4ed | 2010-08-25 14:46:24 -0700 | [diff] [blame] | 1790 | |
Dianne Hackborn | 0586a1b | 2009-09-06 21:08:27 -0700 | [diff] [blame] | 1791 | mSurface.release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1793 | if (mInputChannel != null) { |
| 1794 | if (mInputQueueCallback != null) { |
| 1795 | mInputQueueCallback.onInputQueueDestroyed(mInputQueue); |
| 1796 | mInputQueueCallback = null; |
| 1797 | } else { |
| 1798 | InputQueue.unregisterInputChannel(mInputChannel); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1802 | try { |
| 1803 | sWindowSession.remove(mWindow); |
| 1804 | } catch (RemoteException e) { |
| 1805 | } |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1806 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 1807 | // Dispose the input channel after removing the window so the Window Manager |
| 1808 | // doesn't interpret the input channel being closed as an abnormal termination. |
| 1809 | if (mInputChannel != null) { |
| 1810 | mInputChannel.dispose(); |
| 1811 | mInputChannel = null; |
Jeff Brown | 349703e | 2010-06-22 01:27:15 -0700 | [diff] [blame] | 1812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1813 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1814 | |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 1815 | void updateConfiguration(Configuration config, boolean force) { |
| 1816 | if (DEBUG_CONFIGURATION) Log.v(TAG, |
| 1817 | "Applying new config to window " |
| 1818 | + mWindowAttributes.getTitle() |
| 1819 | + ": " + config); |
| 1820 | synchronized (sConfigCallbacks) { |
| 1821 | for (int i=sConfigCallbacks.size()-1; i>=0; i--) { |
| 1822 | sConfigCallbacks.get(i).onConfigurationChanged(config); |
| 1823 | } |
| 1824 | } |
| 1825 | if (mView != null) { |
| 1826 | // At this point the resources have been updated to |
| 1827 | // have the most recent config, whatever that is. Use |
| 1828 | // the on in them which may be newer. |
| 1829 | if (mView != null) { |
| 1830 | config = mView.getResources().getConfiguration(); |
| 1831 | } |
| 1832 | if (force || mLastConfiguration.diff(config) != 0) { |
| 1833 | mLastConfiguration.setTo(config); |
| 1834 | mView.dispatchConfigurationChanged(config); |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | /** |
| 1840 | * Return true if child is an ancestor of parent, (or equal to the parent). |
| 1841 | */ |
| 1842 | private static boolean isViewDescendantOf(View child, View parent) { |
| 1843 | if (child == parent) { |
| 1844 | return true; |
| 1845 | } |
| 1846 | |
| 1847 | final ViewParent theParent = child.getParent(); |
| 1848 | return (theParent instanceof ViewGroup) && isViewDescendantOf((View) theParent, parent); |
| 1849 | } |
| 1850 | |
Romain Guy | cdb8667 | 2010-03-18 18:54:50 -0700 | [diff] [blame] | 1851 | private static void forceLayout(View view) { |
| 1852 | view.forceLayout(); |
| 1853 | if (view instanceof ViewGroup) { |
| 1854 | ViewGroup group = (ViewGroup) view; |
| 1855 | final int count = group.getChildCount(); |
| 1856 | for (int i = 0; i < count; i++) { |
| 1857 | forceLayout(group.getChildAt(i)); |
| 1858 | } |
| 1859 | } |
| 1860 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1861 | |
| 1862 | public final static int DO_TRAVERSAL = 1000; |
| 1863 | public final static int DIE = 1001; |
| 1864 | public final static int RESIZED = 1002; |
| 1865 | public final static int RESIZED_REPORT = 1003; |
| 1866 | public final static int WINDOW_FOCUS_CHANGED = 1004; |
| 1867 | public final static int DISPATCH_KEY = 1005; |
| 1868 | public final static int DISPATCH_POINTER = 1006; |
| 1869 | public final static int DISPATCH_TRACKBALL = 1007; |
| 1870 | public final static int DISPATCH_APP_VISIBILITY = 1008; |
| 1871 | public final static int DISPATCH_GET_NEW_SURFACE = 1009; |
| 1872 | public final static int FINISHED_EVENT = 1010; |
| 1873 | public final static int DISPATCH_KEY_FROM_IME = 1011; |
| 1874 | public final static int FINISH_INPUT_CONNECTION = 1012; |
| 1875 | public final static int CHECK_FOCUS = 1013; |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 1876 | public final static int CLOSE_SYSTEM_DIALOGS = 1014; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1877 | public final static int DISPATCH_DRAG_EVENT = 1015; |
Chris Tate | 91e9bb3 | 2010-10-12 12:58:43 -0700 | [diff] [blame] | 1878 | public final static int DISPATCH_DRAG_LOCATION_EVENT = 1016; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1879 | |
| 1880 | @Override |
| 1881 | public void handleMessage(Message msg) { |
| 1882 | switch (msg.what) { |
| 1883 | case View.AttachInfo.INVALIDATE_MSG: |
| 1884 | ((View) msg.obj).invalidate(); |
| 1885 | break; |
| 1886 | case View.AttachInfo.INVALIDATE_RECT_MSG: |
| 1887 | final View.AttachInfo.InvalidateInfo info = (View.AttachInfo.InvalidateInfo) msg.obj; |
| 1888 | info.target.invalidate(info.left, info.top, info.right, info.bottom); |
| 1889 | info.release(); |
| 1890 | break; |
| 1891 | case DO_TRAVERSAL: |
| 1892 | if (mProfile) { |
| 1893 | Debug.startMethodTracing("ViewRoot"); |
| 1894 | } |
| 1895 | |
| 1896 | performTraversals(); |
| 1897 | |
| 1898 | if (mProfile) { |
| 1899 | Debug.stopMethodTracing(); |
| 1900 | mProfile = false; |
| 1901 | } |
| 1902 | break; |
| 1903 | case FINISHED_EVENT: |
| 1904 | handleFinishedEvent(msg.arg1, msg.arg2 != 0); |
| 1905 | break; |
| 1906 | case DISPATCH_KEY: |
Jeff Brown | 92ff1dd | 2010-08-11 16:16:06 -0700 | [diff] [blame] | 1907 | deliverKeyEvent((KeyEvent)msg.obj, msg.arg1 != 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | break; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 1909 | case DISPATCH_POINTER: |
| 1910 | deliverPointerEvent((MotionEvent) msg.obj, msg.arg1 != 0); |
| 1911 | break; |
| 1912 | case DISPATCH_TRACKBALL: |
| 1913 | deliverTrackballEvent((MotionEvent) msg.obj, msg.arg1 != 0); |
| 1914 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | case DISPATCH_APP_VISIBILITY: |
| 1916 | handleAppVisibility(msg.arg1 != 0); |
| 1917 | break; |
| 1918 | case DISPATCH_GET_NEW_SURFACE: |
| 1919 | handleGetNewSurface(); |
| 1920 | break; |
| 1921 | case RESIZED: |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1922 | ResizedInfo ri = (ResizedInfo)msg.obj; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 1923 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1924 | if (mWinFrame.width() == msg.arg1 && mWinFrame.height() == msg.arg2 |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1925 | && mPendingContentInsets.equals(ri.coveredInsets) |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 1926 | && mPendingVisibleInsets.equals(ri.visibleInsets) |
| 1927 | && ((ResizedInfo)msg.obj).newConfig == null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1928 | break; |
| 1929 | } |
| 1930 | // fall through... |
| 1931 | case RESIZED_REPORT: |
| 1932 | if (mAdded) { |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1933 | Configuration config = ((ResizedInfo)msg.obj).newConfig; |
| 1934 | if (config != null) { |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 1935 | updateConfiguration(config, false); |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1936 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1937 | mWinFrame.left = 0; |
| 1938 | mWinFrame.right = msg.arg1; |
| 1939 | mWinFrame.top = 0; |
| 1940 | mWinFrame.bottom = msg.arg2; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1941 | mPendingContentInsets.set(((ResizedInfo)msg.obj).coveredInsets); |
| 1942 | mPendingVisibleInsets.set(((ResizedInfo)msg.obj).visibleInsets); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1943 | if (msg.what == RESIZED_REPORT) { |
| 1944 | mReportNextDraw = true; |
| 1945 | } |
Romain Guy | cdb8667 | 2010-03-18 18:54:50 -0700 | [diff] [blame] | 1946 | |
| 1947 | if (mView != null) { |
| 1948 | forceLayout(mView); |
| 1949 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | requestLayout(); |
| 1951 | } |
| 1952 | break; |
| 1953 | case WINDOW_FOCUS_CHANGED: { |
| 1954 | if (mAdded) { |
| 1955 | boolean hasWindowFocus = msg.arg1 != 0; |
| 1956 | mAttachInfo.mHasWindowFocus = hasWindowFocus; |
| 1957 | if (hasWindowFocus) { |
| 1958 | boolean inTouchMode = msg.arg2 != 0; |
Romain Guy | 2d4cff6 | 2010-04-09 15:39:00 -0700 | [diff] [blame] | 1959 | ensureTouchModeLocally(inTouchMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1960 | |
Romain Guy | c361da8 | 2010-10-25 15:29:10 -0700 | [diff] [blame] | 1961 | if (mAttachInfo.mHardwareRenderer != null && |
| 1962 | mSurface != null && mSurface.isValid()) { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 1963 | mAttachInfo.mHardwareRenderer.initializeIfNeeded(mWidth, mHeight, |
| 1964 | mAttachInfo, mHolder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | } |
| 1966 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1967 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1968 | mLastWasImTarget = WindowManager.LayoutParams |
| 1969 | .mayUseInputMethod(mWindowAttributes.flags); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1970 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1971 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1972 | if (mView != null) { |
| 1973 | if (hasWindowFocus && imm != null && mLastWasImTarget) { |
| 1974 | imm.startGettingWindowFocus(mView); |
| 1975 | } |
Dianne Hackborn | 83fe3f5 | 2009-09-12 23:38:30 -0700 | [diff] [blame] | 1976 | mAttachInfo.mKeyDispatchState.reset(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1977 | mView.dispatchWindowFocusChanged(hasWindowFocus); |
| 1978 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1979 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1980 | // Note: must be done after the focus change callbacks, |
| 1981 | // so all of the view state is set up correctly. |
| 1982 | if (hasWindowFocus) { |
| 1983 | if (imm != null && mLastWasImTarget) { |
| 1984 | imm.onWindowFocus(mView, mView.findFocus(), |
| 1985 | mWindowAttributes.softInputMode, |
| 1986 | !mHasHadWindowFocus, mWindowAttributes.flags); |
| 1987 | } |
| 1988 | // Clear the forward bit. We can just do this directly, since |
| 1989 | // the window manager doesn't care about it. |
| 1990 | mWindowAttributes.softInputMode &= |
| 1991 | ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; |
| 1992 | ((WindowManager.LayoutParams)mView.getLayoutParams()) |
| 1993 | .softInputMode &= |
| 1994 | ~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION; |
| 1995 | mHasHadWindowFocus = true; |
| 1996 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1997 | |
| 1998 | if (hasWindowFocus && mView != null) { |
| 1999 | sendAccessibilityEvents(); |
| 2000 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2001 | } |
| 2002 | } break; |
| 2003 | case DIE: |
Dianne Hackborn | 94d6914 | 2009-09-28 22:14:42 -0700 | [diff] [blame] | 2004 | doDie(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2005 | break; |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2006 | case DISPATCH_KEY_FROM_IME: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | if (LOCAL_LOGV) Log.v( |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 2008 | TAG, "Dispatching key " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2009 | + msg.obj + " from IME to " + mView); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2010 | KeyEvent event = (KeyEvent)msg.obj; |
| 2011 | if ((event.getFlags()&KeyEvent.FLAG_FROM_SYSTEM) != 0) { |
| 2012 | // The IME is trying to say this event is from the |
| 2013 | // system! Bad bad bad! |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 2014 | event = KeyEvent.changeFlags(event, event.getFlags() & ~KeyEvent.FLAG_FROM_SYSTEM); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2015 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2016 | deliverKeyEventPostIme((KeyEvent)msg.obj, false); |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 2017 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2018 | case FINISH_INPUT_CONNECTION: { |
| 2019 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 2020 | if (imm != null) { |
| 2021 | imm.reportFinishInputConnection((InputConnection)msg.obj); |
| 2022 | } |
| 2023 | } break; |
| 2024 | case CHECK_FOCUS: { |
| 2025 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 2026 | if (imm != null) { |
| 2027 | imm.checkFocus(); |
| 2028 | } |
| 2029 | } break; |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 2030 | case CLOSE_SYSTEM_DIALOGS: { |
| 2031 | if (mView != null) { |
| 2032 | mView.onCloseSystemDialogs((String)msg.obj); |
| 2033 | } |
| 2034 | } break; |
Chris Tate | 91e9bb3 | 2010-10-12 12:58:43 -0700 | [diff] [blame] | 2035 | case DISPATCH_DRAG_EVENT: |
| 2036 | case DISPATCH_DRAG_LOCATION_EVENT: { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2037 | handleDragEvent((DragEvent)msg.obj); |
| 2038 | } break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | } |
| 2040 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2041 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2042 | private void startInputEvent(InputQueue.FinishedCallback finishedCallback) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 2043 | if (mFinishedCallback != null) { |
| 2044 | Slog.w(TAG, "Received a new input event from the input queue but there is " |
| 2045 | + "already an unfinished input event in progress."); |
| 2046 | } |
| 2047 | |
| 2048 | mFinishedCallback = finishedCallback; |
| 2049 | } |
| 2050 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2051 | private void finishInputEvent(boolean handled) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 2052 | if (LOCAL_LOGV) Log.v(TAG, "Telling window manager input event is finished"); |
Jeff Brown | 92ff1dd | 2010-08-11 16:16:06 -0700 | [diff] [blame] | 2053 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2054 | if (mFinishedCallback != null) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2055 | mFinishedCallback.finished(handled); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2056 | mFinishedCallback = null; |
Jeff Brown | 92ff1dd | 2010-08-11 16:16:06 -0700 | [diff] [blame] | 2057 | } else { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 2058 | Slog.w(TAG, "Attempted to tell the input queue that the current input event " |
| 2059 | + "is finished but there is no input event actually in progress."); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 2060 | } |
| 2061 | } |
| 2062 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2063 | /** |
| 2064 | * Something in the current window tells us we need to change the touch mode. For |
| 2065 | * example, we are not in touch mode, and the user touches the screen. |
| 2066 | * |
| 2067 | * If the touch mode has changed, tell the window manager, and handle it locally. |
| 2068 | * |
| 2069 | * @param inTouchMode Whether we want to be in touch mode. |
| 2070 | * @return True if the touch mode changed and focus changed was changed as a result |
| 2071 | */ |
| 2072 | boolean ensureTouchMode(boolean inTouchMode) { |
| 2073 | if (DBG) Log.d("touchmode", "ensureTouchMode(" + inTouchMode + "), current " |
| 2074 | + "touch mode is " + mAttachInfo.mInTouchMode); |
| 2075 | if (mAttachInfo.mInTouchMode == inTouchMode) return false; |
| 2076 | |
| 2077 | // tell the window manager |
| 2078 | try { |
| 2079 | sWindowSession.setInTouchMode(inTouchMode); |
| 2080 | } catch (RemoteException e) { |
| 2081 | throw new RuntimeException(e); |
| 2082 | } |
| 2083 | |
| 2084 | // handle the change |
Romain Guy | 2d4cff6 | 2010-04-09 15:39:00 -0700 | [diff] [blame] | 2085 | return ensureTouchModeLocally(inTouchMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | /** |
| 2089 | * Ensure that the touch mode for this window is set, and if it is changing, |
| 2090 | * take the appropriate action. |
| 2091 | * @param inTouchMode Whether we want to be in touch mode. |
| 2092 | * @return True if the touch mode changed and focus changed was changed as a result |
| 2093 | */ |
Romain Guy | 2d4cff6 | 2010-04-09 15:39:00 -0700 | [diff] [blame] | 2094 | private boolean ensureTouchModeLocally(boolean inTouchMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2095 | if (DBG) Log.d("touchmode", "ensureTouchModeLocally(" + inTouchMode + "), current " |
| 2096 | + "touch mode is " + mAttachInfo.mInTouchMode); |
| 2097 | |
| 2098 | if (mAttachInfo.mInTouchMode == inTouchMode) return false; |
| 2099 | |
| 2100 | mAttachInfo.mInTouchMode = inTouchMode; |
| 2101 | mAttachInfo.mTreeObserver.dispatchOnTouchModeChanged(inTouchMode); |
| 2102 | |
Romain Guy | 2d4cff6 | 2010-04-09 15:39:00 -0700 | [diff] [blame] | 2103 | return (inTouchMode) ? enterTouchMode() : leaveTouchMode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | private boolean enterTouchMode() { |
| 2107 | if (mView != null) { |
| 2108 | if (mView.hasFocus()) { |
| 2109 | // note: not relying on mFocusedView here because this could |
| 2110 | // be when the window is first being added, and mFocused isn't |
| 2111 | // set yet. |
| 2112 | final View focused = mView.findFocus(); |
| 2113 | if (focused != null && !focused.isFocusableInTouchMode()) { |
| 2114 | |
| 2115 | final ViewGroup ancestorToTakeFocus = |
| 2116 | findAncestorToTakeFocusInTouchMode(focused); |
| 2117 | if (ancestorToTakeFocus != null) { |
| 2118 | // there is an ancestor that wants focus after its descendants that |
| 2119 | // is focusable in touch mode.. give it focus |
| 2120 | return ancestorToTakeFocus.requestFocus(); |
| 2121 | } else { |
| 2122 | // nothing appropriate to have focus in touch mode, clear it out |
| 2123 | mView.unFocus(); |
| 2124 | mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(focused, null); |
| 2125 | mFocusedView = null; |
| 2126 | return true; |
| 2127 | } |
| 2128 | } |
| 2129 | } |
| 2130 | } |
| 2131 | return false; |
| 2132 | } |
| 2133 | |
| 2134 | |
| 2135 | /** |
| 2136 | * Find an ancestor of focused that wants focus after its descendants and is |
| 2137 | * focusable in touch mode. |
| 2138 | * @param focused The currently focused view. |
| 2139 | * @return An appropriate view, or null if no such view exists. |
| 2140 | */ |
| 2141 | private ViewGroup findAncestorToTakeFocusInTouchMode(View focused) { |
| 2142 | ViewParent parent = focused.getParent(); |
| 2143 | while (parent instanceof ViewGroup) { |
| 2144 | final ViewGroup vgParent = (ViewGroup) parent; |
| 2145 | if (vgParent.getDescendantFocusability() == ViewGroup.FOCUS_AFTER_DESCENDANTS |
| 2146 | && vgParent.isFocusableInTouchMode()) { |
| 2147 | return vgParent; |
| 2148 | } |
| 2149 | if (vgParent.isRootNamespace()) { |
| 2150 | return null; |
| 2151 | } else { |
| 2152 | parent = vgParent.getParent(); |
| 2153 | } |
| 2154 | } |
| 2155 | return null; |
| 2156 | } |
| 2157 | |
| 2158 | private boolean leaveTouchMode() { |
| 2159 | if (mView != null) { |
| 2160 | if (mView.hasFocus()) { |
| 2161 | // i learned the hard way to not trust mFocusedView :) |
| 2162 | mFocusedView = mView.findFocus(); |
| 2163 | if (!(mFocusedView instanceof ViewGroup)) { |
| 2164 | // some view has focus, let it keep it |
| 2165 | return false; |
| 2166 | } else if (((ViewGroup)mFocusedView).getDescendantFocusability() != |
| 2167 | ViewGroup.FOCUS_AFTER_DESCENDANTS) { |
| 2168 | // some view group has focus, and doesn't prefer its children |
| 2169 | // over itself for focus, so let them keep it. |
| 2170 | return false; |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | // find the best view to give focus to in this brave new non-touch-mode |
| 2175 | // world |
| 2176 | final View focused = focusSearch(null, View.FOCUS_DOWN); |
| 2177 | if (focused != null) { |
| 2178 | return focused.requestFocus(View.FOCUS_DOWN); |
| 2179 | } |
| 2180 | } |
| 2181 | return false; |
| 2182 | } |
| 2183 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2184 | private void deliverPointerEvent(MotionEvent event, boolean sendDone) { |
| 2185 | // If there is no view, then the event will not be handled. |
| 2186 | if (mView == null || !mAdded) { |
| 2187 | finishPointerEvent(event, sendDone, false); |
| 2188 | return; |
| 2189 | } |
| 2190 | |
| 2191 | // Translate the pointer event for compatibility, if needed. |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2192 | if (mTranslator != null) { |
| 2193 | mTranslator.translateEventInScreenToAppWindow(event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2194 | } |
| 2195 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2196 | // Enter touch mode on the down. |
| 2197 | boolean isDown = event.getAction() == MotionEvent.ACTION_DOWN; |
| 2198 | if (isDown) { |
| 2199 | ensureTouchMode(true); |
| 2200 | } |
| 2201 | if(Config.LOGV) { |
| 2202 | captureMotionLog("captureDispatchPointer", event); |
| 2203 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2204 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2205 | // Offset the scroll position. |
| 2206 | if (mCurScrollY != 0) { |
| 2207 | event.offsetLocation(0, mCurScrollY); |
| 2208 | } |
| 2209 | if (MEASURE_LATENCY) { |
| 2210 | lt.sample("A Dispatching TouchEvents", System.nanoTime() - event.getEventTimeNano()); |
| 2211 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2212 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2213 | // Remember the touch position for possible drag-initiation. |
| 2214 | mLastTouchPoint.x = event.getRawX(); |
| 2215 | mLastTouchPoint.y = event.getRawY(); |
| 2216 | |
| 2217 | // Dispatch touch to view hierarchy. |
| 2218 | boolean handled = mView.dispatchTouchEvent(event); |
| 2219 | if (MEASURE_LATENCY) { |
| 2220 | lt.sample("B Dispatched TouchEvents ", System.nanoTime() - event.getEventTimeNano()); |
| 2221 | } |
| 2222 | if (handled) { |
| 2223 | finishPointerEvent(event, sendDone, true); |
| 2224 | return; |
| 2225 | } |
| 2226 | |
| 2227 | // Apply edge slop and try again, if appropriate. |
| 2228 | final int edgeFlags = event.getEdgeFlags(); |
| 2229 | if (edgeFlags != 0 && mView instanceof ViewGroup) { |
| 2230 | final int edgeSlop = mViewConfiguration.getScaledEdgeSlop(); |
| 2231 | int direction = View.FOCUS_UP; |
| 2232 | int x = (int)event.getX(); |
| 2233 | int y = (int)event.getY(); |
| 2234 | final int[] deltas = new int[2]; |
| 2235 | |
| 2236 | if ((edgeFlags & MotionEvent.EDGE_TOP) != 0) { |
| 2237 | direction = View.FOCUS_DOWN; |
| 2238 | if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) { |
| 2239 | deltas[0] = edgeSlop; |
| 2240 | x += edgeSlop; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2241 | } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2242 | deltas[0] = -edgeSlop; |
| 2243 | x -= edgeSlop; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2244 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2245 | } else if ((edgeFlags & MotionEvent.EDGE_BOTTOM) != 0) { |
| 2246 | direction = View.FOCUS_UP; |
| 2247 | if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) { |
| 2248 | deltas[0] = edgeSlop; |
| 2249 | x += edgeSlop; |
| 2250 | } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) { |
| 2251 | deltas[0] = -edgeSlop; |
| 2252 | x -= edgeSlop; |
| 2253 | } |
| 2254 | } else if ((edgeFlags & MotionEvent.EDGE_LEFT) != 0) { |
| 2255 | direction = View.FOCUS_RIGHT; |
| 2256 | } else if ((edgeFlags & MotionEvent.EDGE_RIGHT) != 0) { |
| 2257 | direction = View.FOCUS_LEFT; |
| 2258 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2259 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2260 | View nearest = FocusFinder.getInstance().findNearestTouchable( |
| 2261 | ((ViewGroup) mView), x, y, direction, deltas); |
| 2262 | if (nearest != null) { |
| 2263 | event.offsetLocation(deltas[0], deltas[1]); |
| 2264 | event.setEdgeFlags(0); |
| 2265 | if (mView.dispatchTouchEvent(event)) { |
| 2266 | finishPointerEvent(event, sendDone, true); |
| 2267 | return; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2268 | } |
| 2269 | } |
| 2270 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2271 | |
| 2272 | // Pointer event was unhandled. |
| 2273 | finishPointerEvent(event, sendDone, false); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2276 | private void finishPointerEvent(MotionEvent event, boolean sendDone, boolean handled) { |
| 2277 | event.recycle(); |
| 2278 | if (sendDone) { |
| 2279 | finishInputEvent(handled); |
| 2280 | } |
| 2281 | if (LOCAL_LOGV || WATCH_POINTER) Log.i(TAG, "Done dispatching!"); |
| 2282 | } |
| 2283 | |
| 2284 | private void deliverTrackballEvent(MotionEvent event, boolean sendDone) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2285 | if (DEBUG_TRACKBALL) Log.v(TAG, "Motion event:" + event); |
| 2286 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2287 | // If there is no view, then the event will not be handled. |
| 2288 | if (mView == null || !mAdded) { |
| 2289 | finishTrackballEvent(event, sendDone, false); |
| 2290 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2291 | } |
| 2292 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2293 | // Deliver the trackball event to the view. |
| 2294 | if (mView.dispatchTrackballEvent(event)) { |
| 2295 | // If we reach this, we delivered a trackball event to mView and |
| 2296 | // mView consumed it. Because we will not translate the trackball |
| 2297 | // event into a key event, touch mode will not exit, so we exit |
| 2298 | // touch mode here. |
| 2299 | ensureTouchMode(false); |
| 2300 | |
| 2301 | finishTrackballEvent(event, sendDone, true); |
| 2302 | mLastTrackballTime = Integer.MIN_VALUE; |
| 2303 | return; |
| 2304 | } |
| 2305 | |
| 2306 | // Translate the trackball event into DPAD keys and try to deliver those. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2307 | final TrackballAxis x = mTrackballAxisX; |
| 2308 | final TrackballAxis y = mTrackballAxisY; |
| 2309 | |
| 2310 | long curTime = SystemClock.uptimeMillis(); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2311 | if ((mLastTrackballTime + MAX_TRACKBALL_DELAY) < curTime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2312 | // It has been too long since the last movement, |
| 2313 | // so restart at the beginning. |
| 2314 | x.reset(0); |
| 2315 | y.reset(0); |
| 2316 | mLastTrackballTime = curTime; |
| 2317 | } |
| 2318 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2319 | final int action = event.getAction(); |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2320 | final int metaState = event.getMetaState(); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2321 | switch (action) { |
| 2322 | case MotionEvent.ACTION_DOWN: |
| 2323 | x.reset(2); |
| 2324 | y.reset(2); |
| 2325 | deliverKeyEvent(new KeyEvent(curTime, curTime, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2326 | KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState, |
| 2327 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, |
| 2328 | InputDevice.SOURCE_KEYBOARD), false); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2329 | break; |
| 2330 | case MotionEvent.ACTION_UP: |
| 2331 | x.reset(2); |
| 2332 | y.reset(2); |
| 2333 | deliverKeyEvent(new KeyEvent(curTime, curTime, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2334 | KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER, 0, metaState, |
| 2335 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, |
| 2336 | InputDevice.SOURCE_KEYBOARD), false); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2337 | break; |
| 2338 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2339 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2340 | if (DEBUG_TRACKBALL) Log.v(TAG, "TB X=" + x.position + " step=" |
| 2341 | + x.step + " dir=" + x.dir + " acc=" + x.acceleration |
| 2342 | + " move=" + event.getX() |
| 2343 | + " / Y=" + y.position + " step=" |
| 2344 | + y.step + " dir=" + y.dir + " acc=" + y.acceleration |
| 2345 | + " move=" + event.getY()); |
| 2346 | final float xOff = x.collect(event.getX(), event.getEventTime(), "X"); |
| 2347 | final float yOff = y.collect(event.getY(), event.getEventTime(), "Y"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2348 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2349 | // Generate DPAD events based on the trackball movement. |
| 2350 | // We pick the axis that has moved the most as the direction of |
| 2351 | // the DPAD. When we generate DPAD events for one axis, then the |
| 2352 | // other axis is reset -- we don't want to perform DPAD jumps due |
| 2353 | // to slight movements in the trackball when making major movements |
| 2354 | // along the other axis. |
| 2355 | int keycode = 0; |
| 2356 | int movement = 0; |
| 2357 | float accel = 1; |
| 2358 | if (xOff > yOff) { |
| 2359 | movement = x.generate((2/event.getXPrecision())); |
| 2360 | if (movement != 0) { |
| 2361 | keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_RIGHT |
| 2362 | : KeyEvent.KEYCODE_DPAD_LEFT; |
| 2363 | accel = x.acceleration; |
| 2364 | y.reset(2); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2365 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2366 | } else if (yOff > 0) { |
| 2367 | movement = y.generate((2/event.getYPrecision())); |
| 2368 | if (movement != 0) { |
| 2369 | keycode = movement > 0 ? KeyEvent.KEYCODE_DPAD_DOWN |
| 2370 | : KeyEvent.KEYCODE_DPAD_UP; |
| 2371 | accel = y.acceleration; |
| 2372 | x.reset(2); |
| 2373 | } |
| 2374 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2375 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2376 | if (keycode != 0) { |
| 2377 | if (movement < 0) movement = -movement; |
| 2378 | int accelMovement = (int)(movement * accel); |
| 2379 | if (DEBUG_TRACKBALL) Log.v(TAG, "Move: movement=" + movement |
| 2380 | + " accelMovement=" + accelMovement |
| 2381 | + " accel=" + accel); |
| 2382 | if (accelMovement > movement) { |
| 2383 | if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: " |
| 2384 | + keycode); |
| 2385 | movement--; |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2386 | int repeatCount = accelMovement - movement; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2387 | deliverKeyEvent(new KeyEvent(curTime, curTime, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2388 | KeyEvent.ACTION_MULTIPLE, keycode, repeatCount, metaState, |
| 2389 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, |
| 2390 | InputDevice.SOURCE_KEYBOARD), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2391 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2392 | while (movement > 0) { |
| 2393 | if (DEBUG_TRACKBALL) Log.v("foo", "Delivering fake DPAD: " |
| 2394 | + keycode); |
| 2395 | movement--; |
| 2396 | curTime = SystemClock.uptimeMillis(); |
| 2397 | deliverKeyEvent(new KeyEvent(curTime, curTime, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2398 | KeyEvent.ACTION_DOWN, keycode, 0, metaState, |
| 2399 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, |
| 2400 | InputDevice.SOURCE_KEYBOARD), false); |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2401 | deliverKeyEvent(new KeyEvent(curTime, curTime, |
Jeff Brown | 49ed71d | 2010-12-06 17:13:33 -0800 | [diff] [blame] | 2402 | KeyEvent.ACTION_UP, keycode, 0, metaState, |
| 2403 | KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_FALLBACK, |
| 2404 | InputDevice.SOURCE_KEYBOARD), false); |
| 2405 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 2406 | mLastTrackballTime = curTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2407 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2408 | |
| 2409 | // Unfortunately we can't tell whether the application consumed the keys, so |
| 2410 | // we always consider the trackball event handled. |
| 2411 | finishTrackballEvent(event, sendDone, true); |
| 2412 | } |
| 2413 | |
| 2414 | private void finishTrackballEvent(MotionEvent event, boolean sendDone, boolean handled) { |
| 2415 | event.recycle(); |
| 2416 | if (sendDone) { |
| 2417 | finishInputEvent(handled); |
| 2418 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2422 | * Returns true if the key is used for keyboard navigation. |
| 2423 | * @param keyEvent The key event. |
| 2424 | * @return True if the key is used for keyboard navigation. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2425 | */ |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2426 | private static boolean isNavigationKey(KeyEvent keyEvent) { |
| 2427 | switch (keyEvent.getKeyCode()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2428 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 2429 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 2430 | case KeyEvent.KEYCODE_DPAD_UP: |
| 2431 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2432 | case KeyEvent.KEYCODE_DPAD_CENTER: |
| 2433 | case KeyEvent.KEYCODE_PAGE_UP: |
| 2434 | case KeyEvent.KEYCODE_PAGE_DOWN: |
| 2435 | case KeyEvent.KEYCODE_MOVE_HOME: |
| 2436 | case KeyEvent.KEYCODE_MOVE_END: |
| 2437 | case KeyEvent.KEYCODE_TAB: |
| 2438 | case KeyEvent.KEYCODE_SPACE: |
| 2439 | case KeyEvent.KEYCODE_ENTER: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2440 | return true; |
| 2441 | } |
| 2442 | return false; |
| 2443 | } |
| 2444 | |
| 2445 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2446 | * Returns true if the key is used for typing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2447 | * @param keyEvent The key event. |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2448 | * @return True if the key is used for typing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2449 | */ |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2450 | private static boolean isTypingKey(KeyEvent keyEvent) { |
| 2451 | return keyEvent.getUnicodeChar() > 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2452 | } |
| 2453 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2454 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2455 | * See if the key event means we should leave touch mode (and leave touch mode if so). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2456 | * @param event The key event. |
| 2457 | * @return Whether this key event should be consumed (meaning the act of |
| 2458 | * leaving touch mode alone is considered the event). |
| 2459 | */ |
| 2460 | private boolean checkForLeavingTouchModeAndConsume(KeyEvent event) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2461 | // Only relevant in touch mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2462 | if (!mAttachInfo.mInTouchMode) { |
| 2463 | return false; |
| 2464 | } |
| 2465 | |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2466 | // Only consider leaving touch mode on DOWN or MULTIPLE actions, never on UP. |
| 2467 | final int action = event.getAction(); |
| 2468 | if (action != KeyEvent.ACTION_DOWN && action != KeyEvent.ACTION_MULTIPLE) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2469 | return false; |
| 2470 | } |
| 2471 | |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2472 | // Don't leave touch mode if the IME told us not to. |
| 2473 | if ((event.getFlags() & KeyEvent.FLAG_KEEP_TOUCH_MODE) != 0) { |
| 2474 | return false; |
| 2475 | } |
| 2476 | |
| 2477 | // If the key can be used for keyboard navigation then leave touch mode |
| 2478 | // and select a focused view if needed (in ensureTouchMode). |
| 2479 | // When a new focused view is selected, we consume the navigation key because |
| 2480 | // navigation doesn't make much sense unless a view already has focus so |
| 2481 | // the key's purpose is to set focus. |
| 2482 | if (isNavigationKey(event)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2483 | return ensureTouchMode(false); |
| 2484 | } |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2485 | |
| 2486 | // If the key can be used for typing then leave touch mode |
| 2487 | // and select a focused view if needed (in ensureTouchMode). |
| 2488 | // Always allow the view to process the typing key. |
| 2489 | if (isTypingKey(event)) { |
| 2490 | ensureTouchMode(false); |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | return false; |
| 2495 | } |
| 2496 | |
| 2497 | /** |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2498 | * log motion events |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2499 | */ |
| 2500 | private static void captureMotionLog(String subTag, MotionEvent ev) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2501 | //check dynamic switch |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2502 | if (ev == null || |
| 2503 | SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) { |
| 2504 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2505 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2506 | |
| 2507 | StringBuilder sb = new StringBuilder(subTag + ": "); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2508 | sb.append(ev.getDownTime()).append(','); |
| 2509 | sb.append(ev.getEventTime()).append(','); |
| 2510 | sb.append(ev.getAction()).append(','); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2511 | sb.append(ev.getX()).append(','); |
| 2512 | sb.append(ev.getY()).append(','); |
| 2513 | sb.append(ev.getPressure()).append(','); |
| 2514 | sb.append(ev.getSize()).append(','); |
| 2515 | sb.append(ev.getMetaState()).append(','); |
| 2516 | sb.append(ev.getXPrecision()).append(','); |
| 2517 | sb.append(ev.getYPrecision()).append(','); |
| 2518 | sb.append(ev.getDeviceId()).append(','); |
| 2519 | sb.append(ev.getEdgeFlags()); |
| 2520 | Log.d(TAG, sb.toString()); |
| 2521 | } |
| 2522 | /** |
| 2523 | * log motion events |
| 2524 | */ |
| 2525 | private static void captureKeyLog(String subTag, KeyEvent ev) { |
| 2526 | //check dynamic switch |
| 2527 | if (ev == null || |
| 2528 | SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_EVENT, 0) == 0) { |
| 2529 | return; |
| 2530 | } |
| 2531 | StringBuilder sb = new StringBuilder(subTag + ": "); |
| 2532 | sb.append(ev.getDownTime()).append(','); |
| 2533 | sb.append(ev.getEventTime()).append(','); |
| 2534 | sb.append(ev.getAction()).append(','); |
| 2535 | sb.append(ev.getKeyCode()).append(','); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2536 | sb.append(ev.getRepeatCount()).append(','); |
| 2537 | sb.append(ev.getMetaState()).append(','); |
| 2538 | sb.append(ev.getDeviceId()).append(','); |
| 2539 | sb.append(ev.getScanCode()); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2540 | Log.d(TAG, sb.toString()); |
| 2541 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2542 | |
| 2543 | int enqueuePendingEvent(Object event, boolean sendDone) { |
| 2544 | int seq = mPendingEventSeq+1; |
| 2545 | if (seq < 0) seq = 0; |
| 2546 | mPendingEventSeq = seq; |
| 2547 | mPendingEvents.put(seq, event); |
| 2548 | return sendDone ? seq : -seq; |
| 2549 | } |
| 2550 | |
| 2551 | Object retrievePendingEvent(int seq) { |
| 2552 | if (seq < 0) seq = -seq; |
| 2553 | Object event = mPendingEvents.get(seq); |
| 2554 | if (event != null) { |
| 2555 | mPendingEvents.remove(seq); |
| 2556 | } |
| 2557 | return event; |
| 2558 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2559 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2560 | private void deliverKeyEvent(KeyEvent event, boolean sendDone) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2561 | // If there is no view, then the event will not be handled. |
| 2562 | if (mView == null || !mAdded) { |
| 2563 | finishKeyEvent(event, sendDone, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2564 | return; |
| 2565 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2566 | |
| 2567 | if (LOCAL_LOGV) Log.v(TAG, "Dispatching key " + event + " to " + mView); |
| 2568 | |
| 2569 | // Perform predispatching before the IME. |
| 2570 | if (mView.dispatchKeyEventPreIme(event)) { |
| 2571 | finishKeyEvent(event, sendDone, true); |
| 2572 | return; |
| 2573 | } |
| 2574 | |
| 2575 | // Dispatch to the IME before propagating down the view hierarchy. |
| 2576 | // The IME will eventually call back into handleFinishedEvent. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2577 | if (mLastWasImTarget) { |
| 2578 | InputMethodManager imm = InputMethodManager.peekInstance(); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2579 | if (imm != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2580 | int seq = enqueuePendingEvent(event, sendDone); |
| 2581 | if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq=" |
| 2582 | + seq + " event=" + event); |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2583 | imm.dispatchKeyEvent(mView.getContext(), seq, event, mInputMethodCallback); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2584 | return; |
| 2585 | } |
| 2586 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2587 | |
| 2588 | // Not dispatching to IME, continue with post IME actions. |
| 2589 | deliverKeyEventPostIme(event, sendDone); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2590 | } |
| 2591 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2592 | private void handleFinishedEvent(int seq, boolean handled) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2593 | final KeyEvent event = (KeyEvent)retrievePendingEvent(seq); |
| 2594 | if (DEBUG_IMF) Log.v(TAG, "IME finished event: seq=" + seq |
| 2595 | + " handled=" + handled + " event=" + event); |
| 2596 | if (event != null) { |
| 2597 | final boolean sendDone = seq >= 0; |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2598 | if (handled) { |
| 2599 | finishKeyEvent(event, sendDone, true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2600 | } else { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2601 | deliverKeyEventPostIme(event, sendDone); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2605 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2606 | private void deliverKeyEventPostIme(KeyEvent event, boolean sendDone) { |
| 2607 | // If the view went away, then the event will not be handled. |
| 2608 | if (mView == null || !mAdded) { |
| 2609 | finishKeyEvent(event, sendDone, false); |
| 2610 | return; |
| 2611 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2612 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2613 | // If the key's purpose is to exit touch mode then we consume it and consider it handled. |
| 2614 | if (checkForLeavingTouchModeAndConsume(event)) { |
| 2615 | finishKeyEvent(event, sendDone, true); |
| 2616 | return; |
| 2617 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2618 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2619 | if (Config.LOGV) { |
| 2620 | captureKeyLog("captureDispatchKeyEvent", event); |
| 2621 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2622 | |
Jeff Brown | 9065504 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 2623 | // Make sure the fallback event policy sees all keys that will be delivered to the |
| 2624 | // view hierarchy. |
| 2625 | mFallbackEventHandler.preDispatchKeyEvent(event); |
| 2626 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2627 | // Deliver the key to the view hierarchy. |
| 2628 | if (mView.dispatchKeyEvent(event)) { |
| 2629 | finishKeyEvent(event, sendDone, true); |
| 2630 | return; |
| 2631 | } |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 2632 | |
Jeff Brown | c1df907 | 2010-12-21 16:38:50 -0800 | [diff] [blame] | 2633 | // If the Control modifier is held, try to interpret the key as a shortcut. |
| 2634 | if (event.getAction() == KeyEvent.ACTION_UP |
| 2635 | && event.isCtrlPressed() |
| 2636 | && !KeyEvent.isModifierKey(event.getKeyCode())) { |
| 2637 | if (mView.dispatchKeyShortcutEvent(event)) { |
| 2638 | finishKeyEvent(event, sendDone, true); |
| 2639 | return; |
| 2640 | } |
| 2641 | } |
| 2642 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2643 | // Apply the fallback event policy. |
| 2644 | if (mFallbackEventHandler.dispatchKeyEvent(event)) { |
| 2645 | finishKeyEvent(event, sendDone, true); |
| 2646 | return; |
| 2647 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2648 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2649 | // Handle automatic focus changes. |
| 2650 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 2651 | int direction = 0; |
| 2652 | switch (event.getKeyCode()) { |
| 2653 | case KeyEvent.KEYCODE_DPAD_LEFT: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2654 | if (event.hasNoModifiers()) { |
| 2655 | direction = View.FOCUS_LEFT; |
| 2656 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2657 | break; |
| 2658 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2659 | if (event.hasNoModifiers()) { |
| 2660 | direction = View.FOCUS_RIGHT; |
| 2661 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2662 | break; |
| 2663 | case KeyEvent.KEYCODE_DPAD_UP: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2664 | if (event.hasNoModifiers()) { |
| 2665 | direction = View.FOCUS_UP; |
| 2666 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2667 | break; |
| 2668 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 2669 | if (event.hasNoModifiers()) { |
| 2670 | direction = View.FOCUS_DOWN; |
| 2671 | } |
| 2672 | break; |
| 2673 | case KeyEvent.KEYCODE_TAB: |
| 2674 | if (event.hasNoModifiers()) { |
| 2675 | direction = View.FOCUS_FORWARD; |
| 2676 | } else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) { |
| 2677 | direction = View.FOCUS_BACKWARD; |
| 2678 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2679 | break; |
| 2680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2681 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2682 | if (direction != 0) { |
| 2683 | View focused = mView != null ? mView.findFocus() : null; |
| 2684 | if (focused != null) { |
| 2685 | View v = focused.focusSearch(direction); |
| 2686 | if (v != null && v != focused) { |
| 2687 | // do the math the get the interesting rect |
| 2688 | // of previous focused into the coord system of |
| 2689 | // newly focused view |
| 2690 | focused.getFocusedRect(mTempRect); |
| 2691 | if (mView instanceof ViewGroup) { |
| 2692 | ((ViewGroup) mView).offsetDescendantRectToMyCoords( |
| 2693 | focused, mTempRect); |
| 2694 | ((ViewGroup) mView).offsetRectIntoDescendantCoords( |
| 2695 | v, mTempRect); |
| 2696 | } |
| 2697 | if (v.requestFocus(direction, mTempRect)) { |
| 2698 | playSoundEffect( |
| 2699 | SoundEffectConstants.getContantForFocusDirection(direction)); |
| 2700 | finishKeyEvent(event, sendDone, true); |
| 2701 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2702 | } |
| 2703 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2704 | |
| 2705 | // Give the focused view a last chance to handle the dpad key. |
| 2706 | if (mView.dispatchUnhandledMove(focused, direction)) { |
| 2707 | finishKeyEvent(event, sendDone, true); |
| 2708 | return; |
| 2709 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2710 | } |
| 2711 | } |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2712 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2713 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 2714 | // Key was unhandled. |
| 2715 | finishKeyEvent(event, sendDone, false); |
| 2716 | } |
| 2717 | |
| 2718 | private void finishKeyEvent(KeyEvent event, boolean sendDone, boolean handled) { |
| 2719 | if (sendDone) { |
| 2720 | finishInputEvent(handled); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2724 | /* drag/drop */ |
Christopher Tate | 407b4e9 | 2010-11-30 17:14:08 -0800 | [diff] [blame] | 2725 | void setLocalDragState(Object obj) { |
| 2726 | mLocalDragState = obj; |
| 2727 | } |
| 2728 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2729 | private void handleDragEvent(DragEvent event) { |
| 2730 | // From the root, only drag start/end/location are dispatched. entered/exited |
| 2731 | // are determined and dispatched by the viewgroup hierarchy, who then report |
| 2732 | // that back here for ultimate reporting back to the framework. |
| 2733 | if (mView != null && mAdded) { |
| 2734 | final int what = event.mAction; |
| 2735 | |
| 2736 | if (what == DragEvent.ACTION_DRAG_EXITED) { |
| 2737 | // A direct EXITED event means that the window manager knows we've just crossed |
| 2738 | // a window boundary, so the current drag target within this one must have |
| 2739 | // just been exited. Send it the usual notifications and then we're done |
| 2740 | // for now. |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 2741 | mView.dispatchDragEvent(event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2742 | } else { |
| 2743 | // Cache the drag description when the operation starts, then fill it in |
| 2744 | // on subsequent calls as a convenience |
| 2745 | if (what == DragEvent.ACTION_DRAG_STARTED) { |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 2746 | mCurrentDragView = null; // Start the current-recipient tracking |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2747 | mDragDescription = event.mClipDescription; |
| 2748 | } else { |
| 2749 | event.mClipDescription = mDragDescription; |
| 2750 | } |
| 2751 | |
| 2752 | // For events with a [screen] location, translate into window coordinates |
| 2753 | if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) { |
| 2754 | mDragPoint.set(event.mX, event.mY); |
| 2755 | if (mTranslator != null) { |
| 2756 | mTranslator.translatePointInScreenToAppWindow(mDragPoint); |
| 2757 | } |
| 2758 | |
| 2759 | if (mCurScrollY != 0) { |
| 2760 | mDragPoint.offset(0, mCurScrollY); |
| 2761 | } |
| 2762 | |
| 2763 | event.mX = mDragPoint.x; |
| 2764 | event.mY = mDragPoint.y; |
| 2765 | } |
| 2766 | |
| 2767 | // Remember who the current drag target is pre-dispatch |
| 2768 | final View prevDragView = mCurrentDragView; |
| 2769 | |
| 2770 | // Now dispatch the drag/drop event |
Chris Tate | d4533f1 | 2010-10-19 15:15:08 -0700 | [diff] [blame] | 2771 | boolean result = mView.dispatchDragEvent(event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2772 | |
| 2773 | // If we changed apparent drag target, tell the OS about it |
| 2774 | if (prevDragView != mCurrentDragView) { |
| 2775 | try { |
| 2776 | if (prevDragView != null) { |
| 2777 | sWindowSession.dragRecipientExited(mWindow); |
| 2778 | } |
| 2779 | if (mCurrentDragView != null) { |
| 2780 | sWindowSession.dragRecipientEntered(mWindow); |
| 2781 | } |
| 2782 | } catch (RemoteException e) { |
| 2783 | Slog.e(TAG, "Unable to note drag target change"); |
| 2784 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2785 | } |
Chris Tate | d4533f1 | 2010-10-19 15:15:08 -0700 | [diff] [blame] | 2786 | |
Christopher Tate | 407b4e9 | 2010-11-30 17:14:08 -0800 | [diff] [blame] | 2787 | // Report the drop result when we're done |
Chris Tate | d4533f1 | 2010-10-19 15:15:08 -0700 | [diff] [blame] | 2788 | if (what == DragEvent.ACTION_DROP) { |
| 2789 | try { |
| 2790 | Log.i(TAG, "Reporting drop result: " + result); |
| 2791 | sWindowSession.reportDropResult(mWindow, result); |
| 2792 | } catch (RemoteException e) { |
| 2793 | Log.e(TAG, "Unable to report drop result"); |
| 2794 | } |
| 2795 | } |
Christopher Tate | 407b4e9 | 2010-11-30 17:14:08 -0800 | [diff] [blame] | 2796 | |
| 2797 | // When the drag operation ends, release any local state object |
| 2798 | // that may have been in use |
| 2799 | if (what == DragEvent.ACTION_DRAG_ENDED) { |
| 2800 | setLocalDragState(null); |
| 2801 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2802 | } |
| 2803 | } |
| 2804 | event.recycle(); |
| 2805 | } |
| 2806 | |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2807 | public void getLastTouchPoint(Point outLocation) { |
| 2808 | outLocation.x = (int) mLastTouchPoint.x; |
| 2809 | outLocation.y = (int) mLastTouchPoint.y; |
| 2810 | } |
| 2811 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 2812 | public void setDragFocus(View newDragTarget) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2813 | if (mCurrentDragView != newDragTarget) { |
Chris Tate | 048691c | 2010-10-12 17:39:18 -0700 | [diff] [blame] | 2814 | mCurrentDragView = newDragTarget; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2815 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 2816 | } |
| 2817 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2818 | private AudioManager getAudioManager() { |
| 2819 | if (mView == null) { |
| 2820 | throw new IllegalStateException("getAudioManager called when there is no mView"); |
| 2821 | } |
| 2822 | if (mAudioManager == null) { |
| 2823 | mAudioManager = (AudioManager) mView.getContext().getSystemService(Context.AUDIO_SERVICE); |
| 2824 | } |
| 2825 | return mAudioManager; |
| 2826 | } |
| 2827 | |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 2828 | private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility, |
| 2829 | boolean insetsPending) throws RemoteException { |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 2830 | |
| 2831 | float appScale = mAttachInfo.mApplicationScale; |
Mitsuru Oshima | 3d91492 | 2009-05-13 22:29:15 -0700 | [diff] [blame] | 2832 | boolean restore = false; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 2833 | if (params != null && mTranslator != null) { |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 2834 | restore = true; |
| 2835 | params.backup(); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 2836 | mTranslator.translateWindowLayout(params); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 2837 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 2838 | if (params != null) { |
| 2839 | if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params); |
Mitsuru Oshima | 3d91492 | 2009-05-13 22:29:15 -0700 | [diff] [blame] | 2840 | } |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 2841 | mPendingConfiguration.seq = 0; |
Dianne Hackborn | f123e49 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 2842 | //Log.d(TAG, ">>>>>> CALLING relayout"); |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 2843 | int relayoutResult = sWindowSession.relayout( |
| 2844 | mWindow, params, |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 2845 | (int) (mView.getMeasuredWidth() * appScale + 0.5f), |
| 2846 | (int) (mView.getMeasuredHeight() * appScale + 0.5f), |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 2847 | viewVisibility, insetsPending, mWinFrame, |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 2848 | mPendingContentInsets, mPendingVisibleInsets, |
| 2849 | mPendingConfiguration, mSurface); |
Dianne Hackborn | f123e49 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 2850 | //Log.d(TAG, "<<<<<< BACK FROM relayout"); |
Mitsuru Oshima | 3d91492 | 2009-05-13 22:29:15 -0700 | [diff] [blame] | 2851 | if (restore) { |
Mitsuru Oshima | e5fb328 | 2009-06-09 21:16:08 -0700 | [diff] [blame] | 2852 | params.restore(); |
Mitsuru Oshima | 3d91492 | 2009-05-13 22:29:15 -0700 | [diff] [blame] | 2853 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 2854 | |
| 2855 | if (mTranslator != null) { |
| 2856 | mTranslator.translateRectInScreenToAppWinFrame(mWinFrame); |
| 2857 | mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets); |
| 2858 | mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets); |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 2859 | } |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 2860 | return relayoutResult; |
| 2861 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2862 | |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 2863 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2864 | * {@inheritDoc} |
| 2865 | */ |
| 2866 | public void playSoundEffect(int effectId) { |
| 2867 | checkThread(); |
| 2868 | |
Jean-Michel Trivi | 13b18fd | 2010-05-05 09:18:15 -0700 | [diff] [blame] | 2869 | try { |
| 2870 | final AudioManager audioManager = getAudioManager(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2871 | |
Jean-Michel Trivi | 13b18fd | 2010-05-05 09:18:15 -0700 | [diff] [blame] | 2872 | switch (effectId) { |
| 2873 | case SoundEffectConstants.CLICK: |
| 2874 | audioManager.playSoundEffect(AudioManager.FX_KEY_CLICK); |
| 2875 | return; |
| 2876 | case SoundEffectConstants.NAVIGATION_DOWN: |
| 2877 | audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_DOWN); |
| 2878 | return; |
| 2879 | case SoundEffectConstants.NAVIGATION_LEFT: |
| 2880 | audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_LEFT); |
| 2881 | return; |
| 2882 | case SoundEffectConstants.NAVIGATION_RIGHT: |
| 2883 | audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_RIGHT); |
| 2884 | return; |
| 2885 | case SoundEffectConstants.NAVIGATION_UP: |
| 2886 | audioManager.playSoundEffect(AudioManager.FX_FOCUS_NAVIGATION_UP); |
| 2887 | return; |
| 2888 | default: |
| 2889 | throw new IllegalArgumentException("unknown effect id " + effectId + |
| 2890 | " not defined in " + SoundEffectConstants.class.getCanonicalName()); |
| 2891 | } |
| 2892 | } catch (IllegalStateException e) { |
| 2893 | // Exception thrown by getAudioManager() when mView is null |
| 2894 | Log.e(TAG, "FATAL EXCEPTION when attempting to play sound effect: " + e); |
| 2895 | e.printStackTrace(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | /** |
| 2900 | * {@inheritDoc} |
| 2901 | */ |
| 2902 | public boolean performHapticFeedback(int effectId, boolean always) { |
| 2903 | try { |
| 2904 | return sWindowSession.performHapticFeedback(mWindow, effectId, always); |
| 2905 | } catch (RemoteException e) { |
| 2906 | return false; |
| 2907 | } |
| 2908 | } |
| 2909 | |
| 2910 | /** |
| 2911 | * {@inheritDoc} |
| 2912 | */ |
| 2913 | public View focusSearch(View focused, int direction) { |
| 2914 | checkThread(); |
| 2915 | if (!(mView instanceof ViewGroup)) { |
| 2916 | return null; |
| 2917 | } |
| 2918 | return FocusFinder.getInstance().findNextFocus((ViewGroup) mView, focused, direction); |
| 2919 | } |
| 2920 | |
| 2921 | public void debug() { |
| 2922 | mView.debug(); |
| 2923 | } |
| 2924 | |
| 2925 | public void die(boolean immediate) { |
Dianne Hackborn | 94d6914 | 2009-09-28 22:14:42 -0700 | [diff] [blame] | 2926 | if (immediate) { |
| 2927 | doDie(); |
| 2928 | } else { |
| 2929 | sendEmptyMessage(DIE); |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | void doDie() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2934 | checkThread(); |
Jeff Brown | b75fa30 | 2010-07-15 23:47:29 -0700 | [diff] [blame] | 2935 | if (LOCAL_LOGV) Log.v(TAG, "DIE in " + this + " of " + mSurface); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2936 | synchronized (this) { |
| 2937 | if (mAdded && !mFirst) { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 2938 | destroyHardwareRenderer(); |
| 2939 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2940 | int viewVisibility = mView.getVisibility(); |
| 2941 | boolean viewVisibilityChanged = mViewVisibility != viewVisibility; |
| 2942 | if (mWindowAttributesChanged || viewVisibilityChanged) { |
| 2943 | // If layout params have been changed, first give them |
| 2944 | // to the window manager to make sure it has the correct |
| 2945 | // animation info. |
| 2946 | try { |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 2947 | if ((relayoutWindow(mWindowAttributes, viewVisibility, false) |
| 2948 | & WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2949 | sWindowSession.finishDrawing(mWindow); |
| 2950 | } |
| 2951 | } catch (RemoteException e) { |
| 2952 | } |
| 2953 | } |
| 2954 | |
Dianne Hackborn | 0586a1b | 2009-09-06 21:08:27 -0700 | [diff] [blame] | 2955 | mSurface.release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2956 | } |
| 2957 | if (mAdded) { |
| 2958 | mAdded = false; |
Dianne Hackborn | 94d6914 | 2009-09-28 22:14:42 -0700 | [diff] [blame] | 2959 | dispatchDetachedFromWindow(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2960 | } |
| 2961 | } |
| 2962 | } |
| 2963 | |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 2964 | private void destroyHardwareRenderer() { |
Romain Guy | b051e89 | 2010-09-28 19:09:36 -0700 | [diff] [blame] | 2965 | if (mAttachInfo.mHardwareRenderer != null) { |
| 2966 | mAttachInfo.mHardwareRenderer.destroy(true); |
| 2967 | mAttachInfo.mHardwareRenderer = null; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 2968 | mAttachInfo.mHardwareAccelerated = false; |
| 2969 | } |
| 2970 | } |
| 2971 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2972 | public void dispatchFinishedEvent(int seq, boolean handled) { |
| 2973 | Message msg = obtainMessage(FINISHED_EVENT); |
| 2974 | msg.arg1 = seq; |
| 2975 | msg.arg2 = handled ? 1 : 0; |
| 2976 | sendMessage(msg); |
| 2977 | } |
Mitsuru Oshima | 3d91492 | 2009-05-13 22:29:15 -0700 | [diff] [blame] | 2978 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2979 | public void dispatchResized(int w, int h, Rect coveredInsets, |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 2980 | Rect visibleInsets, boolean reportDraw, Configuration newConfig) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2981 | if (DEBUG_LAYOUT) Log.v(TAG, "Resizing " + this + ": w=" + w |
| 2982 | + " h=" + h + " coveredInsets=" + coveredInsets.toShortString() |
| 2983 | + " visibleInsets=" + visibleInsets.toShortString() |
| 2984 | + " reportDraw=" + reportDraw); |
| 2985 | Message msg = obtainMessage(reportDraw ? RESIZED_REPORT :RESIZED); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 2986 | if (mTranslator != null) { |
| 2987 | mTranslator.translateRectInScreenToAppWindow(coveredInsets); |
| 2988 | mTranslator.translateRectInScreenToAppWindow(visibleInsets); |
| 2989 | w *= mTranslator.applicationInvertedScale; |
| 2990 | h *= mTranslator.applicationInvertedScale; |
Mitsuru Oshima | 9189cab | 2009-06-03 11:19:12 -0700 | [diff] [blame] | 2991 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 2992 | msg.arg1 = w; |
| 2993 | msg.arg2 = h; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 2994 | ResizedInfo ri = new ResizedInfo(); |
| 2995 | ri.coveredInsets = new Rect(coveredInsets); |
| 2996 | ri.visibleInsets = new Rect(visibleInsets); |
| 2997 | ri.newConfig = newConfig; |
| 2998 | msg.obj = ri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2999 | sendMessage(msg); |
| 3000 | } |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3001 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 3002 | private InputQueue.FinishedCallback mFinishedCallback; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3003 | |
| 3004 | private final InputHandler mInputHandler = new InputHandler() { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 3005 | public void handleKey(KeyEvent event, InputQueue.FinishedCallback finishedCallback) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3006 | startInputEvent(finishedCallback); |
Jeff Brown | 92ff1dd | 2010-08-11 16:16:06 -0700 | [diff] [blame] | 3007 | dispatchKey(event, true); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3008 | } |
| 3009 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 3010 | public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3011 | startInputEvent(finishedCallback); |
| 3012 | dispatchMotion(event, true); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 3013 | } |
| 3014 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3015 | |
| 3016 | public void dispatchKey(KeyEvent event) { |
Jeff Brown | 92ff1dd | 2010-08-11 16:16:06 -0700 | [diff] [blame] | 3017 | dispatchKey(event, false); |
| 3018 | } |
| 3019 | |
| 3020 | private void dispatchKey(KeyEvent event, boolean sendDone) { |
| 3021 | //noinspection ConstantConditions |
| 3022 | if (false && event.getAction() == KeyEvent.ACTION_DOWN) { |
| 3023 | if (event.getKeyCode() == KeyEvent.KEYCODE_CAMERA) { |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 3024 | if (DBG) Log.d("keydisp", "==================================================="); |
| 3025 | if (DBG) Log.d("keydisp", "Focused view Hierarchy is:"); |
| 3026 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3027 | debug(); |
| 3028 | |
Romain Guy | 812ccbe | 2010-06-01 14:07:24 -0700 | [diff] [blame] | 3029 | if (DBG) Log.d("keydisp", "==================================================="); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | Message msg = obtainMessage(DISPATCH_KEY); |
| 3034 | msg.obj = event; |
Jeff Brown | 92ff1dd | 2010-08-11 16:16:06 -0700 | [diff] [blame] | 3035 | msg.arg1 = sendDone ? 1 : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3036 | |
| 3037 | if (LOCAL_LOGV) Log.v( |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 3038 | TAG, "sending key " + event + " to " + mView); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3039 | |
| 3040 | sendMessageAtTime(msg, event.getEventTime()); |
| 3041 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 3042 | |
| 3043 | public void dispatchMotion(MotionEvent event) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3044 | dispatchMotion(event, false); |
| 3045 | } |
| 3046 | |
| 3047 | private void dispatchMotion(MotionEvent event, boolean sendDone) { |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 3048 | int source = event.getSource(); |
| 3049 | if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3050 | dispatchPointer(event, sendDone); |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 3051 | } else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3052 | dispatchTrackball(event, sendDone); |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 3053 | } else { |
| 3054 | // TODO |
| 3055 | Log.v(TAG, "Dropping unsupported motion event (unimplemented): " + event); |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3056 | if (sendDone) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 3057 | finishInputEvent(false); |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3058 | } |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 3059 | } |
| 3060 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3061 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 3062 | public void dispatchPointer(MotionEvent event) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3063 | dispatchPointer(event, false); |
| 3064 | } |
| 3065 | |
| 3066 | private void dispatchPointer(MotionEvent event, boolean sendDone) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3067 | Message msg = obtainMessage(DISPATCH_POINTER); |
| 3068 | msg.obj = event; |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3069 | msg.arg1 = sendDone ? 1 : 0; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 3070 | sendMessageAtTime(msg, event.getEventTime()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3071 | } |
| 3072 | |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 3073 | public void dispatchTrackball(MotionEvent event) { |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3074 | dispatchTrackball(event, false); |
| 3075 | } |
| 3076 | |
| 3077 | private void dispatchTrackball(MotionEvent event, boolean sendDone) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3078 | Message msg = obtainMessage(DISPATCH_TRACKBALL); |
| 3079 | msg.obj = event; |
Jeff Brown | 93ed4e3 | 2010-09-23 13:51:48 -0700 | [diff] [blame] | 3080 | msg.arg1 = sendDone ? 1 : 0; |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 3081 | sendMessageAtTime(msg, event.getEventTime()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3082 | } |
Jeff Brown | 00fa7bd | 2010-07-02 15:37:36 -0700 | [diff] [blame] | 3083 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3084 | public void dispatchAppVisibility(boolean visible) { |
| 3085 | Message msg = obtainMessage(DISPATCH_APP_VISIBILITY); |
| 3086 | msg.arg1 = visible ? 1 : 0; |
| 3087 | sendMessage(msg); |
| 3088 | } |
| 3089 | |
| 3090 | public void dispatchGetNewSurface() { |
| 3091 | Message msg = obtainMessage(DISPATCH_GET_NEW_SURFACE); |
| 3092 | sendMessage(msg); |
| 3093 | } |
| 3094 | |
| 3095 | public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) { |
| 3096 | Message msg = Message.obtain(); |
| 3097 | msg.what = WINDOW_FOCUS_CHANGED; |
| 3098 | msg.arg1 = hasFocus ? 1 : 0; |
| 3099 | msg.arg2 = inTouchMode ? 1 : 0; |
| 3100 | sendMessage(msg); |
| 3101 | } |
| 3102 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3103 | public void dispatchCloseSystemDialogs(String reason) { |
| 3104 | Message msg = Message.obtain(); |
| 3105 | msg.what = CLOSE_SYSTEM_DIALOGS; |
| 3106 | msg.obj = reason; |
| 3107 | sendMessage(msg); |
| 3108 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 3109 | |
| 3110 | public void dispatchDragEvent(DragEvent event) { |
Chris Tate | 91e9bb3 | 2010-10-12 12:58:43 -0700 | [diff] [blame] | 3111 | final int what; |
| 3112 | if (event.getAction() == DragEvent.ACTION_DRAG_LOCATION) { |
| 3113 | what = DISPATCH_DRAG_LOCATION_EVENT; |
| 3114 | removeMessages(what); |
| 3115 | } else { |
| 3116 | what = DISPATCH_DRAG_EVENT; |
| 3117 | } |
Christopher Tate | 407b4e9 | 2010-11-30 17:14:08 -0800 | [diff] [blame] | 3118 | event.mLocalState = mLocalDragState; // only present when this app called startDrag() |
Chris Tate | 91e9bb3 | 2010-10-12 12:58:43 -0700 | [diff] [blame] | 3119 | Message msg = obtainMessage(what, event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 3120 | sendMessage(msg); |
| 3121 | } |
| 3122 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 3123 | /** |
| 3124 | * The window is getting focus so if there is anything focused/selected |
| 3125 | * send an {@link AccessibilityEvent} to announce that. |
| 3126 | */ |
| 3127 | private void sendAccessibilityEvents() { |
| 3128 | if (!AccessibilityManager.getInstance(mView.getContext()).isEnabled()) { |
| 3129 | return; |
| 3130 | } |
| 3131 | mView.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED); |
| 3132 | View focusedView = mView.findFocus(); |
| 3133 | if (focusedView != null && focusedView != mView) { |
| 3134 | focusedView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED); |
| 3135 | } |
| 3136 | } |
| 3137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3138 | public boolean showContextMenuForChild(View originalView) { |
| 3139 | return false; |
| 3140 | } |
| 3141 | |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 3142 | public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { |
| 3143 | return null; |
| 3144 | } |
| 3145 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3146 | public void createContextMenu(ContextMenu menu) { |
| 3147 | } |
| 3148 | |
| 3149 | public void childDrawableStateChanged(View child) { |
| 3150 | } |
| 3151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3152 | void checkThread() { |
| 3153 | if (mThread != Thread.currentThread()) { |
| 3154 | throw new CalledFromWrongThreadException( |
| 3155 | "Only the original thread that created a view hierarchy can touch its views."); |
| 3156 | } |
| 3157 | } |
| 3158 | |
| 3159 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 3160 | // ViewRoot never intercepts touch event, so this can be a no-op |
| 3161 | } |
| 3162 | |
| 3163 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, |
| 3164 | boolean immediate) { |
| 3165 | return scrollToRectOrFocus(rectangle, immediate); |
| 3166 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3167 | |
Dianne Hackborn | dc8a7f6 | 2010-05-10 11:29:34 -0700 | [diff] [blame] | 3168 | class TakenSurfaceHolder extends BaseSurfaceHolder { |
| 3169 | @Override |
| 3170 | public boolean onAllowLockCanvas() { |
| 3171 | return mDrawingAllowed; |
| 3172 | } |
| 3173 | |
| 3174 | @Override |
| 3175 | public void onRelayoutContainer() { |
| 3176 | // Not currently interesting -- from changing between fixed and layout size. |
| 3177 | } |
| 3178 | |
| 3179 | public void setFormat(int format) { |
| 3180 | ((RootViewSurfaceTaker)mView).setSurfaceFormat(format); |
| 3181 | } |
| 3182 | |
| 3183 | public void setType(int type) { |
| 3184 | ((RootViewSurfaceTaker)mView).setSurfaceType(type); |
| 3185 | } |
| 3186 | |
| 3187 | @Override |
| 3188 | public void onUpdateSurface() { |
| 3189 | // We take care of format and type changes on our own. |
| 3190 | throw new IllegalStateException("Shouldn't be here"); |
| 3191 | } |
| 3192 | |
| 3193 | public boolean isCreating() { |
| 3194 | return mIsCreating; |
| 3195 | } |
| 3196 | |
| 3197 | @Override |
| 3198 | public void setFixedSize(int width, int height) { |
| 3199 | throw new UnsupportedOperationException( |
| 3200 | "Currently only support sizing from layout"); |
| 3201 | } |
| 3202 | |
| 3203 | public void setKeepScreenOn(boolean screenOn) { |
| 3204 | ((RootViewSurfaceTaker)mView).setSurfaceKeepScreenOn(screenOn); |
| 3205 | } |
| 3206 | } |
| 3207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3208 | static class InputMethodCallback extends IInputMethodCallback.Stub { |
| 3209 | private WeakReference<ViewRoot> mViewRoot; |
| 3210 | |
| 3211 | public InputMethodCallback(ViewRoot viewRoot) { |
| 3212 | mViewRoot = new WeakReference<ViewRoot>(viewRoot); |
| 3213 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3215 | public void finishedEvent(int seq, boolean handled) { |
| 3216 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3217 | if (viewRoot != null) { |
| 3218 | viewRoot.dispatchFinishedEvent(seq, handled); |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | public void sessionCreated(IInputMethodSession session) throws RemoteException { |
| 3223 | // Stub -- not for use in the client. |
| 3224 | } |
| 3225 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3227 | static class W extends IWindow.Stub { |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 3228 | private final WeakReference<ViewRoot> mViewRoot; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3229 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3230 | W(ViewRoot viewRoot) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3231 | mViewRoot = new WeakReference<ViewRoot>(viewRoot); |
| 3232 | } |
| 3233 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3234 | public void resized(int w, int h, Rect coveredInsets, Rect visibleInsets, |
| 3235 | boolean reportDraw, Configuration newConfig) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3236 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3237 | if (viewRoot != null) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3238 | viewRoot.dispatchResized(w, h, coveredInsets, visibleInsets, reportDraw, newConfig); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3239 | } |
| 3240 | } |
| 3241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3242 | public void dispatchAppVisibility(boolean visible) { |
| 3243 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3244 | if (viewRoot != null) { |
| 3245 | viewRoot.dispatchAppVisibility(visible); |
| 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | public void dispatchGetNewSurface() { |
| 3250 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3251 | if (viewRoot != null) { |
| 3252 | viewRoot.dispatchGetNewSurface(); |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | public void windowFocusChanged(boolean hasFocus, boolean inTouchMode) { |
| 3257 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3258 | if (viewRoot != null) { |
| 3259 | viewRoot.windowFocusChanged(hasFocus, inTouchMode); |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | private static int checkCallingPermission(String permission) { |
| 3264 | if (!Process.supportsProcesses()) { |
| 3265 | return PackageManager.PERMISSION_GRANTED; |
| 3266 | } |
| 3267 | |
| 3268 | try { |
| 3269 | return ActivityManagerNative.getDefault().checkPermission( |
| 3270 | permission, Binder.getCallingPid(), Binder.getCallingUid()); |
| 3271 | } catch (RemoteException e) { |
| 3272 | return PackageManager.PERMISSION_DENIED; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | public void executeCommand(String command, String parameters, ParcelFileDescriptor out) { |
| 3277 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3278 | if (viewRoot != null) { |
| 3279 | final View view = viewRoot.mView; |
| 3280 | if (view != null) { |
| 3281 | if (checkCallingPermission(Manifest.permission.DUMP) != |
| 3282 | PackageManager.PERMISSION_GRANTED) { |
| 3283 | throw new SecurityException("Insufficient permissions to invoke" |
| 3284 | + " executeCommand() from pid=" + Binder.getCallingPid() |
| 3285 | + ", uid=" + Binder.getCallingUid()); |
| 3286 | } |
| 3287 | |
| 3288 | OutputStream clientStream = null; |
| 3289 | try { |
| 3290 | clientStream = new ParcelFileDescriptor.AutoCloseOutputStream(out); |
| 3291 | ViewDebug.dispatchCommand(view, command, parameters, clientStream); |
| 3292 | } catch (IOException e) { |
| 3293 | e.printStackTrace(); |
| 3294 | } finally { |
| 3295 | if (clientStream != null) { |
| 3296 | try { |
| 3297 | clientStream.close(); |
| 3298 | } catch (IOException e) { |
| 3299 | e.printStackTrace(); |
| 3300 | } |
| 3301 | } |
| 3302 | } |
| 3303 | } |
| 3304 | } |
| 3305 | } |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 3306 | |
Dianne Hackborn | ffa4248 | 2009-09-23 22:20:11 -0700 | [diff] [blame] | 3307 | public void closeSystemDialogs(String reason) { |
| 3308 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3309 | if (viewRoot != null) { |
| 3310 | viewRoot.dispatchCloseSystemDialogs(reason); |
| 3311 | } |
| 3312 | } |
| 3313 | |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 3314 | public void dispatchWallpaperOffsets(float x, float y, float xStep, float yStep, |
| 3315 | boolean sync) { |
Dianne Hackborn | 19382ac | 2009-09-11 21:13:37 -0700 | [diff] [blame] | 3316 | if (sync) { |
| 3317 | try { |
| 3318 | sWindowSession.wallpaperOffsetsComplete(asBinder()); |
| 3319 | } catch (RemoteException e) { |
| 3320 | } |
| 3321 | } |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 3322 | } |
Dianne Hackborn | 7580493 | 2009-10-20 20:15:20 -0700 | [diff] [blame] | 3323 | |
| 3324 | public void dispatchWallpaperCommand(String action, int x, int y, |
| 3325 | int z, Bundle extras, boolean sync) { |
| 3326 | if (sync) { |
| 3327 | try { |
| 3328 | sWindowSession.wallpaperCommandComplete(asBinder(), null); |
| 3329 | } catch (RemoteException e) { |
| 3330 | } |
| 3331 | } |
| 3332 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 3333 | |
| 3334 | /* Drag/drop */ |
| 3335 | public void dispatchDragEvent(DragEvent event) { |
| 3336 | final ViewRoot viewRoot = mViewRoot.get(); |
| 3337 | if (viewRoot != null) { |
| 3338 | viewRoot.dispatchDragEvent(event); |
| 3339 | } |
| 3340 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | /** |
| 3344 | * Maintains state information for a single trackball axis, generating |
| 3345 | * discrete (DPAD) movements based on raw trackball motion. |
| 3346 | */ |
| 3347 | static final class TrackballAxis { |
| 3348 | /** |
| 3349 | * The maximum amount of acceleration we will apply. |
| 3350 | */ |
| 3351 | static final float MAX_ACCELERATION = 20; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3353 | /** |
| 3354 | * The maximum amount of time (in milliseconds) between events in order |
| 3355 | * for us to consider the user to be doing fast trackball movements, |
| 3356 | * and thus apply an acceleration. |
| 3357 | */ |
| 3358 | static final long FAST_MOVE_TIME = 150; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3360 | /** |
| 3361 | * Scaling factor to the time (in milliseconds) between events to how |
| 3362 | * much to multiple/divide the current acceleration. When movement |
| 3363 | * is < FAST_MOVE_TIME this multiplies the acceleration; when > |
| 3364 | * FAST_MOVE_TIME it divides it. |
| 3365 | */ |
| 3366 | static final float ACCEL_MOVE_SCALING_FACTOR = (1.0f/40); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3367 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3368 | float position; |
| 3369 | float absPosition; |
| 3370 | float acceleration = 1; |
| 3371 | long lastMoveTime = 0; |
| 3372 | int step; |
| 3373 | int dir; |
| 3374 | int nonAccelMovement; |
| 3375 | |
| 3376 | void reset(int _step) { |
| 3377 | position = 0; |
| 3378 | acceleration = 1; |
| 3379 | lastMoveTime = 0; |
| 3380 | step = _step; |
| 3381 | dir = 0; |
| 3382 | } |
| 3383 | |
| 3384 | /** |
| 3385 | * Add trackball movement into the state. If the direction of movement |
| 3386 | * has been reversed, the state is reset before adding the |
| 3387 | * movement (so that you don't have to compensate for any previously |
| 3388 | * collected movement before see the result of the movement in the |
| 3389 | * new direction). |
| 3390 | * |
| 3391 | * @return Returns the absolute value of the amount of movement |
| 3392 | * collected so far. |
| 3393 | */ |
| 3394 | float collect(float off, long time, String axis) { |
| 3395 | long normTime; |
| 3396 | if (off > 0) { |
| 3397 | normTime = (long)(off * FAST_MOVE_TIME); |
| 3398 | if (dir < 0) { |
| 3399 | if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to positive!"); |
| 3400 | position = 0; |
| 3401 | step = 0; |
| 3402 | acceleration = 1; |
| 3403 | lastMoveTime = 0; |
| 3404 | } |
| 3405 | dir = 1; |
| 3406 | } else if (off < 0) { |
| 3407 | normTime = (long)((-off) * FAST_MOVE_TIME); |
| 3408 | if (dir > 0) { |
| 3409 | if (DEBUG_TRACKBALL) Log.v(TAG, axis + " reversed to negative!"); |
| 3410 | position = 0; |
| 3411 | step = 0; |
| 3412 | acceleration = 1; |
| 3413 | lastMoveTime = 0; |
| 3414 | } |
| 3415 | dir = -1; |
| 3416 | } else { |
| 3417 | normTime = 0; |
| 3418 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3419 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3420 | // The number of milliseconds between each movement that is |
| 3421 | // considered "normal" and will not result in any acceleration |
| 3422 | // or deceleration, scaled by the offset we have here. |
| 3423 | if (normTime > 0) { |
| 3424 | long delta = time - lastMoveTime; |
| 3425 | lastMoveTime = time; |
| 3426 | float acc = acceleration; |
| 3427 | if (delta < normTime) { |
| 3428 | // The user is scrolling rapidly, so increase acceleration. |
| 3429 | float scale = (normTime-delta) * ACCEL_MOVE_SCALING_FACTOR; |
| 3430 | if (scale > 1) acc *= scale; |
| 3431 | if (DEBUG_TRACKBALL) Log.v(TAG, axis + " accelerate: off=" |
| 3432 | + off + " normTime=" + normTime + " delta=" + delta |
| 3433 | + " scale=" + scale + " acc=" + acc); |
| 3434 | acceleration = acc < MAX_ACCELERATION ? acc : MAX_ACCELERATION; |
| 3435 | } else { |
| 3436 | // The user is scrolling slowly, so decrease acceleration. |
| 3437 | float scale = (delta-normTime) * ACCEL_MOVE_SCALING_FACTOR; |
| 3438 | if (scale > 1) acc /= scale; |
| 3439 | if (DEBUG_TRACKBALL) Log.v(TAG, axis + " deccelerate: off=" |
| 3440 | + off + " normTime=" + normTime + " delta=" + delta |
| 3441 | + " scale=" + scale + " acc=" + acc); |
| 3442 | acceleration = acc > 1 ? acc : 1; |
| 3443 | } |
| 3444 | } |
| 3445 | position += off; |
| 3446 | return (absPosition = Math.abs(position)); |
| 3447 | } |
| 3448 | |
| 3449 | /** |
| 3450 | * Generate the number of discrete movement events appropriate for |
| 3451 | * the currently collected trackball movement. |
| 3452 | * |
| 3453 | * @param precision The minimum movement required to generate the |
| 3454 | * first discrete movement. |
| 3455 | * |
| 3456 | * @return Returns the number of discrete movements, either positive |
| 3457 | * or negative, or 0 if there is not enough trackball movement yet |
| 3458 | * for a discrete movement. |
| 3459 | */ |
| 3460 | int generate(float precision) { |
| 3461 | int movement = 0; |
| 3462 | nonAccelMovement = 0; |
| 3463 | do { |
| 3464 | final int dir = position >= 0 ? 1 : -1; |
| 3465 | switch (step) { |
| 3466 | // If we are going to execute the first step, then we want |
| 3467 | // to do this as soon as possible instead of waiting for |
| 3468 | // a full movement, in order to make things look responsive. |
| 3469 | case 0: |
| 3470 | if (absPosition < precision) { |
| 3471 | return movement; |
| 3472 | } |
| 3473 | movement += dir; |
| 3474 | nonAccelMovement += dir; |
| 3475 | step = 1; |
| 3476 | break; |
| 3477 | // If we have generated the first movement, then we need |
| 3478 | // to wait for the second complete trackball motion before |
| 3479 | // generating the second discrete movement. |
| 3480 | case 1: |
| 3481 | if (absPosition < 2) { |
| 3482 | return movement; |
| 3483 | } |
| 3484 | movement += dir; |
| 3485 | nonAccelMovement += dir; |
| 3486 | position += dir > 0 ? -2 : 2; |
| 3487 | absPosition = Math.abs(position); |
| 3488 | step = 2; |
| 3489 | break; |
| 3490 | // After the first two, we generate discrete movements |
| 3491 | // consistently with the trackball, applying an acceleration |
| 3492 | // if the trackball is moving quickly. This is a simple |
| 3493 | // acceleration on top of what we already compute based |
| 3494 | // on how quickly the wheel is being turned, to apply |
| 3495 | // a longer increasing acceleration to continuous movement |
| 3496 | // in one direction. |
| 3497 | default: |
| 3498 | if (absPosition < 1) { |
| 3499 | return movement; |
| 3500 | } |
| 3501 | movement += dir; |
| 3502 | position += dir >= 0 ? -1 : 1; |
| 3503 | absPosition = Math.abs(position); |
| 3504 | float acc = acceleration; |
| 3505 | acc *= 1.1f; |
| 3506 | acceleration = acc < MAX_ACCELERATION ? acc : acceleration; |
| 3507 | break; |
| 3508 | } |
| 3509 | } while (true); |
| 3510 | } |
| 3511 | } |
| 3512 | |
| 3513 | public static final class CalledFromWrongThreadException extends AndroidRuntimeException { |
| 3514 | public CalledFromWrongThreadException(String msg) { |
| 3515 | super(msg); |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | private SurfaceHolder mHolder = new SurfaceHolder() { |
| 3520 | // we only need a SurfaceHolder for opengl. it would be nice |
| 3521 | // to implement everything else though, especially the callback |
| 3522 | // support (opengl doesn't make use of it right now, but eventually |
| 3523 | // will). |
| 3524 | public Surface getSurface() { |
| 3525 | return mSurface; |
| 3526 | } |
| 3527 | |
| 3528 | public boolean isCreating() { |
| 3529 | return false; |
| 3530 | } |
| 3531 | |
| 3532 | public void addCallback(Callback callback) { |
| 3533 | } |
| 3534 | |
| 3535 | public void removeCallback(Callback callback) { |
| 3536 | } |
| 3537 | |
| 3538 | public void setFixedSize(int width, int height) { |
| 3539 | } |
| 3540 | |
| 3541 | public void setSizeFromLayout() { |
| 3542 | } |
| 3543 | |
| 3544 | public void setFormat(int format) { |
| 3545 | } |
| 3546 | |
| 3547 | public void setType(int type) { |
| 3548 | } |
| 3549 | |
| 3550 | public void setKeepScreenOn(boolean screenOn) { |
| 3551 | } |
| 3552 | |
| 3553 | public Canvas lockCanvas() { |
| 3554 | return null; |
| 3555 | } |
| 3556 | |
| 3557 | public Canvas lockCanvas(Rect dirty) { |
| 3558 | return null; |
| 3559 | } |
| 3560 | |
| 3561 | public void unlockCanvasAndPost(Canvas canvas) { |
| 3562 | } |
| 3563 | public Rect getSurfaceFrame() { |
| 3564 | return null; |
| 3565 | } |
| 3566 | }; |
| 3567 | |
| 3568 | static RunQueue getRunQueue() { |
| 3569 | RunQueue rq = sRunQueues.get(); |
| 3570 | if (rq != null) { |
| 3571 | return rq; |
| 3572 | } |
| 3573 | rq = new RunQueue(); |
| 3574 | sRunQueues.set(rq); |
| 3575 | return rq; |
| 3576 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3577 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3578 | /** |
| 3579 | * @hide |
| 3580 | */ |
| 3581 | static final class RunQueue { |
| 3582 | private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>(); |
| 3583 | |
| 3584 | void post(Runnable action) { |
| 3585 | postDelayed(action, 0); |
| 3586 | } |
| 3587 | |
| 3588 | void postDelayed(Runnable action, long delayMillis) { |
| 3589 | HandlerAction handlerAction = new HandlerAction(); |
| 3590 | handlerAction.action = action; |
| 3591 | handlerAction.delay = delayMillis; |
| 3592 | |
| 3593 | synchronized (mActions) { |
| 3594 | mActions.add(handlerAction); |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | void removeCallbacks(Runnable action) { |
| 3599 | final HandlerAction handlerAction = new HandlerAction(); |
| 3600 | handlerAction.action = action; |
| 3601 | |
| 3602 | synchronized (mActions) { |
| 3603 | final ArrayList<HandlerAction> actions = mActions; |
| 3604 | |
| 3605 | while (actions.remove(handlerAction)) { |
| 3606 | // Keep going |
| 3607 | } |
| 3608 | } |
| 3609 | } |
| 3610 | |
| 3611 | void executeActions(Handler handler) { |
| 3612 | synchronized (mActions) { |
| 3613 | final ArrayList<HandlerAction> actions = mActions; |
| 3614 | final int count = actions.size(); |
| 3615 | |
| 3616 | for (int i = 0; i < count; i++) { |
| 3617 | final HandlerAction handlerAction = actions.get(i); |
| 3618 | handler.postDelayed(handlerAction.action, handlerAction.delay); |
| 3619 | } |
| 3620 | |
Romain Guy | 15df670 | 2009-08-17 20:17:30 -0700 | [diff] [blame] | 3621 | actions.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3622 | } |
| 3623 | } |
| 3624 | |
| 3625 | private static class HandlerAction { |
| 3626 | Runnable action; |
| 3627 | long delay; |
| 3628 | |
| 3629 | @Override |
| 3630 | public boolean equals(Object o) { |
| 3631 | if (this == o) return true; |
| 3632 | if (o == null || getClass() != o.getClass()) return false; |
| 3633 | |
| 3634 | HandlerAction that = (HandlerAction) o; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3635 | return !(action != null ? !action.equals(that.action) : that.action != null); |
| 3636 | |
| 3637 | } |
| 3638 | |
| 3639 | @Override |
| 3640 | public int hashCode() { |
| 3641 | int result = action != null ? action.hashCode() : 0; |
| 3642 | result = 31 * result + (int) (delay ^ (delay >>> 32)); |
| 3643 | return result; |
| 3644 | } |
| 3645 | } |
| 3646 | } |
| 3647 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3648 | private static native void nativeShowFPS(Canvas canvas, int durationMillis); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3649 | } |