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 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 19 | #include <nativehelper/JNIHelp.h> |
Siarhei Vishniakou | e890f59 | 2018-10-15 18:53:06 -0700 | [diff] [blame] | 20 | #include "core_jni_helpers.h" |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 21 | #include "jni.h" |
| 22 | #include <android_runtime/AndroidRuntime.h> |
| 23 | #include <utils/threads.h> |
| 24 | |
Robert Carr | e1db320 | 2018-07-23 15:24:59 -0700 | [diff] [blame] | 25 | #include "android_hardware_input_InputApplicationHandle.h" |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 26 | #include "android_util_Binder.h" |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | static struct { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 31 | jfieldID ptr; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 32 | jfieldID name; |
| 33 | jfieldID dispatchingTimeoutNanos; |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 34 | jfieldID token; |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 35 | } gInputApplicationHandleClassInfo; |
| 36 | |
| 37 | static Mutex gHandleMutex; |
| 38 | |
| 39 | |
| 40 | // --- NativeInputApplicationHandle --- |
| 41 | |
| 42 | NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) : |
| 43 | mObjWeak(objWeak) { |
| 44 | } |
| 45 | |
| 46 | NativeInputApplicationHandle::~NativeInputApplicationHandle() { |
| 47 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 48 | env->DeleteWeakGlobalRef(mObjWeak); |
| 49 | } |
| 50 | |
| 51 | jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) { |
| 52 | return env->NewLocalRef(mObjWeak); |
| 53 | } |
| 54 | |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 55 | bool NativeInputApplicationHandle::updateInfo() { |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 56 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 57 | jobject obj = env->NewLocalRef(mObjWeak); |
| 58 | if (!obj) { |
| 59 | return false; |
| 60 | } |
Riddle Hsu | a5eb010 | 2020-12-03 15:57:36 +0800 | [diff] [blame^] | 61 | if (mInfo.token.get() != nullptr) { |
| 62 | // The java fields are immutable, so it doesn't need to update again. |
| 63 | env->DeleteLocalRef(obj); |
| 64 | return true; |
| 65 | } |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 66 | |
Arthur Hung | e7dc501 | 2019-03-20 17:04:26 +0800 | [diff] [blame] | 67 | mInfo.name = getStringField(env, obj, gInputApplicationHandleClassInfo.name, "<null>"); |
Jeff Brown | cc4f7db | 2011-08-30 20:34:48 -0700 | [diff] [blame] | 68 | |
Arthur Hung | e7dc501 | 2019-03-20 17:04:26 +0800 | [diff] [blame] | 69 | mInfo.dispatchingTimeout = env->GetLongField(obj, |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 70 | gInputApplicationHandleClassInfo.dispatchingTimeoutNanos); |
| 71 | |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 72 | jobject tokenObj = env->GetObjectField(obj, |
| 73 | gInputApplicationHandleClassInfo.token); |
| 74 | if (tokenObj) { |
Arthur Hung | e7dc501 | 2019-03-20 17:04:26 +0800 | [diff] [blame] | 75 | mInfo.token = ibinderForJavaObject(env, tokenObj); |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 76 | env->DeleteLocalRef(tokenObj); |
| 77 | } else { |
Arthur Hung | e7dc501 | 2019-03-20 17:04:26 +0800 | [diff] [blame] | 78 | mInfo.token.clear(); |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 81 | env->DeleteLocalRef(obj); |
Arthur Hung | e7dc501 | 2019-03-20 17:04:26 +0800 | [diff] [blame] | 82 | return mInfo.token.get() != nullptr; |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 85 | |
| 86 | // --- Global functions --- |
| 87 | |
Riddle Hsu | cd958bc | 2019-01-23 15:40:26 +0800 | [diff] [blame] | 88 | sp<InputApplicationHandle> android_view_InputApplicationHandle_getHandle( |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 89 | JNIEnv* env, jobject inputApplicationHandleObj) { |
| 90 | if (!inputApplicationHandleObj) { |
| 91 | return NULL; |
| 92 | } |
| 93 | |
| 94 | AutoMutex _l(gHandleMutex); |
| 95 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 96 | jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 97 | NativeInputApplicationHandle* handle; |
| 98 | if (ptr) { |
| 99 | handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr); |
| 100 | } else { |
| 101 | jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj); |
| 102 | handle = new NativeInputApplicationHandle(objWeak); |
Riddle Hsu | cd958bc | 2019-01-23 15:40:26 +0800 | [diff] [blame] | 103 | handle->incStrong((void*)android_view_InputApplicationHandle_getHandle); |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 104 | env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr, |
| 105 | reinterpret_cast<jlong>(handle)); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 106 | } |
| 107 | return handle; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | // --- JNI --- |
| 112 | |
Riddle Hsu | cd958bc | 2019-01-23 15:40:26 +0800 | [diff] [blame] | 113 | static void android_view_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 114 | AutoMutex _l(gHandleMutex); |
| 115 | |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 116 | jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 117 | if (ptr) { |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 118 | env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 119 | |
| 120 | NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr); |
Riddle Hsu | cd958bc | 2019-01-23 15:40:26 +0800 | [diff] [blame] | 121 | handle->decStrong((void*)android_view_InputApplicationHandle_getHandle); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 126 | static const JNINativeMethod gInputApplicationHandleMethods[] = { |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 127 | /* name, signature, funcPtr */ |
| 128 | { "nativeDispose", "()V", |
Riddle Hsu | cd958bc | 2019-01-23 15:40:26 +0800 | [diff] [blame] | 129 | (void*) android_view_InputApplicationHandle_nativeDispose }, |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #define FIND_CLASS(var, className) \ |
| 133 | var = env->FindClass(className); \ |
Chih-Hung Hsieh | 6c89616 | 2016-05-19 15:29:38 -0700 | [diff] [blame] | 134 | LOG_FATAL_IF(! (var), "Unable to find class " className); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 135 | |
| 136 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 137 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
Chih-Hung Hsieh | 6c89616 | 2016-05-19 15:29:38 -0700 | [diff] [blame] | 138 | LOG_FATAL_IF(! (var), "Unable to find field " fieldName); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 139 | |
Riddle Hsu | cd958bc | 2019-01-23 15:40:26 +0800 | [diff] [blame] | 140 | int register_android_view_InputApplicationHandle(JNIEnv* env) { |
Robert Carr | 788f574 | 2018-07-30 17:46:45 -0700 | [diff] [blame] | 141 | int res = jniRegisterNativeMethods(env, "android/view/InputApplicationHandle", |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 142 | gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods)); |
Bernhard Rosenkränzer | 9c1c90e | 2014-11-12 14:45:58 +0100 | [diff] [blame] | 143 | (void) res; // Faked use when LOG_NDEBUG. |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 144 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 145 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 146 | jclass clazz; |
Robert Carr | 788f574 | 2018-07-30 17:46:45 -0700 | [diff] [blame] | 147 | FIND_CLASS(clazz, "android/view/InputApplicationHandle"); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 148 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 149 | GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz, |
Ashok Bhat | 7e2a9dc | 2014-01-02 19:43:30 +0000 | [diff] [blame] | 150 | "ptr", "J"); |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 151 | |
Jeff Brown | 9302c87 | 2011-07-13 22:51:29 -0700 | [diff] [blame] | 152 | GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz, |
| 153 | "name", "Ljava/lang/String;"); |
| 154 | |
| 155 | GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutNanos, |
| 156 | clazz, |
| 157 | "dispatchingTimeoutNanos", "J"); |
| 158 | |
Robert Carr | 0bcbe64 | 2018-10-11 19:07:43 -0700 | [diff] [blame] | 159 | GET_FIELD_ID(gInputApplicationHandleClassInfo.token, clazz, |
| 160 | "token", "Landroid/os/IBinder;"); |
| 161 | |
Jeff Brown | 928e054 | 2011-01-10 11:17:36 -0800 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | } /* namespace android */ |