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