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 /** 65 * Returns the relative x-axis movement, in pixels. 66 */ getRelativeX()67 public float getRelativeX() { 68 return mRelativeX; 69 } 70 71 /** 72 * Returns the relative x-axis movement, in pixels. 73 */ getRelativeY()74 public float getRelativeY() { 75 return mRelativeY; 76 } 77 78 /** 79 * Returns the time this event occurred, in the {@link SystemClock#uptimeMillis()} time base but 80 * with nanosecond (instead of millisecond) precision. 81 * 82 * @see InputEvent#getEventTime() 83 */ getEventTimeNanos()84 public long getEventTimeNanos() { 85 return mEventTimeNanos; 86 } 87 88 /** 89 * Builder for {@link VirtualMouseRelativeEvent}. 90 */ 91 public static final class Builder { 92 93 private float mRelativeX; 94 private float mRelativeY; 95 private long mEventTimeNanos = 0L; 96 97 /** 98 * Creates a {@link VirtualMouseRelativeEvent} object with the current builder 99 * configuration. 100 */ build()101 public @NonNull VirtualMouseRelativeEvent build() { 102 return new VirtualMouseRelativeEvent(mRelativeX, mRelativeY, mEventTimeNanos); 103 } 104 105 /** 106 * Sets the relative x-axis movement, in pixels. 107 * 108 * @return this builder, to allow for chaining of calls 109 */ setRelativeX(float relativeX)110 public @NonNull Builder setRelativeX(float relativeX) { 111 mRelativeX = relativeX; 112 return this; 113 } 114 115 /** 116 * Sets the relative y-axis movement, in pixels. 117 * 118 * @return this builder, to allow for chaining of calls 119 */ setRelativeY(float relativeY)120 public @NonNull Builder setRelativeY(float relativeY) { 121 mRelativeY = relativeY; 122 return this; 123 } 124 125 /** 126 * Sets the time (in nanoseconds) when this specific event was generated. This may be 127 * obtained from {@link SystemClock#uptimeMillis()} (with nanosecond precision instead of 128 * millisecond), but can be different depending on the use case. 129 * This field is optional and can be omitted. 130 * 131 * @return this builder, to allow for chaining of calls 132 * @see InputEvent#getEventTime() 133 */ setEventTimeNanos(long eventTimeNanos)134 public @NonNull Builder setEventTimeNanos(long eventTimeNanos) { 135 if (eventTimeNanos < 0L) { 136 throw new IllegalArgumentException("Event time cannot be negative"); 137 } 138 this.mEventTimeNanos = eventTimeNanos; 139 return this; 140 } 141 } 142 143 public static final @NonNull Parcelable.Creator<VirtualMouseRelativeEvent> CREATOR = 144 new Parcelable.Creator<VirtualMouseRelativeEvent>() { 145 public VirtualMouseRelativeEvent createFromParcel(Parcel source) { 146 return new VirtualMouseRelativeEvent(source); 147 } 148 149 public VirtualMouseRelativeEvent[] newArray(int size) { 150 return new VirtualMouseRelativeEvent[size]; 151 } 152 }; 153 } 154