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