Behave properly when app is suspended

This commit is contained in:
Paul Rouget 2018-08-14 14:26:10 +02:00
parent e051c5880e
commit ae407e9a65
7 changed files with 69 additions and 9 deletions

View file

@ -208,6 +208,12 @@ impl ServoGlue {
self.process_event(event) self.process_event(event)
} }
/// Redraw the page.
pub fn refresh(&mut self) -> Result<(), &'static str> {
info!("refresh");
self.process_event(WindowEvent::Refresh)
}
/// Stop loading the page. /// Stop loading the page.
pub fn stop(&mut self) -> Result<(), &'static str> { pub fn stop(&mut self) -> Result<(), &'static str> {
debug!("TODO can't stop won't stop"); debug!("TODO can't stop won't stop");

View file

@ -144,6 +144,12 @@ pub extern "C" fn stop() {
call(|s| s.stop()); call(|s| s.stop());
} }
#[no_mangle]
pub extern "C" fn refresh() {
debug!("refresh");
call(|s| s.refresh());
}
#[no_mangle] #[no_mangle]
pub extern "C" fn go_back() { pub extern "C" fn go_back() {
debug!("go_back"); debug!("go_back");

View file

@ -145,6 +145,12 @@ pub fn Java_com_mozilla_servoview_JNIServo_stop(env: JNIEnv, _class: JClass) {
call(env, |s| s.stop()); call(env, |s| s.stop());
} }
#[no_mangle]
pub fn Java_com_mozilla_servoview_JNIServo_refresh(env: JNIEnv, _class: JClass) {
debug!("refresh");
call(env, |s| s.refresh());
}
#[no_mangle] #[no_mangle]
pub fn Java_com_mozilla_servoview_JNIServo_goBack(env: JNIEnv, _class: JClass) { pub fn Java_com_mozilla_servoview_JNIServo_goBack(env: JNIEnv, _class: JClass) {
debug!("goBack"); debug!("goBack");

View file

@ -151,4 +151,15 @@ public class MainActivity extends Activity implements Servo.Client {
mFwdButton.setEnabled(canGoForward); mFwdButton.setEnabled(canGoForward);
} }
@Override
public void onPause() {
mServoView.onPause();
super.onPause();
}
@Override
public void onResume() {
mServoView.onResume();
super.onResume();
}
} }

View file

