1 /*
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 
17 package android.view;
18 
19 import static android.view.KeyEvent.FLAG_CANCELED;
20 import static android.view.KeyEvent.FLAG_IS_ACCESSIBILITY_EVENT;
21 
22 import static java.lang.annotation.RetentionPolicy.SOURCE;
23 
24 import android.annotation.IntDef;
25 import android.annotation.Nullable;
26 import android.annotation.SuppressLint;
27 import android.os.Parcel;
28 import android.os.Parcelable;
29 
30 import com.android.internal.util.DataClass;
31 
32 import java.lang.annotation.Retention;
33 
34 /**
35  * KeyEvent that has been verified by the system.
36  * The data contained in this class is always a subset of a {@link KeyEvent}. Use this class to
37  * check which data has been confirmed by the system to be authentic.
38  *
39  * Most applications do not need to use this class.
40  *
41  * {@see android.hardware.input.InputManager#verifyInputEvent}
42  */
43 @DataClass(genHiddenConstructor = true, genEqualsHashCode = true)
44 public final class VerifiedKeyEvent extends VerifiedInputEvent implements Parcelable {
45     private static final String TAG = "VerifiedKeyEvent";
46 
47     /** @hide */
48     @Retention(SOURCE)
49     @IntDef({KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP})
50     public @interface KeyEventAction {};
51 
52     /**
53      * The action of this key event.  May be either {@link KeyEvent#ACTION_DOWN} or
54      * {@link KeyEvent#ACTION_UP}.
55      *
56      * @see KeyEvent#getAction()
57      */
58     @KeyEventAction
59     private int mAction;
60 
61     /**
62      * Retrieve the time of the most recent key down event, in the
63      * {@link android.os.SystemClock#uptimeMillis} time base, but in nanoseconds. If this
64      * is a down event, this will be the same as {@link VerifiedInputEvent#getEventTimeNanos()}.
65      *
66      * @see KeyEvent#getDownTime()
67      */
68     @SuppressLint({"MethodNameUnits"})
69     private long mDownTimeNanos;
70 
71     /**
72      * Returns the flags for this key event.
73      *
74      * @see KeyEvent#getFlags()
75      * @see KeyEvent#FLAG_CANCELED
76      * @see KeyEvent#FLAG_IS_ACCESSIBILITY_EVENT
77      *
78      * @hide
79      */
80     private int mFlags;
81 
82     /**
83      * Retrieve the key code of the key event.
84      *
85      * @see KeyEvent#getKeyCode()
86      */
87     private int mKeyCode;
88 
89     /**
90      * Retrieve the hardware key id of this key event. These values are not reliable
91      * and vary from device to device.
92      *
93      * @see KeyEvent#getScanCode()
94      */
95     private int mScanCode;
96 
97     /**
98      * <p>Returns the state of the meta keys.</p>
99      *
100      * @return an integer in which each bit set to 1 represents a pressed meta key
101      * @see KeyEvent#getMetaState()
102      */
103     private int mMetaState;
104 
105     /**
106      * Retrieve the repeat count of the event.  For key down events, this is the number of times
107      * the key has repeated with the first down starting at 0 and counting up from there.
108      * For key up events, this is always equal to zero. For multiple key events,
109      * this is the number of down/up pairs that have occurred.
110      */
111     private int mRepeatCount;
112 
113     /**
114      * Get a specific flag of this key event, if possible. Return null if the flag value could
115      * not be checked.
116      *
117      * @param flag the flag of interest
118      * @return Boolean(true) if the key event has the requested flag
119      *         Boolean(false) if the key event does not have the requested flag
120      *         null if the flag value could not be checked
121      *
122      * @see KeyEvent#getFlags()
123      * @see KeyEvent#FLAG_CANCELED
124      */
getFlag(int flag)125     public @Nullable Boolean getFlag(int flag) {
126         switch(flag) {
127             // InputDispatcher only verifies a subset of the KeyEvent flags.
128             // These values must be kept in sync with Input.cpp
129             case FLAG_CANCELED:
130             case FLAG_IS_ACCESSIBILITY_EVENT:
131                 return (mFlags & flag) != 0;
132         }
133         return null;
134     }
135 
136     // The codegen tool doesn't fully support subclasses, since it works on a per-file basis.
137     // To modify this file:
138     // 1. run codegen on this file
139     // 2. edit the constructor signature
140     // 3. add the "super" call for constructor that receives a Parcel
141     // 4. add the "super" call to the writeToParcel method
142     // 5. Update "equals" and "hashcode" methods to include VerifiedInputEvent fields
143 
144 
145 
146     // Code below generated by codegen v1.0.20.
147     //
148     // DO NOT MODIFY!
149     // CHECKSTYLE:OFF Generated code
150     //
151     // To regenerate run:
152     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/view/VerifiedKeyEvent.java
153     //
154     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
155     //   Settings > Editor > Code Style > Formatter Control
156     //@formatter:off
157 
158 
159     /**
160      * Creates a new VerifiedKeyEvent.
161      *
162      * @param action
163      *   The action of this key event.  May be either {@link KeyEvent#ACTION_DOWN} or
164      *   {@link KeyEvent#ACTION_UP}.
165      * @param downTimeNanos
166      *   Retrieve the time of the most recent key down event, in the
167      *   {@link android.os.SystemClock#uptimeMillis} time base, but in nanoseconds. If this
168      *   is a down event, this will be the same as {@link VerifiedInputEvent#getEventTimeNanos()}.
169      * @param flags
170      *   Returns the flags for this key event.
171      * @param keyCode
172      *   Retrieve the key code of the key event.
173      * @param scanCode
174      *   Retrieve the hardware key id of this key event. These values are not reliable
175      *   and vary from device to device.
176      * @param metaState
177      *   <p>Returns the state of the meta keys.</p>
178      * @param repeatCount
179      *   Retrieve the repeat count of the event.  For key down events, this is the number of times
180      *   the key has repeated with the first down starting at 0 and counting up from there.
181      *   For key up events, this is always equal to zero. For multiple key events,
182      *   this is the number of down/up pairs that have occurred.
183      * @hide
184      */
VerifiedKeyEvent( int deviceId, long eventTimeNanos, int source, int displayId, @KeyEventAction int action, @SuppressLint({ "MethodNameUnits" }) long downTimeNanos, int flags, int keyCode, int scanCode, int metaState, int repeatCount)185     public VerifiedKeyEvent(
186             int deviceId,
187             long eventTimeNanos,
188             int source,
189             int displayId,
190             @KeyEventAction int action,
191             @SuppressLint({ "MethodNameUnits" }) long downTimeNanos,
192             int flags,
193             int keyCode,
194             int scanCode,
195             int metaState,
196             int repeatCount) {
197         super(VERIFIED_KEY, deviceId, eventTimeNanos, source, displayId);
198         this.mAction = action;
199         com.android.internal.util.AnnotationValidations.validate(
200                 KeyEventAction.class, null, mAction);
201         this.mDownTimeNanos = downTimeNanos;
202         this.mFlags = flags;
203         this.mKeyCode = keyCode;
204         this.mScanCode = scanCode;
205         this.mMetaState = metaState;
206         this.mRepeatCount = repeatCount;
207     }
208 
209     /**
210      * The action of this key event.  May be either {@link KeyEvent#ACTION_DOWN} or
211      * {@link KeyEvent#ACTION_UP}.
212      *
213      * @see KeyEvent#getAction()
214      */
215     @DataClass.Generated.Member
getAction()216     public @KeyEventAction int getAction() {
217         return mAction;
218     }
219 
220     /**
221      * Retrieve the time of the most recent key down event, in the
222      * {@link android.os.SystemClock#uptimeMillis} time base, but in nanoseconds. If this
223      * is a down event, this will be the same as {@link VerifiedInputEvent#getEventTimeNanos()}.
224      *
225      * @see KeyEvent#getDownTime()
226      */
227     @DataClass.Generated.Member
getDownTimeNanos()228     public @SuppressLint({ "MethodNameUnits" }) long getDownTimeNanos() {
229         return mDownTimeNanos;
230     }
231 
232     /**
233      * Returns the flags for this key event.
234      *
235      * @see KeyEvent#getFlags()
236      * @see KeyEvent#FLAG_CANCELED
237      * @hide
238      */
239     @DataClass.Generated.Member
getFlags()240     public int getFlags() {
241         return mFlags;
242     }
243 
244     /**
245      * Retrieve the key code of the key event.
246      *
247      * @see KeyEvent#getKeyCode()
248      */
249     @DataClass.Generated.Member
getKeyCode()250     public int getKeyCode() {
251         return mKeyCode;
252     }
253 
254     /**
255      * Retrieve the hardware key id of this key event. These values are not reliable
256      * and vary from device to device.
257      *
258      * @see KeyEvent#getScanCode()
259      */
260     @DataClass.Generated.Member
getScanCode()261     public int getScanCode() {
262         return mScanCode;
263     }
264 
265     /**
266      * <p>Returns the state of the meta keys.</p>
267      *
268      * @return an integer in which each bit set to 1 represents a pressed meta key
269      * @see KeyEvent#getMetaState()
270      */
271     @DataClass.Generated.Member
getMetaState()272     public int getMetaState() {
273         return mMetaState;
274     }
275 
276     /**
277      * Retrieve the repeat count of the event.  For key down events, this is the number of times
278      * the key has repeated with the first down starting at 0 and counting up from there.
279      * For key up events, this is always equal to zero. For multiple key events,
280      * this is the number of down/up pairs that have occurred.
281      */
282     @DataClass.Generated.Member
getRepeatCount()283     public int getRepeatCount() {
284         return mRepeatCount;
285     }
286 
287     @Override
288     @DataClass.Generated.Member
equals(@ullable Object o)289     public boolean equals(@Nullable Object o) {
290         // You can override field equality logic by defining either of the methods like:
291         // boolean fieldNameEquals(VerifiedKeyEvent other) { ... }
292         // boolean fieldNameEquals(FieldType otherValue) { ... }
293 
294         if (this == o) return true;
295         if (o == null || getClass() != o.getClass()) return false;
296         @SuppressWarnings("unchecked")
297         VerifiedKeyEvent that = (VerifiedKeyEvent) o;
298         //noinspection PointlessBooleanExpression
299         return true
300                 && super.equals(that)
301                 && mAction == that.mAction
302                 && mDownTimeNanos == that.mDownTimeNanos
303                 && mFlags == that.mFlags
304                 && mKeyCode == that.mKeyCode
305                 && mScanCode == that.mScanCode
306                 && mMetaState == that.mMetaState
307                 && mRepeatCount == that.mRepeatCount;
308     }
309 
310     @Override
311     @DataClass.Generated.Member
hashCode()312     public int hashCode() {
313         // You can override field hashCode logic by defining methods like:
314         // int fieldNameHashCode() { ... }
315 
316         int _hash = 1;
317         _hash = 31 * _hash + super.hashCode();
318         _hash = 31 * _hash + mAction;
319         _hash = 31 * _hash + Long.hashCode(mDownTimeNanos);
320         _hash = 31 * _hash + mFlags;
321         _hash = 31 * _hash + mKeyCode;
322         _hash = 31 * _hash + mScanCode;
323         _hash = 31 * _hash + mMetaState;
324         _hash = 31 * _hash + mRepeatCount;
325         return _hash;
326     }
327 
328     @Override
329     @DataClass.Generated.Member
writeToParcel(@ndroid.annotation.NonNull Parcel dest, int flags)330     public void writeToParcel(@android.annotation.NonNull Parcel dest, int flags) {
331         // You can override field parcelling by defining methods like:
332         // void parcelFieldName(Parcel dest, int flags) { ... }
333         super.writeToParcel(dest, flags);
334 
335         dest.writeInt(mAction);
336         dest.writeLong(mDownTimeNanos);
337         dest.writeInt(mFlags);
338         dest.writeInt(mKeyCode);
339         dest.writeInt(mScanCode);
340         dest.writeInt(mMetaState);
341         dest.writeInt(mRepeatCount);
342     }
343 
344     @Override
345     @DataClass.Generated.Member
describeContents()346     public int describeContents() { return 0; }
347 
348     /** @hide */
349     @SuppressWarnings({"unchecked", "RedundantCast"})
350     @DataClass.Generated.Member
VerifiedKeyEvent(@ndroid.annotation.NonNull Parcel in)351     /* package-private */ VerifiedKeyEvent(@android.annotation.NonNull Parcel in) {
352         // You can override field unparcelling by defining methods like:
353         // static FieldType unparcelFieldName(Parcel in) { ... }
354         super(in, VERIFIED_KEY);
355 
356         int action = in.readInt();
357         long downTimeNanos = in.readLong();
358         int flags = in.readInt();
359         int keyCode = in.readInt();
360         int scanCode = in.readInt();
361         int metaState = in.readInt();
362         int repeatCount = in.readInt();
363 
364         this.mAction = action;
365         com.android.internal.util.AnnotationValidations.validate(
366                 KeyEventAction.class, null, mAction);
367         this.mDownTimeNanos = downTimeNanos;
368         this.mFlags = flags;
369         this.mKeyCode = keyCode;
370         this.mScanCode = scanCode;
371         this.mMetaState = metaState;
372         this.mRepeatCount = repeatCount;
373 
374         // onConstructed(); // You can define this method to get a callback
375     }
376 
377     @DataClass.Generated.Member
378     public static final @android.annotation.NonNull Parcelable.Creator<VerifiedKeyEvent> CREATOR
379             = new Parcelable.Creator<VerifiedKeyEvent>() {
380         @Override
381         public VerifiedKeyEvent[] newArray(int size) {
382             return new VerifiedKeyEvent[size];
383         }
384 
385         @Override
386         public VerifiedKeyEvent createFromParcel(@android.annotation.NonNull Parcel in) {
387             return new VerifiedKeyEvent(in);
388         }
389     };
390 
391     @DataClass.Generated(
392             time = 1604509197793L,
393             codegenVersion = "1.0.20",
394             sourceFile = "frameworks/base/core/java/android/view/VerifiedKeyEvent.java",
395             inputSignatures = "private static final  java.lang.String TAG\nprivate @android.view.VerifiedKeyEvent.KeyEventAction int mAction\nprivate @android.annotation.SuppressLint long mDownTimeNanos\nprivate  int mFlags\nprivate  int mKeyCode\nprivate  int mScanCode\nprivate  int mMetaState\nprivate  int mRepeatCount\npublic @android.annotation.Nullable java.lang.Boolean getFlag(int)\nclass VerifiedKeyEvent extends android.view.VerifiedInputEvent implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genHiddenConstructor=true, genEqualsHashCode=true)")
396     @Deprecated
__metadata()397     private void __metadata() {}
398 
399 
400     //@formatter:on
401     // End of generated code
402 
403 }
404