Merge branch 'devel/master' into sandbox/dkdk/tizen
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor-framework / android / application-launcher.h
1 #ifndef DALI_INTEGRATION_APPLICATION_LAUNCHER_H
2 #define DALI_INTEGRATION_APPLICATION_LAUNCHER_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <dali/devel-api/adaptor-framework/application-devel.h>
22 #include <jni.h>
23
24 using namespace Dali;
25
26 // Wraps DALi application instance for DaliView JNI interface
27 struct ApplicationLauncher
28 {
29   static RefObject* applicationObject;
30   ApplicationLauncher(Application& application)
31   {
32     applicationObject = application.GetObjectPtr();
33   }
34 };
35
36 // Static DALi application instance for DaliView
37 RefObject* ApplicationLauncher::applicationObject;
38
39 namespace
40 {
41 // JNI native "nativeOnCreate" callback when DaliView is created
42 jlong OnCreate(JNIEnv* jenv, jobject obj)
43 {
44   // Extra reference to prevent finalize clearing the app
45   ApplicationLauncher::applicationObject->Reference();
46
47   // not blocking, does nothing except for the setting of the running flag
48   DevelApplication::DownCast(ApplicationLauncher::applicationObject).MainLoop();
49
50   return reinterpret_cast<jlong>(ApplicationLauncher::applicationObject);
51 };
52
53 } // namespace
54
55 // JNI daliview library on load entry function. Registers nativeOnCreate callback for DaliView Java class.
56 JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
57 {
58   JNIEnv* env = nullptr;
59   if(vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
60   {
61     return JNI_ERR;
62   }
63
64   // Find DaliView Java class. JNI_OnLoad is called from the correct class loader context for this to work.
65   jclass clazz = env->FindClass("com/sec/daliview/DaliView");
66   if(clazz == nullptr)
67   {
68     return JNI_ERR;
69   }
70
71   // Register your class' native methods.
72   static const JNINativeMethod methods[] =
73     {
74       {"nativeOnCreate", "()J", (void*)&OnCreate},
75     };
76
77   int rc = env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(JNINativeMethod));
78   if(rc != JNI_OK)
79     return rc;
80
81   return JNI_VERSION_1_6;
82 };
83
84 // This macro facilitates creation of DALi application and launch when DaliView is created.
85 // The DALi application controller is passed to the application created by DaliView JNI library.
86 #define RUN(Controller)                                 \
87   Application         application = Application::New(); \
88   Controller          controller(application);          \
89   ApplicationLauncher launcher(application);
90
91 #endif // DALI_INTEGRATION_APPLICATION_LAUNCHER_H