blob: c1ecae8618273ce1591d7c7297b7eec664600ceb [file] [log] [blame]
Jeff Brown928e0542011-01-10 11:17:36 -08001/*
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 Moreland2279b252017-07-19 09:50:45 -070019#include <nativehelper/JNIHelp.h>
Siarhei Vishniakoue890f592018-10-15 18:53:06 -070020#include "core_jni_helpers.h"
Jeff Brown928e0542011-01-10 11:17:36 -080021#include "jni.h"
22#include <android_runtime/AndroidRuntime.h>
23#include <utils/threads.h>
24
Robert Carre1db3202018-07-23 15:24:59 -070025#include "android_hardware_input_InputApplicationHandle.h"
Robert Carr0bcbe642018-10-11 19:07:43 -070026#include "android_util_Binder.h"
Jeff Brown928e0542011-01-10 11:17:36 -080027
28namespace android {
29
30static struct {
Jeff Brown928e0542011-01-10 11:17:36 -080031 jfieldID ptr;
Jeff Brown9302c872011-07-13 22:51:29 -070032 jfieldID name;
33 jfieldID dispatchingTimeoutNanos;
Robert Carr0bcbe642018-10-11 19:07:43 -070034 jfieldID token;
Jeff Brown928e0542011-01-10 11:17:36 -080035} gInputApplicationHandleClassInfo;
36
37static Mutex gHandleMutex;
38
39
40// --- NativeInputApplicationHandle ---
41
42NativeInputApplicationHandle::NativeInputApplicationHandle(jweak objWeak) :
43 mObjWeak(objWeak) {
44}
45
46NativeInputApplicationHandle::~NativeInputApplicationHandle() {
47 JNIEnv* env = AndroidRuntime::getJNIEnv();
48 env->DeleteWeakGlobalRef(mObjWeak);
49}
50
51jobject NativeInputApplicationHandle::getInputApplicationHandleObjLocalRef(JNIEnv* env) {
52 return env->NewLocalRef(mObjWeak);
53}
54
Jeff Browncc4f7db2011-08-30 20:34:48 -070055bool NativeInputApplicationHandle::updateInfo() {
Jeff Brown9302c872011-07-13 22:51:29 -070056 JNIEnv* env = AndroidRuntime::getJNIEnv();
57 jobject obj = env->NewLocalRef(mObjWeak);
58 if (!obj) {
59 return false;
60 }
Riddle Hsua5eb0102020-12-03 15:57:36 +080061 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 Brown9302c872011-07-13 22:51:29 -070066
Arthur Hunge7dc5012019-03-20 17:04:26 +080067 mInfo.name = getStringField(env, obj, gInputApplicationHandleClassInfo.name, "<null>");
Jeff Browncc4f7db2011-08-30 20:34:48 -070068
Arthur Hunge7dc5012019-03-20 17:04:26 +080069 mInfo.dispatchingTimeout = env->GetLongField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -070070 gInputApplicationHandleClassInfo.dispatchingTimeoutNanos);
71
Robert Carr0bcbe642018-10-11 19:07:43 -070072 jobject tokenObj = env->GetObjectField(obj,
73 gInputApplicationHandleClassInfo.token);
74 if (tokenObj) {
Arthur Hunge7dc5012019-03-20 17:04:26 +080075 mInfo.token = ibinderForJavaObject(env, tokenObj);
Robert Carr0bcbe642018-10-11 19:07:43 -070076 env->DeleteLocalRef(tokenObj);
77 } else {
Arthur Hunge7dc5012019-03-20 17:04:26 +080078 mInfo.token.clear();
Robert Carr0bcbe642018-10-11 19:07:43 -070079 }
80
Jeff Brown9302c872011-07-13 22:51:29 -070081 env->DeleteLocalRef(obj);
Arthur Hunge7dc5012019-03-20 17:04:26 +080082 return mInfo.token.get() != nullptr;
Jeff Brown9302c872011-07-13 22:51:29 -070083}
84
Jeff Brown928e0542011-01-10 11:17:36 -080085
86// --- Global functions ---
87
Riddle Hsucd958bc2019-01-23 15:40:26 +080088sp<InputApplicationHandle> android_view_InputApplicationHandle_getHandle(
Jeff Brown928e0542011-01-10 11:17:36 -080089 JNIEnv* env, jobject inputApplicationHandleObj) {
90 if (!inputApplicationHandleObj) {
91 return NULL;
92 }
93
94 AutoMutex _l(gHandleMutex);
95
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +000096 jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -080097 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 Hsucd958bc2019-01-23 15:40:26 +0800103 handle->incStrong((void*)android_view_InputApplicationHandle_getHandle);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000104 env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
105 reinterpret_cast<jlong>(handle));
Jeff Brown928e0542011-01-10 11:17:36 -0800106 }
107 return handle;
108}
109
110
111// --- JNI ---
112
Riddle Hsucd958bc2019-01-23 15:40:26 +0800113static void android_view_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
Jeff Brown928e0542011-01-10 11:17:36 -0800114 AutoMutex _l(gHandleMutex);
115
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000116 jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800117 if (ptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000118 env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0);
Jeff Brown928e0542011-01-10 11:17:36 -0800119
120 NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
Riddle Hsucd958bc2019-01-23 15:40:26 +0800121 handle->decStrong((void*)android_view_InputApplicationHandle_getHandle);
Jeff Brown928e0542011-01-10 11:17:36 -0800122 }
123}
124
125
Daniel Micay76f6a862015-09-19 17:31:01 -0400126static const JNINativeMethod gInputApplicationHandleMethods[] = {
Jeff Brown928e0542011-01-10 11:17:36 -0800127 /* name, signature, funcPtr */
128 { "nativeDispose", "()V",
Riddle Hsucd958bc2019-01-23 15:40:26 +0800129 (void*) android_view_InputApplicationHandle_nativeDispose },
Jeff Brown928e0542011-01-10 11:17:36 -0800130};
131
132#define FIND_CLASS(var, className) \
133 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700134 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown928e0542011-01-10 11:17:36 -0800135
136#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
137 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700138 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown928e0542011-01-10 11:17:36 -0800139
Riddle Hsucd958bc2019-01-23 15:40:26 +0800140int register_android_view_InputApplicationHandle(JNIEnv* env) {
Robert Carr788f5742018-07-30 17:46:45 -0700141 int res = jniRegisterNativeMethods(env, "android/view/InputApplicationHandle",
Jeff Brown928e0542011-01-10 11:17:36 -0800142 gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +0100143 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown928e0542011-01-10 11:17:36 -0800144 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
145
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800146 jclass clazz;
Robert Carr788f5742018-07-30 17:46:45 -0700147 FIND_CLASS(clazz, "android/view/InputApplicationHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800148
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800149 GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000150 "ptr", "J");
Jeff Brown928e0542011-01-10 11:17:36 -0800151
Jeff Brown9302c872011-07-13 22:51:29 -0700152 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 Carr0bcbe642018-10-11 19:07:43 -0700159 GET_FIELD_ID(gInputApplicationHandleClassInfo.token, clazz,
160 "token", "Landroid/os/IBinder;");
161
Jeff Brown928e0542011-01-10 11:17:36 -0800162 return 0;
163}
164
165} /* namespace android */