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     jfieldID ptr;
32     jfieldID name;
33     jfieldID dispatchingTimeoutNanos;
34     jfieldID token;
35 } gInputApplicationHandleClassInfo;
36 
37 static Mutex gHandleMutex;
38 
39 
40 // --- NativeInputApplicationHandle ---
41 
NativeInputApplicationHandle(jweak objWeak)42 NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) :
43         mObjWeak(objWeak) {
44 }
45 
~NativeInputApplicationHandle()46 NativeInputApplicationHandle::~NativeInputApplicationHandle() {
47     JNIEnv* env = AndroidRuntime::getJNIEnv();
48     env->DeleteWeakGlobalRef(mObjWeak);
49 }
50 
getInputApplicationHandleObjLocalRef(JNIEnv * env)51 jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) {
52     return env->NewLocalRef(mObjWeak);
53 }
54 
updateInfo()55 bool NativeInputApplicationHandle::updateInfo() {
56     JNIEnv* env = AndroidRuntime::getJNIEnv();
57     jobject obj = env->NewLocalRef(mObjWeak);
58     if (!obj) {
59         return false;
60     }
61 
62     mInfo.name = getStringField(env, obj, gInputApplicationHandleClassInfo.name, "<null>");
63 
64     mInfo.dispatchingTimeout = env->GetLongField(obj,
65             gInputApplicationHandleClassInfo.dispatchingTimeoutNanos);
66 
67     jobject tokenObj = env->GetObjectField(obj,
68             gInputApplicationHandleClassInfo.token);
69     if (tokenObj) {
70         mInfo.token = ibinderForJavaObject(env, tokenObj);
71         env->DeleteLocalRef(tokenObj);
72     } else {
73         mInfo.token.clear();
74     }
75 
76     env->DeleteLocalRef(obj);
77     return mInfo.token.get() != nullptr;
78 }
79 
80 
81 // --- Global functions ---
82 
android_view_InputApplicationHandle_getHandle(JNIEnv * env,jobject inputApplicationHandleObj)83 sp<InputApplicationHandle> android_view_InputApplicationHandle_getHandle(
84         JNIEnv* env, jobject inputApplicationHandleObj) {
85     if (!inputApplicationHandleObj) {
86         return NULL;
87     }
88 
89     AutoMutex _l(gHandleMutex);
90 
91     jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
92     NativeInputApplicationHandle* handle;
93     if (ptr) {
94         handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
95     } else {
96         jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj);
97         handle = new NativeInputApplicationHandle(objWeak);
98         handle->incStrong((void*)android_view_InputApplicationHandle_getHandle);
99         env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
100                 reinterpret_cast<jlong>(handle));
101     }
102     return handle;
103 }
104 
105 
106 // --- JNI ---
107 
android_view_InputApplicationHandle_nativeDispose(JNIEnv * env,jobject obj)108 static void android_view_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
109     AutoMutex _l(gHandleMutex);
110 
111     jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr);
112     if (ptr) {
113         env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0);
114 
115         NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
116         handle->decStrong((void*)android_view_InputApplicationHandle_getHandle);
117     }
118 }
119 
120 
121 static const JNINativeMethod gInputApplicationHandleMethods[] = {
122     /* name, signature, funcPtr */
123     { "nativeDispose", "()V",
124             (void*) android_view_InputApplicationHandle_nativeDispose },
125 };
126 
127 #define FIND_CLASS(var, className) \
128         var = env->FindClass(className); \
129         LOG_FATAL_IF(! (var), "Unable to find class " className);
130 
131 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
132         var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
133         LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
134 
register_android_view_InputApplicationHandle(JNIEnv * env)135 int register_android_view_InputApplicationHandle(JNIEnv* env) {
136     int res = jniRegisterNativeMethods(env, "android/view/InputApplicationHandle",
137             gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
138     (void) res;  // Faked use when LOG_NDEBUG.
139     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
140 
141     jclass clazz;
142     FIND_CLASS(clazz, "android/view/InputApplicationHandle");
143 
144     GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
145             "ptr", "J");
146 
147     GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz,
148             "name", "Ljava/lang/String;");
149 
150     GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutNanos,
151             clazz,
152             "dispatchingTimeoutNanos", "J");
153 
154     GET_FIELD_ID(gInputApplicationHandleClassInfo.token, clazz,
155             "token", "Landroid/os/IBinder;");
156 
157     return 0;
158 }
159 
160 } /* namespace android */
161