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-base/logging.h>
22 #include <android/graphics/bitmap.h>
23 #include <android_runtime/AndroidRuntime.h>
24 #include <android_runtime/Log.h>
25 #include <nativehelper/JNIHelp.h>
26 #include <nativehelper/ScopedLocalRef.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 jfieldID mDrawNativeDropShadow;
41 } gPointerIconClassInfo;
42
43
44 // --- Global Functions ---
45
android_view_PointerIcon_toNative(JNIEnv * env,jobject pointerIconObj)46 PointerIcon android_view_PointerIcon_toNative(JNIEnv* env, jobject pointerIconObj) {
47 if (!pointerIconObj) {
48 LOG(FATAL) << __func__ << ": pointerIconObj is null";
49 }
50 PointerIcon icon;
51 icon.style = static_cast<PointerIconStyle>(
52 env->GetIntField(pointerIconObj, gPointerIconClassInfo.mType));
53 icon.hotSpotX = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotX);
54 icon.hotSpotY = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotY);
55 icon.drawNativeDropShadow =
56 env->GetBooleanField(pointerIconObj, gPointerIconClassInfo.mDrawNativeDropShadow);
57
58 ScopedLocalRef<jobject> bitmapObj(
59 env, env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmap));
60 if (bitmapObj.get()) {
61 icon.bitmap = graphics::Bitmap(env, bitmapObj.get());
62 }
63
64 ScopedLocalRef<jobjectArray> bitmapFramesObj(env, reinterpret_cast<jobjectArray>(
65 env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmapFrames)));
66 if (bitmapFramesObj.get()) {
67 icon.durationPerFrame =
68 env->GetIntField(pointerIconObj, gPointerIconClassInfo.mDurationPerFrame);
69 jsize size = env->GetArrayLength(bitmapFramesObj.get());
70 icon.bitmapFrames.resize(size);
71 for (jsize i = 0; i < size; ++i) {
72 ScopedLocalRef<jobject> bitmapObj(env, env->GetObjectArrayElement(bitmapFramesObj.get(), i));
73 icon.bitmapFrames[i] = graphics::Bitmap(env, bitmapObj.get());
74 }
75 }
76
77 return icon;
78 }
79
80 // --- JNI Registration ---
81
register_android_view_PointerIcon(JNIEnv * env)82 int register_android_view_PointerIcon(JNIEnv* env) {
83 jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
84 gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
85
86 gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
87 "mBitmap", "Landroid/graphics/Bitmap;");
88
89 gPointerIconClassInfo.mType = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
90 "mType", "I");
91
92 gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
93 "mHotSpotX", "F");
94
95 gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
96 "mHotSpotY", "F");
97
98 gPointerIconClassInfo.mBitmapFrames = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
99 "mBitmapFrames", "[Landroid/graphics/Bitmap;");
100
101 gPointerIconClassInfo.mDrawNativeDropShadow =
102 GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, "mDrawNativeDropShadow", "Z");
103
104 gPointerIconClassInfo.mDurationPerFrame = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
105 "mDurationPerFrame", "I");
106
107 return 0;
108 }
109
110 } // namespace android
111