1 /*
2  * Copyright (C) 2011 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 "InputApplicationHandle"
18 
19 #include <nativehelper/JNIHelp.h>
20 #include "core_jni_helpers.h"
21 #include "jni.h"
22 #include <android_runtime/AndroidRuntime.h>
23 #include <utils/threads.h>
24 
25 #include "android_hardware_input_InputApplicationHandle.h"
26 #include "android_util_Binder.h"
27 
28 namespace android {
29 
30 static struct {
31     jclass clazz;
32     jmethodID ctor;
33     jfieldID ptr;
34     jfieldID name;
35     jfieldID dispatchingTimeoutMillis;
36     jfieldID token;
37 } gInputApplicationHandleClassInfo;
38 
39 static Mutex gHandleMutex;
40 
41 
42 // --- NativeInputApplicationHandle ---
43 
NativeInputApplicationHandle(jweak objWeak)44 NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) :
45         mObjWeak(objWeak) {
46 }
47 
~NativeInputApplicationHandle()48 NativeInputApplicationHandle::~NativeInputApplicationHandle() {
49     JNIEnv* env = AndroidRuntime::getJNIEnv();
50     env->DeleteWeakGlobalRef(mObjWeak);
51 }
52 
getInputApplicationHandleObjLocalRef(JNIEnv * env)53 jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) {
54     return env->NewLocalRef(mObjWeak);
55 }
56 
updateInfo()57 bool NativeInputApplicationHandle::updateInfo() {
58     JNIEnv* env = AndroidRuntime::getJNIEnv();
59     ScopedLocalRef<jobject> obj(env, env->NewLocalRef(mObjWeak));
60     if (!obj.get()) {
61         return false;
62     }
63     if (mInfo.token.get() != nullptr) {
64         // The java fields are immutable, so it doesn't need to update again.
65         return true;
66     }
67 
68     mInfo.name = getStringField(env, obj.get(), gInputApplicationHandleClassInfo.name, "<null>");
69 
70     mInfo.dispatchingTimeoutMillis =
71             env->GetLongField(obj.get(), gInputApplicationHandleClassInfo.dispatchingTimeoutMillis);
72 
73     ScopedLocalRef<jobject> tokenObj(env, env->GetObjectField(obj.get(),
74             gInputApplicationHandleClassInfo.token));
75     if (tokenObj.get()) {
76         mInfo.token = ibinderForJavaObject(env, tokenObj.get());
77     } else {
78         mInfo.token.clear();
79     }
80 
81     return mInfo.token.get() != nullptr;
82 }
83 
84 // --- Global functions ---
85 
android_view_InputApplicationHandle_getHandle(JNIEnv * env,jobject inputApplicationHandleObj)86 std::shared_ptr<InputApplicationHandle> android_view_InputApplicationHandle_getHandle(
87         JNIEnv* env, jobject inputApplicationHandleObj) {
88     if (!inputApplicationHandleObj) {
89         return NULL;
90     }
91 
92     AutoMutex _l(gHandleMutex);
93     jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
94     std::shared_ptr<NativeInputApplicationHandle>* handle;
95     if (ptr) {
96         handle = reinterpret_cast<std::shared_ptr<NativeInputApplicationHandle>*>(ptr);
97     } else {
98         jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj);
99         handle = new std::shared_ptr(std::make_shared<NativeInputApplicationHandle>(objWeak));
100         env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
101                 reinterpret_cast<jlong>(handle));
102     }
103     return *handle;
104 }
105 
android_view_InputApplicationHandle_fromInputApplicationInfo(JNIEnv * env,gui::InputApplicationInfo inputApplicationInfo)106 jobject android_view_InputApplicationHandle_fromInputApplicationInfo(
107         JNIEnv* env, gui::InputApplicationInfo inputApplicationInfo) {
108     jobject binderObject = javaObjectForIBinder(env, inputApplicationInfo.token);
109     ScopedLocalRef<jstring> name(env, env->NewStringUTF(inputApplicationInfo.name.data()));
110     return env->NewObject(gInputApplicationHandleClassInfo.clazz,
111                           gInputApplicationHandleClassInfo.ctor, binderObject, name.get(),
112                           inputApplicationInfo.dispatchingTimeoutMillis);
113 }
114 
115 // --- JNI ---
116 
android_view_InputApplicationHandle_nativeDispose(JNIEnv * env,jobject obj)117 static void android_view_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
118     AutoMutex _l(gHandleMutex);
119 
120     jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr);
121     if (ptr) {
122         env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0);
123 
124         std::shared_ptr<NativeInputApplicationHandle>* handle =
125                 reinterpret_cast<std::shared_ptr<NativeInputApplicationHandle>*>(ptr);
126         delete handle;
127     }
128 }
129 
130 
131 static const JNINativeMethod gInputApplicationHandleMethods[] = {
132     /* name, signature, funcPtr */
133     { "nativeDispose", "()V",
134             (void*) android_view_InputApplicationHandle_nativeDispose },
135 };
136 
137 #define FIND_CLASS(var, className) \
138         var = env->FindClass(className); \
139         LOG_FATAL_IF(! (var), "Unable to find class " className);
140 
141 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
142         var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
143         LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
144 
145 #define GET_METHOD_ID(var, clazz, methodName, methodSignature)  \
146     var = env->GetMethodID(clazz, methodName, methodSignature); \
147     LOG_ALWAYS_FATAL_IF(!(var), "Unable to find method " methodName);
148 
register_android_view_InputApplicationHandle(JNIEnv * env)149 int register_android_view_InputApplicationHandle(JNIEnv* env) {
150     int res = jniRegisterNativeMethods(env, "android/view/InputApplicationHandle",
151             gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
152     (void) res;  // Faked use when LOG_NDEBUG.
153     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
154 
155     jclass clazz;
156     FIND_CLASS(clazz, "android/view/InputApplicationHandle");
157     gInputApplicationHandleClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
158 
159     GET_METHOD_ID(gInputApplicationHandleClassInfo.ctor, clazz, "<init>",
160                   "(Landroid/os/IBinder;Ljava/lang/String;J)V");
161 
162     GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
163             "ptr", "J");
164 
165     GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz,
166             "name", "Ljava/lang/String;");
167 
168     GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutMillis, clazz,
169                  "dispatchingTimeoutMillis", "J");
170 
171     GET_FIELD_ID(gInputApplicationHandleClassInfo.token, clazz,
172             "token", "Landroid/os/IBinder;");
173 
174     return 0;
175 }
176 
177 } /* namespace android */
178