blob: a1d1d4f0733d673e446edf810023d133999d5996 [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
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
Jeff Brown9302c872011-07-13 22:51:29 -070025#include <android/graphics/Region.h>
Michael Wrighta407d6a2014-02-05 18:02:40 -080026#include <ui/Region.h>
Jeff Brown9302c872011-07-13 22:51:29 -070027
Robert Carre1db3202018-07-23 15:24:59 -070028#include "android_hardware_input_InputWindowHandle.h"
29#include "android_hardware_input_InputApplicationHandle.h"
Robert Carr0bcbe642018-10-11 19:07:43 -070030#include "android_util_Binder.h"
Jeff Brown928e0542011-01-10 11:17:36 -080031
32namespace android {
33
Vishnu Nair40efb712019-03-12 13:37:51 -070034struct WeakRefHandleField {
35 jfieldID handle;
36 jmethodID get;
37};
38
Jeff Brown928e0542011-01-10 11:17:36 -080039static struct {
Jeff Brown928e0542011-01-10 11:17:36 -080040 jfieldID ptr;
41 jfieldID inputApplicationHandle;
Robert Carreadae822018-10-11 19:07:03 -070042 jfieldID token;
Jeff Brown9302c872011-07-13 22:51:29 -070043 jfieldID name;
44 jfieldID layoutParamsFlags;
45 jfieldID layoutParamsType;
46 jfieldID dispatchingTimeoutNanos;
47 jfieldID frameLeft;
48 jfieldID frameTop;
49 jfieldID frameRight;
50 jfieldID frameBottom;
Robert Carrfcc08522018-11-14 14:02:52 -080051 jfieldID surfaceInset;
Jeff Brown9302c872011-07-13 22:51:29 -070052 jfieldID scaleFactor;
53 jfieldID touchableRegion;
54 jfieldID visible;
55 jfieldID canReceiveKeys;
56 jfieldID hasFocus;
57 jfieldID hasWallpaper;
58 jfieldID paused;
59 jfieldID layer;
60 jfieldID ownerPid;
61 jfieldID ownerUid;
62 jfieldID inputFeatures;
Jeff Brown83d616a2012-09-09 20:33:43 -070063 jfieldID displayId;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080064 jfieldID portalToDisplayId;
Vishnu Nair40efb712019-03-12 13:37:51 -070065 jfieldID replaceTouchableRegionWithCrop;
66 WeakRefHandleField touchableRegionCropHandle;
Jeff Brown928e0542011-01-10 11:17:36 -080067} gInputWindowHandleClassInfo;
68
69static Mutex gHandleMutex;
70
71
72// --- NativeInputWindowHandle ---
73
Robert Carr0bcbe642018-10-11 19:07:43 -070074NativeInputWindowHandle::NativeInputWindowHandle(jweak objWeak) :
Jeff Brown928e0542011-01-10 11:17:36 -080075 mObjWeak(objWeak) {
76}
77
78NativeInputWindowHandle::~NativeInputWindowHandle() {
79 JNIEnv* env = AndroidRuntime::getJNIEnv();
80 env->DeleteWeakGlobalRef(mObjWeak);
81}
82
83jobject NativeInputWindowHandle::getInputWindowHandleObjLocalRef(JNIEnv* env) {
84 return env->NewLocalRef(mObjWeak);
85}
86
Jeff Browncc4f7db2011-08-30 20:34:48 -070087bool NativeInputWindowHandle::updateInfo() {
Jeff Brown9302c872011-07-13 22:51:29 -070088 JNIEnv* env = AndroidRuntime::getJNIEnv();
89 jobject obj = env->NewLocalRef(mObjWeak);
90 if (!obj) {
Arthur Hung06ad38d2018-11-05 19:23:21 +080091 releaseChannel();
Jeff Brown9302c872011-07-13 22:51:29 -070092 return false;
93 }
94
Arthur Hung06ad38d2018-11-05 19:23:21 +080095 mInfo.touchableRegion.clear();
Jeff Browncc4f7db2011-08-30 20:34:48 -070096
Siarhei Vishniakoue890f592018-10-15 18:53:06 -070097 jobject tokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.token);
Robert Carreadae822018-10-11 19:07:03 -070098 if (tokenObj) {
99 mInfo.token = ibinderForJavaObject(env, tokenObj);
Vishnu Nair40efb712019-03-12 13:37:51 -0700100 env->DeleteLocalRef(tokenObj);
Jeff Brown9302c872011-07-13 22:51:29 -0700101 } else {
Robert Carreadae822018-10-11 19:07:03 -0700102 mInfo.token.clear();
Jeff Brown9302c872011-07-13 22:51:29 -0700103 }
104
Siarhei Vishniakoue890f592018-10-15 18:53:06 -0700105 mInfo.name = getStringField(env, obj, gInputWindowHandleClassInfo.name, "<null>");
Jeff Brown9302c872011-07-13 22:51:29 -0700106
Arthur Hung06ad38d2018-11-05 19:23:21 +0800107 mInfo.layoutParamsFlags = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700108 gInputWindowHandleClassInfo.layoutParamsFlags);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800109 mInfo.layoutParamsType = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700110 gInputWindowHandleClassInfo.layoutParamsType);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800111 mInfo.dispatchingTimeout = env->GetLongField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700112 gInputWindowHandleClassInfo.dispatchingTimeoutNanos);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800113 mInfo.frameLeft = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700114 gInputWindowHandleClassInfo.frameLeft);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800115 mInfo.frameTop = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700116 gInputWindowHandleClassInfo.frameTop);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800117 mInfo.frameRight = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700118 gInputWindowHandleClassInfo.frameRight);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800119 mInfo.frameBottom = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700120 gInputWindowHandleClassInfo.frameBottom);
Robert Carrfcc08522018-11-14 14:02:52 -0800121 mInfo.surfaceInset = env->GetIntField(obj,
122 gInputWindowHandleClassInfo.surfaceInset);
Robert Carr51141c02018-11-26 13:11:05 -0800123 mInfo.globalScaleFactor = env->GetFloatField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700124 gInputWindowHandleClassInfo.scaleFactor);
125
126 jobject regionObj = env->GetObjectField(obj,
127 gInputWindowHandleClassInfo.touchableRegion);
128 if (regionObj) {
129 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
Michael Wrighta407d6a2014-02-05 18:02:40 -0800130 for (SkRegion::Iterator it(*region); !it.done(); it.next()) {
131 const SkIRect& rect = it.rect();
Arthur Hung06ad38d2018-11-05 19:23:21 +0800132 mInfo.addTouchableRegion(Rect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom));
Michael Wrighta407d6a2014-02-05 18:02:40 -0800133 }
Jeff Brown9302c872011-07-13 22:51:29 -0700134 env->DeleteLocalRef(regionObj);
Jeff Brown9302c872011-07-13 22:51:29 -0700135 }
136
Arthur Hung06ad38d2018-11-05 19:23:21 +0800137 mInfo.visible = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700138 gInputWindowHandleClassInfo.visible);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800139 mInfo.canReceiveKeys = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700140 gInputWindowHandleClassInfo.canReceiveKeys);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800141 mInfo.hasFocus = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700142 gInputWindowHandleClassInfo.hasFocus);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800143 mInfo.hasWallpaper = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700144 gInputWindowHandleClassInfo.hasWallpaper);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800145 mInfo.paused = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700146 gInputWindowHandleClassInfo.paused);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800147 mInfo.layer = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700148 gInputWindowHandleClassInfo.layer);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800149 mInfo.ownerPid = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700150 gInputWindowHandleClassInfo.ownerPid);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800151 mInfo.ownerUid = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700152 gInputWindowHandleClassInfo.ownerUid);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800153 mInfo.inputFeatures = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700154 gInputWindowHandleClassInfo.inputFeatures);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800155 mInfo.displayId = env->GetIntField(obj,
Jeff Brown83d616a2012-09-09 20:33:43 -0700156 gInputWindowHandleClassInfo.displayId);
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800157 mInfo.portalToDisplayId = env->GetIntField(obj,
158 gInputWindowHandleClassInfo.portalToDisplayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700159
Robert Carr0bcbe642018-10-11 19:07:43 -0700160 jobject inputApplicationHandleObj = env->GetObjectField(obj,
161 gInputWindowHandleClassInfo.inputApplicationHandle);
162 if (inputApplicationHandleObj) {
163 sp<InputApplicationHandle> inputApplicationHandle =
Riddle Hsucd958bc2019-01-23 15:40:26 +0800164 android_view_InputApplicationHandle_getHandle(env, inputApplicationHandleObj);
Robert Carr0bcbe642018-10-11 19:07:43 -0700165 if (inputApplicationHandle != nullptr) {
166 inputApplicationHandle->updateInfo();
167 mInfo.applicationInfo = *(inputApplicationHandle->getInfo());
168 }
169 env->DeleteLocalRef(inputApplicationHandleObj);
170 }
171
Vishnu Nair40efb712019-03-12 13:37:51 -0700172 mInfo.replaceTouchableRegionWithCrop = env->GetBooleanField(obj,
173 gInputWindowHandleClassInfo.replaceTouchableRegionWithCrop);
174
175 jobject handleObj = env->GetObjectField(obj,
176 gInputWindowHandleClassInfo.touchableRegionCropHandle.handle);
177 if (handleObj) {
178 // Promote java weak reference.
179 jobject strongHandleObj = env->CallObjectMethod(handleObj,
180 gInputWindowHandleClassInfo.touchableRegionCropHandle.get);
181 if (strongHandleObj) {
182 mInfo.touchableRegionCropHandle = ibinderForJavaObject(env, strongHandleObj);
183 env->DeleteLocalRef(strongHandleObj);
184 } else {
185 mInfo.touchableRegionCropHandle.clear();
186 }
187 env->DeleteLocalRef(handleObj);
188 }
189
Jeff Brown9302c872011-07-13 22:51:29 -0700190 env->DeleteLocalRef(obj);
191 return true;
192}
193
Jeff Brown928e0542011-01-10 11:17:36 -0800194
195// --- Global functions ---
196
Riddle Hsucd958bc2019-01-23 15:40:26 +0800197sp<NativeInputWindowHandle> android_view_InputWindowHandle_getHandle(
Jeff Brown928e0542011-01-10 11:17:36 -0800198 JNIEnv* env, jobject inputWindowHandleObj) {
199 if (!inputWindowHandleObj) {
200 return NULL;
201 }
202
203 AutoMutex _l(gHandleMutex);
204
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000205 jlong ptr = env->GetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800206 NativeInputWindowHandle* handle;
207 if (ptr) {
208 handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
209 } else {
Jeff Brown928e0542011-01-10 11:17:36 -0800210 jweak objWeak = env->NewWeakGlobalRef(inputWindowHandleObj);
Robert Carr0bcbe642018-10-11 19:07:43 -0700211 handle = new NativeInputWindowHandle(objWeak);
Riddle Hsucd958bc2019-01-23 15:40:26 +0800212 handle->incStrong((void*)android_view_InputWindowHandle_getHandle);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000213 env->SetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr,
214 reinterpret_cast<jlong>(handle));
Jeff Brown928e0542011-01-10 11:17:36 -0800215 }
216 return handle;
217}
218
219
220// --- JNI ---
221
Riddle Hsucd958bc2019-01-23 15:40:26 +0800222static void android_view_InputWindowHandle_nativeDispose(JNIEnv* env, jobject obj) {
Jeff Brown928e0542011-01-10 11:17:36 -0800223 AutoMutex _l(gHandleMutex);
224
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000225 jlong ptr = env->GetLongField(obj, gInputWindowHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800226 if (ptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000227 env->SetLongField(obj, gInputWindowHandleClassInfo.ptr, 0);
Jeff Brown928e0542011-01-10 11:17:36 -0800228
229 NativeInputWindowHandle* handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
Riddle Hsucd958bc2019-01-23 15:40:26 +0800230 handle->decStrong((void*)android_view_InputWindowHandle_getHandle);
Jeff Brown928e0542011-01-10 11:17:36 -0800231 }
232}
233
234
Daniel Micay76f6a862015-09-19 17:31:01 -0400235static const JNINativeMethod gInputWindowHandleMethods[] = {
Jeff Brown928e0542011-01-10 11:17:36 -0800236 /* name, signature, funcPtr */
237 { "nativeDispose", "()V",
Riddle Hsucd958bc2019-01-23 15:40:26 +0800238 (void*) android_view_InputWindowHandle_nativeDispose },
Jeff Brown928e0542011-01-10 11:17:36 -0800239};
240
241#define FIND_CLASS(var, className) \
242 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700243 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown928e0542011-01-10 11:17:36 -0800244
245#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
246 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700247 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown928e0542011-01-10 11:17:36 -0800248
Vishnu Nair40efb712019-03-12 13:37:51 -0700249#define GET_METHOD_ID(var, clazz, methodName, methodSignature) \
250 var = env->GetMethodID(clazz, methodName, methodSignature); \
251 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
252
Riddle Hsucd958bc2019-01-23 15:40:26 +0800253int register_android_view_InputWindowHandle(JNIEnv* env) {
Robert Carr788f5742018-07-30 17:46:45 -0700254 int res = jniRegisterNativeMethods(env, "android/view/InputWindowHandle",
Jeff Brown928e0542011-01-10 11:17:36 -0800255 gInputWindowHandleMethods, NELEM(gInputWindowHandleMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +0100256 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown928e0542011-01-10 11:17:36 -0800257 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
258
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800259 jclass clazz;
Robert Carr788f5742018-07-30 17:46:45 -0700260 FIND_CLASS(clazz, "android/view/InputWindowHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800261
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800262 GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000263 "ptr", "J");
Jeff Brown928e0542011-01-10 11:17:36 -0800264
Siarhei Vishniakoue890f592018-10-15 18:53:06 -0700265 GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle, clazz,
Robert Carr788f5742018-07-30 17:46:45 -0700266 "inputApplicationHandle", "Landroid/view/InputApplicationHandle;");
Jeff Brown928e0542011-01-10 11:17:36 -0800267
Robert Carreadae822018-10-11 19:07:03 -0700268 GET_FIELD_ID(gInputWindowHandleClassInfo.token, clazz,
269 "token", "Landroid/os/IBinder;");
Jeff Brown9302c872011-07-13 22:51:29 -0700270
271 GET_FIELD_ID(gInputWindowHandleClassInfo.name, clazz,
272 "name", "Ljava/lang/String;");
273
274 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsFlags, clazz,
275 "layoutParamsFlags", "I");
276
277 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsType, clazz,
278 "layoutParamsType", "I");
279
280 GET_FIELD_ID(gInputWindowHandleClassInfo.dispatchingTimeoutNanos, clazz,
281 "dispatchingTimeoutNanos", "J");
282
283 GET_FIELD_ID(gInputWindowHandleClassInfo.frameLeft, clazz,
284 "frameLeft", "I");
285
286 GET_FIELD_ID(gInputWindowHandleClassInfo.frameTop, clazz,
287 "frameTop", "I");
288
289 GET_FIELD_ID(gInputWindowHandleClassInfo.frameRight, clazz,
290 "frameRight", "I");
291
292 GET_FIELD_ID(gInputWindowHandleClassInfo.frameBottom, clazz,
293 "frameBottom", "I");
294
Robert Carrfcc08522018-11-14 14:02:52 -0800295 GET_FIELD_ID(gInputWindowHandleClassInfo.surfaceInset, clazz,
296 "surfaceInset", "I");
297
Jeff Brown9302c872011-07-13 22:51:29 -0700298 GET_FIELD_ID(gInputWindowHandleClassInfo.scaleFactor, clazz,
299 "scaleFactor", "F");
300
301 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegion, clazz,
302 "touchableRegion", "Landroid/graphics/Region;");
303
304 GET_FIELD_ID(gInputWindowHandleClassInfo.visible, clazz,
305 "visible", "Z");
306
307 GET_FIELD_ID(gInputWindowHandleClassInfo.canReceiveKeys, clazz,
308 "canReceiveKeys", "Z");
309
310 GET_FIELD_ID(gInputWindowHandleClassInfo.hasFocus, clazz,
311 "hasFocus", "Z");
312
313 GET_FIELD_ID(gInputWindowHandleClassInfo.hasWallpaper, clazz,
314 "hasWallpaper", "Z");
315
316 GET_FIELD_ID(gInputWindowHandleClassInfo.paused, clazz,
317 "paused", "Z");
318
319 GET_FIELD_ID(gInputWindowHandleClassInfo.layer, clazz,
320 "layer", "I");
321
322 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerPid, clazz,
323 "ownerPid", "I");
324
325 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerUid, clazz,
326 "ownerUid", "I");
327
328 GET_FIELD_ID(gInputWindowHandleClassInfo.inputFeatures, clazz,
329 "inputFeatures", "I");
Jeff Brown83d616a2012-09-09 20:33:43 -0700330
331 GET_FIELD_ID(gInputWindowHandleClassInfo.displayId, clazz,
332 "displayId", "I");
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800333
334 GET_FIELD_ID(gInputWindowHandleClassInfo.portalToDisplayId, clazz,
335 "portalToDisplayId", "I");
Vishnu Nair40efb712019-03-12 13:37:51 -0700336
337 GET_FIELD_ID(gInputWindowHandleClassInfo.replaceTouchableRegionWithCrop, clazz,
338 "replaceTouchableRegionWithCrop", "Z");
339
340 jclass weakRefClazz;
341 FIND_CLASS(weakRefClazz, "java/lang/ref/Reference");
342
343 GET_METHOD_ID(gInputWindowHandleClassInfo.touchableRegionCropHandle.get, weakRefClazz,
344 "get", "()Ljava/lang/Object;")
345
346 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegionCropHandle.handle, clazz,
347 "touchableRegionCropHandle", "Ljava/lang/ref/WeakReference;");
Jeff Brown928e0542011-01-10 11:17:36 -0800348 return 0;
349}
350
351} /* namespace android */