blob: d04adbfc742b4bc0ce4d75fca43080a0f9e78c36 [file] [log] [blame]
Jeff Brown2352b972011-04-12 22:39:53 -07001/*
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 Brunk87eac992013-09-09 17:44:59 -070024#include <android_runtime/Log.h>
Jeff Brown2352b972011-04-12 22:39:53 -070025#include <utils/Log.h>
26#include <android/graphics/GraphicsJNI.h>
27
Andreas Gampe987f79f2014-11-18 17:29:46 -080028#include "core_jni_helpers.h"
29
Jeff Brown2352b972011-04-12 22:39:53 -070030namespace 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()) {
Steve Block8564c8d2012-01-05 23:22:43 +000049 ALOGW("An exception occurred while getting a pointer icon with style %d.", style);
Jeff Brown2352b972011-04-12 22:39:53 -070050 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) {
Steve Block8564c8d2012-01-05 23:22:43 +000068 ALOGW("An exception occurred while loading a pointer icon.");
Jeff Brown2352b972011-04-12 22:39:53 -070069 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 Recked207b92015-04-10 13:52:57 -070083 GraphicsJNI::getSkBitmap(env, bitmapObj, &(outPointerIcon->bitmap));
Jeff Brown2352b972011-04-12 22:39:53 -070084 env->DeleteLocalRef(bitmapObj);
85 }
86
87 env->DeleteLocalRef(loadedPointerIconObj);
88 return OK;
89}
90
91status_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 Brown2352b972011-04-12 22:39:53 -0700108int register_android_view_PointerIcon(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800109 jclass clazz = FindClassOrDie(env, "android/view/PointerIcon");
110 gPointerIconClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
Jeff Brown2352b972011-04-12 22:39:53 -0700111
Andreas Gampe987f79f2014-11-18 17:29:46 -0800112 gPointerIconClassInfo.mBitmap = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700113 "mBitmap", "Landroid/graphics/Bitmap;");
114
Andreas Gampe987f79f2014-11-18 17:29:46 -0800115 gPointerIconClassInfo.mStyle = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700116 "mStyle", "I");
117
Andreas Gampe987f79f2014-11-18 17:29:46 -0800118 gPointerIconClassInfo.mHotSpotX = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700119 "mHotSpotX", "F");
120
Andreas Gampe987f79f2014-11-18 17:29:46 -0800121 gPointerIconClassInfo.mHotSpotY = GetFieldIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700122 "mHotSpotY", "F");
123
Andreas Gampe987f79f2014-11-18 17:29:46 -0800124 gPointerIconClassInfo.getSystemIcon = GetStaticMethodIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700125 "getSystemIcon", "(Landroid/content/Context;I)Landroid/view/PointerIcon;");
126
Andreas Gampe987f79f2014-11-18 17:29:46 -0800127 gPointerIconClassInfo.load = GetMethodIDOrDie(env, gPointerIconClassInfo.clazz,
Jeff Brown2352b972011-04-12 22:39:53 -0700128 "load", "(Landroid/content/Context;)Landroid/view/PointerIcon;");
129
130 return 0;
131}
132
133} // namespace android