`AxmolActivity` refactoring and fixes (#2185)

* 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
This commit is contained in:
smilediver 2024-09-28 07:41:54 +03:00 committed by GitHub
parent 08b1657e14
commit 62eebaa8af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 78 additions and 79 deletions

View File

@ -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;
@ -151,6 +148,7 @@ public abstract class AxmolActivity extends Activity implements AxmolEngineListe
@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
@ -202,68 +200,63 @@ public abstract class AxmolActivity extends Activity implements AxmolEngineListe
// ===========================================================
@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);
protected void onResume() {
Log.i(TAG, "onResume()");
super.onResume();
this.hasFocus = hasFocus;
if (this.hasFocus && !paused) {
resume();
}
}
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();

View File

@ -37,6 +37,8 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import java.util.concurrent.CountDownLatch;
public class AxmolGLSurfaceView extends GLSurfaceView {
// ===========================================================
// Constants
@ -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;
@ -185,35 +188,6 @@ public class AxmolGLSurfaceView extends GLSurfaceView {
// 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
@ -427,6 +401,37 @@ public class AxmolGLSurfaceView extends GLSurfaceView {
// 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
// ===========================================================

View File

@ -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
@ -76,11 +77,11 @@ public class AxmolRenderer implements GLSurfaceView.Renderer {
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;
}
}
@ -160,17 +161,17 @@ public class AxmolRenderer implements GLSurfaceView.Renderer {
* 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;
}
}