| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.input; |
| 18 |
|
| 19 |
import android.annotation.FloatRange; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.view.MotionEvent; |
| 26 |
|
| 27 |
import java.lang.annotation.Retention; |
| 28 |
import java.lang.annotation.RetentionPolicy; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@SystemApi |
| 39 |
public final class VirtualTouchEvent implements Parcelable { |
| 40 |
|
| 41 |
|
| 42 |
public static final int TOOL_TYPE_UNKNOWN = MotionEvent.TOOL_TYPE_UNKNOWN; |
| 43 |
|
| 44 |
public static final int TOOL_TYPE_FINGER = MotionEvent.TOOL_TYPE_FINGER; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public static final int TOOL_TYPE_PALM = MotionEvent.TOOL_TYPE_PALM; |
| 50 |
|
| 51 |
@IntDef(prefix = { "TOOL_TYPE_" }, value = { |
| 52 |
TOOL_TYPE_UNKNOWN, |
| 53 |
TOOL_TYPE_FINGER, |
| 54 |
TOOL_TYPE_PALM, |
| 55 |
}) |
| 56 |
@Retention(RetentionPolicy.SOURCE) |
| 57 |
public @interface ToolType {} |
| 58 |
|
| 59 |
|
| 60 |
public static final int ACTION_UNKNOWN = -1; |
| 61 |
|
| 62 |
public static final int ACTION_DOWN = MotionEvent.ACTION_DOWN; |
| 63 |
|
| 64 |
public static final int ACTION_UP = MotionEvent.ACTION_UP; |
| 65 |
|
| 66 |
public static final int ACTION_MOVE = MotionEvent.ACTION_MOVE; |
| 67 |
|
| 68 |
public static final int ACTION_CANCEL = MotionEvent.ACTION_CANCEL; |
| 69 |
|
| 70 |
@IntDef(prefix = { "ACTION_" }, value = { |
| 71 |
ACTION_UNKNOWN, |
| 72 |
ACTION_DOWN, |
| 73 |
ACTION_UP, |
| 74 |
ACTION_MOVE, |
| 75 |
ACTION_CANCEL, |
| 76 |
}) |
| 77 |
@Retention(RetentionPolicy.SOURCE) |
| 78 |
public @interface Action {} |
| 79 |
|
| 80 |
private final int mPointerId; |
| 81 |
private final @ToolType int mToolType; |
| 82 |
private final @Action int mAction; |
| 83 |
private final float mX; |
| 84 |
private final float mY; |
| 85 |
private final float mPressure; |
| 86 |
private final float mMajorAxisSize; |
| 87 |
|
| 88 |
private VirtualTouchEvent(int pointerId, @ToolType int toolType, @Action int action, |
| 89 |
float x, float y, float pressure, float majorAxisSize) { |
| 90 |
mPointerId = pointerId; |
| 91 |
mToolType = toolType; |
| 92 |
mAction = action; |
| 93 |
mX = x; |
| 94 |
mY = y; |
| 95 |
mPressure = pressure; |
| 96 |
mMajorAxisSize = majorAxisSize; |
| 97 |
} |
| 98 |
|
| 99 |
private VirtualTouchEvent(@NonNull Parcel parcel) { |
| 100 |
mPointerId = parcel.readInt(); |
| 101 |
mToolType = parcel.readInt(); |
| 102 |
mAction = parcel.readInt(); |
| 103 |
mX = parcel.readFloat(); |
| 104 |
mY = parcel.readFloat(); |
| 105 |
mPressure = parcel.readFloat(); |
| 106 |
mMajorAxisSize = parcel.readFloat(); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 111 |
dest.writeInt(mPointerId); |
| 112 |
dest.writeInt(mToolType); |
| 113 |
dest.writeInt(mAction); |
| 114 |
dest.writeFloat(mX); |
| 115 |
dest.writeFloat(mY); |
| 116 |
dest.writeFloat(mPressure); |
| 117 |
dest.writeFloat(mMajorAxisSize); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public int describeContents() { |
| 122 |
return 0; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public int getPointerId() { |
| 129 |
return mPointerId; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public @ToolType int getToolType() { |
| 136 |
return mToolType; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public @Action int getAction() { |
| 143 |
return mAction; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public float getX() { |
| 150 |
return mX; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public float getY() { |
| 157 |
return mY; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public float getPressure() { |
| 164 |
return mPressure; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public float getMajorAxisSize() { |
| 171 |
return mMajorAxisSize; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static final class Builder { |
| 178 |
|
| 179 |
private @ToolType int mToolType = TOOL_TYPE_UNKNOWN; |
| 180 |
private int mPointerId = MotionEvent.INVALID_POINTER_ID; |
| 181 |
private @Action int mAction = ACTION_UNKNOWN; |
| 182 |
private float mX = Float.NaN; |
| 183 |
private float mY = Float.NaN; |
| 184 |
private float mPressure = Float.NaN; |
| 185 |
private float mMajorAxisSize = Float.NaN; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public @NonNull VirtualTouchEvent build() { |
| 191 |
if (mToolType == TOOL_TYPE_UNKNOWN || mPointerId == MotionEvent.INVALID_POINTER_ID |
| 192 |
|| mAction == ACTION_UNKNOWN || Float.isNaN(mX) || Float.isNaN(mY)) { |
| 193 |
throw new IllegalArgumentException( |
| 194 |
"Cannot build virtual touch event with unset required fields"); |
| 195 |
} |
| 196 |
if ((mToolType == TOOL_TYPE_PALM && mAction != ACTION_CANCEL) |
| 197 |
|| (mAction == ACTION_CANCEL && mToolType != TOOL_TYPE_PALM)) { |
| 198 |
throw new IllegalArgumentException( |
| 199 |
"ACTION_CANCEL and TOOL_TYPE_PALM must always appear together"); |
| 200 |
} |
| 201 |
return new VirtualTouchEvent(mPointerId, mToolType, mAction, mX, mY, mPressure, |
| 202 |
mMajorAxisSize); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public @NonNull Builder setPointerId(int pointerId) { |
| 211 |
mPointerId = pointerId; |
| 212 |
return this; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public @NonNull Builder setToolType(@ToolType int toolType) { |
| 221 |
if (toolType != TOOL_TYPE_FINGER && toolType != TOOL_TYPE_PALM) { |
| 222 |
throw new IllegalArgumentException("Unsupported touch event tool type"); |
| 223 |
} |
| 224 |
mToolType = toolType; |
| 225 |
return this; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public @NonNull Builder setAction(@Action int action) { |
| 234 |
if (action != ACTION_DOWN && action != ACTION_UP && action != ACTION_MOVE |
| 235 |
&& action != ACTION_CANCEL) { |
| 236 |
throw new IllegalArgumentException("Unsupported touch event action type"); |
| 237 |
} |
| 238 |
mAction = action; |
| 239 |
return this; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public @NonNull Builder setX(float absX) { |
| 248 |
mX = absX; |
| 249 |
return this; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public @NonNull Builder setY(float absY) { |
| 258 |
mY = absY; |
| 259 |
return this; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public @NonNull Builder setPressure(@FloatRange(from = 0f) float pressure) { |
| 268 |
if (pressure < 0f) { |
| 269 |
throw new IllegalArgumentException("Touch event pressure cannot be negative"); |
| 270 |
} |
| 271 |
mPressure = pressure; |
| 272 |
return this; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public @NonNull Builder setMajorAxisSize(@FloatRange(from = 0f) float majorAxisSize) { |
| 281 |
if (majorAxisSize < 0f) { |
| 282 |
throw new IllegalArgumentException( |
| 283 |
"Touch event major axis size cannot be negative"); |
| 284 |
} |
| 285 |
mMajorAxisSize = majorAxisSize; |
| 286 |
return this; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
public static final @NonNull Parcelable.Creator<VirtualTouchEvent> CREATOR = |
| 291 |
new Parcelable.Creator<VirtualTouchEvent>() { |
| 292 |
public VirtualTouchEvent createFromParcel(Parcel source) { |
| 293 |
return new VirtualTouchEvent(source); |
| 294 |
} |
| 295 |
public VirtualTouchEvent[] newArray(int size) { |
| 296 |
return new VirtualTouchEvent[size]; |
| 297 |
} |
| 298 |
}; |
| 299 |
} |