blob: ed2ce506ab23fbdb58741c9317617e9c065afd13 [file] [log] [blame]
Jeff Brown2352b972011-04-12 22:39:53 -07001/*
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 "PointerIcon-JNI"
18
Steven Moreland2279b252017-07-19 09:50:45 -070019#include <nativehelper/JNIHelp.h>
Jeff Brown2352b972011-04-12 22:39:53 -070020
21#include "android_view_PointerIcon.h"
22
23#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070024#include <android_runtime/Log.h>
Jeff Brown2352b972011-04-12 22:39:53 -070025#include <utils/Log.h>
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040026#include <android/graphics/bitmap.h>
Steven Moreland2279b252017-07-19 09:50:45 -070027#include <nativehelper/ScopedLocalRef.h>
Jeff Brown2352b972011-04-12 22:39:53 -070028
Andreas Gampe987f79f2014-11-18 17:29:46 -080029#include "core_jni_helpers.h"
30
Jeff Brown2352b972011-04-12 22:39:53 -070031namespace android {
32
33static struct {
34 jclass clazz;
Michael Wrightf9d9ce772016-05-13 17:44:16 +010035 jfieldID mType;
Jeff Brown2352b972011-04-12 22:39:53 -070036 jfieldID mBitmap;
37 jfieldID mHotSpotX;
38 jfieldID mHotSpotY;
Jun Mukai808196f2015-10-28 16:46:44 -070039 jfieldID mBitmapFrames;
40 jfieldID mDurationPerFrame;
Jeff Brown2352b972011-04-12 22:39:53 -070041 jmethodID getSystemIcon;
42 jmethodID load;
43} gPointerIconClassInfo;
44
45
46// --- Global Functions ---
47
48jobject android_view_PointerIcon_getSystemIcon(JNIEnv* env, jobject contextObj, int32_t style) {
49 jobject pointerIconObj = env->CallStaticObjectMethod(gPointerIconClassInfo.clazz,
50 gPointerIconClassInfo.getSystemIcon, contextObj, style);
51 if (env->ExceptionCheck()) {
Steve Block8564c8d2012-01-05 23:22:43 +000052 ALOGW("An exception occurred while getting a pointer icon with style %d.", style);
Jeff Brown2352b972011-04-12 22:39:53 -070053 LOGW_EX(env);
54 env->ExceptionClear();
55 return NULL;
56 }
57 return pointerIconObj;
58}
59
60status_t android_view_PointerIcon_load(JNIEnv* env, jobject pointerIconObj, jobject contextObj,
61 PointerIcon* outPointerIcon) {
62 outPointerIcon->reset();
63
64 if (!pointerIconObj) {
65 return OK;
66 }
67
Jun Mukaid4eaef72015-10-30 15:54:33 -070068 ScopedLocalRef<jobject> loadedPointerIconObj(env, env->CallObjectMethod(pointerIconObj,
69 gPointerIconClassInfo.load, contextObj));
70 if (env->ExceptionCheck() || !loadedPointerIconObj.get()) {
Steve Block8564c8d2012-01-05 23:22:43 +000071 ALOGW("An exception occurred while loading a pointer icon.");
Jeff Brown2352b972011-04-12 22:39:53 -070072 LOGW_EX(env);
73 env->ExceptionClear();
74 return UNKNOWN_ERROR;
75 }
Jun Mukaid4eaef72015-10-30 15:54:33 -070076 return android_view_PointerIcon_getLoadedIcon(env, loadedPointerIconObj.get(), outPointerIcon);
77}
Jeff Brown2352b972011-04-12 22:39:53 -070078
Jun Mukaid4eaef72015-10-30 15:54:33 -070079status_t android_view_PointerIcon_getLoadedIcon(JNIEnv* env, jobject pointerIconObj,
80 PointerIcon* outPointerIcon) {
Michael Wright350680792017-01-18 18:09:29 +000081 if (!pointerIconObj) {
82 return BAD_VALUE;
83 }
Michael Wrightf9d9ce772016-05-13 17:44:16 +010084 outPointerIcon->style = env->GetIntField(pointerIconObj, gPointerIconClassInfo.mType);
Jun Mukaid4eaef72015-10-30 15:54:33 -070085 outPointerIcon->hotSpotX = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotX);
86 outPointerIcon->hotSpotY = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotY);
Jeff Brown2352b972011-04-12 22:39:53 -070087
Jun Mukaid4eaef72015-10-30 15:54:33 -070088 ScopedLocalRef<jobject> bitmapObj(
89 env, env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmap));
90 if (bitmapObj.get()) {
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -040091 outPointerIcon->bitmap = graphics::Bitmap(env, bitmapObj.get());
Jeff Brown2352b972011-04-12 22:39:53 -070092 }
93
Jun Mukai808196f2015-10-28 16:46:44 -070094 ScopedLocalRef<jobjectArray> bitmapFramesObj(env, reinterpret_cast<jobjectArray>(
Jun Mukaid4eaef72015-10-30 15:54:33 -070095 env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmapFrames)));
Jun Mukai808196f2015-10-28 16:46:44 -070096 if (bitmapFramesObj.get()) {
97 outPointerIcon->durationPerFrame = env->GetIntField(
Jun Mukaid4eaef72015-10-30 15:54:33 -070098 pointerIconObj, gPointerIconClassInfo.mDurationPerFrame);
Jun Mukai808196f2015-10-28 16:46:44 -070099 jsize size = env->GetArrayLength(bitmapFramesObj.get());
100 outPointerIcon->bitmapFrames.resize(size);
101 for (jsize i = 0; i < size; ++i) {
102 ScopedLocalRef<jobject> bitmapObj(env, env->GetObjectArrayElement(bitmapFramesObj.get(), i));
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400103 outPointerIcon->bitmapFrames[i] = graphics::Bitmap(env, bitmapObj.get());
Jun Mukai808196f2015-10-28 16:46:44 -0700104 }
105 }
106
Jeff Brown2352b972011-04-12 22:39:53 -0700107 return OK;
108}
109
110status_t android_view_PointerIcon_loadSystemIcon(JNIEnv* env, jobject contextObj,
111 int32_t style, PointerIcon* outPointerIcon) {
112 jobject pointerIconObj = android_view_PointerIcon_getSystemIcon(env, contextObj, style);
113 if (!pointerIconObj) {
114 outPointerIcon->reset();
115 return UNKNOWN_ERROR;
116 }
117
118 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
119 contextObj, outPointerIcon);
120 env->DeleteLocalRef(pointerIconObj);
121 return status;
122}
123
124
125// --- JNI Registration ---
126
Jeff Brown2352b972011-04-12 22:39:53 -0700127int register_android_view_PointerIcon(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800128 jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
129 gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
Jeff Brown2352b972011-04-12 22:39:53 -0700130
Andreas Gampe987f79f2014-11-18 17:29:46 -0800131 gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700132 "mBitmap", "Landroid/graphics/Bitmap;");
133
Michael Wrightf9d9ce772016-05-13 17:44:16 +0100134 gPointerIconClassInfo.mType = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
135 "mType", "I");
Jeff Brown2352b972011-04-12 22:39:53 -0700136
Andreas Gampe987f79f2014-11-18 17:29:46 -0800137 gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700138 "mHotSpotX", "F");
139
Andreas Gampe987f79f2014-11-18 17:29:46 -0800140 gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700141 "mHotSpotY", "F");
142
Jun Mukai808196f2015-10-28 16:46:44 -0700143 gPointerIconClassInfo.mBitmapFrames = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
144 "mBitmapFrames", "[Landroid/graphics/Bitmap;");
145
146 gPointerIconClassInfo.mDurationPerFrame = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
147 "mDurationPerFrame", "I");
148
Andreas Gampe987f79f2014-11-18 17:29:46 -0800149 gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700150 "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
151
Andreas Gampe987f79f2014-11-18 17:29:46 -0800152 gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700153 "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");
154
155 return 0;
156}
157
158} // namespace android