Skip to content

Commit

Permalink
AxmolActivity refactoring and fixes (axmolengine#2185)
Browse files Browse the repository at this point in the history
* AxmolActivity: add logging of activity lifecycle changes

* AxmolActivity: simplify and fix director not resumed after system dialog

* AxmolRenderer: fix lost state when app activity is relaunched

* AxmolGLSurfaceView: remove accidental exception throw
  • Loading branch information
smilediver authored Sep 28, 2024
1 parent 08b1657 commit 62eebaa
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 79 deletions.
75 changes: 34 additions & 41 deletions core/platform/android/java/src/org/axmol/lib/AxmolActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ public abstract class AxmolActivity extends Activity implements AxmolEngineListe
private static AxmolActivity sContext = null;
private WebViewHelper mWebViewHelper = null;
private EditBoxHelper mEditBoxHelper = null;
private boolean hasFocus = false;
private boolean showVirtualButton = false;
private boolean paused = true;
private boolean rendererPaused = true;

public AxmolGLSurfaceView getGLSurfaceView(){
return mGLSurfaceView;
Expand Down Expand Up @@ -151,6 +148,7 @@ protected void onLoadNativeLibraries() {

@Override
protected void onCreate(final Bundle savedInstanceState) {
Log.i(TAG, "onCreate()");
super.onCreate(savedInstanceState);

// Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
Expand Down Expand Up @@ -202,68 +200,63 @@ protected void onCreate(final Bundle savedInstanceState) {
// ===========================================================

@Override
protected void onResume() {
Log.d(TAG, "onResume()");
paused = false;
super.onResume();
if (this.hasFocus) {
resume();
}
protected void onStart() {
Log.i(TAG, "onStart()");
super.onStart();

mGLSurfaceView.onResume();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.d(TAG, "onWindowFocusChanged() hasFocus=" + hasFocus);
super.onWindowFocusChanged(hasFocus);

this.hasFocus = hasFocus;
if (this.hasFocus && !paused) {
resume();
}
}
protected void onResume() {
Log.i(TAG, "onResume()");
super.onResume();

private void resume() {
this.hideVirtualButton();
hideVirtualButton();
AxmolEngine.onResume();
if (rendererPaused) {
mGLSurfaceView.onResume();
rendererPaused = false;
}
mGLSurfaceView.handleOnResume();
mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
}

private void resumeIfHasFocus() {
//It is possible for the app to receive the onWindowsFocusChanged(true) event
//even though it is locked or asleep
boolean readyToPlay = !isDeviceLocked() && !isDeviceAsleep();

if(hasFocus && readyToPlay) {
resume();
}
}

@Override
protected void onPause() {
Log.d(TAG, "onPause()");
paused = true;
super.onPause();
Log.i(TAG, "onPause()");

AxmolEngine.onPause();
mGLSurfaceView.onPause();
mGLSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
mGLSurfaceView.handleOnPause();

super.onPause();
}

@Override
protected void onStop() {
Log.i(TAG, "onStop()");

mGLSurfaceView.waitForPauseToComplete();
mGLSurfaceView.onPause();

super.onStop();
rendererPaused = true;
mGLSurfaceView.onStop();
}

@Override
protected void onRestart() {
Log.i(TAG, "onRestart()");
super.onRestart();
}

@Override
protected void onDestroy() {
Log.i(TAG, "onDestroy()");
super.onDestroy();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.i(TAG, "onWindowFocusChanged() hasFocus=" + hasFocus);
super.onWindowFocusChanged(hasFocus);
}

@Override
public void showDialog(final String pTitle, final String pMessage) {
Message msg = new Message();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ of this software and associated documentation files (the "Software"), to deal
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import java.util.concurrent.CountDownLatch;

public class AxmolGLSurfaceView extends GLSurfaceView {
// ===========================================================
// Constants
Expand All @@ -62,7 +64,8 @@ public class AxmolGLSurfaceView extends GLSurfaceView {

private boolean mSoftKeyboardShown = false;
private boolean mMultipleTouchEnabled = true;
private boolean mPaused = true;

private CountDownLatch mNativePauseComplete;

public boolean isSoftKeyboardShown() {
return mSoftKeyboardShown;
Expand Down Expand Up @@ -185,35 +188,6 @@ public void setEditText(final AxmolEditBox pEditText) {
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public void onResume() {
if (mPaused) {
mPaused = false;
super.onResume();
this.queueEvent(new Runnable() {
@Override
public void run() {
AxmolGLSurfaceView.this.mRenderer.handleOnResume();
}
});
}
}

@Override
public void onPause() {
this.queueEvent(new Runnable() {
@Override
public void run() {
AxmolGLSurfaceView.this.mRenderer.handleOnPause();
}
});
}

public void onStop() {
mPaused = true;
super.onPause();
}

@Override
public boolean onTouchEvent(final MotionEvent pMotionEvent) {
// these data are used in ACTION_MOVE and ACTION_CANCEL
Expand Down Expand Up @@ -427,6 +401,37 @@ public void run() {
// Methods
// ===========================================================

public void handleOnResume() {
this.queueEvent(new Runnable() {
@Override
public void run() {
AxmolGLSurfaceView.this.mRenderer.handleOnResume();
}
});
}

public void handleOnPause() {
mNativePauseComplete = new CountDownLatch(1);

CountDownLatch complete = mNativePauseComplete;
this.queueEvent(new Runnable() {
@Override
public void run() {
AxmolGLSurfaceView.this.mRenderer.handleOnPause();
complete.countDown();
}
});
}

public void waitForPauseToComplete() {
while (mNativePauseComplete.getCount() > 0) {
try {
mNativePauseComplete.await();
} catch (InterruptedException e) {
}
}
}

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
Expand Down
17 changes: 9 additions & 8 deletions core/platform/android/java/src/org/axmol/lib/AxmolRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ public class AxmolRenderer implements GLSurfaceView.Renderer {
private long mLastTickInNanoSeconds;
private int mScreenWidth;
private int mScreenHeight;
private boolean mNativeInitCompleted = false;
private boolean mIsPaused = false;

private static boolean gNativeInitialized = false;
private static boolean gNativeIsPaused = false;

// ===========================================================
// Constructors
Expand Down Expand Up @@ -76,11 +77,11 @@ public void onSurfaceCreated(final GL10 GL10, final EGLConfig EGLConfig) {
AxmolRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight);
this.mLastTickInNanoSeconds = System.nanoTime();

if (mNativeInitCompleted) {
if (gNativeInitialized) {
// This must be from an OpenGL context loss
nativeOnContextLost();
} else {
mNativeInitCompleted = true;
gNativeInitialized = true;
}
}

Expand Down Expand Up @@ -160,17 +161,17 @@ public void handleOnPause() {
* onSurfaceCreated is invoked. Can not invoke any
* native method before onSurfaceCreated is invoked
*/
if (!mNativeInitCompleted)
if (!gNativeInitialized)
return;

AxmolRenderer.nativeOnPause();
mIsPaused = true;
gNativeIsPaused = true;
}

public void handleOnResume() {
if (mIsPaused) {
if (gNativeIsPaused) {
AxmolRenderer.nativeOnResume();
mIsPaused = false;
gNativeIsPaused = false;
}
}

Expand Down

0 comments on commit 62eebaa

Please sign in to comment.