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> |
| 27 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 28 | #include "core_jni_helpers.h" |
| 29 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 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 | |
| 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()) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 49 | 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] | 50 | LOGW_EX(env); |
| 51 | env->ExceptionClear(); |
| 52 | return NULL; |
| 53 | } |
| 54 | return pointerIconObj; |
| 55 | } |
| 56 | |
| 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) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 68 | ALOGW("An exception occurred while loading a pointer icon."); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 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) { |
John Reck | ed207b9 | 2015-04-10 13:52:57 -0700 | [diff] [blame^] | 83 | GraphicsJNI::getSkBitmap(env, bitmapObj, &(outPointerIcon->bitmap)); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 84 | env->DeleteLocalRef(bitmapObj); |
| 85 | } |
| 86 | |
| 87 | env->DeleteLocalRef(loadedPointerIconObj); |
| 88 | return OK; |
| 89 | } |
| 90 | |
| 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 | |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 108 | int register_android_view_PointerIcon(JNIEnv* env) { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 109 | jclass clazz = FindClassOrDie(env, "android/view/PointerIcon"); |
| 110 | gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 111 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 112 | gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 113 | "mBitmap", "Landroid/graphics/Bitmap;"); |
| 114 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 115 | gPointerIconClassInfo.mStyle = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 116 | "mStyle", "I"); |
| 117 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 118 | gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 119 | "mHotSpotX", "F"); |
| 120 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 121 | gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 122 | "mHotSpotY", "F"); |
| 123 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 124 | gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 125 | "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;"); |
| 126 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 127 | gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz, |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 128 | "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;"); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | } // namespace android |