1 /*
2  * Copyright (C) 2010 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 #define LOG_TAG "KeyEvent-JNI"
18 
19 #include <nativehelper/JNIHelp.h>
20 
21 #include <android_runtime/AndroidRuntime.h>
22 #include <android_runtime/Log.h>
23 #include <attestation/HmacKeyManager.h>
24 #include <input/Input.h>
25 #include <nativehelper/ScopedPrimitiveArray.h>
26 #include <nativehelper/ScopedUtfChars.h>
27 #include <utils/Log.h>
28 #include <optional>
29 #include "android_view_KeyEvent.h"
30 
31 #include "core_jni_helpers.h"
32 
33 namespace android {
34 
35 /**
36  * Convert an std::array of bytes into a Java object.
37  */
38 template <size_t N>
toJbyteArray(JNIEnv * env,const std::array<uint8_t,N> & data)39 static ScopedLocalRef<jbyteArray> toJbyteArray(JNIEnv* env, const std::array<uint8_t, N>& data) {
40     ScopedLocalRef<jbyteArray> array(env, env->NewByteArray(N));
41     if (array.get() == nullptr) {
42         jniThrowException(env, "java/lang/OutOfMemoryError", nullptr);
43         return array;
44     }
45     static_assert(sizeof(char) == sizeof(uint8_t));
46     env->SetByteArrayRegion(array.get(), 0, N, reinterpret_cast<const signed char*>(data.data()));
47     return array;
48 }
49 
50 /**
51  * Convert a Java object into an std::array of bytes of size N.
52  * If the object is null,  or the length is unexpected, return std::nullopt.
53  */
54 template <size_t N>
fromJobject(JNIEnv * env,jobject object)55 static std::optional<std::array<uint8_t, N>> fromJobject(JNIEnv* env, jobject object) {
56     if (object == nullptr) {
57         return std::nullopt;
58     }
59     jbyteArray javaArray = reinterpret_cast<jbyteArray>(object);
60     ScopedByteArrayRO bytes(env, javaArray);
61     if (bytes.size() != N) {
62         ALOGE("Could not initialize array from java object, expected length %zu but got %zu", N,
63               bytes.size());
64         return std::nullopt;
65     }
66     std::array<uint8_t, N> array;
67     std::move(bytes.get(), bytes.get() + N, array.begin());
68     return array;
69 }
70 
71 // ----------------------------------------------------------------------------
72 
73 static struct {
74     jclass clazz;
75 
76     jmethodID obtain;
77     jmethodID recycle;
78 
79     jfieldID mId;
80     jfieldID mDeviceId;
81     jfieldID mSource;
82     jfieldID mDisplayId;
83     jfieldID mHmac;
84     jfieldID mMetaState;
85     jfieldID mAction;
86     jfieldID mKeyCode;
87     jfieldID mScanCode;
88     jfieldID mRepeatCount;
89     jfieldID mFlags;
90     jfieldID mDownTime;
91     jfieldID mEventTime;
92     jfieldID mCharacters;
93 } gKeyEventClassInfo;
94 
95 // ----------------------------------------------------------------------------
96 
android_view_KeyEvent_obtainAsCopy(JNIEnv * env,const KeyEvent & event)97 ScopedLocalRef<jobject> android_view_KeyEvent_obtainAsCopy(JNIEnv* env, const KeyEvent& event) {
98     ScopedLocalRef<jbyteArray> hmac = toJbyteArray(env, event.getHmac());
99     ScopedLocalRef<jobject>
100             eventObj(env,
101                      env->CallStaticObjectMethod(gKeyEventClassInfo.clazz,
102                                                  gKeyEventClassInfo.obtain, event.getId(),
103                                                  event.getDownTime(), event.getEventTime(),
104                                                  event.getAction(), event.getKeyCode(),
105                                                  event.getRepeatCount(), event.getMetaState(),
106                                                  event.getDeviceId(), event.getScanCode(),
107                                                  event.getFlags(), event.getSource(),
108                                                  event.getDisplayId(), hmac.get(), nullptr));
109     if (env->ExceptionCheck()) {
110         ALOGE("An exception occurred while obtaining a key event.");
111         LOGE_EX(env);
112         env->ExceptionClear();
113         return ScopedLocalRef<jobject>(env);
114     }
115     return eventObj;
116 }
117 
android_view_KeyEvent_obtainAsCopy(JNIEnv * env,jobject eventObj)118 KeyEvent android_view_KeyEvent_obtainAsCopy(JNIEnv* env, jobject eventObj) {
119     jint id = env->GetIntField(eventObj, gKeyEventClassInfo.mId);
120     jint deviceId = env->GetIntField(eventObj, gKeyEventClassInfo.mDeviceId);
121     jint source = env->GetIntField(eventObj, gKeyEventClassInfo.mSource);
122     jint displayId = env->GetIntField(eventObj, gKeyEventClassInfo.mDisplayId);
123     jobject hmacObj = env->GetObjectField(eventObj, gKeyEventClassInfo.mHmac);
124     std::optional<std::array<uint8_t, 32>> hmac = fromJobject<32>(env, hmacObj);
125     if (!hmac) {
126         hmac = INVALID_HMAC;
127     }
128     jint metaState = env->GetIntField(eventObj, gKeyEventClassInfo.mMetaState);
129     jint action = env->GetIntField(eventObj, gKeyEventClassInfo.mAction);
130     jint keyCode = env->GetIntField(eventObj, gKeyEventClassInfo.mKeyCode);
131     jint scanCode = env->GetIntField(eventObj, gKeyEventClassInfo.mScanCode);
132     jint repeatCount = env->GetIntField(eventObj, gKeyEventClassInfo.mRepeatCount);
133     jint flags = env->GetIntField(eventObj, gKeyEventClassInfo.mFlags);
134     jlong downTime = env->GetLongField(eventObj, gKeyEventClassInfo.mDownTime);
135     jlong eventTime = env->GetLongField(eventObj, gKeyEventClassInfo.mEventTime);
136 
137     KeyEvent event;
138     event.initialize(id, deviceId, source, ui::LogicalDisplayId{displayId}, *hmac, action, flags,
139                      keyCode, scanCode, metaState, repeatCount, downTime, eventTime);
140     return event;
141 }
142 
android_view_KeyEvent_recycle(JNIEnv * env,jobject eventObj)143 status_t android_view_KeyEvent_recycle(JNIEnv* env, jobject eventObj) {
144     env->CallVoidMethod(eventObj, gKeyEventClassInfo.recycle);
145     if (env->ExceptionCheck()) {
146         ALOGW("An exception occurred while recycling a key event.");
147         LOGW_EX(env);
148         env->ExceptionClear();
149         return UNKNOWN_ERROR;
150     }
151     return OK;
152 }
153 
android_view_KeyEvent_nativeKeyCodeToString(JNIEnv * env,jobject clazz,jint keyCode)154 static jstring android_view_KeyEvent_nativeKeyCodeToString(JNIEnv* env, jobject clazz,
155         jint keyCode) {
156     return env->NewStringUTF(KeyEvent::getLabel(keyCode));
157 }
158 
android_view_KeyEvent_nativeKeyCodeFromString(JNIEnv * env,jobject clazz,jstring label)159 static jint android_view_KeyEvent_nativeKeyCodeFromString(JNIEnv* env, jobject clazz,
160         jstring label) {
161     ScopedUtfChars keyLabel(env, label);
162     return KeyEvent::getKeyCodeFromLabel(keyLabel.c_str()).value_or(AKEYCODE_UNKNOWN);
163 }
164 
android_view_KeyEvent_nativeNextId()165 static jint android_view_KeyEvent_nativeNextId() {
166     return static_cast<jint>(InputEvent::nextId());
167 }
168 
169 // ----------------------------------------------------------------------------
170 
171 static const JNINativeMethod g_methods[] = {
172         {"nativeKeyCodeToString", "(I)Ljava/lang/String;",
173          (void*)android_view_KeyEvent_nativeKeyCodeToString},
174         {"nativeKeyCodeFromString", "(Ljava/lang/String;)I",
175          (void*)android_view_KeyEvent_nativeKeyCodeFromString},
176         {"nativeNextId", "()I", (void*)android_view_KeyEvent_nativeNextId},
177 };
178 
register_android_view_KeyEvent(JNIEnv * env)179 int register_android_view_KeyEvent(JNIEnv* env) {
180     jclass clazz = FindClassOrDie(env, "android/view/KeyEvent");
181     gKeyEventClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
182 
183     gKeyEventClassInfo.obtain =
184             GetStaticMethodIDOrDie(env, gKeyEventClassInfo.clazz, "obtain",
185                                    "(IJJIIIIIIIII[BLjava/lang/String;)Landroid/view/KeyEvent;");
186     gKeyEventClassInfo.recycle = GetMethodIDOrDie(env, gKeyEventClassInfo.clazz,
187             "recycle", "()V");
188 
189     gKeyEventClassInfo.mId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mId", "I");
190     gKeyEventClassInfo.mDeviceId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDeviceId", "I");
191     gKeyEventClassInfo.mSource = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mSource", "I");
192     gKeyEventClassInfo.mDisplayId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDisplayId",
193                                                     "I");
194     gKeyEventClassInfo.mHmac = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mHmac", "[B");
195     gKeyEventClassInfo.mMetaState = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mMetaState",
196                                                     "I");
197     gKeyEventClassInfo.mAction = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mAction", "I");
198     gKeyEventClassInfo.mKeyCode = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mKeyCode", "I");
199     gKeyEventClassInfo.mScanCode = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mScanCode", "I");
200     gKeyEventClassInfo.mRepeatCount = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mRepeatCount",
201                                                       "I");
202     gKeyEventClassInfo.mFlags = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mFlags", "I");
203     gKeyEventClassInfo.mDownTime = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDownTime", "J");
204     gKeyEventClassInfo.mEventTime = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mEventTime",
205                                                     "J");
206     gKeyEventClassInfo.mCharacters = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mCharacters",
207                                                      "Ljava/lang/String;");
208 
209     return RegisterMethodsOrDie(env, "android/view/KeyEvent", g_methods, NELEM(g_methods));
210 }
211 
212 } // namespace android
213