Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / build / android / app / src / main / cpp / dali-demo-native-activity-jni.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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 #ifndef ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H
18 #define ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H
19
20 #include <android_native_app_glue.h>
21 #include <string>
22
23 class DaliDemoNativeActivity
24 {
25 public:
26   DaliDemoNativeActivity(ANativeActivity* activity)
27   : activity(activity)
28   {
29   }
30
31   class JString
32   {
33   public:
34     JString(JNIEnv* env, const std::string& str)
35     : env(env),
36       string(env->NewStringUTF(str.c_str()))
37     {
38     }
39
40     JString(JNIEnv* env, jstring str)
41     : env(env),
42       string(str)
43     {
44     }
45
46     std::string ToString()
47     {
48       std::string out;
49       if(string)
50       {
51         const char* utf = env->GetStringUTFChars(string, 0);
52         out             = std::string(utf);
53         env->ReleaseStringUTFChars(string, utf);
54       }
55       return out;
56     }
57
58     ~JString()
59     {
60       if(string)
61       {
62         env->DeleteLocalRef(string);
63       }
64     }
65
66   private:
67     friend class DaliDemoNativeActivity;
68     JNIEnv* env;
69     jstring string;
70   };
71
72   class NativeActivityJNI
73   {
74   public:
75     NativeActivityJNI(ANativeActivity* activity)
76     : activity(activity)
77     {
78       activity->vm->AttachCurrentThread(&env, nullptr);
79       clazz = env->GetObjectClass(activity->clazz);
80     }
81
82     ~NativeActivityJNI()
83     {
84       activity->vm->DetachCurrentThread();
85     }
86
87     std::string CallStringMethod(const std::string& name, const std::string& arg)
88     {
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();
93     }
94
95     void CallVoidMethod(const std::string& name, const std::string& arg)
96     {
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);
100     }
101
102   private:
103     ANativeActivity* activity;
104     JNIEnv*          env;
105     jclass           clazz;
106   };
107
108   std::string GetMetaData(const std::string& key)
109   {
110     NativeActivityJNI nativeActivityJNI(activity);
111     return nativeActivityJNI.CallStringMethod("getMetaData", key);
112   }
113
114   std::string GetIntentStringExtra(const std::string& key)
115   {
116     NativeActivityJNI nativeActivityJNI(activity);
117     return nativeActivityJNI.CallStringMethod("getIntentStringExtra", key);
118   }
119
120   void LaunchExample(const std::string& exampleName)
121   {
122     NativeActivityJNI nativeActivityJNI(activity);
123     return nativeActivityJNI.CallVoidMethod("launchExample", exampleName);
124   }
125
126 private:
127   ANativeActivity* activity;
128 };
129
130 #endif //ANDROID_DALI_DEMO_NATIVE_ACTIVITY_JNI_H