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.NonNull;
20 import android.annotation.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.os.SystemClock;
24 import android.view.InputEvent;
25 
26 /**
27  * An event describing a mouse movement interaction originating from a remote device.
28  *
29  * See {@link android.view.MotionEvent}.
30  *
31  * @hide
32  */
33 @SystemApi
34 public final class VirtualMouseRelativeEvent implements Parcelable {
35 
36     private final float mRelativeX;
37     private final float mRelativeY;
38     private final long mEventTimeNanos;
39 
VirtualMouseRelativeEvent(float relativeX, float relativeY, long eventTimeNanos)40     private VirtualMouseRelativeEvent(float relativeX, float relativeY, long eventTimeNanos) {
41         mRelativeX = relativeX;
42         mRelativeY = relativeY;
43         mEventTimeNanos = eventTimeNanos;
44     }
45 
VirtualMouseRelativeEvent(@onNull Parcel parcel)46     private VirtualMouseRelativeEvent(@NonNull Parcel parcel) {
47         mRelativeX = parcel.readFloat();
48         mRelativeY = parcel.readFloat();
49         mEventTimeNanos = parcel.readLong();
50     }
51 
52     @Override
writeToParcel(@onNull Parcel parcel, int parcelableFlags)53     public void writeToParcel(@NonNull Parcel parcel, int parcelableFlags) {
54         parcel.writeFloat(mRelativeX);
55         parcel.writeFloat(mRelativeY);
56         parcel.writeLong(mEventTimeNanos);
57     }
58 
59     @Override
describeContents()60     public int describeContents() {
61         return 0;
62     }
63 
64     @Override
toString()65     public String toString() {
66         return "VirtualMouseRelativeEvent("
67                 + " x=" + mRelativeX
68                 + " y=" + mRelativeY
69                 + " eventTime(ns)=" + mEventTimeNanos;
70     }
71 
72     /**
73      * Returns the relative x-axis movement, in pixels.
74      */
getRelativeX()75     public float getRelativeX() {
76         return mRelativeX;
77     }
78 
79     /**
80      * Returns the relative x-axis movement, in pixels.
81      */
getRelativeY()82     public float getRelativeY() {
83         return mRelativeY;
84     }
85 
86     /**
87      * Returns the time this event occurred, in the {@link SystemClock#uptimeMillis()} time base but
88      * with nanosecond (instead of millisecond) precision.
89      *
90      * @see InputEvent#getEventTime()
91      */
getEventTimeNanos()92     public long getEventTimeNanos() {
93         return mEventTimeNanos;
94     }
95 
96     /**
97      * Builder for {@link VirtualMouseRelativeEvent}.
98      */
99     public static final class Builder {
100 
101         private float mRelativeX;
102         private float mRelativeY;
103         private long mEventTimeNanos = 0L;
104 
105         /**
106          * Creates a {@link VirtualMouseRelativeEvent} object with the current builder
107          * configuration.
108          */
build()109         public @NonNull VirtualMouseRelativeEvent build() {
110             return new VirtualMouseRelativeEvent(mRelativeX, mRelativeY, mEventTimeNanos);
111         }
112 
113         /**
114          * Sets the relative x-axis movement, in pixels.
115          *
116          * @return this builder, to allow for chaining of calls
117          */
setRelativeX(float relativeX)118         public @NonNull Builder setRelativeX(float relativeX) {
119             mRelativeX = relativeX;
120             return this;
121         }
122 
123         /**
124          * Sets the relative y-axis movement, in pixels.
125          *
126          * @return this builder, to allow for chaining of calls
127          */
setRelativeY(float relativeY)128         public @NonNull Builder setRelativeY(float relativeY) {
129             mRelativeY = relativeY;
130             return this;
131         }
132 
133         /**
134          * Sets the time (in nanoseconds) when this specific event was generated. This may be
135          * obtained from {@link SystemClock#uptimeMillis()} (with nanosecond precision instead of
136          * millisecond), but can be different depending on the use case.
137          * This field is optional and can be omitted.
138          *
139          * @return this builder, to allow for chaining of calls
140          * @see InputEvent#getEventTime()
141          */
setEventTimeNanos(long eventTimeNanos)142         public @NonNull Builder setEventTimeNanos(long eventTimeNanos) {
143             if (eventTimeNanos < 0L) {
144                 throw new IllegalArgumentException("Event time cannot be negative");
145             }
146             this.mEventTimeNanos = eventTimeNanos;
147             return this;
148         }
149     }
150 
151     public static final @NonNull Parcelable.Creator<VirtualMouseRelativeEvent> CREATOR =
152             new Parcelable.Creator<VirtualMouseRelativeEvent>() {
153                 public VirtualMouseRelativeEvent createFromParcel(Parcel source) {
154                     return new VirtualMouseRelativeEvent(source);
155                 }
156 
157                 public VirtualMouseRelativeEvent[] newArray(int size) {
158                     return new VirtualMouseRelativeEvent[size];
159                 }
160             };
161 }
162