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