Skip to content

Commit

Permalink
Android: add robustness. check that the native code is run for the fi…
Browse files Browse the repository at this point in the history
…rst time.
  • Loading branch information
1bsyl committed Jun 9, 2023
1 parent ae9d8ac commit 8096f72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
19 changes: 19 additions & 0 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,24 @@ public void onClick(DialogInterface dialog,int id) {
return;
}


/* Control activity re-creation */
/* Robustness: check that the native code is run for the first time.
* (Maybe Activity was reset, but not the native code.) */
{
int run_count = SDLActivity.nativeCheckSDLThreadCounter(); /* get and increment a native counter */
if (run_count != 0) {
boolean allow_recreate = SDLActivity.nativeAllowRecreateActivity();
if (allow_recreate) {
Log.v(TAG, "activity re-created // run_count: " + run_count);
} else {
Log.v(TAG, "activity finished // run_count: " + run_count);
System.exit(0);
return;
}
}
}

// Set up JNI
SDL.setupJNI();

Expand Down Expand Up @@ -971,6 +989,7 @@ public static native void onNativeTouch(int touchDevId, int pointerFingerId,
public static native void onNativeLocaleChanged();
public static native void onNativeDarkModeChanged(boolean enabled);
public static native boolean nativeAllowRecreateActivity();
public static native int nativeCheckSDLThreadCounter();

/**
* This method is called by SDL using JNI.
Expand Down
14 changes: 13 additions & 1 deletion src/core/android/SDL_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ JNIEXPORT void JNICALL SDL_JAVA_INTERFACE(nativePermissionResult)(
JNIEXPORT jboolean JNICALL SDL_JAVA_INTERFACE(nativeAllowRecreateActivity)(
JNIEnv *env, jclass jcls);

JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeCheckSDLThreadCounter)(
JNIEnv *env, jclass jcls);

static JNINativeMethod SDLActivity_tab[] = {
{ "nativeGetVersion", "()Ljava/lang/String;", SDL_JAVA_INTERFACE(nativeGetVersion) },
{ "nativeSetupJNI", "()I", SDL_JAVA_INTERFACE(nativeSetupJNI) },
Expand Down Expand Up @@ -203,6 +206,7 @@ static JNINativeMethod SDLActivity_tab[] = {
{ "nativeAddTouch", "(ILjava/lang/String;)V", SDL_JAVA_INTERFACE(nativeAddTouch) },
{ "nativePermissionResult", "(IZ)V", SDL_JAVA_INTERFACE(nativePermissionResult) },
{ "nativeAllowRecreateActivity", "()Z", SDL_JAVA_INTERFACE(nativeAllowRecreateActivity) },
{ "nativeCheckSDLThreadCounter", "()I", SDL_JAVA_INTERFACE(nativeCheckSDLThreadCounter) }
};

/* Java class SDLInputConnection */
Expand Down Expand Up @@ -738,7 +742,15 @@ JNIEXPORT void JNICALL SDL_JAVA_CONTROLLER_INTERFACE(nativeSetupJNI)(JNIEnv *env
/* SDL main function prototype */
typedef int (*SDL_main_func)(int argc, char *argv[]);

static int run_count = 1;
static int run_count = 0;

JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeCheckSDLThreadCounter)(
JNIEnv *env, jclass jcls)
{
int tmp = run_count;
run_count += 1;
return tmp;
}

static void SDLCALL SDL_AllowRecreateActivityChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
Expand Down

0 comments on commit 8096f72

Please sign in to comment.