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)
}
/// Redraw the page.
pub fn refresh(&mut self) -> Result<(), &'static str> {
info!("refresh");
self.process_event(WindowEvent::Refresh)
}
/// Stop loading the page.
pub fn stop(&mut self) -> Result<(), &'static str> {
debug!("TODO can't stop won't stop");

View file

@ -144,6 +144,12 @@ pub extern "C" fn stop() {
call(|s| s.stop());
}
#[no_mangle]
pub extern "C" fn refresh() {
debug!("refresh");
call(|s| s.refresh());
}
#[no_mangle]
pub extern "C" fn 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());
}
#[no_mangle]
pub fn Java_com_mozilla_servoview_JNIServo_refresh(env: JNIEnv, _class: JClass) {
debug!("refresh");
call(env, |s| s.refresh());
}
#[no_mangle]
pub fn Java_com_mozilla_servoview_JNIServo_goBack(env: JNIEnv, _class: JClass) {
debug!("goBack");

View file

@ -151,4 +151,15 @@ public class MainActivity extends Activity implements Servo.Client {
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 refresh();
public native void goBack();
public native void goForward();

View file

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

View file

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