Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 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 | |
| 26 | namespace android { |
| 27 | |
| 28 | static struct { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 29 | jfieldID ptr; |
| 30 | } gInputApplicationHandleClassInfo; |
| 31 | |
| 32 | static Mutex gHandleMutex; |
| 33 | |
| 34 | |
| 35 | // --- NativeInputApplicationHandle --- |
| 36 | |
| 37 | NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) : |
| 38 | mObjWeak(objWeak) { |
| 39 | } |
| 40 | |
| 41 | NativeInputApplicationHandle::~NativeInputApplicationHandle() { |
| 42 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 43 | env->DeleteWeakGlobalRef(mObjWeak); |
| 44 | } |
| 45 | |
| 46 | jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) { |
| 47 | return env->NewLocalRef(mObjWeak); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | // --- Global functions --- |
| 52 | |
| 53 | sp<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 | |
| 78 | static 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 | |
| 91 | static 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); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame^] | 99 | LOG_FATAL_IF(! var, "Unable to find class " className); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 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 | |
| 105 | int register_android_server_InputApplicationHandle(JNIEnv* env) { |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 106 | int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputApplicationHandle", |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 107 | gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods)); |
| 108 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 109 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame^] | 110 | jclass clazz; |
| 111 | FIND_CLASS(clazz, "com/android/server/wm/InputApplicationHandle"); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 112 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame^] | 113 | GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 114 | "ptr", "I"); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | } /* namespace android */ |