blob: 531b3ed39fc3c1553de1a5d0c053def9b95a54b7 [file] [log] [blame]
Siarhei Vishniakoucf075282020-01-15 17:35:58 -08001/*
2 * Copyright (C) 2020 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
17package android.view;
18
19import static java.lang.annotation.RetentionPolicy.SOURCE;
20
21import android.annotation.IntDef;
22import android.annotation.NonNull;
23import android.annotation.SuppressLint;
24import android.os.Parcel;
25import android.os.Parcelable;
26
27import java.lang.annotation.Retention;
28
29
30/**
31 * Base class for verified events.
32 * Verified events contain the subset of an InputEvent that the system can verify.
33 * Data contained inside VerifiedInputEvent's should be considered trusted and contain only
34 * the original event data that first came from the system.
35 *
36 * @see android.hardware.input.InputManager#verifyInputEvent(InputEvent)
37 */
38@SuppressLint("ParcelNotFinal")
39public abstract class VerifiedInputEvent implements Parcelable {
40 private static final String TAG = "VerifiedInputEvent";
41
42 /** @hide */
43 protected static final int VERIFIED_KEY = 1;
44 /** @hide */
45 protected static final int VERIFIED_MOTION = 2;
46
47 /** @hide */
48 @Retention(SOURCE)
49 @IntDef(prefix = "VERIFIED", value = {VERIFIED_KEY, VERIFIED_MOTION})
50 public @interface VerifiedInputEventType {};
51
52 @VerifiedInputEventType
53 private int mType;
54
55 private int mDeviceId;
56 private long mEventTimeNanos;
57 private int mSource;
58 private int mDisplayId;
59
60 /** @hide */
61 protected VerifiedInputEvent(int type, int deviceId, long eventTimeNanos, int source,
62 int displayId) {
63 mType = type;
64 mDeviceId = deviceId;
65 mEventTimeNanos = eventTimeNanos;
66 mSource = source;
67 mDisplayId = displayId;
68 }
69 /** @hide */
70 protected VerifiedInputEvent(@NonNull Parcel in, int expectedType) {
71 mType = in.readInt();
72 if (mType != expectedType) {
73 throw new IllegalArgumentException("Unexpected input event type token in parcel.");
74 }
75 mDeviceId = in.readInt();
76 mEventTimeNanos = in.readLong();
77 mSource = in.readInt();
78 mDisplayId = in.readInt();
79 }
80
81 /**
82 * Get the id of the device that generated this event.
83 *
84 * @see InputEvent#getDeviceId()
85 */
86 public int getDeviceId() {
87 return mDeviceId;
88 }
89
90 /**
91 * Get the time this event occurred, in the {@link android.os.SystemClock#uptimeMillis()}
92 * time base.
93 *
94 * @see InputEvent#getEventTime()
95 * @see KeyEvent#getEventTimeNano()
96 * @see MotionEvent#getEventTimeNano()
97 */
98 @SuppressLint("MethodNameUnits")
99 public long getEventTimeNanos() {
100 return mEventTimeNanos;
101 }
102
103 /**
104 * Get the source of the event.
105 *
106 * @see InputEvent#getSource()
107 */
108 public int getSource() {
109 return mSource;
110 }
111
112 /**
113 * Get the display id that is associated with this event.
114 *
115 * @see Display#getDisplayId()
116 */
117 public int getDisplayId() {
118 return mDisplayId;
119 }
120
121 /** {@inheritDoc} */
122 @Override
123 public void writeToParcel(@NonNull Parcel dest, int flags) {
124 dest.writeInt(mType);
125 dest.writeInt(mDeviceId);
126 dest.writeLong(mEventTimeNanos);
127 dest.writeInt(mSource);
128 dest.writeInt(mDisplayId);
129 }
130
131 /** {@inheritDoc} */
132 @Override
133 public int describeContents() {
134 return 0;
135 }
136
137 private static int peekInt(@NonNull Parcel parcel) {
138 final int initialDataPosition = parcel.dataPosition();
139 int data = parcel.readInt();
140 parcel.setDataPosition(initialDataPosition);
141 return data;
142 }
143
144 public static final @NonNull Parcelable.Creator<VerifiedInputEvent> CREATOR =
145 new Parcelable.Creator<VerifiedInputEvent>() {
146 @Override
147 public VerifiedInputEvent[] newArray(int size) {
148 return new VerifiedInputEvent[size];
149 }
150
151 @Override
152 public VerifiedInputEvent createFromParcel(@NonNull Parcel in) {
153 final int type = peekInt(in);
154 if (type == VERIFIED_KEY) {
155 return VerifiedKeyEvent.CREATOR.createFromParcel(in);
156 } else if (type == VERIFIED_MOTION) {
157 return VerifiedMotionEvent.CREATOR.createFromParcel(in);
158 }
159 throw new IllegalArgumentException("Unexpected input event type in parcel.");
160 }
161 };
162}