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