1 /*
2  * Copyright (C) 2012 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 #include <input/Input.h>
18 
19 #include <android_runtime/AndroidRuntime.h>
20 #include <jni.h>
21 #include <nativehelper/JNIHelp.h>
22 
23 #include <nativehelper/ScopedLocalRef.h>
24 
25 #include "android_view_InputDevice.h"
26 #include "android_view_KeyCharacterMap.h"
27 
28 #include "core_jni_helpers.h"
29 
30 namespace android {
31 
32 static struct {
33     jclass clazz;
34 
35     jmethodID ctor;
36     jmethodID addMotionRange;
37 } gInputDeviceClassInfo;
38 
android_view_InputDevice_create(JNIEnv * env,const InputDeviceInfo & deviceInfo)39 jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& deviceInfo) {
40     ScopedLocalRef<jstring> nameObj(env, env->NewStringUTF(deviceInfo.getDisplayName().c_str()));
41     if (!nameObj.get()) {
42         return NULL;
43     }
44 
45     // b/274058082: Pass a copy of the key character map to avoid concurrent
46     // access
47     std::shared_ptr<KeyCharacterMap> map = deviceInfo.getKeyCharacterMap();
48     if (map != nullptr) {
49         map = std::make_shared<KeyCharacterMap>(*map);
50     }
51 
52     ScopedLocalRef<jstring> descriptorObj(env,
53             env->NewStringUTF(deviceInfo.getIdentifier().descriptor.c_str()));
54     if (!descriptorObj.get()) {
55         return NULL;
56     }
57 
58     std::optional<KeyboardLayoutInfo> layoutInfo = deviceInfo.getKeyboardLayoutInfo();
59     ScopedLocalRef<jstring> keyboardLanguageTagObj(env,
60                                                    env->NewStringUTF(
61                                                            layoutInfo
62                                                                    ? layoutInfo->languageTag.c_str()
63                                                                    : NULL));
64     ScopedLocalRef<jstring> keyboardLayoutTypeObj(env,
65                                                   env->NewStringUTF(
66                                                           layoutInfo
67                                                                   ? layoutInfo->layoutType.c_str()
68                                                                   : NULL));
69 
70     ScopedLocalRef<jobject> kcmObj(env,
71                                    android_view_KeyCharacterMap_create(env, deviceInfo.getId(),
72                                                                        map));
73     if (!kcmObj.get()) {
74         return NULL;
75     }
76 
77     const InputDeviceIdentifier& ident = deviceInfo.getIdentifier();
78     const auto usiVersion = deviceInfo.getUsiVersion().value_or(InputDeviceUsiVersion{-1, -1});
79 
80     ScopedLocalRef<jobject>
81             inputDeviceObj(env,
82                            env->NewObject(gInputDeviceClassInfo.clazz, gInputDeviceClassInfo.ctor,
83                                           deviceInfo.getId(), deviceInfo.getGeneration(),
84                                           deviceInfo.getControllerNumber(), nameObj.get(),
85                                           static_cast<int32_t>(ident.vendor),
86                                           static_cast<int32_t>(ident.product), descriptorObj.get(),
87                                           deviceInfo.isExternal(), deviceInfo.getSources(),
88                                           deviceInfo.getKeyboardType(), kcmObj.get(),
89                                           keyboardLanguageTagObj.get(), keyboardLayoutTypeObj.get(),
90                                           deviceInfo.hasVibrator(), deviceInfo.hasMic(),
91                                           deviceInfo.hasButtonUnderPad(), deviceInfo.hasSensor(),
92                                           deviceInfo.hasBattery(), usiVersion.majorVersion,
93                                           usiVersion.minorVersion,
94                                           deviceInfo.getAssociatedDisplayId()));
95     // Note: We do not populate the Bluetooth address into the InputDevice object to avoid leaking
96     // it to apps that do not have the Bluetooth permission.
97 
98     const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
99     for (const InputDeviceInfo::MotionRange& range: ranges) {
100         env->CallVoidMethod(inputDeviceObj.get(), gInputDeviceClassInfo.addMotionRange, range.axis,
101                 range.source, range.min, range.max, range.flat, range.fuzz, range.resolution);
102         if (env->ExceptionCheck()) {
103             return NULL;
104         }
105     }
106 
107     return env->NewLocalRef(inputDeviceObj.get());
108 }
109 
register_android_view_InputDevice(JNIEnv * env)110 int register_android_view_InputDevice(JNIEnv* env)
111 {
112     gInputDeviceClassInfo.clazz = FindClassOrDie(env, "android/view/InputDevice");
113     gInputDeviceClassInfo.clazz = MakeGlobalRefOrDie(env, gInputDeviceClassInfo.clazz);
114 
115     gInputDeviceClassInfo.ctor = GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "<init>",
116                                                   "(IIILjava/lang/String;IILjava/lang/"
117                                                   "String;ZIILandroid/view/KeyCharacterMap;Ljava/"
118                                                   "lang/String;Ljava/lang/String;ZZZZZIII)V");
119 
120     gInputDeviceClassInfo.addMotionRange =
121             GetMethodIDOrDie(env, gInputDeviceClassInfo.clazz, "addMotionRange", "(IIFFFFF)V");
122     return 0;
123 }
124 
125 }; // namespace android
126