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 "JNIHelp.h"
20
21 #include "android_view_PointerIcon.h"
22
23 #include <android_runtime/AndroidRuntime.h>
24 #include <android_runtime/Log.h>
25 #include <utils/Log.h>
26 #include <android/graphics/GraphicsJNI.h>
27
28 #include "core_jni_helpers.h"
29
30 namespace android {
31
32 static struct {
33 jclass clazz;
34 jfieldID mStyle;
35 jfieldID mBitmap;
36 jfieldID mHotSpotX;
37 jfieldID mHotSpotY;
38 jmethodID getSystemIcon;
39 jmethodID load;
40 } gPointerIconClassInfo;
41
42
43 // --- Global Functions ---
44
android_view_PointerIcon_getSystemIcon(JNIEnv * env,jobject contextObj,int32_t style)45 jobject android_view_PointerIcon_getSystemIcon(JNIEnv* env, jobject contextObj, int32_t style) {
46 jobject pointerIconObj = env->CallStaticObjectMethod(gPointerIconClassInfo.clazz,
47 gPointerIconClassInfo.getSystemIcon, contextObj, style);
48 if (env->ExceptionCheck()) {
49 ALOGW("An exception occurred while getting a pointer icon with style %d.", style);
50 LOGW_EX(env);
51 env->ExceptionClear();
52 return NULL;
53 }
54 return pointerIconObj;
55 }
56
android_view_PointerIcon_load(JNIEnv * env,jobject pointerIconObj,jobject contextObj,PointerIcon * outPointerIcon)57 status_t android_view_PointerIcon_load(JNIEnv* env, jobject pointerIconObj, jobject contextObj,
58 PointerIcon* outPointerIcon) {
59 outPointerIcon->reset();
60
61 if (!pointerIconObj) {
62 return OK;
63 }
64
65 jobject loadedPointerIconObj = env->CallObjectMethod(pointerIconObj,
66 gPointerIconClassInfo.load, contextObj);
67 if (env->ExceptionCheck() || !loadedPointerIconObj) {
68 ALOGW("An exception occurred while loading a pointer icon.");
69 LOGW_EX(env);
70 env->ExceptionClear();
71 return UNKNOWN_ERROR;
72 }
73
74 outPointerIcon->style = env->GetIntField(loadedPointerIconObj,
75 gPointerIconClassInfo.mStyle);
76 outPointerIcon->hotSpotX = env->GetFloatField(loadedPointerIconObj,
77 gPointerIconClassInfo.mHotSpotX);
78 outPointerIcon->hotSpotY = env->GetFloatField(loadedPointerIconObj,
79 gPointerIconClassInfo.mHotSpotY);
80
81 jobject bitmapObj = env->GetObjectField(loadedPointerIconObj, gPointerIconClassInfo.mBitmap);
82 if (bitmapObj) {
83 GraphicsJNI::getSkBitmap(env, bitmapObj, &(outPointerIcon->bitmap));
84 env->DeleteLocalRef(bitmapObj);
85 }
86
87 env->DeleteLocalRef(loadedPointerIconObj);
88 return OK;
89 }
90
android_view_PointerIcon_loadSystemIcon(JNIEnv * env,jobject contextObj,int32_t style,PointerIcon * outPointerIcon)91 status_t android_view_PointerIcon_loadSystemIcon(JNIEnv* env, jobject contextObj,
92 int32_t style, PointerIcon* outPointerIcon) {
93 jobject pointerIconObj = android_view_PointerIcon_getSystemIcon(env, contextObj, style);
94 if (!pointerIconObj) {
95 outPointerIcon->reset();
96 return UNKNOWN_ERROR;
97 }
98
99 status_t status = android_view_PointerIcon_load(env, pointerIconObj,
100 contextObj, outPointerIcon);
101 env->DeleteLocalRef(pointerIconObj);
102 return status;
103 }
104
105
106 // --- JNI Registration ---
107
register_android_view_PointerIcon(JNIEnv * env)108 int register_android_view_PointerIcon(JNIEnv* env) {
109 jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
110 gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
111
112 gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
113 "mBitmap", "Landroid/graphics/Bitmap;");
114
115 gPointerIconClassInfo.mStyle = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
116 "mStyle", "I");
117
118 gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
119 "mHotSpotX", "F");
120
121 gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
122 "mHotSpotY", "F");
123
124 gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz,
125 "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
126
127 gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz,
128 "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");
129
130 return 0;
131 }
132
133 } // namespace android
134