• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_fromNative(JNIEnv * env,const KeyEvent * event)97 jobject android_view_KeyEvent_fromNative(JNIEnv* env, const KeyEvent* event) {
98     ScopedLocalRef<jbyteArray> hmac = toJbyteArray(env, event->getHmac());
99     jobject eventObj =
100             env->CallStaticObjectMethod(gKeyEventClassInfo.clazz, gKeyEventClassInfo.obtain,
101                                         event->getId(), event->getDownTime(), event->getEventTime(),
102                                         event->getAction(), event->getKeyCode(),
103                                         event->getRepeatCount(), event->getMetaState(),
104                                         event->getDeviceId(), event->getScanCode(),
105                                         event->getFlags(), event->getSource(),
106                                         event->getDisplayId(), hmac.get(), nullptr);
107     if (env->ExceptionCheck()) {
108         ALOGE("An exception occurred while obtaining a key event.");
109         LOGE_EX(env);
110         env->ExceptionClear();
111         return NULL;
112     }
113     return eventObj;
114 }
115 
android_view_KeyEvent_toNative(JNIEnv * env,jobject eventObj,KeyEvent * event)116 status_t android_view_KeyEvent_toNative(JNIEnv* env, jobject eventObj,
117         KeyEvent* event) {
118     jint id = env->GetIntField(eventObj, gKeyEventClassInfo.mId);
119     jint deviceId = env->GetIntField(eventObj, gKeyEventClassInfo.mDeviceId);
120     jint source = env->GetIntField(eventObj, gKeyEventClassInfo.mSource);
121     jint displayId = env->GetIntField(eventObj, gKeyEventClassInfo.mDisplayId);
122     jobject hmacObj = env->GetObjectField(eventObj, gKeyEventClassInfo.mHmac);
123     std::optional<std::array<uint8_t, 32>> hmac = fromJobject<32>(env, hmacObj);
124     if (!hmac) {
125         hmac = INVALID_HMAC;
126     }
127     jint metaState = env->GetIntField(eventObj, gKeyEventClassInfo.mMetaState);
128     jint action = env->GetIntField(eventObj, gKeyEventClassInfo.mAction);
129     jint keyCode = env->GetIntField(eventObj, gKeyEventClassInfo.mKeyCode);
130     jint scanCode = env->GetIntField(eventObj, gKeyEventClassInfo.mScanCode);
131     jint repeatCount = env->GetIntField(eventObj, gKeyEventClassInfo.mRepeatCount);
132     jint flags = env->GetIntField(eventObj, gKeyEventClassInfo.mFlags);
133     jlong downTime = env->GetLongField(eventObj, gKeyEventClassInfo.mDownTime);
134     jlong eventTime = env->GetLongField(eventObj, gKeyEventClassInfo.mEventTime);
135 
136     event->initialize(id, deviceId, source, displayId, *hmac, action, flags, keyCode, scanCode,
137                       metaState, repeatCount, downTime, eventTime);
138     return OK;
139 }
140 
android_view_KeyEvent_recycle(JNIEnv * env,jobject eventObj)141 status_t android_view_KeyEvent_recycle(JNIEnv* env, jobject eventObj) {
142     env->CallVoidMethod(eventObj, gKeyEventClassInfo.recycle);
143     if (env->ExceptionCheck()) {
144         ALOGW("An exception occurred while recycling a key event.");
145         LOGW_EX(env);
146         env->ExceptionClear();
147         return UNKNOWN_ERROR;
148     }
149     return OK;
150 }
151 
android_view_KeyEvent_nativeKeyCodeToString(JNIEnv * env,jobject clazz,jint keyCode)152 static jstring android_view_KeyEvent_nativeKeyCodeToString(JNIEnv* env, jobject clazz,
153         jint keyCode) {
154     return env->NewStringUTF(KeyEvent::getLabel(keyCode));
155 }
156 
android_view_KeyEvent_nativeKeyCodeFromString(JNIEnv * env,jobject clazz,jstring label)157 static jint android_view_KeyEvent_nativeKeyCodeFromString(JNIEnv* env, jobject clazz,
158         jstring label) {
159     ScopedUtfChars keyLabel(env, label);
160     return KeyEvent::getKeyCodeFromLabel(keyLabel.c_str());
161 }
162 
android_view_KeyEvent_nativeNextId()163 static jint android_view_KeyEvent_nativeNextId() {
164     return static_cast<jint>(InputEvent::nextId());
165 }
166 
167 // ----------------------------------------------------------------------------
168 
169 static const JNINativeMethod g_methods[] = {
170         {"nativeKeyCodeToString", "(I)Ljava/lang/String;",
171          (void*)android_view_KeyEvent_nativeKeyCodeToString},
172         {"nativeKeyCodeFromString", "(Ljava/lang/String;)I",
173          (void*)android_view_KeyEvent_nativeKeyCodeFromString},
174         {"nativeNextId", "()I", (void*)android_view_KeyEvent_nativeNextId},
175 };
176 
register_android_view_KeyEvent(JNIEnv * env)177 int register_android_view_KeyEvent(JNIEnv* env) {
178     jclass clazz = FindClassOrDie(env, "android/view/KeyEvent");
179     gKeyEventClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
180 
181     gKeyEventClassInfo.obtain =
182             GetStaticMethodIDOrDie(env, gKeyEventClassInfo.clazz, "obtain",
183                                    "(IJJIIIIIIIII[BLjava/lang/String;)Landroid/view/KeyEvent;");
184     gKeyEventClassInfo.recycle = GetMethodIDOrDie(env, gKeyEventClassInfo.clazz,
185             "recycle", "()V");
186 
187     gKeyEventClassInfo.mId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mId", "I");
188     gKeyEventClassInfo.mDeviceId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDeviceId", "I");
189     gKeyEventClassInfo.mSource = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mSource", "I");
190     gKeyEventClassInfo.mDisplayId = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDisplayId",
191                                                     "I");
192     gKeyEventClassInfo.mHmac = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mHmac", "[B");
193     gKeyEventClassInfo.mMetaState = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mMetaState",
194                                                     "I");
195     gKeyEventClassInfo.mAction = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mAction", "I");
196     gKeyEventClassInfo.mKeyCode = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mKeyCode", "I");
197     gKeyEventClassInfo.mScanCode = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mScanCode", "I");
198     gKeyEventClassInfo.mRepeatCount = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mRepeatCount",
199                                                       "I");
200     gKeyEventClassInfo.mFlags = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mFlags", "I");
201     gKeyEventClassInfo.mDownTime = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mDownTime", "J");
202     gKeyEventClassInfo.mEventTime = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mEventTime",
203                                                     "J");
204     gKeyEventClassInfo.mCharacters = GetFieldIDOrDie(env, gKeyEventClassInfo.clazz, "mCharacters",
205                                                      "Ljava/lang/String;");
206 
207     return RegisterMethodsOrDie(env, "android/view/KeyEvent", g_methods, NELEM(g_methods));
208 }
209 
210 } // namespace android
211