• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.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  * An event describing a mouse button click interaction originating from a remote device.
31  *
32  * @hide
33  */
34 @SystemApi
35 public final class VirtualMouseButtonEvent implements Parcelable {
36 
37     /** @hide */
38     public static final int ACTION_UNKNOWN = -1;
39     /** Action indicating the mouse button has been pressed. */
40     public static final int ACTION_BUTTON_PRESS = MotionEvent.ACTION_BUTTON_PRESS;
41     /** Action indicating the mouse button has been released. */
42     public static final int ACTION_BUTTON_RELEASE = MotionEvent.ACTION_BUTTON_RELEASE;
43     /** @hide */
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     /** @hide */
53     public static final int BUTTON_UNKNOWN = -1;
54     /** Action indicating the mouse button involved in this event is in the left position. */
55     public static final int BUTTON_PRIMARY = MotionEvent.BUTTON_PRIMARY;
56     /** Action indicating the mouse button involved in this event is in the middle position. */
57     public static final int BUTTON_TERTIARY = MotionEvent.BUTTON_TERTIARY;
58     /** Action indicating the mouse button involved in this event is in the right position. */
59     public static final int BUTTON_SECONDARY = MotionEvent.BUTTON_SECONDARY;
60     /**
61      * Action indicating the mouse button involved in this event is intended to go back to the
62      * previous.
63      */
64     public static final int BUTTON_BACK = MotionEvent.BUTTON_BACK;
65     /**
66      * Action indicating the mouse button involved in this event is intended to move forward to the
67      * next.
68      */
69     public static final int BUTTON_FORWARD = MotionEvent.BUTTON_FORWARD;
70     /** @hide */
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 
VirtualMouseButtonEvent(@ction int action, @Button int buttonCode)85     private VirtualMouseButtonEvent(@Action int action, @Button int buttonCode) {
86         mAction = action;
87         mButtonCode = buttonCode;
88     }
89 
VirtualMouseButtonEvent(@onNull Parcel parcel)90     private VirtualMouseButtonEvent(@NonNull Parcel parcel) {
91         mAction = parcel.readInt();
92         mButtonCode = parcel.readInt();
93     }
94 
95     @Override
writeToParcel(@onNull Parcel parcel, int parcelableFlags)96     public void writeToParcel(@NonNull Parcel parcel, int parcelableFlags) {
97         parcel.writeInt(mAction);
98         parcel.writeInt(mButtonCode);
99     }
100 
101     @Override
describeContents()102     public int describeContents() {
103         return 0;
104     }
105 
106     /**
107      * Returns the button code associated with this event.
108      */
getButtonCode()109     public @Button int getButtonCode() {
110         return mButtonCode;
111     }
112 
113     /**
114      * Returns the action associated with this event.
115      */
getAction()116     public @Action int getAction() {
117         return mAction;
118     }
119 
120     /**
121      * Builder for {@link VirtualMouseButtonEvent}.
122      */
123     public static final class Builder {
124 
125         private @Action int mAction = ACTION_UNKNOWN;
126         private @Button int mButtonCode = -1;
127 
128         /**
129          * Creates a {@link VirtualMouseButtonEvent} object with the current builder configuration.
130          */
build()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          * Sets the button code of the event.
141          *
142          * @return this builder, to allow for chaining of calls
143          */
setButtonCode(int buttonCode)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          * Sets the action of the event.
158          *
159          * @return this builder, to allow for chaining of calls
160          */
setAction(@ction int action)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 }
181