2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H
18 #define ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H
21 #include <android_native_app_glue.h>
23 class DaliDemoNativeActivity
26 DaliDemoNativeActivity(ANativeActivity* activity)
34 JString(JNIEnv* env, const std::string& str)
36 string(env->NewStringUTF(str.c_str()))
40 JString(JNIEnv* env, jstring str)
46 std::string ToString()
51 const char* utf = env->GetStringUTFChars(string, 0);
52 out = std::string(utf);
53 env->ReleaseStringUTFChars(string, utf);
62 env->DeleteLocalRef(string);
67 friend class DaliDemoNativeActivity;
72 class NativeActivityJNI
75 NativeActivityJNI(ANativeActivity* activity)
78 activity->vm->AttachCurrentThread(&env, nullptr);
79 clazz = env->GetObjectClass(activity->clazz);
84 activity->vm->DetachCurrentThread();
87 std::string CallStringMethod(const std::string& name, const std::string& arg)
89 jmethodID methodID = env->GetMethodID(clazz, name.c_str(), "(Ljava/lang/String;)Ljava/lang/String;");
90 JString argument(env, arg);
91 JString returnValue(env, (jstring)env->CallObjectMethod(activity->clazz, methodID, argument.string));
92 return returnValue.ToString();
95 void CallVoidMethod(const std::string& name, const std::string& arg)
97 jmethodID methodID = env->GetMethodID(clazz, name.c_str(), "(Ljava/lang/String;)V");
98 JString argument(env, arg);
99 env->CallVoidMethod(activity->clazz, methodID, argument.string);
103 ANativeActivity* activity;
108 std::string GetMetaData(const std::string& key)
110 NativeActivityJNI nativeActivityJNI(activity);
111 return nativeActivityJNI.CallStringMethod("getMetaData", key);
114 std::string GetIntentStringExtra(const std::string& key)
116 NativeActivityJNI nativeActivityJNI(activity);
117 return nativeActivityJNI.CallStringMethod("getIntentStringExtra", key);
120 void LaunchExample(const std::string& exampleName)
122 NativeActivityJNI nativeActivityJNI(activity);
123 return nativeActivityJNI.CallVoidMethod("launchExample", exampleName);
127 ANativeActivity* activity;
130 #endif //ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H