android_view_PointerIcon.cpp revision b2915245b74b3b5541b123e38403f8e26426b4b7
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 <android_runtime/Log.h>
25#include <utils/Log.h>
26#include <android/graphics/GraphicsJNI.h>
27
28#include "core_jni_helpers.h"
29
30namespace android {
31
32static 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
45jobject 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()) {
49        ALOGW("An exception occurred while getting a pointer icon with style %d.", style);
50        LOGW_EX(env);
51        env->ExceptionClear();
52        return NULL;
53    }
54    return pointerIconObj;
55}
56
57status_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) {
68        ALOGW("An exception occurred while loading a pointer icon.");
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) {
83        SkBitmap* bitmap = GraphicsJNI::getSkBitmap(env, bitmapObj);
84        if (bitmap) {
85            outPointerIcon->bitmap = *bitmap; // use a shared pixel ref
86        }
87        env->DeleteLocalRef(bitmapObj);
88    }
89
90    env->DeleteLocalRef(loadedPointerIconObj);
91    return OK;
92}
93
94status_t android_view_PointerIcon_loadSystemIcon(JNIEnv* env, jobject contextObj,
95        int32_t style, PointerIcon* outPointerIcon) {
96    jobject pointerIconObj = android_view_PointerIcon_getSystemIcon(env, contextObj, style);
97    if (!pointerIconObj) {
98        outPointerIcon->reset();
99        return UNKNOWN_ERROR;
100    }
101
102    status_t status = android_view_PointerIcon_load(env, pointerIconObj,
103            contextObj, outPointerIcon);
104    env->DeleteLocalRef(pointerIconObj);
105    return status;
106}
107
108
109// --- JNI Registration ---
110
111int register_android_view_PointerIcon(JNIEnv* env) {
112    jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
113    gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
114
115    gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
116            "mBitmap", "Landroid/graphics/Bitmap;");
117
118    gPointerIconClassInfo.mStyle = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
119            "mStyle", "I");
120
121    gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
122            "mHotSpotX", "F");
123
124    gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
125            "mHotSpotY", "F");
126
127    gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz,
128            "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
129
130    gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz,
131            "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");
132
133    return 0;
134}
135
136} // namespace android
137