Start of calling the jvm from the C code

This commit is contained in:
Edouard Dupin 2012-04-18 18:14:13 +02:00
parent 7ad9bce29c
commit cda5d8cd40
9 changed files with 259 additions and 19 deletions

View File

@ -35,6 +35,9 @@ import android.content.res.AssetManager;
*
*/
public class __PROJECT_NAME__ extends Activity {
private static native void TouchEvent();
private static native void ActivitySetJavaVortualMachineStart();
private static native void ActivitySetJavaVortualMachineStop();
private static native void ActivityOnCreate();
private static native void ActivityOnStart();
private static native void ActivityOnReStart();
@ -50,9 +53,13 @@ public class __PROJECT_NAME__ extends Activity {
System.loadLibrary("__PROJECT_PACKAGE__");
}
@Override protected void onCreate(Bundle savedInstanceState) {
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// set the java evironement in the C sources :
ActivitySetJavaVortualMachineStart();
// Load the application directory
ActivityParamSetArchiveDir(1, getFilesDir().toString());
ActivityParamSetArchiveDir(2, getCacheDir().toString());
@ -87,45 +94,68 @@ public class __PROJECT_NAME__ extends Activity {
setContentView(mGLView);
}
@Override protected void onStart() {
@Override protected void onStart()
{
super.onStart();
// call C
ActivityOnStart();
}
@Override protected void onRestart() {
@Override protected void onRestart()
{
super.onRestart();
// call C
ActivityOnReStart();
}
@Override protected void onResume() {
@Override protected void onResume()
{
super.onResume();
mGLView.onResume();
// call C
ActivityOnResume();
}
@Override protected void onPause() {
@Override protected void onPause()
{
super.onPause();
mGLView.onPause();
// call C
ActivityOnPause();
}
@Override protected void onStop() {
@Override protected void onStop()
{
super.onStop();
// call C
ActivityOnStop();
}
@Override protected void onDestroy() {
@Override protected void onDestroy()
{
super.onDestroy();
// call C
ActivityOnDestroy();
// Remove the java Virtual machine pointer form the C code
ActivitySetJavaVortualMachineStop();
}
public void onConfigurationChanged(Configuration newConfig) {
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
public static void eventFromCPP(String[] args)
{
if (args[0] == "Keyboard_Show") {
TouchEvent();
TouchEvent();
TouchEvent();
} else if (args[0] == "Keyboard_Hide") {
TouchEvent();
TouchEvent();
} else {
TouchEvent();
}
}
}

View File

@ -42,9 +42,123 @@ void EWOL_ThreadEventInputState(int pointerID, bool isUp, float x, float y);
void EWOL_NativeRender(void);
void EWOL_NativeGLDestroy(void);
// get a resources from the java environement :
static JNIEnv* JavaVirtualMachinePointer = NULL; // the JVM
static jclass javaClassActivity = 0; // main activity class (android ...)
static jmethodID javaClassActivityEntryPoint = 0; // basic methode to call ...
// generic classes
static jclass javaDefaultClassString = 0; // default string class
static JavaVM* g_JavaVM = NULL;
void SendSystemMessage(const char * dataString)
{
APPL_DEBUG("C->java : send message to the java : \"" << dataString << "\"");
if (NULL == g_JavaVM) {
APPL_DEBUG("C->java : JVM not initialised");
return;
}
JNIEnv *JavaVirtualMachinePointer;
int status = g_JavaVM->GetEnv((void **) &JavaVirtualMachinePointer, JNI_VERSION_1_6);
if (status == JNI_EDETACHED) {
status = g_JavaVM->AttachCurrentThread(&JavaVirtualMachinePointer, NULL);
if (status != JNI_OK) {
APPL_DEBUG("C->java : AttachCurrentThread failed : " << status);
return;
}
jclass javaClassActivity = JavaVirtualMachinePointer->FindClass("com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__" );
if (javaClassActivity == 0) {
APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__ class");
// remove access on the virtual machine :
JavaVirtualMachinePointer = NULL;
return;
}
jmethodID javaClassActivityEntryPoint = JavaVirtualMachinePointer->GetStaticMethodID(javaClassActivity, "eventFromCPP", "([Ljava/lang/String;)V" );
if (javaClassActivityEntryPoint == 0) {
APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__.eventFromCPP" );
// remove access on the virtual machine :
JavaVirtualMachinePointer = NULL;
return;
}
jclass javaDefaultClassString = JavaVirtualMachinePointer->FindClass("java/lang/String" );
if (javaDefaultClassString == 0) {
APPL_DEBUG("C->java : Can't find java/lang/String" );
// remove access on the virtual machine :
JavaVirtualMachinePointer = NULL;
return;
}
// create the string to the java
jstring jstr = JavaVirtualMachinePointer->NewStringUTF(dataString);
if (jstr == 0) {
APPL_DEBUG("C->java : Out of memory" );
return;
}
// create argument list
jobjectArray args = JavaVirtualMachinePointer->NewObjectArray(1, javaDefaultClassString, jstr);
if (args == 0) {
APPL_DEBUG("C->java : Out of memory" );
return;
}
//Call java ...
JavaVirtualMachinePointer->CallStaticVoidMethod(javaClassActivity, javaClassActivityEntryPoint, args);
// manage execption :
if (JavaVirtualMachinePointer->ExceptionOccurred()) {
JavaVirtualMachinePointer->ExceptionDescribe();
}
} else {
APPL_DEBUG("C->java : do nothing ... ");
}
/*
if (NULL == JavaVirtualMachinePointer) {
APPL_DEBUG("C->java : JVM not initialised");
return;
}
// create the string to the java
jstring jstr = JavaVirtualMachinePointer->NewStringUTF(dataString);
if (jstr == 0) {
APPL_DEBUG("C->java : Out of memory" );
return;
}
// create argument list
jobjectArray args = JavaVirtualMachinePointer->NewObjectArray(1, javaDefaultClassString, jstr);
if (args == 0) {
APPL_DEBUG("C->java : Out of memory" );
return;
}
//Call java ...
JavaVirtualMachinePointer->CallStaticVoidMethod(javaClassActivity, javaClassActivityEntryPoint, args);
// manage execption :
if (JavaVirtualMachinePointer->ExceptionOccurred()) {
JavaVirtualMachinePointer->ExceptionDescribe();
}
*/
}
extern "C"
{
// JNI OnLoad
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* jvm, void* reserved)
{
// get the java virtual machine handle ...
g_JavaVM = jvm;
APPL_DEBUG("JNI-> load the jvm ..." );
return JNI_VERSION_1_6;
}
// JNI OnUnLoad
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
{
g_JavaVM = NULL;
APPL_DEBUG("JNI-> Un-load the jvm ..." );
}
/* Call to initialize the graphics state */
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivityParamSetArchiveDir( JNIEnv* env, jobject thiz, jint mode, jstring myString)
@ -55,6 +169,49 @@ extern "C"
//env->ReleaseStringUTFChars(str,myString,0);
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivitySetJavaVortualMachineStart( JNIEnv* env )
{
APPL_DEBUG("*******************************************");
APPL_DEBUG("** Set JVM Pointer **");
APPL_DEBUG("*******************************************");
JavaVirtualMachinePointer = env;
// get default needed all time elements :
if (NULL != JavaVirtualMachinePointer) {
javaClassActivity = JavaVirtualMachinePointer->FindClass("com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__" );
if (javaClassActivity == 0) {
APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__ class");
// remove access on the virtual machine :
JavaVirtualMachinePointer = NULL;
return;
}
javaClassActivityEntryPoint = JavaVirtualMachinePointer->GetStaticMethodID(javaClassActivity, "eventFromCPP", "([Ljava/lang/String;)V" );
if (javaClassActivityEntryPoint == 0) {
APPL_DEBUG("C->java : Can't find com.__PROJECT_VENDOR__.__PROJECT_PACKAGE__.__PROJECT_NAME__.eventFromCPP" );
// remove access on the virtual machine :
JavaVirtualMachinePointer = NULL;
return;
}
javaDefaultClassString = JavaVirtualMachinePointer->FindClass("java/lang/String" );
if (javaDefaultClassString == 0) {
APPL_DEBUG("C->java : Can't find java/lang/String" );
// remove access on the virtual machine :
JavaVirtualMachinePointer = NULL;
return;
}
}
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivitySetJavaVortualMachineStop( JNIEnv* env )
{
APPL_DEBUG("*******************************************");
APPL_DEBUG("** Remove JVM Pointer **");
APPL_DEBUG("*******************************************");
JavaVirtualMachinePointer = NULL;
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___TouchEvent( JNIEnv* env )
{
APPL_DEBUG(" ==> Touch Event");
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivityOnCreate( JNIEnv* env )
{
APPL_DEBUG("*******************************************");
@ -67,6 +224,7 @@ extern "C"
APPL_DEBUG("*******************************************");
APPL_DEBUG("** Activity On Start **");
APPL_DEBUG("*******************************************");
//SendSystemMessage(" testmessages ... ");
}
void Java_com___PROJECT_VENDOR_____PROJECT_PACKAGE_____PROJECT_NAME___ActivityOnReStart( JNIEnv* env )
{

View File

@ -37,17 +37,16 @@ void TOOLS_DisplayFuncName(int32_t ligne, const char* className, const char* fun
if (NULL == className) {
if (NULL == libName) {
snprintf(tmpName, FUNCTION_NAME_SIZE, "???????? | (l=%5d) %s ",ligne, funcName);
snprintf(tmpName, FUNCTION_NAME_SIZE-1, "???????? | (l=%5d) %s ",ligne, funcName);
} else {
snprintf(tmpName, FUNCTION_NAME_SIZE, "%s | (l=%5d) %s ",libName, ligne, funcName);
snprintf(tmpName, FUNCTION_NAME_SIZE-1, "%s | (l=%5d) %s ",libName, ligne, funcName);
}
} else {
if (NULL == libName) {
snprintf(tmpName, FUNCTION_NAME_SIZE, "???????? | (l=%5d) %s::%s ",ligne, className, funcName);
snprintf(tmpName, FUNCTION_NAME_SIZE-1, "???????? | (l=%5d) %s::%s ",ligne, className, funcName);
} else {
snprintf(tmpName, FUNCTION_NAME_SIZE, "%s | (l=%5d) %s::%s ", libName, ligne, className, funcName);
snprintf(tmpName, FUNCTION_NAME_SIZE-1, "%s | (l=%5d) %s::%s ", libName, ligne, className, funcName);
}
}
tmpName[FUNCTION_NAME_SIZE-4] = ' ';
tmpName[FUNCTION_NAME_SIZE-3] = '|';

View File

@ -34,9 +34,9 @@
#if defined(__PLATFORM__Android)
# include <android/log.h>
# define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "====> EWOL", __VA_ARGS__))
# define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "====> EWOL", __VA_ARGS__))
# define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "====> EWOL", __VA_ARGS__))
# define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "EWOL", __VA_ARGS__))
# define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "EWOL", __VA_ARGS__))
# define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "EWOL", __VA_ARGS__))
#endif
#define MAX_LOG_SIZE (16000)

View File

@ -133,7 +133,7 @@ void guiAbstraction::SendKeyboardEventMove(bool isDown, ewol::eventKbMoveType_te
}
/*
void guiAbstraction::KeyboardShow(ewol::keyboardMode_te mode)
{
if (NULL != gui_uniqueWindows) {
@ -148,7 +148,7 @@ void guiAbstraction::KeyboardHide(void)
}
ForceRedrawAll();
}
*/

View File

@ -247,7 +247,20 @@ void guiAbstraction::Stop(void)
{
// TODo : send a message to the android system to stop ...
}
// java system to send message :
void SendSystemMessage(const char * dataString);
void guiAbstraction::KeyboardShow(ewol::keyboardMode_te mode)
{
// send a message at the java :
SendSystemMessage("Keyboard_Show");
}
void guiAbstraction::KeyboardHide(void)
{
// send a message at the java :
SendSystemMessage("Keyboard_Hide");
}
void guiAbstraction::ChangeSize(int32_t w, int32_t h)
{

View File

@ -1006,6 +1006,16 @@ void guiAbstraction::Stop(void)
}
void guiAbstraction::KeyboardShow(ewol::keyboardMode_te mode)
{
// nothing to do : No keyboard on computer ...
}
void guiAbstraction::KeyboardHide(void)
{
// nothing to do : No keyboard on computer ...
}
void guiAbstraction::ChangeSize(int32_t w, int32_t h)

View File

@ -87,7 +87,8 @@ const char * const ewol::Menu::GetObjectType(void)
void ewol::Menu::SubWidgetRemoveAll(void)
{
EWOL_ERROR("Not availlable");
Clear();
ewol::SizerHori::SubWidgetRemoveAll();
}
void ewol::Menu::SubWidgetAdd(ewol::Widget* newWidget)
@ -253,3 +254,25 @@ void ewol::Menu::OnReceiveMessage(ewol::EObject * CallerObject, const char * eve
}
}
}
/**
* @brief Inform object that an other object is removed ...
* @param[in] removeObject Pointer on the EObject remeved ==> the user must remove all reference on this EObject
* @note : Sub classes must call this class
* @return ---
*/
void ewol::Menu::OnObjectRemove(ewol::EObject * removeObject)
{
ewol::SizerHori::OnObjectRemove(removeObject);
if (m_widgetContextMenu == removeObject) {
m_widgetContextMenu = NULL;
}
for(int32_t jjj=0; jjj<m_listElement.Size(); jjj++) {
if (NULL != m_listElement[jjj]) {
if (m_listElement[jjj]->m_widgetPointer == removeObject) {
m_listElement[jjj]->m_widgetPointer = NULL;
}
}
}
}

View File

@ -88,6 +88,13 @@ namespace ewol {
* @return ---
*/
virtual void OnReceiveMessage(ewol::EObject * CallerObject, const char * eventId, etk::UString data);
/**
* @brief Inform object that an other object is removed ...
* @param[in] removeObject Pointer on the EObject remeved ==> the user must remove all reference on this EObject
* @note : Sub classes must call this class
* @return ---
*/
virtual void OnObjectRemove(ewol::EObject * removeObject);
};
extern const char * const TYPE_EOBJECT_WIDGET_MENU;