[ndk] Fixed sample SanAngeles to properly pause/resume
Previously re-launch of background SanAngeles app shows black or
freeze frame
Two fixes:
1. Not to reinitialize timer each time when DemoRenderer is created
2. Stop advancing internal clock when onPause(), and start when
onResume()
Change-Id: I6bef9d7c462cd4d504e76099a4d28c8618043f46
diff --git a/ndk/platforms/android-4/samples/san-angeles/jni/app-android.c b/ndk/platforms/android-4/samples/san-angeles/jni/app-android.c
index 0e552a7..feb7549 100644
--- a/ndk/platforms/android-4/samples/san-angeles/jni/app-android.c
+++ b/ndk/platforms/android-4/samples/san-angeles/jni/app-android.c
@@ -47,9 +47,7 @@
{
importGLInit();
appInit();
- gAppAlive = 1;
- sDemoStopped = 0;
- sTimeOffsetInit = 0;
+ gAppAlive = 1;
}
void
@@ -71,19 +69,44 @@
/* This is called to indicate to the render loop that it should
* stop as soon as possible.
*/
+
+void _pause()
+{
+ /* we paused the animation, so store the current
+ * time in sTimeStopped for future nativeRender calls */
+ sDemoStopped = 1;
+ sTimeStopped = _getTime();
+}
+
+void _resume()
+{
+ /* we resumed the animation, so adjust the time offset
+ * to take care of the pause interval. */
+ sDemoStopped = 0;
+ sTimeOffset -= _getTime() - sTimeStopped;
+}
+
+
+void
+Java_com_example_SanAngeles_DemoGLSurfaceView_nativeTogglePauseResume( JNIEnv* env )
+{
+ sDemoStopped = !sDemoStopped;
+ if (sDemoStopped)
+ _pause();
+ else
+ _resume();
+}
+
void
Java_com_example_SanAngeles_DemoGLSurfaceView_nativePause( JNIEnv* env )
{
- sDemoStopped = !sDemoStopped;
- if (sDemoStopped) {
- /* we paused the animation, so store the current
- * time in sTimeStopped for future nativeRender calls */
- sTimeStopped = _getTime();
- } else {
- /* we resumed the animation, so adjust the time offset
- * to take care of the pause interval. */
- sTimeOffset -= _getTime() - sTimeStopped;
- }
+ _pause();
+}
+
+void
+Java_com_example_SanAngeles_DemoGLSurfaceView_nativeResume( JNIEnv* env )
+{
+ _resume();
}
/* Call to render the next GL frame */
diff --git a/ndk/platforms/android-4/samples/san-angeles/src/com/example/SanAngeles/DemoActivity.java b/ndk/platforms/android-4/samples/san-angeles/src/com/example/SanAngeles/DemoActivity.java
index 696be78..076b8a7 100644
--- a/ndk/platforms/android-4/samples/san-angeles/src/com/example/SanAngeles/DemoActivity.java
+++ b/ndk/platforms/android-4/samples/san-angeles/src/com/example/SanAngeles/DemoActivity.java
@@ -86,14 +86,29 @@
public boolean onTouchEvent(final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
- nativePause();
+ nativeTogglePauseResume();
}
return true;
}
+ @Override
+ public void onPause() {
+ super.onPause();
+ nativePause();
+ }
+
+ @Override
+ public void onResume() {
+ super.onResume();
+ nativeResume();
+ }
+
+
DemoRenderer mRenderer;
private static native void nativePause();
+ private static native void nativeResume();
+ private static native void nativeTogglePauseResume();
}
class DemoRenderer implements GLSurfaceView.Renderer {