1 /* 2 * Copyright (C) 2022 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.inputmethod; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SuppressLint; 22 import android.graphics.PointF; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 import java.util.Objects; 27 28 /** A subclass of {@link HandwritingGesture} for removing whitespace from text. */ 29 public final class RemoveSpaceGesture extends HandwritingGesture implements Parcelable { 30 31 private final PointF mStartPoint; 32 private final PointF mEndPoint; 33 RemoveSpaceGesture(PointF startPoint, PointF endPoint, String fallbackText)34 private RemoveSpaceGesture(PointF startPoint, PointF endPoint, String fallbackText) { 35 mType = GESTURE_TYPE_REMOVE_SPACE; 36 mStartPoint = startPoint; 37 mEndPoint = endPoint; 38 mFallbackText = fallbackText; 39 } 40 RemoveSpaceGesture(@onNull Parcel source)41 private RemoveSpaceGesture(@NonNull Parcel source) { 42 mType = GESTURE_TYPE_REMOVE_SPACE; 43 mStartPoint = source.readTypedObject(PointF.CREATOR); 44 mEndPoint = source.readTypedObject(PointF.CREATOR); 45 mFallbackText = source.readString8(); 46 } 47 48 /** Returns the start point in screen coordinates set with {@link Builder#setPoints}. */ 49 @NonNull getStartPoint()50 public PointF getStartPoint() { 51 return mStartPoint; 52 } 53 54 /** Returns the end point in screen coordinates set with {@link Builder#setPoints}. */ 55 @NonNull getEndPoint()56 public PointF getEndPoint() { 57 return mEndPoint; 58 } 59 60 /** Builder for {@link RemoveSpaceGesture}. This class is not designed to be thread-safe. */ 61 public static final class Builder { 62 private PointF mStartPoint; 63 private PointF mEndPoint; 64 private String mFallbackText; 65 66 /** 67 * Sets the start and end points in screen coordinates of the line to apply the remove space 68 * operation. All whitespace characters touched by the line joining the points will be 69 * deleted. 70 * 71 * <p>The operation will only be performed on a single line of text. If the start and end 72 * points are on different lines of text, the line will be adjusted to cover only the first 73 * line of text containing one of the points. 74 */ 75 @NonNull 76 @SuppressLint("MissingGetterMatchingBuilder") setPoints(@onNull PointF startPoint, @NonNull PointF endPoint)77 public Builder setPoints(@NonNull PointF startPoint, @NonNull PointF endPoint) { 78 mStartPoint = startPoint; 79 mEndPoint = endPoint; 80 return this; 81 } 82 83 /** 84 * Sets fallback text that will be committed at current cursor position if there is no 85 * whitespace beneath the gesture line. 86 */ 87 @NonNull setFallbackText(@ullable String fallbackText)88 public Builder setFallbackText(@Nullable String fallbackText) { 89 mFallbackText = fallbackText; 90 return this; 91 } 92 93 /** 94 * @return {@link RemoveSpaceGesture} using parameters in this {@link Builder}. 95 * @throws IllegalArgumentException if one or more positional parameters are not specified. 96 */ 97 @NonNull build()98 public RemoveSpaceGesture build() { 99 if (mStartPoint == null || mEndPoint == null) { 100 throw new IllegalArgumentException("Start and end points must be set."); 101 } 102 return new RemoveSpaceGesture(mStartPoint, mEndPoint, mFallbackText); 103 } 104 } 105 106 /** Used to make this class parcelable. */ 107 @NonNull 108 public static final Parcelable.Creator<RemoveSpaceGesture> CREATOR = 109 new Parcelable.Creator<RemoveSpaceGesture>() { 110 @Override 111 public RemoveSpaceGesture createFromParcel(Parcel source) { 112 return new RemoveSpaceGesture(source); 113 } 114 115 @Override 116 public RemoveSpaceGesture[] newArray(int size) { 117 return new RemoveSpaceGesture[size]; 118 } 119 }; 120 121 @Override hashCode()122 public int hashCode() { 123 return Objects.hash(mStartPoint, mEndPoint, mFallbackText); 124 } 125 126 @Override equals(Object o)127 public boolean equals(Object o) { 128 if (!(o instanceof RemoveSpaceGesture)) return false; 129 RemoveSpaceGesture that = (RemoveSpaceGesture) o; 130 return Objects.equals(mStartPoint, that.mStartPoint) 131 && Objects.equals(mEndPoint, that.mEndPoint) 132 && Objects.equals(mFallbackText, that.mFallbackText); 133 } 134 135 @Override describeContents()136 public int describeContents() { 137 return 0; 138 } 139 140 /** 141 * Used to package this object into a {@link Parcel}. 142 * 143 * @param dest {@link Parcel} to be written 144 * @param flags flags used for parceling 145 */ 146 @Override writeToParcel(@onNull Parcel dest, int flags)147 public void writeToParcel(@NonNull Parcel dest, int flags) { 148 dest.writeTypedObject(mStartPoint, flags); 149 dest.writeTypedObject(mEndPoint, flags); 150 dest.writeString8(mFallbackText); 151 } 152 } 153