| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.graphics.Rect; |
| 22 |
import android.view.inputmethod.InputMethodManager; |
| 23 |
|
| 24 |
import com.android.internal.annotations.VisibleForTesting; |
| 25 |
|
| 26 |
import java.lang.ref.WeakReference; |
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.Iterator; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class HandwritingInitiator { |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private final int mTouchSlop; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private final long mHandwritingTimeoutInMillis; |
| 62 |
|
| 63 |
private State mState = new State(); |
| 64 |
private final HandwritingAreaTracker mHandwritingAreasTracker = new HandwritingAreaTracker(); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private void reset() { |
| 73 |
mState = new State(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
@Nullable |
| 78 |
@VisibleForTesting |
| 79 |
public WeakReference<View> mConnectedView = null; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private int mConnectionCount = 0; |
| 87 |
private final InputMethodManager mImm; |
| 88 |
|
| 89 |
@VisibleForTesting |
| 90 |
public HandwritingInitiator(@NonNull ViewConfiguration viewConfiguration, |
| 91 |
@NonNull InputMethodManager inputMethodManager) { |
| 92 |
mTouchSlop = viewConfiguration.getScaledTouchSlop(); |
| 93 |
mHandwritingTimeoutInMillis = ViewConfiguration.getLongPressTimeout(); |
| 94 |
mImm = inputMethodManager; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@VisibleForTesting |
| 105 |
public void onTouchEvent(@NonNull MotionEvent motionEvent) { |
| 106 |
final int maskedAction = motionEvent.getActionMasked(); |
| 107 |
switch (maskedAction) { |
| 108 |
case MotionEvent.ACTION_DOWN: |
| 109 |
case MotionEvent.ACTION_POINTER_DOWN: |
| 110 |
final int actionIndex = motionEvent.getActionIndex(); |
| 111 |
final int toolType = motionEvent.getToolType(actionIndex); |
| 112 |
|
| 113 |
|
| 114 |
if (toolType != MotionEvent.TOOL_TYPE_STYLUS |
| 115 |
&& toolType != MotionEvent.TOOL_TYPE_ERASER) { |
| 116 |
|
| 117 |
return; |
| 118 |
} |
| 119 |
mState.mStylusPointerId = motionEvent.getPointerId(actionIndex); |
| 120 |
mState.mStylusDownTimeInMillis = motionEvent.getEventTime(); |
| 121 |
mState.mStylusDownX = motionEvent.getX(actionIndex); |
| 122 |
mState.mStylusDownY = motionEvent.getY(actionIndex); |
| 123 |
mState.mShouldInitHandwriting = true; |
| 124 |
mState.mExceedTouchSlop = false; |
| 125 |
break; |
| 126 |
case MotionEvent.ACTION_POINTER_UP: |
| 127 |
final int pointerId = motionEvent.getPointerId(motionEvent.getActionIndex()); |
| 128 |
if (pointerId != mState.mStylusPointerId) { |
| 129 |
|
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
case MotionEvent.ACTION_CANCEL: |
| 134 |
case MotionEvent.ACTION_UP: |
| 135 |
|
| 136 |
|
| 137 |
reset(); |
| 138 |
break; |
| 139 |
case MotionEvent.ACTION_MOVE: |
| 140 |
|
| 141 |
|
| 142 |
if (!mState.mShouldInitHandwriting || mState.mExceedTouchSlop) { |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
final long timeElapsed = |
| 147 |
motionEvent.getEventTime() - mState.mStylusDownTimeInMillis; |
| 148 |
if (timeElapsed > mHandwritingTimeoutInMillis) { |
| 149 |
reset(); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
final int pointerIndex = motionEvent.findPointerIndex(mState.mStylusPointerId); |
| 154 |
final float x = motionEvent.getX(pointerIndex); |
| 155 |
final float y = motionEvent.getY(pointerIndex); |
| 156 |
if (largerThanTouchSlop(x, y, mState.mStylusDownX, mState.mStylusDownY)) { |
| 157 |
mState.mExceedTouchSlop = true; |
| 158 |
View candidateView = |
| 159 |
findBestCandidateView(mState.mStylusDownX, mState.mStylusDownY); |
| 160 |
if (candidateView != null) { |
| 161 |
if (candidateView == getConnectedView()) { |
| 162 |
startHandwriting(candidateView); |
| 163 |
} else { |
| 164 |
candidateView.requestFocus(); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@Nullable |
| 172 |
private View getConnectedView() { |
| 173 |
if (mConnectedView == null) return null; |
| 174 |
return mConnectedView.get(); |
| 175 |
} |
| 176 |
|
| 177 |
private void clearConnectedView() { |
| 178 |
mConnectedView = null; |
| 179 |
mConnectionCount = 0; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public void onInputConnectionCreated(@NonNull View view) { |
| 190 |
if (!view.isAutoHandwritingEnabled()) { |
| 191 |
clearConnectedView(); |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
final View connectedView = getConnectedView(); |
| 196 |
if (connectedView == view) { |
| 197 |
++mConnectionCount; |
| 198 |
} else { |
| 199 |
mConnectedView = new WeakReference<>(view); |
| 200 |
mConnectionCount = 1; |
| 201 |
if (mState.mShouldInitHandwriting) { |
| 202 |
tryStartHandwriting(); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public void onInputConnectionClosed(@NonNull View view) { |
| 214 |
final View connectedView = getConnectedView(); |
| 215 |
if (connectedView == null) return; |
| 216 |
if (connectedView == view) { |
| 217 |
--mConnectionCount; |
| 218 |
if (mConnectionCount == 0) { |
| 219 |
clearConnectedView(); |
| 220 |
} |
| 221 |
} else { |
| 222 |
|
| 223 |
clearConnectedView(); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
private void tryStartHandwriting() { |
| 239 |
if (!mState.mExceedTouchSlop) { |
| 240 |
return; |
| 241 |
} |
| 242 |
final View connectedView = getConnectedView(); |
| 243 |
if (connectedView == null) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
if (!connectedView.isAutoHandwritingEnabled()) { |
| 248 |
clearConnectedView(); |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
final Rect handwritingArea = getViewHandwritingArea(connectedView); |
| 253 |
if (contains(handwritingArea, mState.mStylusDownX, mState.mStylusDownY)) { |
| 254 |
startHandwriting(connectedView); |
| 255 |
} else { |
| 256 |
reset(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
@VisibleForTesting |
| 262 |
public void startHandwriting(@NonNull View view) { |
| 263 |
mImm.startStylusHandwriting(view); |
| 264 |
reset(); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public void updateHandwritingAreasForView(@NonNull View view) { |
| 272 |
mHandwritingAreasTracker.updateHandwritingAreaForView(view); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
@Nullable |
| 283 |
private View findBestCandidateView(float x, float y) { |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
final View connectedView = getConnectedView(); |
| 288 |
if (connectedView != null && connectedView.isAutoHandwritingEnabled()) { |
| 289 |
final Rect handwritingArea = getViewHandwritingArea(connectedView); |
| 290 |
if (contains(handwritingArea, x, y)) { |
| 291 |
return connectedView; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
final List<HandwritableViewInfo> handwritableViewInfos = |
| 297 |
mHandwritingAreasTracker.computeViewInfos(); |
| 298 |
for (HandwritableViewInfo viewInfo : handwritableViewInfos) { |
| 299 |
final View view = viewInfo.getView(); |
| 300 |
if (!view.isAutoHandwritingEnabled()) continue; |
| 301 |
if (contains(viewInfo.getHandwritingArea(), x, y)) { |
| 302 |
return viewInfo.getView(); |
| 303 |
} |
| 304 |
} |
| 305 |
return null; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
@Nullable |
| 314 |
private static Rect getViewHandwritingArea(@NonNull View view) { |
| 315 |
final ViewParent viewParent = view.getParent(); |
| 316 |
if (viewParent != null && view.isAttachedToWindow() && view.isAggregatedVisible()) { |
| 317 |
final Rect localHandwritingArea = view.getHandwritingArea(); |
| 318 |
final Rect globalHandwritingArea = new Rect(); |
| 319 |
if (localHandwritingArea != null) { |
| 320 |
globalHandwritingArea.set(localHandwritingArea); |
| 321 |
} else { |
| 322 |
globalHandwritingArea.set(0, 0, view.getWidth(), view.getHeight()); |
| 323 |
} |
| 324 |
if (viewParent.getChildVisibleRect(view, globalHandwritingArea, null)) { |
| 325 |
return globalHandwritingArea; |
| 326 |
} |
| 327 |
} |
| 328 |
return null; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
private boolean contains(@Nullable Rect rect, float x, float y) { |
| 335 |
if (rect == null) return false; |
| 336 |
return x >= rect.left && x < rect.right && y >= rect.top && y < rect.bottom; |
| 337 |
} |
| 338 |
|
| 339 |
private boolean largerThanTouchSlop(float x1, float y1, float x2, float y2) { |
| 340 |
float dx = x1 - x2; |
| 341 |
float dy = y1 - y2; |
| 342 |
return dx * dx + dy * dy > mTouchSlop * mTouchSlop; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
private static class State { |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
private boolean mShouldInitHandwriting = false; |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
private boolean mExceedTouchSlop = false; |
| 366 |
|
| 367 |
|
| 368 |
private int mStylusPointerId = -1; |
| 369 |
|
| 370 |
private long mStylusDownTimeInMillis = -1; |
| 371 |
|
| 372 |
private float mStylusDownX = Float.NaN; |
| 373 |
private float mStylusDownY = Float.NaN; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
private static boolean isViewActive(@Nullable View view) { |
| 378 |
return view != null && view.isAttachedToWindow() && view.isAggregatedVisible() |
| 379 |
&& view.isAutoHandwritingEnabled(); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
@VisibleForTesting |
| 388 |
public static class HandwritingAreaTracker { |
| 389 |
private final List<HandwritableViewInfo> mHandwritableViewInfos; |
| 390 |
|
| 391 |
public HandwritingAreaTracker() { |
| 392 |
mHandwritableViewInfos = new ArrayList<>(); |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
public void updateHandwritingAreaForView(@NonNull View view) { |
| 407 |
Iterator<HandwritableViewInfo> iterator = mHandwritableViewInfos.iterator(); |
| 408 |
boolean found = false; |
| 409 |
while (iterator.hasNext()) { |
| 410 |
final HandwritableViewInfo handwritableViewInfo = iterator.next(); |
| 411 |
final View curView = handwritableViewInfo.getView(); |
| 412 |
if (!isViewActive(curView)) { |
| 413 |
iterator.remove(); |
| 414 |
} |
| 415 |
if (curView == view) { |
| 416 |
found = true; |
| 417 |
handwritableViewInfo.mIsDirty = true; |
| 418 |
} |
| 419 |
} |
| 420 |
if (!found && isViewActive(view)) { |
| 421 |
|
| 422 |
|
| 423 |
mHandwritableViewInfos.add(new HandwritableViewInfo(view)); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
@NonNull |
| 432 |
public List<HandwritableViewInfo> computeViewInfos() { |
| 433 |
mHandwritableViewInfos.removeIf(viewInfo -> !viewInfo.update()); |
| 434 |
return mHandwritableViewInfos; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
@VisibleForTesting |
| 444 |
public static class HandwritableViewInfo { |
| 445 |
final WeakReference<View> mViewRef; |
| 446 |
Rect mHandwritingArea = null; |
| 447 |
@VisibleForTesting |
| 448 |
public boolean mIsDirty = true; |
| 449 |
|
| 450 |
@VisibleForTesting |
| 451 |
public HandwritableViewInfo(@NonNull View view) { |
| 452 |
mViewRef = new WeakReference<>(view); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
@Nullable |
| 457 |
public View getView() { |
| 458 |
return mViewRef.get(); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
@Nullable |
| 466 |
public Rect getHandwritingArea() { |
| 467 |
return mHandwritingArea; |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
public boolean update() { |
| 479 |
final View view = getView(); |
| 480 |
if (!isViewActive(view)) { |
| 481 |
return false; |
| 482 |
} |
| 483 |
|
| 484 |
if (!mIsDirty) { |
| 485 |
return true; |
| 486 |
} |
| 487 |
final Rect handwritingArea = view.getHandwritingArea(); |
| 488 |
if (handwritingArea == null) { |
| 489 |
return false; |
| 490 |
} |
| 491 |
|
| 492 |
ViewParent parent = view.getParent(); |
| 493 |
if (parent != null) { |
| 494 |
if (mHandwritingArea == null) { |
| 495 |
mHandwritingArea = new Rect(); |
| 496 |
} |
| 497 |
mHandwritingArea.set(handwritingArea); |
| 498 |
if (!parent.getChildVisibleRect(view, mHandwritingArea, null )) { |
| 499 |
mHandwritingArea = null; |
| 500 |
} |
| 501 |
} |
| 502 |
mIsDirty = false; |
| 503 |
return true; |
| 504 |
} |
| 505 |
} |
| 506 |
} |