diff options
| -rw-r--r-- | core/java/android/view/HandwritingInitiator.java | 10 | ||||
| -rw-r--r-- | core/java/android/view/View.java | 7 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/view/stylus/HandwritingInitiatorTest.java | 26 |
3 files changed, 42 insertions, 1 deletions
diff --git a/core/java/android/view/HandwritingInitiator.java b/core/java/android/view/HandwritingInitiator.java index a47783cdacfe..6b604422ffba 100644 --- a/core/java/android/view/HandwritingInitiator.java +++ b/core/java/android/view/HandwritingInitiator.java @@ -205,6 +205,16 @@ public class HandwritingInitiator { } /** + * Notify HandwritingInitiator that a delegate view (see {@link View#isHandwritingDelegate}) + * gained focus. + */ + public void onDelegateViewFocused(@NonNull View view) { + if (view == getConnectedView()) { + tryAcceptStylusHandwritingDelegation(view); + } + } + + /** * Notify HandwritingInitiator that a new InputConnection is created. * The caller of this method should guarantee that each onInputConnectionCreated call * is paired with a onInputConnectionClosed call. diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 800fc97d03a6..aec3487910d8 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -8324,6 +8324,13 @@ public class View implements Drawable.Callback, KeyEvent.Callback, onFocusLost(); } else if (hasWindowFocus()) { notifyFocusChangeToImeFocusController(true /* hasFocus */); + + if (mIsHandwritingDelegate) { + ViewRootImpl viewRoot = getViewRootImpl(); + if (viewRoot != null) { + viewRoot.getHandwritingInitiator().onDelegateViewFocused(this); + } + } } invalidate(true); diff --git a/core/tests/coretests/src/android/view/stylus/HandwritingInitiatorTest.java b/core/tests/coretests/src/android/view/stylus/HandwritingInitiatorTest.java index 1ec2613dd101..fccb177dad4f 100644 --- a/core/tests/coretests/src/android/view/stylus/HandwritingInitiatorTest.java +++ b/core/tests/coretests/src/android/view/stylus/HandwritingInitiatorTest.java @@ -24,6 +24,7 @@ import static android.view.stylus.HandwritingTestUtil.createView; import static com.google.common.truth.Truth.assertThat; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.reset; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -224,7 +225,7 @@ public class HandwritingInitiatorTest { } @Test - public void onTouchEvent_startHandwriting_delegate() { + public void onTouchEvent_tryAcceptDelegation_delegatorCallbackCreatesInputConnection() { View delegateView = new View(mContext); delegateView.setIsHandwritingDelegate(true); @@ -245,6 +246,29 @@ public class HandwritingInitiatorTest { } @Test + public void onTouchEvent_tryAcceptDelegation_delegatorCallbackFocusesDelegate() { + View delegateView = new View(mContext); + delegateView.setIsHandwritingDelegate(true); + mHandwritingInitiator.onInputConnectionCreated(delegateView); + reset(mHandwritingInitiator); + + mTestView1.setHandwritingDelegatorCallback( + () -> mHandwritingInitiator.onDelegateViewFocused(delegateView)); + + final int x1 = (sHwArea1.left + sHwArea1.right) / 2; + final int y1 = (sHwArea1.top + sHwArea1.bottom) / 2; + MotionEvent stylusEvent1 = createStylusEvent(ACTION_DOWN, x1, y1, 0); + mHandwritingInitiator.onTouchEvent(stylusEvent1); + + final int x2 = x1 + mHandwritingSlop * 2; + final int y2 = y1; + MotionEvent stylusEvent2 = createStylusEvent(ACTION_MOVE, x2, y2, 0); + mHandwritingInitiator.onTouchEvent(stylusEvent2); + + verify(mHandwritingInitiator, times(1)).tryAcceptStylusHandwritingDelegation(delegateView); + } + + @Test public void onTouchEvent_notStartHandwriting_whenHandwritingNotAvailable() { final Rect rect = new Rect(600, 600, 900, 900); final View testView = createView(rect, true /* autoHandwritingEnabled */, |