4a6319f937d1bd909dde49784987a3c15550a5b0
[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) 2019 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 <jni.h>
22 #include <dali/devel-api/adaptor-framework/application-devel.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
42 // JNI native "nativeOnCreate" callback when DaliView is created
43 jlong OnCreate(JNIEnv *jenv, jobject obj)
44 {
45   // Extra reference to prevent finalize clearing the app
46   ApplicationLauncher::applicationObject->Reference();
47
48   // not blocking, does nothing except for the setting of the running flag
49   DevelApplication::DownCast( ApplicationLauncher::applicationObject ).MainLoop();
50
51   return reinterpret_cast<jlong>( ApplicationLauncher::applicationObject );
52 };
53
54 }
55
56 // JNI daliview library on load entry function. Registers nativeOnCreate callback for DaliView Java class.
57 JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved)
58 {
59   JNIEnv* env = nullptr;
60   if( vm->GetEnv( reinterpret_cast<void **>( &env ), JNI_VERSION_1_6) != JNI_OK )
61   {
62     return JNI_ERR;
63   }
64
65   // Find DaliView Java class. JNI_OnLoad is called from the correct class loader context for this to work.
66   jclass clazz = env->FindClass( "com/sec/daliview/DaliView" );
67   if( clazz == nullptr )
68   {
69     return JNI_ERR;
70   }
71
72   // Register your class' native methods.
73   static const JNINativeMethod methods[] =
74   {
75       { "nativeOnCreate", "()J", (void *)&OnCreate },
76   };
77
78   int rc = env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(JNINativeMethod));
79   if (rc != JNI_OK)
80     return rc;
81
82   return JNI_VERSION_1_6;
83 };
84
85 // This macro facilitates creation of DALi application and launch when DaliView is created.
86 // The DALi application controller is passed to the application created by DaliView JNI library.
87 #define RUN(Controller) Application application = Application::New(); \
88                         Controller controller( application ); \
89                         ApplicationLauncher launcher( application ); \
90
91 #endif // DALI_INTEGRATION_APPLICATION_LAUNCHER_H
92