diff --git a/src/api-impl-jni/util.c b/src/api-impl-jni/util.c index c621e4fe..f0494f73 100644 --- a/src/api-impl-jni/util.c +++ b/src/api-impl-jni/util.c @@ -115,7 +115,6 @@ void set_up_handle_cache(JNIEnv *env) (*env)->ExceptionDescribe(env); handle_cache.application.class = _REF((*env)->FindClass(env, "android/app/Application")); - handle_cache.application.object = _REF(_GET_STATIC_OBJ_FIELD(handle_cache.context.class, "this_application", "Landroid/app/Application;")); handle_cache.application.get_app_icon_path = _METHOD(handle_cache.application.class, "get_app_icon_path", "()Ljava/lang/String;"); } diff --git a/src/api-impl-jni/util.h b/src/api-impl-jni/util.h index 8ea57b38..d2a39948 100644 --- a/src/api-impl-jni/util.h +++ b/src/api-impl-jni/util.h @@ -81,7 +81,6 @@ struct handle_cache { } context; struct { jclass class; - jobject object; jmethodID get_app_icon_path; } application; }; diff --git a/src/api-impl/android/content/Context.java b/src/api-impl/android/content/Context.java index b968d6fc..5227ecc1 100644 --- a/src/api-impl/android/content/Context.java +++ b/src/api-impl/android/content/Context.java @@ -34,11 +34,15 @@ import android.util.Log; import android.view.WindowManager; import android.view.WindowManagerImpl; import com.reandroid.arsc.chunk.xml.AndroidManifestBlock; +import com.reandroid.arsc.chunk.xml.ResXmlAttribute; + import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.io.IOException; public class Context extends Object { @@ -74,7 +78,6 @@ public class Context extends Object { dm = new DisplayMetrics(); config = new Configuration(); r = new Resources(assets, dm, config); - this_application = new Application(); // TODO: the application context is presumably not identical to the Activity context, what is the difference for us though? theme = r.newTheme(); application_info = new ApplicationInfo(); @@ -86,6 +89,20 @@ public class Context extends Object { } } + static Application createApplication() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { + Application application; + ResXmlAttribute application_name = manifest.getApplicationElement().searchAttributeByResourceId(AndroidManifestBlock.ID_name); + String className = (application_name != null) ? application_name.getValueAsString() : "android.app.Application"; + if (className.startsWith(".")) { + className = manifest.getPackageName() + className; + } + Class cls = Class.forName(className).asSubclass(Application.class); + Constructor constructor = cls.getConstructor(); + application = constructor.newInstance(); + this_application = application; + return application; + } + public Context() { System.out.println("new Context! this one is: " + this); } diff --git a/src/main-executable/main.c b/src/main-executable/main.c index b3dbc5ed..bbd80d4c 100644 --- a/src/main-executable/main.c +++ b/src/main-executable/main.c @@ -161,6 +161,7 @@ static void open(GtkApplication *app, GFile** files, gint nfiles, const gchar* h int errno_localdir; int ret; jobject activity_object; + jobject application_object; char *apk_classpath = g_file_get_path(files[0]); char *apk_name = g_file_get_basename(files[0]); @@ -343,12 +344,22 @@ static void open(GtkApplication *app, GFile** files, gint nfiles, const gchar* h gtk_window_present(GTK_WINDOW(window)); + // construct Application + application_object = (*env)->CallStaticObjectMethod(env, handle_cache.context.class, + _STATIC_METHOD(handle_cache.context.class, "createApplication", "()Landroid/app/Application;")); + if((*env)->ExceptionCheck(env)) + (*env)->ExceptionDescribe(env); + extract_from_apk("assets/", "assets/"); /* extract native libraries from apk*/ extract_from_apk("lib/" NATIVE_ARCH "/", "lib/"); prepare_main_looper(env); + (*env)->CallVoidMethod(env, application_object, _METHOD(handle_cache.application.class, "onCreate", "()V")); + if((*env)->ExceptionCheck(env)) + (*env)->ExceptionDescribe(env); + // construct main Activity activity_object = (*env)->CallStaticObjectMethod(env, handle_cache.apk_main_activity.class, _STATIC_METHOD(handle_cache.apk_main_activity.class, "createMainActivity", "(Ljava/lang/String;J)Landroid/app/Activity;"), @@ -363,7 +374,7 @@ static void open(GtkApplication *app, GFile** files, gint nfiles, const gchar* h if((*env)->ExceptionCheck(env)) (*env)->ExceptionDescribe(env); -/* const char *app_icon_path = _CSTRING((*env)->CallObjectMethod(env, handle_cache.application.object, handle_cache.application.get_app_icon_path)); +/* const char *app_icon_path = _CSTRING((*env)->CallObjectMethod(env, application_object, handle_cache.application.get_app_icon_path)); if((*env)->ExceptionCheck(env)) (*env)->ExceptionDescribe(env);