• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <nativehelper/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/bitmap.h>
27 #include <nativehelper/ScopedLocalRef.h>
28 
29 #include "core_jni_helpers.h"
30 
31 namespace android {
32 
33 static struct {
34     jclass clazz;
35     jfieldID mType;
36     jfieldID mBitmap;
37     jfieldID mHotSpotX;
38     jfieldID mHotSpotY;
39     jfieldID mBitmapFrames;
40     jfieldID mDurationPerFrame;
41     jmethodID getSystemIcon;
42     jmethodID load;
43 } gPointerIconClassInfo;
44 
45 
46 // --- Global Functions ---
47 
android_view_PointerIcon_getSystemIcon(JNIEnv * env,jobject contextObj,int32_t style)48 jobject 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()) {
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 = env->GetIntField(pointerIconObj, gPointerIconClassInfo.mType);
85     outPointerIcon->hotSpotX = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotX);
86     outPointerIcon->hotSpotY = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotY);
87 
88     ScopedLocalRef<jobject> bitmapObj(
89             env, env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmap));
90     if (bitmapObj.get()) {
91         outPointerIcon->bitmap = graphics::Bitmap(env, bitmapObj.get());
92     }
93 
94     ScopedLocalRef<jobjectArray> bitmapFramesObj(env, reinterpret_cast<jobjectArray>(
95             env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmapFrames)));
96     if (bitmapFramesObj.get()) {
97         outPointerIcon->durationPerFrame = env->GetIntField(
98                 pointerIconObj, gPointerIconClassInfo.mDurationPerFrame);
99         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));
103             outPointerIcon->bitmapFrames[i] = graphics::Bitmap(env, bitmapObj.get());
104         }
105     }
106 
107     return OK;
108 }
109 
android_view_PointerIcon_loadSystemIcon(JNIEnv * env,jobject contextObj,int32_t style,PointerIcon * outPointerIcon)110 status_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 
register_android_view_PointerIcon(JNIEnv * env)127 int register_android_view_PointerIcon(JNIEnv* env) {
128     jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
129     gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
130 
131     gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
132             "mBitmap", "Landroid/graphics/Bitmap;");
133 
134     gPointerIconClassInfo.mType = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
135             "mType", "I");
136 
137     gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
138             "mHotSpotX", "F");
139 
140     gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
141             "mHotSpotY", "F");
142 
143     gPointerIconClassInfo.mBitmapFrames = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
144             "mBitmapFrames", "[Landroid/graphics/Bitmap;");
145 
146     gPointerIconClassInfo.mDurationPerFrame = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
147             "mDurationPerFrame", "I");
148 
149     gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz,
150             "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
151 
152     gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz,
153             "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");
154 
155     return 0;
156 }
157 
158 } // namespace android
159