blob: 57d16496fca5dcd8517aa0abb2e3f6cfdd071354 [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"
Vishnu Nairc06ec082019-08-20 09:12:26 -070031#include <binder/IPCThreadState.h>
Jeff Brown928e0542011-01-10 11:17:36 -080032
33namespace android {
34
Vishnu Nair40efb712019-03-12 13:37:51 -070035struct WeakRefHandleField {
36 jfieldID handle;
37 jmethodID get;
38};
39
Jeff Brown928e0542011-01-10 11:17:36 -080040static struct {
Jeff Brown928e0542011-01-10 11:17:36 -080041 jfieldID ptr;
42 jfieldID inputApplicationHandle;
Robert Carreadae822018-10-11 19:07:03 -070043 jfieldID token;
Jeff Brown9302c872011-07-13 22:51:29 -070044 jfieldID name;
45 jfieldID layoutParamsFlags;
46 jfieldID layoutParamsType;
47 jfieldID dispatchingTimeoutNanos;
48 jfieldID frameLeft;
49 jfieldID frameTop;
50 jfieldID frameRight;
51 jfieldID frameBottom;
Robert Carrfcc08522018-11-14 14:02:52 -080052 jfieldID surfaceInset;
Jeff Brown9302c872011-07-13 22:51:29 -070053 jfieldID scaleFactor;
54 jfieldID touchableRegion;
55 jfieldID visible;
56 jfieldID canReceiveKeys;
57 jfieldID hasFocus;
58 jfieldID hasWallpaper;
59 jfieldID paused;
60 jfieldID layer;
61 jfieldID ownerPid;
62 jfieldID ownerUid;
63 jfieldID inputFeatures;
Jeff Brown83d616a2012-09-09 20:33:43 -070064 jfieldID displayId;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080065 jfieldID portalToDisplayId;
Vishnu Nair40efb712019-03-12 13:37:51 -070066 jfieldID replaceTouchableRegionWithCrop;
67 WeakRefHandleField touchableRegionCropHandle;
Jeff Brown928e0542011-01-10 11:17:36 -080068} gInputWindowHandleClassInfo;
69
70static Mutex gHandleMutex;
71
72
73// --- NativeInputWindowHandle ---
74
Robert Carr0bcbe642018-10-11 19:07:43 -070075NativeInputWindowHandle::NativeInputWindowHandle(jweak objWeak) :
Jeff Brown928e0542011-01-10 11:17:36 -080076 mObjWeak(objWeak) {
77}
78
79NativeInputWindowHandle::~NativeInputWindowHandle() {
80 JNIEnv* env = AndroidRuntime::getJNIEnv();
81 env->DeleteWeakGlobalRef(mObjWeak);
Vishnu Nairc06ec082019-08-20 09:12:26 -070082
83 // Clear the weak reference to the layer handle and flush any binder ref count operations so we
84 // do not hold on to any binder references.
85 // TODO(b/139697085) remove this after it can be flushed automatically
86 mInfo.touchableRegionCropHandle.clear();
87 IPCThreadState::self()->flushCommands();
Jeff Brown928e0542011-01-10 11:17:36 -080088}
89
90jobject NativeInputWindowHandle::getInputWindowHandleObjLocalRef(JNIEnv* env) {
91 return env->NewLocalRef(mObjWeak);
92}
93
Jeff Browncc4f7db2011-08-30 20:34:48 -070094bool NativeInputWindowHandle::updateInfo() {
Jeff Brown9302c872011-07-13 22:51:29 -070095 JNIEnv* env = AndroidRuntime::getJNIEnv();
96 jobject obj = env->NewLocalRef(mObjWeak);
97 if (!obj) {
Arthur Hung06ad38d2018-11-05 19:23:21 +080098 releaseChannel();
Jeff Brown9302c872011-07-13 22:51:29 -070099 return false;
100 }
101
Arthur Hung06ad38d2018-11-05 19:23:21 +0800102 mInfo.touchableRegion.clear();
Jeff Browncc4f7db2011-08-30 20:34:48 -0700103
Siarhei Vishniakoue890f592018-10-15 18:53:06 -0700104 jobject tokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.token);
Robert Carreadae822018-10-11 19:07:03 -0700105 if (tokenObj) {
106 mInfo.token = ibinderForJavaObject(env, tokenObj);
Vishnu Nair40efb712019-03-12 13:37:51 -0700107 env->DeleteLocalRef(tokenObj);
Jeff Brown9302c872011-07-13 22:51:29 -0700108 } else {
Robert Carreadae822018-10-11 19:07:03 -0700109 mInfo.token.clear();
Jeff Brown9302c872011-07-13 22:51:29 -0700110 }
111
Siarhei Vishniakoue890f592018-10-15 18:53:06 -0700112 mInfo.name = getStringField(env, obj, gInputWindowHandleClassInfo.name, "<null>");
Jeff Brown9302c872011-07-13 22:51:29 -0700113
Arthur Hung06ad38d2018-11-05 19:23:21 +0800114 mInfo.layoutParamsFlags = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700115 gInputWindowHandleClassInfo.layoutParamsFlags);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800116 mInfo.layoutParamsType = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700117 gInputWindowHandleClassInfo.layoutParamsType);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800118 mInfo.dispatchingTimeout = env->GetLongField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700119 gInputWindowHandleClassInfo.dispatchingTimeoutNanos);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800120 mInfo.frameLeft = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700121 gInputWindowHandleClassInfo.frameLeft);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800122 mInfo.frameTop = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700123 gInputWindowHandleClassInfo.frameTop);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800124 mInfo.frameRight = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700125 gInputWindowHandleClassInfo.frameRight);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800126 mInfo.frameBottom = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700127 gInputWindowHandleClassInfo.frameBottom);
Robert Carrfcc08522018-11-14 14:02:52 -0800128 mInfo.surfaceInset = env->GetIntField(obj,
129 gInputWindowHandleClassInfo.surfaceInset);
Robert Carr51141c02018-11-26 13:11:05 -0800130 mInfo.globalScaleFactor = env->GetFloatField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700131 gInputWindowHandleClassInfo.scaleFactor);
132
133 jobject regionObj = env->GetObjectField(obj,
134 gInputWindowHandleClassInfo.touchableRegion);
135 if (regionObj) {
136 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj);
Michael Wrighta407d6a2014-02-05 18:02:40 -0800137 for (SkRegion::Iterator it(*region); !it.done(); it.next()) {
138 const SkIRect& rect = it.rect();
Arthur Hung06ad38d2018-11-05 19:23:21 +0800139 mInfo.addTouchableRegion(Rect(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom));
Michael Wrighta407d6a2014-02-05 18:02:40 -0800140 }
Jeff Brown9302c872011-07-13 22:51:29 -0700141 env->DeleteLocalRef(regionObj);
Jeff Brown9302c872011-07-13 22:51:29 -0700142 }
143
Arthur Hung06ad38d2018-11-05 19:23:21 +0800144 mInfo.visible = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700145 gInputWindowHandleClassInfo.visible);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800146 mInfo.canReceiveKeys = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700147 gInputWindowHandleClassInfo.canReceiveKeys);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800148 mInfo.hasFocus = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700149 gInputWindowHandleClassInfo.hasFocus);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800150 mInfo.hasWallpaper = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700151 gInputWindowHandleClassInfo.hasWallpaper);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800152 mInfo.paused = env->GetBooleanField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700153 gInputWindowHandleClassInfo.paused);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800154 mInfo.layer = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700155 gInputWindowHandleClassInfo.layer);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800156 mInfo.ownerPid = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700157 gInputWindowHandleClassInfo.ownerPid);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800158 mInfo.ownerUid = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700159 gInputWindowHandleClassInfo.ownerUid);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800160 mInfo.inputFeatures = env->GetIntField(obj,
Jeff Brown9302c872011-07-13 22:51:29 -0700161 gInputWindowHandleClassInfo.inputFeatures);
Arthur Hung06ad38d2018-11-05 19:23:21 +0800162 mInfo.displayId = env->GetIntField(obj,
Jeff Brown83d616a2012-09-09 20:33:43 -0700163 gInputWindowHandleClassInfo.displayId);
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800164 mInfo.portalToDisplayId = env->GetIntField(obj,
165 gInputWindowHandleClassInfo.portalToDisplayId);
Jeff Brown9302c872011-07-13 22:51:29 -0700166
Robert Carr0bcbe642018-10-11 19:07:43 -0700167 jobject inputApplicationHandleObj = env->GetObjectField(obj,
168 gInputWindowHandleClassInfo.inputApplicationHandle);
169 if (inputApplicationHandleObj) {
170 sp<InputApplicationHandle> inputApplicationHandle =
Riddle Hsucd958bc2019-01-23 15:40:26 +0800171 android_view_InputApplicationHandle_getHandle(env, inputApplicationHandleObj);
Robert Carr0bcbe642018-10-11 19:07:43 -0700172 if (inputApplicationHandle != nullptr) {
173 inputApplicationHandle->updateInfo();
174 mInfo.applicationInfo = *(inputApplicationHandle->getInfo());
175 }
176 env->DeleteLocalRef(inputApplicationHandleObj);
177 }
178
Vishnu Nair40efb712019-03-12 13:37:51 -0700179 mInfo.replaceTouchableRegionWithCrop = env->GetBooleanField(obj,
180 gInputWindowHandleClassInfo.replaceTouchableRegionWithCrop);
181
182 jobject handleObj = env->GetObjectField(obj,
183 gInputWindowHandleClassInfo.touchableRegionCropHandle.handle);
184 if (handleObj) {
185 // Promote java weak reference.
186 jobject strongHandleObj = env->CallObjectMethod(handleObj,
187 gInputWindowHandleClassInfo.touchableRegionCropHandle.get);
188 if (strongHandleObj) {
189 mInfo.touchableRegionCropHandle = ibinderForJavaObject(env, strongHandleObj);
190 env->DeleteLocalRef(strongHandleObj);
191 } else {
192 mInfo.touchableRegionCropHandle.clear();
193 }
194 env->DeleteLocalRef(handleObj);
195 }
196
Jeff Brown9302c872011-07-13 22:51:29 -0700197 env->DeleteLocalRef(obj);
198 return true;
199}
200
Jeff Brown928e0542011-01-10 11:17:36 -0800201
202// --- Global functions ---
203
Riddle Hsucd958bc2019-01-23 15:40:26 +0800204sp<NativeInputWindowHandle> android_view_InputWindowHandle_getHandle(
Jeff Brown928e0542011-01-10 11:17:36 -0800205 JNIEnv* env, jobject inputWindowHandleObj) {
206 if (!inputWindowHandleObj) {
207 return NULL;
208 }
209
210 AutoMutex _l(gHandleMutex);
211
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000212 jlong ptr = env->GetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800213 NativeInputWindowHandle* handle;
214 if (ptr) {
215 handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
216 } else {
Jeff Brown928e0542011-01-10 11:17:36 -0800217 jweak objWeak = env->NewWeakGlobalRef(inputWindowHandleObj);
Robert Carr0bcbe642018-10-11 19:07:43 -0700218 handle = new NativeInputWindowHandle(objWeak);
Riddle Hsucd958bc2019-01-23 15:40:26 +0800219 handle->incStrong((void*)android_view_InputWindowHandle_getHandle);
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000220 env->SetLongField(inputWindowHandleObj, gInputWindowHandleClassInfo.ptr,
221 reinterpret_cast<jlong>(handle));
Jeff Brown928e0542011-01-10 11:17:36 -0800222 }
223 return handle;
224}
225
226
227// --- JNI ---
228
Riddle Hsucd958bc2019-01-23 15:40:26 +0800229static void android_view_InputWindowHandle_nativeDispose(JNIEnv* env, jobject obj) {
Jeff Brown928e0542011-01-10 11:17:36 -0800230 AutoMutex _l(gHandleMutex);
231
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000232 jlong ptr = env->GetLongField(obj, gInputWindowHandleClassInfo.ptr);
Jeff Brown928e0542011-01-10 11:17:36 -0800233 if (ptr) {
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000234 env->SetLongField(obj, gInputWindowHandleClassInfo.ptr, 0);
Jeff Brown928e0542011-01-10 11:17:36 -0800235
236 NativeInputWindowHandle* handle = reinterpret_cast<NativeInputWindowHandle*>(ptr);
Riddle Hsucd958bc2019-01-23 15:40:26 +0800237 handle->decStrong((void*)android_view_InputWindowHandle_getHandle);
Jeff Brown928e0542011-01-10 11:17:36 -0800238 }
239}
240
241
Daniel Micay76f6a862015-09-19 17:31:01 -0400242static const JNINativeMethod gInputWindowHandleMethods[] = {
Jeff Brown928e0542011-01-10 11:17:36 -0800243 /* name, signature, funcPtr */
244 { "nativeDispose", "()V",
Riddle Hsucd958bc2019-01-23 15:40:26 +0800245 (void*) android_view_InputWindowHandle_nativeDispose },
Jeff Brown928e0542011-01-10 11:17:36 -0800246};
247
248#define FIND_CLASS(var, className) \
249 var = env->FindClass(className); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700250 LOG_FATAL_IF(! (var), "Unable to find class " className);
Jeff Brown928e0542011-01-10 11:17:36 -0800251
252#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
253 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
Chih-Hung Hsieh6c896162016-05-19 15:29:38 -0700254 LOG_FATAL_IF(! (var), "Unable to find field " fieldName);
Jeff Brown928e0542011-01-10 11:17:36 -0800255
Vishnu Nair40efb712019-03-12 13:37:51 -0700256#define GET_METHOD_ID(var, clazz, methodName, methodSignature) \
257 var = env->GetMethodID(clazz, methodName, methodSignature); \
258 LOG_FATAL_IF(! (var), "Unable to find method " methodName);
259
Riddle Hsucd958bc2019-01-23 15:40:26 +0800260int register_android_view_InputWindowHandle(JNIEnv* env) {
Robert Carr788f5742018-07-30 17:46:45 -0700261 int res = jniRegisterNativeMethods(env, "android/view/InputWindowHandle",
Jeff Brown928e0542011-01-10 11:17:36 -0800262 gInputWindowHandleMethods, NELEM(gInputWindowHandleMethods));
Bernhard Rosenkränzer9c1c90e2014-11-12 14:45:58 +0100263 (void) res; // Faked use when LOG_NDEBUG.
Jeff Brown928e0542011-01-10 11:17:36 -0800264 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
265
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800266 jclass clazz;
Robert Carr788f5742018-07-30 17:46:45 -0700267 FIND_CLASS(clazz, "android/view/InputWindowHandle");
Jeff Brown928e0542011-01-10 11:17:36 -0800268
Carl Shapiro17cc33a2011-03-05 20:53:16 -0800269 GET_FIELD_ID(gInputWindowHandleClassInfo.ptr, clazz,
Ashok Bhat7e2a9dc2014-01-02 19:43:30 +0000270 "ptr", "J");
Jeff Brown928e0542011-01-10 11:17:36 -0800271
Siarhei Vishniakoue890f592018-10-15 18:53:06 -0700272 GET_FIELD_ID(gInputWindowHandleClassInfo.inputApplicationHandle, clazz,
Robert Carr788f5742018-07-30 17:46:45 -0700273 "inputApplicationHandle", "Landroid/view/InputApplicationHandle;");
Jeff Brown928e0542011-01-10 11:17:36 -0800274
Robert Carreadae822018-10-11 19:07:03 -0700275 GET_FIELD_ID(gInputWindowHandleClassInfo.token, clazz,
276 "token", "Landroid/os/IBinder;");
Jeff Brown9302c872011-07-13 22:51:29 -0700277
278 GET_FIELD_ID(gInputWindowHandleClassInfo.name, clazz,
279 "name", "Ljava/lang/String;");
280
281 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsFlags, clazz,
282 "layoutParamsFlags", "I");
283
284 GET_FIELD_ID(gInputWindowHandleClassInfo.layoutParamsType, clazz,
285 "layoutParamsType", "I");
286
287 GET_FIELD_ID(gInputWindowHandleClassInfo.dispatchingTimeoutNanos, clazz,
288 "dispatchingTimeoutNanos", "J");
289
290 GET_FIELD_ID(gInputWindowHandleClassInfo.frameLeft, clazz,
291 "frameLeft", "I");
292
293 GET_FIELD_ID(gInputWindowHandleClassInfo.frameTop, clazz,
294 "frameTop", "I");
295
296 GET_FIELD_ID(gInputWindowHandleClassInfo.frameRight, clazz,
297 "frameRight", "I");
298
299 GET_FIELD_ID(gInputWindowHandleClassInfo.frameBottom, clazz,
300 "frameBottom", "I");
301
Robert Carrfcc08522018-11-14 14:02:52 -0800302 GET_FIELD_ID(gInputWindowHandleClassInfo.surfaceInset, clazz,
303 "surfaceInset", "I");
304
Jeff Brown9302c872011-07-13 22:51:29 -0700305 GET_FIELD_ID(gInputWindowHandleClassInfo.scaleFactor, clazz,
306 "scaleFactor", "F");
307
308 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegion, clazz,
309 "touchableRegion", "Landroid/graphics/Region;");
310
311 GET_FIELD_ID(gInputWindowHandleClassInfo.visible, clazz,
312 "visible", "Z");
313
314 GET_FIELD_ID(gInputWindowHandleClassInfo.canReceiveKeys, clazz,
315 "canReceiveKeys", "Z");
316
317 GET_FIELD_ID(gInputWindowHandleClassInfo.hasFocus, clazz,
318 "hasFocus", "Z");
319
320 GET_FIELD_ID(gInputWindowHandleClassInfo.hasWallpaper, clazz,
321 "hasWallpaper", "Z");
322
323 GET_FIELD_ID(gInputWindowHandleClassInfo.paused, clazz,
324 "paused", "Z");
325
326 GET_FIELD_ID(gInputWindowHandleClassInfo.layer, clazz,
327 "layer", "I");
328
329 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerPid, clazz,
330 "ownerPid", "I");
331
332 GET_FIELD_ID(gInputWindowHandleClassInfo.ownerUid, clazz,
333 "ownerUid", "I");
334
335 GET_FIELD_ID(gInputWindowHandleClassInfo.inputFeatures, clazz,
336 "inputFeatures", "I");
Jeff Brown83d616a2012-09-09 20:33:43 -0700337
338 GET_FIELD_ID(gInputWindowHandleClassInfo.displayId, clazz,
339 "displayId", "I");
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800340
341 GET_FIELD_ID(gInputWindowHandleClassInfo.portalToDisplayId, clazz,
342 "portalToDisplayId", "I");
Vishnu Nair40efb712019-03-12 13:37:51 -0700343
344 GET_FIELD_ID(gInputWindowHandleClassInfo.replaceTouchableRegionWithCrop, clazz,
345 "replaceTouchableRegionWithCrop", "Z");
346
347 jclass weakRefClazz;
348 FIND_CLASS(weakRefClazz, "java/lang/ref/Reference");
349
350 GET_METHOD_ID(gInputWindowHandleClassInfo.touchableRegionCropHandle.get, weakRefClazz,
351 "get", "()Ljava/lang/Object;")
352
353 GET_FIELD_ID(gInputWindowHandleClassInfo.touchableRegionCropHandle.handle, clazz,
354 "touchableRegionCropHandle", "Ljava/lang/ref/WeakReference;");
Jeff Brown928e0542011-01-10 11:17:36 -0800355 return 0;
356}
357
358} /* namespace android */