| 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.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
import android.view.MotionEvent; |
| 25 |
|
| 26 |
import java.lang.annotation.Retention; |
| 27 |
import java.lang.annotation.RetentionPolicy; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
@SystemApi |
| 35 |
public final class VirtualMouseButtonEvent implements Parcelable { |
| 36 |
|
| 37 |
|
| 38 |
public static final int ACTION_UNKNOWN = -1; |
| 39 |
|
| 40 |
public static final int ACTION_BUTTON_PRESS = MotionEvent.ACTION_BUTTON_PRESS; |
| 41 |
|
| 42 |
public static final int ACTION_BUTTON_RELEASE = MotionEvent.ACTION_BUTTON_RELEASE; |
| 43 |
|
| 44 |
@IntDef(prefix = {"ACTION_"}, value = { |
| 45 |
ACTION_UNKNOWN, |
| 46 |
ACTION_BUTTON_PRESS, |
| 47 |
ACTION_BUTTON_RELEASE, |
| 48 |
}) |
| 49 |
@Retention(RetentionPolicy.SOURCE) |
| 50 |
public @interface Action {} |
| 51 |
|
| 52 |
|
| 53 |
public static final int BUTTON_UNKNOWN = -1; |
| 54 |
|
| 55 |
public static final int BUTTON_PRIMARY = MotionEvent.BUTTON_PRIMARY; |
| 56 |
|
| 57 |
public static final int BUTTON_TERTIARY = MotionEvent.BUTTON_TERTIARY; |
| 58 |
|
| 59 |
public static final int BUTTON_SECONDARY = MotionEvent.BUTTON_SECONDARY; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static final int BUTTON_BACK = MotionEvent.BUTTON_BACK; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public static final int BUTTON_FORWARD = MotionEvent.BUTTON_FORWARD; |
| 70 |
|
| 71 |
@IntDef(prefix = {"BUTTON_"}, value = { |
| 72 |
BUTTON_UNKNOWN, |
| 73 |
BUTTON_PRIMARY, |
| 74 |
BUTTON_TERTIARY, |
| 75 |
BUTTON_SECONDARY, |
| 76 |
BUTTON_BACK, |
| 77 |
BUTTON_FORWARD, |
| 78 |
}) |
| 79 |
@Retention(RetentionPolicy.SOURCE) |
| 80 |
public @interface Button {} |
| 81 |
|
| 82 |
private final @Action int mAction; |
| 83 |
private final @Button int mButtonCode; |
| 84 |
|
| 85 |
private VirtualMouseButtonEvent(@Action int action, @Button int buttonCode) { |
| 86 |
mAction = action; |
| 87 |
mButtonCode = buttonCode; |
| 88 |
} |
| 89 |
|
| 90 |
private VirtualMouseButtonEvent(@NonNull Parcel parcel) { |
| 91 |
mAction = parcel.readInt(); |
| 92 |
mButtonCode = parcel.readInt(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void writeToParcel(@NonNull Parcel parcel, int parcelableFlags) { |
| 97 |
parcel.writeInt(mAction); |
| 98 |
parcel.writeInt(mButtonCode); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public int describeContents() { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public @Button int getButtonCode() { |
| 110 |
return mButtonCode; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public @Action int getAction() { |
| 117 |
return mAction; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static final class Builder { |
| 124 |
|
| 125 |
private @Action int mAction = ACTION_UNKNOWN; |
| 126 |
private @Button int mButtonCode = -1; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public @NonNull VirtualMouseButtonEvent build() { |
| 132 |
if (mAction == ACTION_UNKNOWN || mButtonCode == -1) { |
| 133 |
throw new IllegalArgumentException( |
| 134 |
"Cannot build virtual mouse button event with unset fields"); |
| 135 |
} |
| 136 |
return new VirtualMouseButtonEvent(mAction, mButtonCode); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public @NonNull Builder setButtonCode(int buttonCode) { |
| 145 |
if (buttonCode != BUTTON_PRIMARY |
| 146 |
&& buttonCode != BUTTON_TERTIARY |
| 147 |
&& buttonCode != BUTTON_SECONDARY |
| 148 |
&& buttonCode != BUTTON_BACK |
| 149 |
&& buttonCode != BUTTON_FORWARD) { |
| 150 |
throw new IllegalArgumentException("Unsupported mouse button code"); |
| 151 |
} |
| 152 |
mButtonCode = buttonCode; |
| 153 |
return this; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public @NonNull Builder setAction(@Action int action) { |
| 162 |
if (action != ACTION_BUTTON_PRESS && action != ACTION_BUTTON_RELEASE) { |
| 163 |
throw new IllegalArgumentException("Unsupported mouse button action type"); |
| 164 |
} |
| 165 |
mAction = action; |
| 166 |
return this; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public static final @NonNull Parcelable.Creator<VirtualMouseButtonEvent> CREATOR = |
| 171 |
new Parcelable.Creator<VirtualMouseButtonEvent>() { |
| 172 |
public VirtualMouseButtonEvent createFromParcel(Parcel source) { |
| 173 |
return new VirtualMouseButtonEvent(source); |
| 174 |
} |
| 175 |
|
| 176 |
public VirtualMouseButtonEvent[] newArray(int size) { |
| 177 |
return new VirtualMouseButtonEvent[size]; |
| 178 |
} |
| 179 |
}; |
| 180 |
} |