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