get Application class from AndroidManifest.xml and call onCreate()

This commit is contained in:
Julian Winkler
2023-08-17 10:09:07 +02:00
parent c60e97f13f
commit a8e39cd613
4 changed files with 30 additions and 4 deletions

View File

@@ -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;");
}

View File

@@ -81,7 +81,6 @@ struct handle_cache {
} context;
struct {
jclass class;
jobject object;
jmethodID get_app_icon_path;
} application;
};

View File

@@ -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<? extends Application> cls = Class.forName(className).asSubclass(Application.class);
Constructor<? extends Application> constructor = cls.getConstructor();
application = constructor.newInstance();
this_application = application;
return application;
}
public Context() {
System.out.println("new Context! this one is: " + this);
}

View File

@@ -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);