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> |
| 24 | #include <utils/Log.h> |
| 25 | #include <android/graphics/GraphicsJNI.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | static struct { |
| 30 | jclass clazz; |
| 31 | jfieldID mStyle; |
| 32 | jfieldID mBitmap; |
| 33 | jfieldID mHotSpotX; |
| 34 | jfieldID mHotSpotY; |
| 35 | jmethodID getSystemIcon; |
| 36 | jmethodID load; |
| 37 | } gPointerIconClassInfo; |
| 38 | |
| 39 | |
| 40 | // --- Global Functions --- |
| 41 | |
| 42 | jobject android_view_PointerIcon_getSystemIcon(JNIEnv* env, jobject contextObj, int32_t style) { |
| 43 | jobject pointerIconObj = env->CallStaticObjectMethod(gPointerIconClassInfo.clazz, |
| 44 | gPointerIconClassInfo.getSystemIcon, contextObj, style); |
| 45 | if (env->ExceptionCheck()) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 46 | 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] | 47 | LOGW_EX(env); |
| 48 | env->ExceptionClear(); |
| 49 | return NULL; |
| 50 | } |
| 51 | return pointerIconObj; |
| 52 | } |
| 53 | |
| 54 | status_t android_view_PointerIcon_load(JNIEnv* env, jobject pointerIconObj, jobject contextObj, |
| 55 | PointerIcon* outPointerIcon) { |
| 56 | outPointerIcon->reset(); |
| 57 | |
| 58 | if (!pointerIconObj) { |
| 59 | return OK; |
| 60 | } |
| 61 | |
| 62 | jobject loadedPointerIconObj = env->CallObjectMethod(pointerIconObj, |
| 63 | gPointerIconClassInfo.load, contextObj); |
| 64 | if (env->ExceptionCheck() || !loadedPointerIconObj) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 65 | ALOGW("An exception occurred while loading a pointer icon."); |
Jeff Brown | 2352b97 | 2011-04-12 22:39:53 -0700 | [diff] [blame] | 66 | LOGW_EX(env); |
| 67 | env->ExceptionClear(); |
| 68 | return UNKNOWN_ERROR; |
| 69 | } |
| 70 | |
| 71 | outPointerIcon->style = env->GetIntField(loadedPointerIconObj, |
| 72 | gPointerIconClassInfo.mStyle); |
| 73 | outPointerIcon->hotSpotX = env->GetFloatField(loadedPointerIconObj, |
| 74 | gPointerIconClassInfo.mHotSpotX); |
| 75 | outPointerIcon->hotSpotY = env->GetFloatField(loadedPointerIconObj, |
| 76 | gPointerIconClassInfo.mHotSpotY); |
| 77 | |
| 78 | jobject bitmapObj = env->GetObjectField(loadedPointerIconObj, gPointerIconClassInfo.mBitmap); |
| 79 | if (bitmapObj) { |
| 80 | SkBitmap* bitmap = GraphicsJNI::getNativeBitmap(env, bitmapObj); |
| 81 | if (bitmap) { |
| 82 | outPointerIcon->bitmap = *bitmap; // use a shared pixel ref |
| 83 | } |
| 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 | |
| 108 | #define FIND_CLASS(var, className) \ |
| 109 | var = env->FindClass(className); \ |
| 110 | LOG_FATAL_IF(! var, "Unable to find class " className); \ |
| 111 | var = jclass(env->NewGlobalRef(var)); |
| 112 | |
| 113 | #define GET_STATIC_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 114 | var = env->GetStaticMethodID(clazz, methodName, methodDescriptor); \ |
| 115 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 116 | |
| 117 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 118 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 119 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 120 | |
| 121 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 122 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 123 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 124 | |
| 125 | int register_android_view_PointerIcon(JNIEnv* env) { |
| 126 | FIND_CLASS(gPointerIconClassInfo.clazz, "android/view/PointerIcon"); |
| 127 | |
| 128 | GET_FIELD_ID(gPointerIconClassInfo.mBitmap, gPointerIconClassInfo.clazz, |
| 129 | "mBitmap", "Landroid/graphics/Bitmap;"); |
| 130 | |
| 131 | GET_FIELD_ID(gPointerIconClassInfo.mStyle, gPointerIconClassInfo.clazz, |
| 132 | "mStyle", "I"); |
| 133 | |
| 134 | GET_FIELD_ID(gPointerIconClassInfo.mHotSpotX, gPointerIconClassInfo.clazz, |
| 135 | "mHotSpotX", "F"); |
| 136 | |
| 137 | GET_FIELD_ID(gPointerIconClassInfo.mHotSpotY, gPointerIconClassInfo.clazz, |
| 138 | "mHotSpotY", "F"); |
| 139 | |
| 140 | GET_STATIC_METHOD_ID(gPointerIconClassInfo.getSystemIcon, gPointerIconClassInfo.clazz, |
| 141 | "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;"); |
| 142 | |
| 143 | GET_METHOD_ID(gPointerIconClassInfo.load, gPointerIconClassInfo.clazz, |
| 144 | "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;"); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | } // namespace android |