Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 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> |
Ruben Brunk | 87eac99 | 2013-09-09 17:44:59 -0700 | [diff] [blame] | 24 | #include <android_runtime/Log.h> |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | #include <android/graphics/GraphicsJNI.h> |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 27 | #include "ScopedLocalRef.h" |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 28 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 29 | #include "core_jni_helpers.h" |
| 30 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | |
| 33 | static struct { |
| 34 | jclass clazz; |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 35 | jfieldID mType; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 36 | jfieldID mBitmap; |
| 37 | jfieldID mHotSpotX; |
| 38 | jfieldID mHotSpotY; |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 39 | jfieldID mBitmapFrames; |
| 40 | jfieldID mDurationPerFrame; |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 41 | jmethodID getSystemIcon; |
| 42 | jmethodID load; |
| 43 | } gPointerIconClassInfo; |
| 44 | |
| 45 | |
| 46 | // --- Global Functions --- |
| 47 | |
| 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()) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 52 | ALOGW("An exception occurred while getting a pointer icon with style %d.", style); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 53 | LOGW_EX(env); |
| 54 | env->ExceptionClear(); |
| 55 | return NULL; |
| 56 | } |
| 57 | return pointerIconObj; |
| 58 | } |
| 59 | |
| 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 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 68 | ScopedLocalRef<jobject> loadedPointerIconObj(env, env->CallObjectMethod(pointerIconObj, |
| 69 | gPointerIconClassInfo.load, contextObj)); |
| 70 | if (env->ExceptionCheck() || !loadedPointerIconObj.get()) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 71 | ALOGW("An exception occurred while loading a pointer icon."); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 72 | LOGW_EX(env); |
| 73 | env->ExceptionClear(); |
| 74 | return UNKNOWN_ERROR; |
| 75 | } |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 76 | return android_view_PointerIcon_getLoadedIcon(env, loadedPointerIconObj.get(), outPointerIcon); |
| 77 | } |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 78 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 79 | status_t android_view_PointerIcon_getLoadedIcon(JNIEnv* env, jobject pointerIconObj, |
| 80 | PointerIcon* outPointerIcon) { |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 81 | outPointerIcon->style = env->GetIntField(pointerIconObj, gPointerIconClassInfo.mType); |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 82 | outPointerIcon->hotSpotX = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotX); |
| 83 | outPointerIcon->hotSpotY = env->GetFloatField(pointerIconObj, gPointerIconClassInfo.mHotSpotY); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 84 | |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 85 | ScopedLocalRef<jobject> bitmapObj( |
| 86 | env, env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmap)); |
| 87 | if (bitmapObj.get()) { |
| 88 | GraphicsJNI::getSkBitmap(env, bitmapObj.get(), &(outPointerIcon->bitmap)); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 91 | ScopedLocalRef<jobjectArray> bitmapFramesObj(env, reinterpret_cast<jobjectArray>( |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 92 | env->GetObjectField(pointerIconObj, gPointerIconClassInfo.mBitmapFrames))); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 93 | if (bitmapFramesObj.get()) { |
| 94 | outPointerIcon->durationPerFrame = env->GetIntField( |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 95 | pointerIconObj, gPointerIconClassInfo.mDurationPerFrame); |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 96 | jsize size = env->GetArrayLength(bitmapFramesObj.get()); |
| 97 | outPointerIcon->bitmapFrames.resize(size); |
| 98 | for (jsize i = 0; i < size; ++i) { |
| 99 | ScopedLocalRef<jobject> bitmapObj(env, env->GetObjectArrayElement(bitmapFramesObj.get(), i)); |
| 100 | GraphicsJNI::getSkBitmap(env, bitmapObj.get(), &(outPointerIcon->bitmapFrames[i])); |
| 101 | } |
| 102 | } |
| 103 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 104 | return OK; |
| 105 | } |
| 106 | |
| 107 | status_t android_view_PointerIcon_loadSystemIcon(JNIEnv* env, jobject contextObj, |
| 108 | int32_t style, PointerIcon* outPointerIcon) { |
| 109 | jobject pointerIconObj = android_view_PointerIcon_getSystemIcon(env, contextObj, style); |
| 110 | if (!pointerIconObj) { |
| 111 | outPointerIcon->reset(); |
| 112 | return UNKNOWN_ERROR; |
| 113 | } |
| 114 | |
| 115 | status_t status = android_view_PointerIcon_load(env, pointerIconObj, |
| 116 | contextObj, outPointerIcon); |
| 117 | env->DeleteLocalRef(pointerIconObj); |
| 118 | return status; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | // --- JNI Registration --- |
| 123 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 124 | int register_android_view_PointerIcon(JNIEnv* env) { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 125 | jclass clazz = FindClassOrDie(env, "android/view/PointerIcon"); |
| 126 | gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 127 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 128 | gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 129 | "mBitmap", "Landroid/graphics/Bitmap;"); |
| 130 | |
Michael Wright | e051f6f | 2016-05-13 17:44:16 +0100 | [diff] [blame] | 131 | gPointerIconClassInfo.mType = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
| 132 | "mType", "I"); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 133 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 134 | gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 135 | "mHotSpotX", "F"); |
| 136 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 137 | gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 138 | "mHotSpotY", "F"); |
| 139 | |
Jun Mukai | 808196f | 2015-10-28 16:46:44 -0700 | [diff] [blame] | 140 | gPointerIconClassInfo.mBitmapFrames = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
| 141 | "mBitmapFrames", "[Landroid/graphics/Bitmap;"); |
| 142 | |
| 143 | gPointerIconClassInfo.mDurationPerFrame = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
| 144 | "mDurationPerFrame", "I"); |
| 145 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 146 | gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 147 | "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;"); |
| 148 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 149 | gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 150 | "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;"); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | } // namespace android |