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     /**
69      * Returns the x-axis scroll movement, normalized from -1.0 to 1.0, inclusive. Positive values
70      * indicate scrolling upward; negative values, downward.
71      */
getXAxisMovement()72     public float getXAxisMovement() {
73         return mXAxisMovement;
74     }
75 
76     /**
77      * Returns the y-axis scroll movement, normalized from -1.0 to 1.0, inclusive. Positive values
78      * indicate scrolling towards the right; negative values, to the left.
79      */
getYAxisMovement()80     public float getYAxisMovement() {
81         return mYAxisMovement;
82     }
83 
84     /**
85      * Returns the time this event occurred, in the {@link SystemClock#uptimeMillis()} time base but
86      * with nanosecond (instead of millisecond) precision.
87      *
88      * @see InputEvent#getEventTime()
89      */
getEventTimeNanos()90     public long getEventTimeNanos() {
91         return mEventTimeNanos;
92     }
93 
94     /**
95      * Builder for {@link VirtualMouseScrollEvent}.
96      */
97     public static final class Builder {
98 
99         private float mXAxisMovement;
100         private float mYAxisMovement;
101         private long mEventTimeNanos = 0L;
102 
103         /**
104          * Creates a {@link VirtualMouseScrollEvent} object with the current builder configuration.
105          */
build()106         public @NonNull VirtualMouseScrollEvent build() {
107             return new VirtualMouseScrollEvent(mXAxisMovement, mYAxisMovement, mEventTimeNanos);
108         }
109 
110         /**
111          * Sets the x-axis scroll movement, normalized from -1.0 to 1.0, inclusive. Positive values
112          * indicate scrolling upward; negative values, downward.
113          *
114          * @return this builder, to allow for chaining of calls
115          */
setXAxisMovement( @loatRangefrom = -1.0f, to = 1.0f) float xAxisMovement)116         public @NonNull Builder setXAxisMovement(
117                 @FloatRange(from = -1.0f, to = 1.0f) float xAxisMovement) {
118             Preconditions.checkArgumentInRange(xAxisMovement, -1f, 1f, "xAxisMovement");
119             mXAxisMovement = xAxisMovement;
120             return this;
121         }
122 
123         /**
124          * Sets the y-axis scroll movement, normalized from -1.0 to 1.0, inclusive. Positive values
125          * indicate scrolling towards the right; negative values, to the left.
126          *
127          * @return this builder, to allow for chaining of calls
128          */
setYAxisMovement( @loatRangefrom = -1.0f, to = 1.0f) float yAxisMovement)129         public @NonNull Builder setYAxisMovement(
130                 @FloatRange(from = -1.0f, to = 1.0f) float yAxisMovement) {
131             Preconditions.checkArgumentInRange(yAxisMovement, -1f, 1f, "yAxisMovement");
132             mYAxisMovement = yAxisMovement;
133             return this;
134         }
135 
136         /**
137          * Sets the time (in nanoseconds) when this specific event was generated. This may be
138          * obtained from {@link SystemClock#uptimeMillis()} (with nanosecond precision instead of
139          * millisecond), but can be different depending on the use case.
140          * This field is optional and can be omitted.
141          *
142          * @return this builder, to allow for chaining of calls
143          * @see InputEvent#getEventTime()
144          */
setEventTimeNanos(long eventTimeNanos)145         public @NonNull Builder setEventTimeNanos(long eventTimeNanos) {
146             if (eventTimeNanos < 0L) {
147                 throw new IllegalArgumentException("Event time cannot be negative");
148             }
149             mEventTimeNanos = eventTimeNanos;
150             return this;
151         }
152     }
153 
154     public static final @NonNull Parcelable.Creator<VirtualMouseScrollEvent> CREATOR =
155             new Parcelable.Creator<VirtualMouseScrollEvent>() {
156                 public VirtualMouseScrollEvent createFromParcel(Parcel source) {
157                     return new VirtualMouseScrollEvent(source);
158                 }
159 
160                 public VirtualMouseScrollEvent[] newArray(int size) {
161                     return new VirtualMouseScrollEvent[size];
162                 }
163             };
164 }
165