1 /*
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
19 #include "android_view_PointerIcon.h"
20
21 #include <android/graphics/bitmap.h>
22 #include <android_runtime/AndroidRuntime.h>
23 #include <android_runtime/Log.h>
24 #include <nativehelper/JNIHelp.h>
25 #include <nativehelper/ScopedLocalRef.h>
26 #include <utils/Log.h>
27
28 #include "core_jni_helpers.h"
29
30 namespace android {
31
32 static struct {
33 jclass clazz;
34 jfieldID mType;
35 jfieldID mBitmap;
36 jfieldID mHotSpotX;
37 jfieldID mHotSpotY;
38 jfieldID mBitmapFrames;
39 jfieldID mDurationPerFrame;
40 jmethodID getSystemIcon;
41 jmethodID load;
42 } gPointerIconClassInfo;
43
44
45 // --- Global Functions ---
46
android_view_PointerIcon_getSystemIcon(JNIEnv * env,jobject contextObj,PointerIconStyle style)47 jobject android_view_PointerIcon_getSystemIcon(JNIEnv* env, jobject contextObj,
48 PointerIconStyle style) {
49 jobject pointerIconObj = env->CallStaticObjectMethod(gPointerIconClassInfo.clazz,
50 gPointerIconClassInfo.getSystemIcon, contextObj, style);
51 if (env->ExceptionCheck()) {
52 ALOGW("An exception occurred while getting a pointer icon with style %d.", style);
53 LOGW_EX(env);
54 env->ExceptionClear();
55 return NULL;
56 }
57 return pointerIconObj;
58 }
59
android_view_PointerIcon_load(JNIEnv * env,jobject pointerIconObj,jobject contextObj,PointerIcon * outPointerIcon)60 status_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
68 ScopedLocalRef<jobject> loadedPointerIconObj(env, env->CallObjectMethod(pointerIconObj,
69 gPointerIconClassInfo.load, contextObj));
70 if (env->ExceptionCheck() || !loadedPointerIconObj.get()) {
71 ALOGW("An exception occurred while loading a pointer icon.");
72 LOGW_EX(env);
73 env->ExceptionClear();
74 return UNKNOWN_ERROR;
75 }
76 return android_view_PointerIcon_getLoadedIcon(env, loadedPointerIconObj.get(), outPointerIcon);
77 }
78
android_view_PointerIcon_getLoadedIcon(JNIEnv * env,jobject pointerIconObj,PointerIcon * outPointerIcon)79 status_t android_view_PointerIcon_getLoadedIcon(JNIEnv* env, jobject pointerIconObj,
80 PointerIcon* outPointerIcon) {
81 if (!pointerIconObj) {
82 return BAD_VALUE;
83 }
84 outPointerIcon->style = static_cast<PointerIconStyle>(
85 env->GetIntField(pointerIconObj, gPointerIconClassInfo.mType));
86 outPointerIcon->hotSpotX = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotX);
87 outPointerIcon->hotSpotY = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotY);
88
89 ScopedLocalRef<jobject> bitmapObj(
90 env, env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmap));
91 if (bitmapObj.get()) {
92 outPointerIcon->bitmap = graphics::Bitmap(env, bitmapObj.get());
93 }
94
95 ScopedLocalRef<jobjectArray> bitmapFramesObj(env, reinterpret_cast<jobjectArray>(
96 env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmapFrames)));
97 if (bitmapFramesObj.get()) {
98 outPointerIcon->durationPerFrame = env->GetIntField(
99 pointerIconObj, gPointerIconClassInfo.mDurationPerFrame);
100 jsize size = env->GetArrayLength(bitmapFramesObj.get());
101 outPointerIcon->bitmapFrames.resize(size);
102 for (jsize i = 0; i < size; ++i) {
103 ScopedLocalRef<jobject> bitmapObj(env, env->GetObjectArrayElement(bitmapFramesObj.get(), i));
104 outPointerIcon->bitmapFrames[i] = graphics::Bitmap(env, bitmapObj.get());
105 }
106 }
107
108 return OK;
109 }
110
android_view_PointerIcon_loadSystemIcon(JNIEnv * env,jobject contextObj,PointerIconStyle style,PointerIcon * outPointerIcon)111 status_t android_view_PointerIcon_loadSystemIcon(JNIEnv* env, jobject contextObj,
112 PointerIconStyle style,
113 PointerIcon* outPointerIcon) {
114 jobject pointerIconObj = android_view_PointerIcon_getSystemIcon(env, contextObj, style);
115 if (!pointerIconObj) {
116 outPointerIcon->reset();
117 return UNKNOWN_ERROR;
118 }
119
120 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
121 contextObj, outPointerIcon);
122 env->DeleteLocalRef(pointerIconObj);
123 return status;
124 }
125
126 // --- JNI Registration ---
127
register_android_view_PointerIcon(JNIEnv * env)128 int register_android_view_PointerIcon(JNIEnv* env) {
129 jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
130 gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
131
132 gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
133 "mBitmap", "Landroid/graphics/Bitmap;");
134
135 gPointerIconClassInfo.mType = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
136 "mType", "I");
137
138 gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
139 "mHotSpotX", "F");
140
141 gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
142 "mHotSpotY", "F");
143
144 gPointerIconClassInfo.mBitmapFrames = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
145 "mBitmapFrames", "[Landroid/graphics/Bitmap;");
146
147 gPointerIconClassInfo.mDurationPerFrame = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
148 "mDurationPerFrame", "I");
149
150 gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz,
151 "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
152
153 gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz,
154 "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");
155
156 return 0;
157 }
158
159 } // namespace android
160