com_android_server_InputApplicationHandle.cpp revision 17cc33a35729733aaa0a7706f38b1c45f0b1590a
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 "JNIHelp.h" 20#include "jni.h" 21#include <android_runtime/AndroidRuntime.h> 22#include <utils/threads.h> 23 24#include "com_android_server_InputApplicationHandle.h" 25 26namespace android { 27 28static struct { 29 jfieldID ptr; 30} gInputApplicationHandleClassInfo; 31 32static Mutex gHandleMutex; 33 34 35// --- NativeInputApplicationHandle --- 36 37NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) : 38 mObjWeak(objWeak) { 39} 40 41NativeInputApplicationHandle::~NativeInputApplicationHandle() { 42 JNIEnv* env = AndroidRuntime::getJNIEnv(); 43 env->DeleteWeakGlobalRef(mObjWeak); 44} 45 46jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) { 47 return env->NewLocalRef(mObjWeak); 48} 49 50 51// --- Global functions --- 52 53sp<InputApplicationHandle> android_server_InputApplicationHandle_getHandle( 54 JNIEnv* env, jobject inputApplicationHandleObj) { 55 if (!inputApplicationHandleObj) { 56 return NULL; 57 } 58 59 AutoMutex _l(gHandleMutex); 60 61 int ptr = env->GetIntField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr); 62 NativeInputApplicationHandle* handle; 63 if (ptr) { 64 handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr); 65 } else { 66 jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj); 67 handle = new NativeInputApplicationHandle(objWeak); 68 handle->incStrong(inputApplicationHandleObj); 69 env->SetIntField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr, 70 reinterpret_cast<int>(handle)); 71 } 72 return handle; 73} 74 75 76// --- JNI --- 77 78static void android_server_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) { 79 AutoMutex _l(gHandleMutex); 80 81 int ptr = env->GetIntField(obj, gInputApplicationHandleClassInfo.ptr); 82 if (ptr) { 83 env->SetIntField(obj, gInputApplicationHandleClassInfo.ptr, 0); 84 85 NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr); 86 handle->decStrong(obj); 87 } 88} 89 90 91static JNINativeMethod gInputApplicationHandleMethods[] = { 92 /* name, signature, funcPtr */ 93 { "nativeDispose", "()V", 94 (void*) android_server_InputApplicationHandle_nativeDispose }, 95}; 96 97#define FIND_CLASS(var, className) \ 98 var = env->FindClass(className); \ 99 LOG_FATAL_IF(! var, "Unable to find class " className); 100 101#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ 102 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ 103 LOG_FATAL_IF(! var, "Unable to find field " fieldName); 104 105int register_android_server_InputApplicationHandle(JNIEnv* env) { 106 int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputApplicationHandle", 107 gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods)); 108 LOG_FATAL_IF(res < 0, "Unable to register native methods."); 109 110 jclass clazz; 111 FIND_CLASS(clazz, "com/android/server/wm/InputApplicationHandle"); 112 113 GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz, 114 "ptr", "I"); 115 116 return 0; 117} 118 119} /* namespace android */ 120