| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser.input; | 5 package org.chromium.android_webview; |
| 6 | 6 |
| 7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.graphics.Canvas; | 9 import android.graphics.Canvas; |
| 10 import android.graphics.drawable.Drawable; | 10 import android.graphics.drawable.Drawable; |
| 11 import android.os.Build; | 11 import android.os.Build; |
| 12 import android.os.SystemClock; | 12 import android.os.SystemClock; |
| 13 import android.view.MotionEvent; | 13 import android.view.MotionEvent; |
| 14 import android.view.View; | 14 import android.view.View; |
| 15 import android.view.ViewGroup; | 15 import android.view.ViewGroup; |
| 16 import android.view.WindowManager; | 16 import android.view.WindowManager; |
| 17 import android.view.animation.AnimationUtils; | 17 import android.view.animation.AnimationUtils; |
| 18 import android.widget.PopupWindow; | 18 import android.widget.PopupWindow; |
| 19 | 19 |
| 20 import org.chromium.base.ObserverList; |
| 20 import org.chromium.base.annotations.CalledByNative; | 21 import org.chromium.base.annotations.CalledByNative; |
| 21 import org.chromium.base.annotations.JNINamespace; | 22 import org.chromium.base.annotations.JNINamespace; |
| 22 import org.chromium.content.browser.ContainerViewObserver; | |
| 23 import org.chromium.content.browser.ContentViewCore; | 23 import org.chromium.content.browser.ContentViewCore; |
| 24 import org.chromium.content.browser.PositionObserver; | 24 import org.chromium.content.browser.PositionObserver; |
| 25 import org.chromium.content.browser.ViewPositionObserver; | 25 import org.chromium.content.browser.ViewPositionObserver; |
| 26 import org.chromium.content.browser.input.HandleViewResources; |
| 26 import org.chromium.content_public.browser.GestureStateListener; | 27 import org.chromium.content_public.browser.GestureStateListener; |
| 27 import org.chromium.ui.touch_selection.TouchHandleOrientation; | 28 import org.chromium.ui.touch_selection.TouchHandleOrientation; |
| 28 | 29 |
| 29 import java.lang.reflect.InvocationTargetException; | 30 import java.lang.reflect.InvocationTargetException; |
| 30 import java.lang.reflect.Method; | 31 import java.lang.reflect.Method; |
| 31 | 32 |
| 32 /** | 33 /** |
| 33 * View that displays a selection or insertion handle for text editing. | 34 * View that displays a selection or insertion handle for text editing. |
| 34 * | 35 * |
| 35 * While a HandleView is logically a child of some other view, it does not exist
in that View's | 36 * While a HandleView is logically a child of some other view, it does not exist
in that View's |
| 36 * hierarchy. | 37 * hierarchy. |
| 37 * | 38 * |
| 38 */ | 39 */ |
| 39 @JNINamespace("content") | 40 @JNINamespace("android_webview") |
| 40 public class PopupTouchHandleDrawable extends View { | 41 public class PopupTouchHandleDrawable extends View { |
| 41 private final PopupWindow mContainer; | 42 private final PopupWindow mContainer; |
| 42 private final PositionObserver.Listener mParentPositionListener; | 43 private final PositionObserver.Listener mParentPositionListener; |
| 43 private final ContainerViewObserver mParentViewObserver; | |
| 44 private ContentViewCore mContentViewCore; | 44 private ContentViewCore mContentViewCore; |
| 45 private PositionObserver mParentPositionObserver; | 45 private PositionObserver mParentPositionObserver; |
| 46 private Drawable mDrawable; | 46 private Drawable mDrawable; |
| 47 | 47 |
| 48 // The native side of this object. |
| 49 private final long mNativeDrawable; |
| 50 |
| 48 // The position of the handle relative to the parent view. | 51 // The position of the handle relative to the parent view. |
| 49 private int mPositionX; | 52 private int mPositionX; |
| 50 private int mPositionY; | 53 private int mPositionY; |
| 51 | 54 |
| 52 // The position of the parent relative to the application's root view. | 55 // The position of the parent relative to the application's root view. |
| 53 private int mParentPositionX; | 56 private int mParentPositionX; |
| 54 private int mParentPositionY; | 57 private int mParentPositionY; |
| 55 | 58 |
| 56 // The mirror values based on which the handles are inverted about X and Y a
xes. | 59 // The mirror values based on which the handles are inverted about X and Y a
xes. |
| 57 private boolean mMirrorHorizontal; | 60 private boolean mMirrorHorizontal; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 86 // in the same frame. Thus, to ensure the PopupWindow is seen in the right | 89 // in the same frame. Thus, to ensure the PopupWindow is seen in the right |
| 87 // location, when the PopupWindow reappears we delay the visibility update | 90 // location, when the PopupWindow reappears we delay the visibility update |
| 88 // by one frame after setting the position. | 91 // by one frame after setting the position. |
| 89 private boolean mDelayVisibilityUpdateWAR; | 92 private boolean mDelayVisibilityUpdateWAR; |
| 90 | 93 |
| 91 // Deferred runnable to avoid invalidating outside of frame dispatch, | 94 // Deferred runnable to avoid invalidating outside of frame dispatch, |
| 92 // in turn avoiding issues with sync barrier insertion. | 95 // in turn avoiding issues with sync barrier insertion. |
| 93 private Runnable mInvalidationRunnable; | 96 private Runnable mInvalidationRunnable; |
| 94 private boolean mHasPendingInvalidate; | 97 private boolean mHasPendingInvalidate; |
| 95 | 98 |
| 96 private PopupTouchHandleDrawable(ContentViewCore contentViewCore) { | 99 // List of drawables used to inform them of the container view switching. |
| 100 private final ObserverList<PopupTouchHandleDrawable> mDrawableObserverList; |
| 101 |
| 102 private PopupTouchHandleDrawable(ObserverList<PopupTouchHandleDrawable> draw
ableObserverList, |
| 103 ContentViewCore contentViewCore, double dipScale) { |
| 97 super(contentViewCore.getContainerView().getContext()); | 104 super(contentViewCore.getContainerView().getContext()); |
| 105 mDrawableObserverList = drawableObserverList; |
| 106 mDrawableObserverList.addObserver(this); |
| 107 |
| 98 mContentViewCore = contentViewCore; | 108 mContentViewCore = contentViewCore; |
| 99 mContainer = new PopupWindow(mContentViewCore.getWindowAndroid().getCont
ext().get(), | 109 mContainer = new PopupWindow(mContentViewCore.getWindowAndroid().getCont
ext().get(), |
| 100 null, android.R.attr.textSelectHandleWindowStyle); | 110 null, android.R.attr.textSelectHandleWindowStyle); |
| 101 mContainer.setSplitTouchEnabled(true); | 111 mContainer.setSplitTouchEnabled(true); |
| 102 mContainer.setClippingEnabled(false); | 112 mContainer.setClippingEnabled(false); |
| 103 | 113 |
| 104 // The built-in PopupWindow animation causes jank when transitioning bet
ween | 114 // The built-in PopupWindow animation causes jank when transitioning bet
ween |
| 105 // visibility states. We use a custom fade-in animation when necessary. | 115 // visibility states. We use a custom fade-in animation when necessary. |
| 106 mContainer.setAnimationStyle(0); | 116 mContainer.setAnimationStyle(0); |
| 107 | 117 |
| 108 // The SUB_PANEL window layout type improves z-ordering with respect to | 118 // The SUB_PANEL window layout type improves z-ordering with respect to |
| 109 // other popup-based elements. | 119 // other popup-based elements. |
| 110 setWindowLayoutType(mContainer, WindowManager.LayoutParams.TYPE_APPLICAT
ION_SUB_PANEL); | 120 setWindowLayoutType(mContainer, WindowManager.LayoutParams.TYPE_APPLICAT
ION_SUB_PANEL); |
| 111 mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); | 121 mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 112 mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); | 122 mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); |
| 113 | 123 |
| 114 mAlpha = 1.f; | 124 mAlpha = 1.f; |
| 115 mVisible = getVisibility() == VISIBLE; | 125 mVisible = getVisibility() == VISIBLE; |
| 116 mFocused = mContentViewCore.getContainerView().hasWindowFocus(); | 126 mFocused = mContentViewCore.getContainerView().hasWindowFocus(); |
| 117 | 127 |
| 118 mParentPositionObserver = new ViewPositionObserver(mContentViewCore.getC
ontainerView()); | 128 mParentPositionObserver = new ViewPositionObserver(mContentViewCore.getC
ontainerView()); |
| 119 mParentPositionListener = new PositionObserver.Listener() { | 129 mParentPositionListener = new PositionObserver.Listener() { |
| 120 @Override | 130 @Override |
| 121 public void onPositionChanged(int x, int y) { | 131 public void onPositionChanged(int x, int y) { |
| 122 updateParentPosition(x, y); | 132 updateParentPosition(x, y); |
| 123 } | 133 } |
| 124 }; | 134 }; |
| 125 mParentViewObserver = new ContainerViewObserver() { | |
| 126 @Override | |
| 127 public void onContainerViewChanged(ViewGroup newContainerView) { | |
| 128 // If the parent View ever changes, the parent position observer | |
| 129 // must be updated accordingly. | |
| 130 mParentPositionObserver.clearListener(); | |
| 131 mParentPositionObserver = new ViewPositionObserver(newContainerV
iew); | |
| 132 if (mContainer.isShowing()) { | |
| 133 mParentPositionObserver.addListener(mParentPositionListener)
; | |
| 134 } | |
| 135 } | |
| 136 }; | |
| 137 mGestureStateListener = new GestureStateListener() { | 135 mGestureStateListener = new GestureStateListener() { |
| 138 @Override | 136 @Override |
| 139 public void onScrollStarted(int scrollOffsetX, int scrollOffsetY) { | 137 public void onScrollStarted(int scrollOffsetX, int scrollOffsetY) { |
| 140 setIsScrolling(true); | 138 setIsScrolling(true); |
| 141 } | 139 } |
| 142 @Override | 140 @Override |
| 143 public void onScrollEnded(int scrollOffsetX, int scrollOffsetY) { | 141 public void onScrollEnded(int scrollOffsetX, int scrollOffsetY) { |
| 144 setIsScrolling(false); | 142 setIsScrolling(false); |
| 145 } | 143 } |
| 146 @Override | 144 @Override |
| (...skipping 11 matching lines...) Expand all Loading... |
| 158 @Override | 156 @Override |
| 159 public void onWindowFocusChanged(boolean hasWindowFocus) { | 157 public void onWindowFocusChanged(boolean hasWindowFocus) { |
| 160 setIsFocused(hasWindowFocus); | 158 setIsFocused(hasWindowFocus); |
| 161 } | 159 } |
| 162 @Override | 160 @Override |
| 163 public void onDestroyed() { | 161 public void onDestroyed() { |
| 164 destroy(); | 162 destroy(); |
| 165 } | 163 } |
| 166 }; | 164 }; |
| 167 mContentViewCore.addGestureStateListener(mGestureStateListener); | 165 mContentViewCore.addGestureStateListener(mGestureStateListener); |
| 168 mContentViewCore.addContainerViewObserver(mParentViewObserver); | 166 mNativeDrawable = nativeInit(contentViewCore, (float) dipScale, |
| 167 HandleViewResources.getHandleHorizontalPaddingRatio()); |
| 168 } |
| 169 |
| 170 public static PopupTouchHandleDrawable create( |
| 171 ObserverList<PopupTouchHandleDrawable> drawableObserverList, |
| 172 ContentViewCore contentViewCore, double dipScale) { |
| 173 return new PopupTouchHandleDrawable(drawableObserverList, contentViewCor
e, dipScale); |
| 174 } |
| 175 |
| 176 public long getNativeDrawable() { |
| 177 return mNativeDrawable; |
| 169 } | 178 } |
| 170 | 179 |
| 171 private static void setWindowLayoutType(PopupWindow window, int layoutType)
{ | 180 private static void setWindowLayoutType(PopupWindow window, int layoutType)
{ |
| 172 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | 181 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 173 window.setWindowLayoutType(layoutType); | 182 window.setWindowLayoutType(layoutType); |
| 174 return; | 183 return; |
| 175 } | 184 } |
| 176 | 185 |
| 177 try { | 186 try { |
| 178 Method setWindowLayoutTypeMethod = | 187 Method setWindowLayoutTypeMethod = |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 final float offsetX = event.getRawX() - event.getX() - mTempScreenCoords
[0]; | 223 final float offsetX = event.getRawX() - event.getX() - mTempScreenCoords
[0]; |
| 215 final float offsetY = event.getRawY() - event.getY() - mTempScreenCoords
[1]; | 224 final float offsetY = event.getRawY() - event.getY() - mTempScreenCoords
[1]; |
| 216 final MotionEvent offsetEvent = MotionEvent.obtainNoHistory(event); | 225 final MotionEvent offsetEvent = MotionEvent.obtainNoHistory(event); |
| 217 offsetEvent.offsetLocation(offsetX, offsetY); | 226 offsetEvent.offsetLocation(offsetX, offsetY); |
| 218 final boolean handled = mContentViewCore.onTouchHandleEvent(offsetEvent)
; | 227 final boolean handled = mContentViewCore.onTouchHandleEvent(offsetEvent)
; |
| 219 offsetEvent.recycle(); | 228 offsetEvent.recycle(); |
| 220 return handled; | 229 return handled; |
| 221 } | 230 } |
| 222 | 231 |
| 223 @CalledByNative | 232 @CalledByNative |
| 224 private static PopupTouchHandleDrawable create(ContentViewCore contentViewCo
re) { | |
| 225 return new PopupTouchHandleDrawable(contentViewCore); | |
| 226 } | |
| 227 | |
| 228 @CalledByNative | |
| 229 private void setOrientation(int orientation, boolean mirrorVertical, boolean
mirrorHorizontal) { | 233 private void setOrientation(int orientation, boolean mirrorVertical, boolean
mirrorHorizontal) { |
| 230 assert (orientation >= TouchHandleOrientation.LEFT | 234 assert (orientation >= TouchHandleOrientation.LEFT |
| 231 && orientation <= TouchHandleOrientation.UNDEFINED); | 235 && orientation <= TouchHandleOrientation.UNDEFINED); |
| 232 | 236 |
| 233 final boolean orientationChanged = mOrientation != orientation; | 237 final boolean orientationChanged = mOrientation != orientation; |
| 234 final boolean mirroringChanged = | 238 final boolean mirroringChanged = |
| 235 mMirrorHorizontal != mirrorHorizontal || mMirrorVertical != mirr
orVertical; | 239 mMirrorHorizontal != mirrorHorizontal || mMirrorVertical != mirr
orVertical; |
| 236 mOrientation = orientation; | 240 mOrientation = orientation; |
| 237 mMirrorHorizontal = mirrorHorizontal; | 241 mMirrorHorizontal = mirrorHorizontal; |
| 238 mMirrorVertical = mirrorVertical; | 242 mMirrorVertical = mirrorVertical; |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 | 430 |
| 427 @Override | 431 @Override |
| 428 protected void onDetachedFromWindow() { | 432 protected void onDetachedFromWindow() { |
| 429 super.onDetachedFromWindow(); | 433 super.onDetachedFromWindow(); |
| 430 mAttachedToWindow = false; | 434 mAttachedToWindow = false; |
| 431 onVisibilityInputChanged(); | 435 onVisibilityInputChanged(); |
| 432 } | 436 } |
| 433 | 437 |
| 434 @CalledByNative | 438 @CalledByNative |
| 435 private void destroy() { | 439 private void destroy() { |
| 440 mDrawableObserverList.removeObserver(this); |
| 436 if (mContentViewCore == null) return; | 441 if (mContentViewCore == null) return; |
| 437 hide(); | 442 hide(); |
| 438 mContentViewCore.removeGestureStateListener(mGestureStateListener); | 443 mContentViewCore.removeGestureStateListener(mGestureStateListener); |
| 439 mContentViewCore.removeContainerViewObserver(mParentViewObserver); | |
| 440 mContentViewCore = null; | 444 mContentViewCore = null; |
| 441 } | 445 } |
| 442 | 446 |
| 443 @CalledByNative | 447 @CalledByNative |
| 444 private void show() { | 448 private void show() { |
| 445 if (mContentViewCore == null) return; | 449 if (mContentViewCore == null) return; |
| 446 if (mContainer.isShowing()) return; | 450 if (mContainer.isShowing()) return; |
| 447 | 451 |
| 448 // While hidden, the parent position may have become stale. It must be u
pdated before | 452 // While hidden, the parent position may have become stale. It must be u
pdated before |
| 449 // checking isPositionVisible(). | 453 // checking isPositionVisible(). |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 mVisible = visible; | 492 mVisible = visible; |
| 489 onVisibilityInputChanged(); | 493 onVisibilityInputChanged(); |
| 490 } | 494 } |
| 491 | 495 |
| 492 @CalledByNative | 496 @CalledByNative |
| 493 private int getPositionX() { | 497 private int getPositionX() { |
| 494 return mPositionX; | 498 return mPositionX; |
| 495 } | 499 } |
| 496 | 500 |
| 497 @CalledByNative | 501 @CalledByNative |
| 498 private float getHandleHorizontalPaddingRatio() { | |
| 499 return HandleViewResources.getHandleHorizontalPaddingRatio(); | |
| 500 } | |
| 501 | |
| 502 @CalledByNative | |
| 503 private int getPositionY() { | 502 private int getPositionY() { |
| 504 return mPositionY; | 503 return mPositionY; |
| 505 } | 504 } |
| 506 | 505 |
| 507 @CalledByNative | 506 @CalledByNative |
| 508 private int getVisibleWidth() { | 507 private int getVisibleWidth() { |
| 509 if (mDrawable == null) return 0; | 508 if (mDrawable == null) return 0; |
| 510 return mDrawable.getIntrinsicWidth(); | 509 return mDrawable.getIntrinsicWidth(); |
| 511 } | 510 } |
| 512 | 511 |
| 513 @CalledByNative | 512 @CalledByNative |
| 514 private int getVisibleHeight() { | 513 private int getVisibleHeight() { |
| 515 if (mDrawable == null) return 0; | 514 if (mDrawable == null) return 0; |
| 516 return mDrawable.getIntrinsicHeight(); | 515 return mDrawable.getIntrinsicHeight(); |
| 517 } | 516 } |
| 517 |
| 518 public void onContainerViewChanged(ViewGroup newContainerView) { |
| 519 // If the parent View ever changes, the parent position observer |
| 520 // must be updated accordingly. |
| 521 mParentPositionObserver.clearListener(); |
| 522 mParentPositionObserver = new ViewPositionObserver(newContainerView); |
| 523 if (mContainer.isShowing()) { |
| 524 mParentPositionObserver.addListener(mParentPositionListener); |
| 525 } |
| 526 } |
| 527 |
| 528 private native long nativeInit(ContentViewCore contentViewCore, float dipSca
le, |
| 529 float horizontalPaddingRatio); |
| 518 } | 530 } |
| OLD | NEW |