@ -35,6 +35,8 @@ public class JNIServo {
public native void stop(); public native void stop();
public native void refresh();
public native void goBack(); public native void goBack();
public native void goForward(); public native void goForward();

View file

@ -17,6 +17,7 @@ public class Servo {
private AssetManager mAssetMgr; private AssetManager mAssetMgr;
private JNIServo mJNI = new JNIServo(); private JNIServo mJNI = new JNIServo();
private RunCallback mRunCallback; private RunCallback mRunCallback;
private boolean mSuspended;
public Servo( public Servo(
RunCallback runCallback, RunCallback runCallback,
@ -49,6 +50,10 @@ public class Servo {
mRunCallback.inGLThread(() -> mJNI.resize(width, height)); mRunCallback.inGLThread(() -> mJNI.resize(width, height));
} }
public void refresh() {
mRunCallback.inGLThread(() -> mJNI.refresh());
}
public void reload() { public void reload() {
mRunCallback.inGLThread(() -> mJNI.reload()); mRunCallback.inGLThread(() -> mJNI.reload());
} }
@ -85,6 +90,10 @@ public class Servo {
mRunCallback.inGLThread(() -> mJNI.click(x, y)); mRunCallback.inGLThread(() -> mJNI.click(x, y));
} }
public void suspend(boolean suspended) {
mSuspended = suspended;
}
public interface Client { public interface Client {
void onLoadStarted(); void onLoadStarted();
@ -122,7 +131,9 @@ public class Servo {
} }
public void wakeup() { public void wakeup() {
mRunCallback.inGLThread(() -> mJNI.performUpdates()); if (!mSuspended) {
mRunCallback.inGLThread(() -> mJNI.performUpdates());
}
} }
public void flush() { public void flush() {

View file

@ -54,6 +54,7 @@ public class ServoView extends GLSurfaceView
setWillNotCacheDrawing(false); setWillNotCacheDrawing(false);
setEGLContextClientVersion(3); setEGLContextClientVersion(3);
setEGLConfigChooser(8, 8, 8, 8, 24, 0); setEGLConfigChooser(8, 8, 8, 8, 24, 0);
setPreserveEGLContextOnPause(true);
ServoGLRenderer mRenderer = new ServoGLRenderer(this); ServoGLRenderer mRenderer = new ServoGLRenderer(this);
setRenderer(mRenderer); setRenderer(mRenderer);
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
@ -80,9 +81,10 @@ public class ServoView extends GLSurfaceView
mServo.stop(); mServo.stop();
} }
public void onSurfaceResized(int width, int height) { public void onSurfaceInvalidated(int width, int height) {
if (mServo != null) { if (mServo != null) {
mServo.resize(width, height); mServo.resize(width, height);
mServo.refresh();
} }
} }
@ -143,7 +145,7 @@ public class ServoView extends GLSurfaceView
if (mScroller.isFinished() && mFlinging) { if (mScroller.isFinished() && mFlinging) {
mFlinging = false; mFlinging = false;
queueEvent(() -> mServo.scrollEnd(0, 0, mCurX, mCurY)); inGLThread(() -> mServo.scrollEnd(0, 0, mCurX, mCurY));
if (!mAnimating) { if (!mAnimating) {
// Not scrolling. Not animating. We don't need to schedule // Not scrolling. Not animating. We don't need to schedule
// another frame. // another frame.
@ -164,7 +166,7 @@ public class ServoView extends GLSurfaceView
mLastY = mCurY; mLastY = mCurY;
if (dx != 0 || dy != 0) { if (dx != 0 || dy != 0) {
queueEvent(() -> mServo.scroll(dx, dy, mCurX, mCurY)); inGLThread(() -> mServo.scroll(dx, dy, mCurX, mCurY));
} else { } else {
if (mAnimating) { if (mAnimating) {
requestRender(); requestRender();
@ -205,7 +207,7 @@ public class ServoView extends GLSurfaceView
mCurY = (int) e.getY(); mCurY = (int) e.getY();
mLastY = mCurY; mLastY = mCurY;
mScroller.forceFinished(true); mScroller.forceFinished(true);
queueEvent(() -> mServo.scrollStart(0, 0, mCurX, mCurY)); inGLThread(() -> mServo.scrollStart(0, 0, mCurX, mCurY));
Choreographer.getInstance().postFrameCallback(this); Choreographer.getInstance().postFrameCallback(this);
return true; return true;
case (MotionEvent.ACTION_MOVE): case (MotionEvent.ACTION_MOVE):
@ -215,7 +217,7 @@ public class ServoView extends GLSurfaceView
case (MotionEvent.ACTION_UP): case (MotionEvent.ACTION_UP):
case (MotionEvent.ACTION_CANCEL): case (MotionEvent.ACTION_CANCEL):
if (!mFlinging) { if (!mFlinging) {
queueEvent(() -> mServo.scrollEnd(0, 0, mCurX, mCurY)); inGLThread(() -> mServo.scrollEnd(0, 0, mCurX, mCurY));
Choreographer.getInstance().removeFrameCallback(this); Choreographer.getInstance().removeFrameCallback(this);
} }
return true; return true;
@ -225,7 +227,7 @@ public class ServoView extends GLSurfaceView
} }
public boolean onSingleTapUp(MotionEvent e) { public boolean onSingleTapUp(MotionEvent e) {
queueEvent(() -> mServo.click((int) e.getX(), (int) e.getY())); inGLThread(() -> mServo.click((int) e.getX(), (int) e.getY()));
return false; return false;
} }
@ -239,6 +241,22 @@ public class ServoView extends GLSurfaceView
public void onShowPress(MotionEvent e) { public void onShowPress(MotionEvent e) {
} }
@Override
public void onPause() {
super.onPause();
if (mServo != null) {
mServo.suspend(true);
}
}
@Override
public void onResume() {
super.onResume();
if (mServo != null) {
mServo.suspend(false);
}
}
static class ServoGLRenderer implements Renderer { static class ServoGLRenderer implements Renderer {
private final ServoView mView; private final ServoView mView;
@ -254,9 +272,9 @@ public class ServoView extends GLSurfaceView
public void onDrawFrame(GL10 unused) { public void onDrawFrame(GL10 unused) {
} }
public void onSurfaceChanged(GL10 unused, int width, int height) { public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES31.glViewport(0, 0, width, height); GLES31.glViewport(0, 0, width, height);
mView.onSurfaceResized(width, height); mView.onSurfaceInvalidated(width, height);
} }
} }
} }