blob: 71edfd553e7eadce99961699c718abd1d4127859 [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 }
61
Arthur Hunge7dc5012019-03-20 17:04:26 +080062 mInfo.name = getStringField(env, obj, gInputApplicationHandleClassInfo.name, "<null>");
Jeff Browncc4f7db2011-08-30 20:34:48 -070063
Arthur Hunge7dc5012019-03-20 17:04:26 +080064 mInfo.dispatchingTimeout = env->GetLongField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -070065 gInputApplicationHandleClassInfo.dispatchingTimeoutNanos);
66
Robert Carr0bcbe642018-10-11 19:07:43 -070067 jobject tokenObj = env->GetObjectField(obj,
68 gInputApplicationHandleClassInfo.token);
69 if (tokenObj) {
Arthur Hunge7dc5012019-03-20 17:04:26 +080070 mInfo.token = ibinderForJavaObject(env, tokenObj);
Robert Carr0bcbe642018-10-11 19:07:43 -070071 env->DeleteLocalRef(tokenObj);
72 } else {
Arthur Hunge7dc5012019-03-20 17:04:26 +080073 mInfo.token.clear();
Robert Carr0bcbe642018-10-11 19:07:43 -070074 }
75
Jeff Brown9302c872011-07-13 22:51:29 -070076 env->DeleteLocalRef(obj);
Arthur Hunge7dc5012019-03-20 17:04:26 +080077 return mInfo.token.get() != nullptr;
Jeff Brown9302c872011-07-13 22:51:29 -070078}
79
Jeff Brown928e0542011-01-10 11:17:36 -080080
81// --- Global functions ---
82
Riddle Hsucd958bc2019-01-23 15:40:26 +080083sp<InputApplicationHandle> android_view_InputApplicationHandle_getHandle(
Jeff Brown928e0542011-01-10 11:17:36 -080084 JNIEnv* env, jobject inputApplicationHandleObj) {
85 if (!inputApplicationHandleObj) {
86 return NULL;
87 }
88
89 AutoMutex _l(gHandleMutex);
90
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +000091 jlong ptr = env->GetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -080092 NativeInputApplicationHandle* handle;
93 if (ptr) {
94 handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
95 } else {
96 jweak objWeak = env->NewWeakGlobalRef(inputApplicationHandleObj);
97 handle = new NativeInputApplicationHandle(objWeak);
Riddle Hsucd958bc2019-01-23 15:40:26 +080098 handle->incStrong((void*)android_view_InputApplicationHandle_getHandle);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +000099 env->SetLongField(inputApplicationHandleObj, gInputApplicationHandleClassInfo.ptr,
100 reinterpret_cast<jlong>(handle));
Jeff Brown928e0542011-01-10 11:17:36 -0800101 }
102 return handle;
103}
104
105
106// --- JNI ---
107
Riddle Hsucd958bc2019-01-23 15:40:26 +0800108static void android_view_InputApplicationHandle_nativeDispose(JNIEnv* env, jobject obj) {
Jeff Brown928e0542011-01-10 11:17:36 -0800109 AutoMutex _l(gHandleMutex);
110
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000111 jlong ptr = env->GetLongField(obj, gInputApplicationHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800112 if (ptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000113 env->SetLongField(obj, gInputApplicationHandleClassInfo.ptr, 0);
Jeff Brown928e0542011-01-10 11:17:36 -0800114
115 NativeInputApplicationHandle* handle = reinterpret_cast<NativeInputApplicationHandle*>(ptr);
Riddle Hsucd958bc2019-01-23 15:40:26 +0800116 handle->decStrong((void*)android_view_InputApplicationHandle_getHandle);
Jeff Brown928e0542011-01-10 11:17:36 -0800117 }
118}
119
120
Daniel Micay76f6a862015-09-19 17:31:01 -0400121static const JNINativeMethod gInputApplicationHandleMethods[] = {
Jeff Brown928e0542011-01-10 11:17:36 -0800122 /* name, signature, funcPtr */
123 { "nativeDispose", "()V",
Riddle Hsucd958bc2019-01-23 15:40:26 +0800124 (void*) android_view_InputApplicationHandle_nativeDispose },
Jeff Brown928e0542011-01-10 11:17:36 -0800125};
126
127#define FIND_CLASS(var, className) \
128 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700129 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown928e0542011-01-10 11:17:36 -0800130
131#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
132 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700133 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown928e0542011-01-10 11:17:36 -0800134
Riddle Hsucd958bc2019-01-23 15:40:26 +0800135int register_android_view_InputApplicationHandle(JNIEnv* env) {
Robert Carr788f5742018-07-30 17:46:45 -0700136 int res = jniRegisterNativeMethods(env, "android/view/InputApplicationHandle",
Jeff Brown928e0542011-01-10 11:17:36 -0800137 gInputApplicationHandleMethods, NELEM(gInputApplicationHandleMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +0100138 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown928e0542011-01-10 11:17:36 -0800139 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
140
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800141 jclass clazz;
Robert Carr788f5742018-07-30 17:46:45 -0700142 FIND_CLASS(clazz, "android/view/InputApplicationHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800143
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800144 GET_FIELD_ID(gInputApplicationHandleClassInfo.ptr, clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000145 "ptr", "J");
Jeff Brown928e0542011-01-10 11:17:36 -0800146
Jeff Brown9302c872011-07-13 22:51:29 -0700147 GET_FIELD_ID(gInputApplicationHandleClassInfo.name, clazz,
148 "name", "Ljava/lang/String;");
149
150 GET_FIELD_ID(gInputApplicationHandleClassInfo.dispatchingTimeoutNanos,
151 clazz,
152 "dispatchingTimeoutNanos", "J");
153
Robert Carr0bcbe642018-10-11 19:07:43 -0700154 GET_FIELD_ID(gInputApplicationHandleClassInfo.token, clazz,
155 "token", "Landroid/os/IBinder;");
156
Jeff Brown928e0542011-01-10 11:17:36 -0800157 return 0;
158}
159
160} /* namespace android */