dali-demo-native-activity-jni.h
3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H
#define ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H
#include <string>
#include <android_native_app_glue.h>
class DaliDemoNativeActivity
{
public:
DaliDemoNativeActivity(ANativeActivity* activity)
: activity(activity)
{
}
class JString
{
public:
JString(JNIEnv* env, const std::string& str)
: env(env),
string(env->NewStringUTF(str.c_str()))
{
}
JString(JNIEnv* env, jstring str)
: env(env),
string(str)
{
}
std::string ToString()
{
std::string out;
if (string)
{
const char* utf = env->GetStringUTFChars(string, 0);
out = std::string(utf);
env->ReleaseStringUTFChars(string, utf);
}
return out;
}
~JString()
{
if (string)
{
env->DeleteLocalRef(string);
}
}
private:
friend class DaliDemoNativeActivity;
JNIEnv* env;
jstring string;
};
class NativeActivityJNI
{
public:
NativeActivityJNI(ANativeActivity* activity)
: activity(activity)
{
activity->vm->AttachCurrentThread(&env, nullptr);
clazz = env->GetObjectClass(activity->clazz);
}
~NativeActivityJNI()
{
activity->vm->DetachCurrentThread();
}
std::string CallStringMethod(const std::string& name, const std::string& arg)
{
jmethodID methodID = env->GetMethodID(clazz, name.c_str(), "(Ljava/lang/String;)Ljava/lang/String;");
JString argument(env, arg);
JString returnValue(env, (jstring)env->CallObjectMethod(activity->clazz, methodID, argument.string));
return returnValue.ToString();
}
void CallVoidMethod(const std::string& name, const std::string& arg)
{
jmethodID methodID = env->GetMethodID(clazz, name.c_str(), "(Ljava/lang/String;)V");
JString argument(env, arg);
env->CallVoidMethod(activity->clazz, methodID, argument.string);
}
private:
ANativeActivity* activity;
JNIEnv* env;
jclass clazz;
};
std::string GetMetaData(const std::string& key)
{
NativeActivityJNI nativeActivityJNI(activity);
return nativeActivityJNI.CallStringMethod("getMetaData", key);
}
std::string GetIntentStringExtra(const std::string& key)
{
NativeActivityJNI nativeActivityJNI(activity);
return nativeActivityJNI.CallStringMethod("getIntentStringExtra", key);
}
void LaunchExample(const std::string& exampleName)
{
NativeActivityJNI nativeActivityJNI(activity);
return nativeActivityJNI.CallVoidMethod("launchExample", exampleName);
}
private:
ANativeActivity* activity;
};
#endif //ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H