Skip to content

Commit 614985a

Browse files
committed
android/sdlsurfaceview: try to add a support of ondestroy event, to give some time to user for closing its application. But it's not working when Android is reclaiming the app :[
1 parent 18a1a50 commit 614985a

File tree

4 files changed

+112
-14
lines changed

4 files changed

+112
-14
lines changed

recipes/android/src/android.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,12 @@ def action_send(mimetype, filename=None, subject=None, text=None,
218218
j_chooser_title = <bytes>chooser_title
219219
android_action_send(j_mimetype, j_filename, j_subject, j_text,
220220
j_chooser_title)
221+
222+
cdef extern int android_checkstop()
223+
cdef extern void android_ackstop()
224+
225+
def check_stop():
226+
return android_checkstop()
227+
228+
def ack_stop():
229+
android_ackstop()

recipes/android/src/android_jni.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,40 @@ void android_activate_input(void) {
218218
(*env)->CallStaticVoidMethod(env, cls, mid);
219219
}
220220

221+
int android_checkstop(void) {
222+
static JNIEnv *env = NULL;
223+
static jclass *cls = NULL;
224+
static jmethodID mid = NULL;
225+
226+
if (env == NULL) {
227+
env = SDL_ANDROID_GetJNIEnv();
228+
aassert(env);
229+
cls = (*env)->FindClass(env, "org/renpy/android/SDLSurfaceView");
230+
aassert(cls);
231+
mid = (*env)->GetStaticMethodID(env, cls, "checkStop", "()I");
232+
aassert(mid);
233+
}
234+
235+
return (*env)->CallStaticIntMethod(env, cls, mid);
236+
}
237+
238+
void android_ackstop(void) {
239+
static JNIEnv *env = NULL;
240+
static jclass *cls = NULL;
241+
static jmethodID mid = NULL;
242+
243+
if (env == NULL) {
244+
env = SDL_ANDROID_GetJNIEnv();
245+
aassert(env);
246+
cls = (*env)->FindClass(env, "org/renpy/android/SDLSurfaceView");
247+
aassert(cls);
248+
mid = (*env)->GetStaticMethodID(env, cls, "ackStop", "()I");
249+
aassert(mid);
250+
}
251+
252+
(*env)->CallStaticIntMethod(env, cls, mid);
253+
}
254+
221255
void android_action_send(char *mimeType, char *filename, char *subject, char *text, char *chooser_title) {
222256
static JNIEnv *env = NULL;
223257
static jclass *cls = NULL;

src/src/org/renpy/android/PythonActivity.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ public boolean isPaused() {
278278

279279
@Override
280280
public boolean onKeyDown(int keyCode, final KeyEvent event) {
281-
Log.i("python", "key2 " + mView + " " + mView.mStarted);
282-
281+
//Log.i("python", "key2 " + mView + " " + mView.mStarted);
283282
if (mView != null && mView.mStarted && SDLSurfaceView.nativeKey(keyCode, 1, event.getUnicodeChar())) {
284283
return true;
285284
} else {
@@ -289,7 +288,7 @@ public boolean onKeyDown(int keyCode, final KeyEvent event) {
289288

290289
@Override
291290
public boolean onKeyUp(int keyCode, final KeyEvent event) {
292-
Log.i("python", "key up " + mView + " " + mView.mStarted);
291+
//Log.i("python", "key up " + mView + " " + mView.mStarted);
293292
if (mView != null && mView.mStarted && SDLSurfaceView.nativeKey(keyCode, 0, event.getUnicodeChar())) {
294293
return true;
295294
} else {
@@ -308,9 +307,12 @@ public boolean dispatchTouchEvent(final MotionEvent ev) {
308307
}
309308
}
310309

311-
// Ensure we only have one task.
312-
public void onDestroy() {
310+
protected void onDestroy() {
311+
if (mView != null) {
312+
mView.onDestroy();
313+
}
314+
//Log.i(TAG, "on destroy (exit1)");
313315
System.exit(0);
314-
}
315-
316+
}
316317
}
318+

src/src/org/renpy/android/SDLSurfaceView.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,21 +298,23 @@ private void printConfig(EGL10 egl, EGLDisplay display,
298298
private EGLConfig mEglConfig = null;
299299

300300
// The user program is not participating in the pause protocol.
301-
public final int PAUSE_NOT_PARTICIPATING = 0;
301+
static int PAUSE_NOT_PARTICIPATING = 0;
302302

303303
// A pause has not been requested by the OS.
304-
public final int PAUSE_NONE = 1;
304+
static int PAUSE_NONE = 1;
305305

306306
// A pause has been requested by Android, but the user program has
307307
// not bothered responding yet.
308-
public final int PAUSE_REQUEST = 2;
308+
static int PAUSE_REQUEST = 2;
309309

310310
// The user program is waiting in waitForResume.
311-
public final int PAUSE_WAIT_FOR_RESUME = 3;
311+
static int PAUSE_WAIT_FOR_RESUME = 3;
312312

313-
// This stores the state of the pause system.
314-
private int mPause = PAUSE_NOT_PARTICIPATING;
313+
static int PAUSE_STOP_REQUEST = 4;
314+
static int PAUSE_STOP_ACK = 5;
315315

316+
// This stores the state of the pause system.
317+
static int mPause = PAUSE_NOT_PARTICIPATING;
316318

317319
private PowerManager.WakeLock wakeLock;
318320

@@ -330,9 +332,14 @@ private void printConfig(EGL10 egl, EGLDisplay display,
330332
// The resource manager we use.
331333
ResourceManager mResourceManager;
332334

335+
// Our own view
336+
static SDLSurfaceView instance = null;
337+
333338
public SDLSurfaceView(Activity act, String argument) {
334339
super(act);
335340

341+
SDLSurfaceView.instance = this;
342+
336343
mActivity = act;
337344
mResourceManager = new ResourceManager(act);
338345

@@ -435,18 +442,62 @@ public void onResume() {
435442
wakeLock.acquire();
436443
}
437444

445+
public void onDestroy() {
446+
Log.w(TAG, "onDestroy() called");
447+
synchronized (this) {
448+
this.notifyAll();
449+
450+
if ( mPause == PAUSE_STOP_ACK ) {
451+
Log.d(TAG, "onDestroy() app already leaved.");
452+
return;
453+
}
454+
455+
// application didn't leave, give 10s before closing.
456+
// hopefully, this could be enough for launching the on_stop() trigger within the app.
457+
mPause = PAUSE_STOP_REQUEST;
458+
int i = 50;
459+
460+
Log.d(TAG, "onDestroy() stop requested, wait for an event from the app");
461+
for (; i >= 0 && mPause == PAUSE_STOP_REQUEST; i--) {
462+
try {
463+
this.wait(200);
464+
} catch (InterruptedException e) {
465+
break;
466+
}
467+
}
468+
Log.d(TAG, "onDestroy() stop finished waiting.");
469+
}
470+
}
471+
472+
static int checkStop() {
473+
if (mPause == PAUSE_STOP_REQUEST)
474+
return 1;
475+
return 0;
476+
}
477+
478+
static void ackStop() {
479+
Log.d(TAG, "ackStop() notify");
480+
synchronized (instance) {
481+
mPause = PAUSE_STOP_ACK;
482+
instance.notifyAll();
483+
}
484+
}
485+
486+
438487
/**
439488
* This method is part of the SurfaceHolder.Callback interface, and is
440489
* not normally called or subclassed by clients of GLSurfaceView.
441490
*/
442491
public void surfaceCreated(SurfaceHolder holder) {
492+
Log.i(TAG, "surfaceCreated() is not handled :|");
443493
}
444494

445495
/**
446496
* This method is part of the SurfaceHolder.Callback interface, and is
447497
* not normally called or subclassed by clients of GLSurfaceView.
448498
*/
449499
public void surfaceDestroyed(SurfaceHolder holder) {
500+
Log.i(TAG, "surfaceDestroyed() is not handled :|");
450501
}
451502

452503
/**
@@ -556,10 +607,12 @@ public void run() {
556607
nativeSetEnv("PYTHONOPTIMIZE", "2");
557608
nativeSetEnv("PYTHONHOME", mFilesDirectory);
558609
nativeSetEnv("PYTHONPATH", mArgument + ":" + mFilesDirectory + "/lib");
559-
//nativeSetMouseUsed();
560610
nativeSetMultitouchUsed();
561611
nativeInit();
562612

613+
mPause = PAUSE_STOP_ACK;
614+
615+
//Log.i(TAG, "End of native init, stop everything (exit0)");
563616
System.exit(0);
564617
}
565618

0 commit comments

Comments
 (0)