2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "InputApplicationHandle"
21 #include <android_runtime/AndroidRuntime.h>
22 #include <utils/threads.h>
24 #include "com_android_server_input_InputApplicationHandle.h"
31 jfieldID dispatchingTimeoutNanos;
32 } gInputApplicationHandleClassInfo;
34 static Mutex gHandleMutex;
37 // --- NativeInputApplicationHandle ---
39 NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) :
43 NativeInputApplicationHandle::~NativeInputApplicationHandle() {
44 JNIEnv* env = AndroidRuntime::getJNIEnv();
45 env->DeleteWeakGlobalRef(mObjWeak);
48 jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) {
49 return env->NewLocalRef(mObjWeak);
52 bool NativeInputApplicationHandle::updateInfo() {
53 JNIEnv* env = AndroidRuntime::getJNIEnv();
54 jobject obj = env->NewLocalRef(mObjWeak);
61 mInfo = new InputApplicationInfo();
64 jstring nameObj = jstring(env->GetObjectField(obj,
65 gInputApplicationHandleClassInfo.name));
67 const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
68 mInfo->name.setTo(nameStr);
69 env->ReleaseStringUTFChars(nameObj, nameStr);
70 env->DeleteLocalRef(nameObj);
72 mInfo->name.setTo("<null>");
75 mInfo->dispatchingTimeout = env->GetLongField(obj,
76 gInputApplicationHandleClassInfo.dispatchingTimeoutNanos);
78 env->DeleteLocalRef(obj);
83 // --- Global functions ---
85 sp<InputApplicationHandle> android_server_InputApplicationHandle_getHandle(
86 JNIEnv* env, jobject inputApplicationHandleObj) {
87 if (!inputApplicationHandleObj) {
91 AutoMutex _l(gHandleMutex);
93 int ptr = env->GetIntField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
94 NativeInputApplicationHandle* handle;
96 handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
98 jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj);
99 handle = new NativeInputApplicationHandle(objWeak);
100 handle->incStrong((void*)android_server_InputApplicationHandle_getHandle);
101 env->SetIntField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
102 reinterpret_cast<int>(handle));
110 static void android_server_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
111 AutoMutex _l(gHandleMutex);
113 int ptr = env->GetIntField(obj, gInputApplicationHandleClassInfo.ptr);
115 env->SetIntField(obj, gInputApplicationHandleClassInfo.ptr, 0);
117 NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
118 handle->decStrong((void*)android_server_InputApplicationHandle_getHandle);
123 static JNINativeMethod gInputApplicationHandleMethods[] = {
124 /* name, signature, funcPtr */
125 { "nativeDispose", "()V",
126 (void*) android_server_InputApplicationHandle_nativeDispose },
129 #define FIND_CLASS(var, className) \
130 var = env->FindClass(className); \
131 LOG_FATAL_IF(! var, "Unable to find class " className);
133 #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
134 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
135 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
137 int register_android_server_InputApplicationHandle(JNIEnv* env) {
138 int res = jniRegisterNativeMethods(env, "com/android/server/input/InputApplicationHandle",
139 gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
140 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
143 FIND_CLASS(clazz, "com/android/server/input/InputApplicationHandle");
145 GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
148 GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz,
149 "name", "Ljava/lang/String;");
151 GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutNanos,
153 "dispatchingTimeoutNanos", "J");
158 } /* namespace android */