| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.accessibilityservice; |
| 18 |
|
| 19 |
import android.annotation.IntRange; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.graphics.Path; |
| 22 |
import android.graphics.PathMeasure; |
| 23 |
import android.graphics.RectF; |
| 24 |
import android.os.Parcel; |
| 25 |
import android.os.Parcelable; |
| 26 |
import android.view.Display; |
| 27 |
|
| 28 |
import com.android.internal.util.Preconditions; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.List; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public final class GestureDescription { |
| 42 |
|
| 43 |
private static final int MAX_STROKE_COUNT = 20; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private static final long MAX_GESTURE_DURATION_MS = 60 * 1000; |
| 49 |
|
| 50 |
private final List<StrokeDescription> mStrokes = new ArrayList<>(); |
| 51 |
private final float[] mTempPos = new float[2]; |
| 52 |
private final int mDisplayId; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static int getMaxStrokeCount() { |
| 60 |
return MAX_STROKE_COUNT; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static long getMaxGestureDuration() { |
| 69 |
return MAX_GESTURE_DURATION_MS; |
| 70 |
} |
| 71 |
|
| 72 |
private GestureDescription() { |
| 73 |
this(new ArrayList<>()); |
| 74 |
} |
| 75 |
|
| 76 |
private GestureDescription(List<StrokeDescription> strokes) { |
| 77 |
this(strokes, Display.DEFAULT_DISPLAY); |
| 78 |
} |
| 79 |
|
| 80 |
private GestureDescription(List<StrokeDescription> strokes, int displayId) { |
| 81 |
mStrokes.addAll(strokes); |
| 82 |
mDisplayId = displayId; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public int getStrokeCount() { |
| 91 |
return mStrokes.size(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public StrokeDescription getStroke(@IntRange(from = 0) int index) { |
| 102 |
return mStrokes.get(index); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public int getDisplayId() { |
| 112 |
return mDisplayId; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private long getNextKeyPointAtLeast(long offset) { |
| 122 |
long nextKeyPoint = Long.MAX_VALUE; |
| 123 |
for (int i = 0; i < mStrokes.size(); i++) { |
| 124 |
long thisStartTime = mStrokes.get(i).mStartTime; |
| 125 |
if ((thisStartTime < nextKeyPoint) && (thisStartTime >= offset)) { |
| 126 |
nextKeyPoint = thisStartTime; |
| 127 |
} |
| 128 |
long thisEndTime = mStrokes.get(i).mEndTime; |
| 129 |
if ((thisEndTime < nextKeyPoint) && (thisEndTime >= offset)) { |
| 130 |
nextKeyPoint = thisEndTime; |
| 131 |
} |
| 132 |
} |
| 133 |
return (nextKeyPoint == Long.MAX_VALUE) ? -1L : nextKeyPoint; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
private int getPointsForTime(long time, TouchPoint[] touchPoints) { |
| 144 |
int numPointsFound = 0; |
| 145 |
for (int i = 0; i < mStrokes.size(); i++) { |
| 146 |
StrokeDescription strokeDescription = mStrokes.get(i); |
| 147 |
if (strokeDescription.hasPointForTime(time)) { |
| 148 |
touchPoints[numPointsFound].mStrokeId = strokeDescription.getId(); |
| 149 |
touchPoints[numPointsFound].mContinuedStrokeId = |
| 150 |
strokeDescription.getContinuedStrokeId(); |
| 151 |
touchPoints[numPointsFound].mIsStartOfPath = |
| 152 |
(strokeDescription.getContinuedStrokeId() < 0) |
| 153 |
&& (time == strokeDescription.mStartTime); |
| 154 |
touchPoints[numPointsFound].mIsEndOfPath = !strokeDescription.willContinue() |
| 155 |
&& (time == strokeDescription.mEndTime); |
| 156 |
strokeDescription.getPosForTime(time, mTempPos); |
| 157 |
touchPoints[numPointsFound].mX = Math.round(mTempPos[0]); |
| 158 |
touchPoints[numPointsFound].mY = Math.round(mTempPos[1]); |
| 159 |
numPointsFound++; |
| 160 |
} |
| 161 |
} |
| 162 |
return numPointsFound; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private static long getTotalDuration(List<StrokeDescription> paths) { |
| 168 |
long latestEnd = Long.MIN_VALUE; |
| 169 |
for (int i = 0; i < paths.size(); i++) { |
| 170 |
StrokeDescription path = paths.get(i); |
| 171 |
latestEnd = Math.max(latestEnd, path.mEndTime); |
| 172 |
} |
| 173 |
return Math.max(latestEnd, 0); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public static class Builder { |
| 180 |
|
| 181 |
private final List<StrokeDescription> mStrokes = new ArrayList<>(); |
| 182 |
private int mDisplayId = Display.DEFAULT_DISPLAY; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public Builder addStroke(@NonNull StrokeDescription strokeDescription) { |
| 195 |
if (mStrokes.size() >= MAX_STROKE_COUNT) { |
| 196 |
throw new IllegalStateException( |
| 197 |
"Attempting to add too many strokes to a gesture. Maximum is " |
| 198 |
+ MAX_STROKE_COUNT |
| 199 |
+ ", got " |
| 200 |
+ mStrokes.size()); |
| 201 |
} |
| 202 |
|
| 203 |
mStrokes.add(strokeDescription); |
| 204 |
|
| 205 |
if (getTotalDuration(mStrokes) > MAX_GESTURE_DURATION_MS) { |
| 206 |
mStrokes.remove(strokeDescription); |
| 207 |
throw new IllegalStateException( |
| 208 |
"Gesture would exceed maximum duration with new stroke"); |
| 209 |
} |
| 210 |
return this; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public @NonNull Builder setDisplayId(int displayId) { |
| 221 |
mDisplayId = displayId; |
| 222 |
return this; |
| 223 |
} |
| 224 |
|
| 225 |
public GestureDescription build() { |
| 226 |
if (mStrokes.size() == 0) { |
| 227 |
throw new IllegalStateException("Gestures must have at least one stroke"); |
| 228 |
} |
| 229 |
return new GestureDescription(mStrokes, mDisplayId); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public static class StrokeDescription { |
| 237 |
private static final int INVALID_STROKE_ID = -1; |
| 238 |
|
| 239 |
static int sIdCounter; |
| 240 |
|
| 241 |
Path mPath; |
| 242 |
long mStartTime; |
| 243 |
long mEndTime; |
| 244 |
private float mTimeToLengthConversion; |
| 245 |
private PathMeasure mPathMeasure; |
| 246 |
|
| 247 |
float[] mTapLocation; |
| 248 |
int mId; |
| 249 |
boolean mContinued; |
| 250 |
int mContinuedStrokeId = INVALID_STROKE_ID; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public StrokeDescription(@NonNull Path path, |
| 262 |
@IntRange(from = 0) long startTime, |
| 263 |
@IntRange(from = 0) long duration) { |
| 264 |
this(path, startTime, duration, false); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public StrokeDescription(@NonNull Path path, |
| 280 |
@IntRange(from = 0) long startTime, |
| 281 |
@IntRange(from = 0) long duration, |
| 282 |
boolean willContinue) { |
| 283 |
mContinued = willContinue; |
| 284 |
Preconditions.checkArgument(duration > 0, "Duration must be positive"); |
| 285 |
Preconditions.checkArgument(startTime >= 0, "Start time must not be negative"); |
| 286 |
Preconditions.checkArgument(!path.isEmpty(), "Path is empty"); |
| 287 |
RectF bounds = new RectF(); |
| 288 |
path.computeBounds(bounds, false ); |
| 289 |
Preconditions.checkArgument((bounds.bottom >= 0) && (bounds.top >= 0) |
| 290 |
&& (bounds.right >= 0) && (bounds.left >= 0), |
| 291 |
"Path bounds must not be negative"); |
| 292 |
mPath = new Path(path); |
| 293 |
mPathMeasure = new PathMeasure(path, false); |
| 294 |
if (mPathMeasure.getLength() == 0) { |
| 295 |
|
| 296 |
Path tempPath = new Path(path); |
| 297 |
tempPath.lineTo(-1, -1); |
| 298 |
mTapLocation = new float[2]; |
| 299 |
PathMeasure pathMeasure = new PathMeasure(tempPath, false); |
| 300 |
pathMeasure.getPosTan(0, mTapLocation, null); |
| 301 |
} |
| 302 |
if (mPathMeasure.nextContour()) { |
| 303 |
throw new IllegalArgumentException("Path has more than one contour"); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
mPathMeasure.setPath(mPath, false); |
| 310 |
mStartTime = startTime; |
| 311 |
mEndTime = startTime + duration; |
| 312 |
mTimeToLengthConversion = getLength() / duration; |
| 313 |
mId = sIdCounter++; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public Path getPath() { |
| 322 |
return new Path(mPath); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public long getStartTime() { |
| 331 |
return mStartTime; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public long getDuration() { |
| 340 |
return mEndTime - mStartTime; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public int getId() { |
| 351 |
return mId; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public StrokeDescription continueStroke(Path path, long startTime, long duration, |
| 369 |
boolean willContinue) { |
| 370 |
if (!mContinued) { |
| 371 |
throw new IllegalStateException( |
| 372 |
"Only strokes marked willContinue can be continued"); |
| 373 |
} |
| 374 |
StrokeDescription strokeDescription = |
| 375 |
new StrokeDescription(path, startTime, duration, willContinue); |
| 376 |
strokeDescription.mContinuedStrokeId = mId; |
| 377 |
return strokeDescription; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public boolean willContinue() { |
| 386 |
return mContinued; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public int getContinuedStrokeId() { |
| 396 |
return mContinuedStrokeId; |
| 397 |
} |
| 398 |
|
| 399 |
float getLength() { |
| 400 |
return mPathMeasure.getLength(); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
boolean getPosForTime(long time, float[] pos) { |
| 405 |
if (mTapLocation != null) { |
| 406 |
pos[0] = mTapLocation[0]; |
| 407 |
pos[1] = mTapLocation[1]; |
| 408 |
return true; |
| 409 |
} |
| 410 |
if (time == mEndTime) { |
| 411 |
|
| 412 |
return mPathMeasure.getPosTan(getLength(), pos, null); |
| 413 |
} |
| 414 |
float length = mTimeToLengthConversion * ((float) (time - mStartTime)); |
| 415 |
return mPathMeasure.getPosTan(length, pos, null); |
| 416 |
} |
| 417 |
|
| 418 |
boolean hasPointForTime(long time) { |
| 419 |
return ((time >= mStartTime) && (time <= mEndTime)); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public static class TouchPoint implements Parcelable { |
| 429 |
private static final int FLAG_IS_START_OF_PATH = 0x01; |
| 430 |
private static final int FLAG_IS_END_OF_PATH = 0x02; |
| 431 |
|
| 432 |
public int mStrokeId; |
| 433 |
public int mContinuedStrokeId; |
| 434 |
public boolean mIsStartOfPath; |
| 435 |
public boolean mIsEndOfPath; |
| 436 |
public float mX; |
| 437 |
public float mY; |
| 438 |
|
| 439 |
public TouchPoint() { |
| 440 |
} |
| 441 |
|
| 442 |
public TouchPoint(TouchPoint pointToCopy) { |
| 443 |
copyFrom(pointToCopy); |
| 444 |
} |
| 445 |
|
| 446 |
public TouchPoint(Parcel parcel) { |
| 447 |
mStrokeId = parcel.readInt(); |
| 448 |
mContinuedStrokeId = parcel.readInt(); |
| 449 |
int startEnd = parcel.readInt(); |
| 450 |
mIsStartOfPath = (startEnd & FLAG_IS_START_OF_PATH) != 0; |
| 451 |
mIsEndOfPath = (startEnd & FLAG_IS_END_OF_PATH) != 0; |
| 452 |
mX = parcel.readFloat(); |
| 453 |
mY = parcel.readFloat(); |
| 454 |
} |
| 455 |
|
| 456 |
public void copyFrom(TouchPoint other) { |
| 457 |
mStrokeId = other.mStrokeId; |
| 458 |
mContinuedStrokeId = other.mContinuedStrokeId; |
| 459 |
mIsStartOfPath = other.mIsStartOfPath; |
| 460 |
mIsEndOfPath = other.mIsEndOfPath; |
| 461 |
mX = other.mX; |
| 462 |
mY = other.mY; |
| 463 |
} |
| 464 |
|
| 465 |
@Override |
| 466 |
public String toString() { |
| 467 |
return "TouchPoint{" |
| 468 |
+ "mStrokeId=" + mStrokeId |
| 469 |
+ ", mContinuedStrokeId=" + mContinuedStrokeId |
| 470 |
+ ", mIsStartOfPath=" + mIsStartOfPath |
| 471 |
+ ", mIsEndOfPath=" + mIsEndOfPath |
| 472 |
+ ", mX=" + mX |
| 473 |
+ ", mY=" + mY |
| 474 |
+ '}'; |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
public int describeContents() { |
| 479 |
return 0; |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
public void writeToParcel(Parcel dest, int flags) { |
| 484 |
dest.writeInt(mStrokeId); |
| 485 |
dest.writeInt(mContinuedStrokeId); |
| 486 |
int startEnd = mIsStartOfPath ? FLAG_IS_START_OF_PATH : 0; |
| 487 |
startEnd |= mIsEndOfPath ? FLAG_IS_END_OF_PATH : 0; |
| 488 |
dest.writeInt(startEnd); |
| 489 |
dest.writeFloat(mX); |
| 490 |
dest.writeFloat(mY); |
| 491 |
} |
| 492 |
|
| 493 |
public static final @android.annotation.NonNull Parcelable.Creator<TouchPoint> CREATOR |
| 494 |
= new Parcelable.Creator<TouchPoint>() { |
| 495 |
public TouchPoint createFromParcel(Parcel in) { |
| 496 |
return new TouchPoint(in); |
| 497 |
} |
| 498 |
|
| 499 |
public TouchPoint[] newArray(int size) { |
| 500 |
return new TouchPoint[size]; |
| 501 |
} |
| 502 |
}; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public static class GestureStep implements Parcelable { |
| 511 |
public long timeSinceGestureStart; |
| 512 |
public int numTouchPoints; |
| 513 |
public TouchPoint[] touchPoints; |
| 514 |
|
| 515 |
public GestureStep(long timeSinceGestureStart, int numTouchPoints, |
| 516 |
TouchPoint[] touchPointsToCopy) { |
| 517 |
this.timeSinceGestureStart = timeSinceGestureStart; |
| 518 |
this.numTouchPoints = numTouchPoints; |
| 519 |
this.touchPoints = new TouchPoint[numTouchPoints]; |
| 520 |
for (int i = 0; i < numTouchPoints; i++) { |
| 521 |
this.touchPoints[i] = new TouchPoint(touchPointsToCopy[i]); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
public GestureStep(Parcel parcel) { |
| 526 |
timeSinceGestureStart = parcel.readLong(); |
| 527 |
Parcelable[] parcelables = |
| 528 |
parcel.readParcelableArray(TouchPoint.class.getClassLoader(), TouchPoint.class); |
| 529 |
numTouchPoints = (parcelables == null) ? 0 : parcelables.length; |
| 530 |
touchPoints = new TouchPoint[numTouchPoints]; |
| 531 |
for (int i = 0; i < numTouchPoints; i++) { |
| 532 |
touchPoints[i] = (TouchPoint) parcelables[i]; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
@Override |
| 537 |
public int describeContents() { |
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
@Override |
| 542 |
public void writeToParcel(Parcel dest, int flags) { |
| 543 |
dest.writeLong(timeSinceGestureStart); |
| 544 |
dest.writeParcelableArray(touchPoints, flags); |
| 545 |
} |
| 546 |
|
| 547 |
public static final @android.annotation.NonNull Parcelable.Creator<GestureStep> CREATOR |
| 548 |
= new Parcelable.Creator<GestureStep>() { |
| 549 |
public GestureStep createFromParcel(Parcel in) { |
| 550 |
return new GestureStep(in); |
| 551 |
} |
| 552 |
|
| 553 |
public GestureStep[] newArray(int size) { |
| 554 |
return new GestureStep[size]; |
| 555 |
} |
| 556 |
}; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
public static class MotionEventGenerator { |
| 565 |
|
| 566 |
private static TouchPoint[] sCurrentTouchPoints; |
| 567 |
|
| 568 |
public static List<GestureStep> getGestureStepsFromGestureDescription( |
| 569 |
GestureDescription description, int sampleTimeMs) { |
| 570 |
final List<GestureStep> gestureSteps = new ArrayList<>(); |
| 571 |
|
| 572 |
|
| 573 |
final TouchPoint[] currentTouchPoints = |
| 574 |
getCurrentTouchPoints(description.getStrokeCount()); |
| 575 |
int currentTouchPointSize = 0; |
| 576 |
|
| 577 |
long timeSinceGestureStart = 0; |
| 578 |
long nextKeyPointTime = description.getNextKeyPointAtLeast(timeSinceGestureStart); |
| 579 |
while (nextKeyPointTime >= 0) { |
| 580 |
timeSinceGestureStart = (currentTouchPointSize == 0) ? nextKeyPointTime |
| 581 |
: Math.min(nextKeyPointTime, timeSinceGestureStart + sampleTimeMs); |
| 582 |
currentTouchPointSize = description.getPointsForTime(timeSinceGestureStart, |
| 583 |
currentTouchPoints); |
| 584 |
gestureSteps.add(new GestureStep(timeSinceGestureStart, currentTouchPointSize, |
| 585 |
currentTouchPoints)); |
| 586 |
|
| 587 |
|
| 588 |
nextKeyPointTime = description.getNextKeyPointAtLeast(timeSinceGestureStart + 1); |
| 589 |
} |
| 590 |
return gestureSteps; |
| 591 |
} |
| 592 |
|
| 593 |
private static TouchPoint[] getCurrentTouchPoints(int requiredCapacity) { |
| 594 |
if ((sCurrentTouchPoints == null) || (sCurrentTouchPoints.length < requiredCapacity)) { |
| 595 |
sCurrentTouchPoints = new TouchPoint[requiredCapacity]; |
| 596 |
for (int i = 0; i < requiredCapacity; i++) { |
| 597 |
sCurrentTouchPoints[i] = new TouchPoint(); |
| 598 |
} |
| 599 |
} |
| 600 |
return sCurrentTouchPoints; |
| 601 |
} |
| 602 |
} |
| 603 |
} |