• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 dispatchingTimeoutMillis;
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     ScopedLocalRef<jobject> obj(env, env->NewLocalRef(mObjWeak));
58     if (!obj.get()) {
59         return false;
60     }
61     if (mInfo.token.get() != nullptr) {
62         // The java fields are immutable, so it doesn't need to update again.
63         return true;
64     }
65 
66     mInfo.name = getStringField(env, obj.get(), gInputApplicationHandleClassInfo.name, "<null>");
67 
68     mInfo.dispatchingTimeoutMillis =
69             env->GetLongField(obj.get(), gInputApplicationHandleClassInfo.dispatchingTimeoutMillis);
70 
71     ScopedLocalRef<jobject> tokenObj(env, env->GetObjectField(obj.get(),
72             gInputApplicationHandleClassInfo.token));
73     if (tokenObj.get()) {
74         mInfo.token = ibinderForJavaObject(env, tokenObj.get());
75     } else {
76         mInfo.token.clear();
77     }
78 
79     return mInfo.token.get() != nullptr;
80 }
81 
82 // --- Global functions ---
83 
android_view_InputApplicationHandle_getHandle(JNIEnv * env,jobject inputApplicationHandleObj)84 std::shared_ptr<InputApplicationHandle> android_view_InputApplicationHandle_getHandle(
85         JNIEnv* env, jobject inputApplicationHandleObj) {
86     if (!inputApplicationHandleObj) {
87         return NULL;
88     }
89 
90     AutoMutex _l(gHandleMutex);
91     jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
92     std::shared_ptr<NativeInputApplicationHandle>* handle;
93     if (ptr) {
94         handle = reinterpret_cast<std::shared_ptr<NativeInputApplicationHandle>*>(ptr);
95     } else {
96         jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj);
97         handle = new std::shared_ptr(std::make_shared<NativeInputApplicationHandle>(objWeak));
98         env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
99                 reinterpret_cast<jlong>(handle));
100     }
101     return *handle;
102 }
103 
104 // --- JNI ---
105 
android_view_InputApplicationHandle_nativeDispose(JNIEnv * env,jobject obj)106 static void android_view_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
107     AutoMutex _l(gHandleMutex);
108 
109     jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr);
110     if (ptr) {
111         env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0);
112 
113         std::shared_ptr<NativeInputApplicationHandle>* handle =
114                 reinterpret_cast<std::shared_ptr<NativeInputApplicationHandle>*>(ptr);
115         delete handle;
116     }
117 }
118 
119 
120 static const JNINativeMethod gInputApplicationHandleMethods[] = {
121     /* name, signature, funcPtr */
122     { "nativeDispose", "()V",
123             (void*) android_view_InputApplicationHandle_nativeDispose },
124 };
125 
126 #define FIND_CLASS(var, className) \
127         var = env->FindClass(className); \
128         LOG_FATAL_IF(! (var), "Unable to find class " className);
129 
130 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
131         var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
132         LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
133 
register_android_view_InputApplicationHandle(JNIEnv * env)134 int register_android_view_InputApplicationHandle(JNIEnv* env) {
135     int res = jniRegisterNativeMethods(env, "android/view/InputApplicationHandle",
136             gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
137     (void) res;  // Faked use when LOG_NDEBUG.
138     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
139 
140     jclass clazz;
141     FIND_CLASS(clazz, "android/view/InputApplicationHandle");
142 
143     GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
144             "ptr", "J");
145 
146     GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz,
147             "name", "Ljava/lang/String;");
148 
149     GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutMillis, clazz,
150                  "dispatchingTimeoutMillis", "J");
151 
152     GET_FIELD_ID(gInputApplicationHandleClassInfo.token, clazz,
153             "token", "Landroid/os/IBinder;");
154 
155     return 0;
156 }
157 
158 } /* namespace android */
159