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 | |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 24 | #include "com_android_server_input_InputApplicationHandle.h" |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | static struct { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 29 | jfieldID ptr; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 30 | jfieldID name; |
| 31 | jfieldID dispatchingTimeoutNanos; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 32 | } gInputApplicationHandleClassInfo; |
| 33 | |
| 34 | static Mutex gHandleMutex; |
| 35 | |
| 36 | |
| 37 | // --- NativeInputApplicationHandle --- |
| 38 | |
| 39 | NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) : |
| 40 | mObjWeak(objWeak) { |
| 41 | } |
| 42 | |
| 43 | NativeInputApplicationHandle::~NativeInputApplicationHandle() { |
| 44 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 45 | env->DeleteWeakGlobalRef(mObjWeak); |
| 46 | } |
| 47 | |
| 48 | jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) { |
| 49 | return env->NewLocalRef(mObjWeak); |
| 50 | } |
| 51 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 52 | bool NativeInputApplicationHandle::updateInfo() { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 53 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 54 | jobject obj = env->NewLocalRef(mObjWeak); |
| 55 | if (!obj) { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 56 | releaseInfo(); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 60 | if (!mInfo) { |
| 61 | mInfo = new InputApplicationInfo(); |
| 62 | } |
| 63 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 64 | jstring nameObj = jstring(env->GetObjectField(obj, |
| 65 | gInputApplicationHandleClassInfo.name)); |
| 66 | if (nameObj) { |
| 67 | const char* nameStr = env->GetStringUTFChars(nameObj, NULL); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 68 | mInfo->name.setTo(nameStr); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 69 | env->ReleaseStringUTFChars(nameObj, nameStr); |
| 70 | env->DeleteLocalRef(nameObj); |
| 71 | } else { |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 72 | mInfo->name.setTo("<null>"); |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 75 | mInfo->dispatchingTimeout = env->GetLongField(obj, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 76 | gInputApplicationHandleClassInfo.dispatchingTimeoutNanos); |
| 77 | |
| 78 | env->DeleteLocalRef(obj); |
| 79 | return true; |
| 80 | } |
| 81 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 82 | |
| 83 | // --- Global functions --- |
| 84 | |
| 85 | sp<InputApplicationHandle> android_server_InputApplicationHandle_getHandle( |
| 86 | JNIEnv* env, jobject inputApplicationHandleObj) { |
| 87 | if (!inputApplicationHandleObj) { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | AutoMutex _l(gHandleMutex); |
| 92 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 93 | jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 94 | NativeInputApplicationHandle* handle; |
| 95 | if (ptr) { |
| 96 | handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr); |
| 97 | } else { |
| 98 | jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj); |
| 99 | handle = new NativeInputApplicationHandle(objWeak); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 100 | handle->incStrong((void*)android_server_InputApplicationHandle_getHandle); |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 101 | env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr, |
| 102 | reinterpret_cast<jlong>(handle)); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 103 | } |
| 104 | return handle; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | // --- JNI --- |
| 109 | |
| 110 | static void android_server_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) { |
| 111 | AutoMutex _l(gHandleMutex); |
| 112 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 113 | jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 114 | if (ptr) { |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 115 | env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 116 | |
| 117 | NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 118 | handle->decStrong((void*)android_server_InputApplicationHandle_getHandle); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame^] | 123 | static const JNINativeMethod gInputApplicationHandleMethods[] = { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 124 | /* name, signature, funcPtr */ |
| 125 | { "nativeDispose", "()V", |
| 126 | (void*) android_server_InputApplicationHandle_nativeDispose }, |
| 127 | }; |
| 128 | |
| 129 | #define FIND_CLASS(var, className) \ |
| 130 | var = env->FindClass(className); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 131 | LOG_FATAL_IF(! var, "Unable to find class " className); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 132 | |
| 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); |
| 136 | |
| 137 | int register_android_server_InputApplicationHandle(JNIEnv* env) { |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 138 | int res = jniRegisterNativeMethods(env, "com/android/server/input/InputApplicationHandle", |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 139 | gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods)); |
Bernhard Rosenkränzer | 9c1c90e | 2014-11-12 14:45:58 +0100 | [diff] [blame] | 140 | (void) res; // Faked use when LOG_NDEBUG. |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 141 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 142 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 143 | jclass clazz; |
Jeff Brown | 4532e61 | 2012-04-05 14:27:12 -0700 | [diff] [blame] | 144 | FIND_CLASS(clazz, "com/android/server/input/InputApplicationHandle"); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 145 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 146 | GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz, |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 147 | "ptr", "J"); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 148 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 149 | GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz, |
| 150 | "name", "Ljava/lang/String;"); |
| 151 | |
| 152 | GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutNanos, |
| 153 | clazz, |
| 154 | "dispatchingTimeoutNanos", "J"); |
| 155 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | } /* namespace android */ |