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