blob: aaf679c62c1be2cffebe1966b820251fa81fb749 [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 "InputWindowHandle"
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_InputWindowHandle.h"
25#include "com_android_server_InputApplicationHandle.h"
26
27namespace android {
28
29static struct {
Jeff Brown928e0542011-01-10 11:17:36 -080030 jfieldID ptr;
31 jfieldID inputApplicationHandle;
32} gInputWindowHandleClassInfo;
33
34static Mutex gHandleMutex;
35
36
37// --- NativeInputWindowHandle ---
38
39NativeInputWindowHandle::NativeInputWindowHandle(
40 const sp<InputApplicationHandle>& inputApplicationHandle, jweak objWeak) :
41 InputWindowHandle(inputApplicationHandle),
42 mObjWeak(objWeak) {
43}
44
45NativeInputWindowHandle::~NativeInputWindowHandle() {
46 JNIEnv* env = AndroidRuntime::getJNIEnv();
47 env->DeleteWeakGlobalRef(mObjWeak);
48}
49
50jobject NativeInputWindowHandle::getInputWindowHandleObjLocalRef(JNIEnv* env) {
51 return env->NewLocalRef(mObjWeak);
52}
53
54
55// --- Global functions ---
56
57sp<NativeInputWindowHandle> android_server_InputWindowHandle_getHandle(
58 JNIEnv* env, jobject inputWindowHandleObj) {
59 if (!inputWindowHandleObj) {
60 return NULL;
61 }
62
63 AutoMutex _l(gHandleMutex);
64
65 int ptr = env->GetIntField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr);
66 NativeInputWindowHandle* handle;
67 if (ptr) {
68 handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
69 } else {
70 jobject inputApplicationHandleObj = env->GetObjectField(inputWindowHandleObj,
71 gInputWindowHandleClassInfo.inputApplicationHandle);
72 sp<InputApplicationHandle> inputApplicationHandle =
73 android_server_InputApplicationHandle_getHandle(env, inputApplicationHandleObj);
74 env->DeleteLocalRef(inputApplicationHandleObj);
75
76 jweak objWeak = env->NewWeakGlobalRef(inputWindowHandleObj);
77 handle = new NativeInputWindowHandle(inputApplicationHandle, objWeak);
78 handle->incStrong(inputWindowHandleObj);
79 env->SetIntField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr,
80 reinterpret_cast<int>(handle));
81 }
82 return handle;
83}
84
85
86// --- JNI ---
87
88static void android_server_InputWindowHandle_nativeDispose(JNIEnv* env, jobject obj) {
89 AutoMutex _l(gHandleMutex);
90
91 int ptr = env->GetIntField(obj, gInputWindowHandleClassInfo.ptr);
92 if (ptr) {
93 env->SetIntField(obj, gInputWindowHandleClassInfo.ptr, 0);
94
95 NativeInputWindowHandle* handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
96 handle->decStrong(obj);
97 }
98}
99
100
101static JNINativeMethod gInputWindowHandleMethods[] = {
102 /* name, signature, funcPtr */
103 { "nativeDispose", "()V",
104 (void*) android_server_InputWindowHandle_nativeDispose },
105};
106
107#define FIND_CLASS(var, className) \
108 var = env->FindClass(className); \
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800109 LOG_FATAL_IF(! var, "Unable to find class " className);
Jeff Brown928e0542011-01-10 11:17:36 -0800110
111#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
112 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
113 LOG_FATAL_IF(! var, "Unable to find field " fieldName);
114
115int register_android_server_InputWindowHandle(JNIEnv* env) {
Dianne Hackborna924dc0d2011-02-17 14:22:17 -0800116 int res = jniRegisterNativeMethods(env, "com/android/server/wm/InputWindowHandle",
Jeff Brown928e0542011-01-10 11:17:36 -0800117 gInputWindowHandleMethods, NELEM(gInputWindowHandleMethods));
118 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
119
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800120 jclass clazz;
121 FIND_CLASS(clazz, "com/android/server/wm/InputWindowHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800122
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800123 GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
Jeff Brown928e0542011-01-10 11:17:36 -0800124 "ptr", "I");
125
126 GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle,
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800127 clazz,
Dianne Hackborna924dc0d2011-02-17 14:22:17 -0800128 "inputApplicationHandle", "Lcom/android/server/wm/InputApplicationHandle;");
Jeff Brown928e0542011-01-10 11:17:36 -0800129
130 return 0;
131}
132
133} /* namespace android */