Skip to content

Commit

Permalink
Behave properly when app is suspended
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrouget committed Aug 14, 2018
1 parent e051c58 commit ae407e9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
6 changes: 6 additions & 0 deletions ports/libsimpleservo/src/api.rs
Expand Up @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions ports/libsimpleservo/src/capi.rs
Expand Up @@ -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");
Expand Down
6 changes: 6 additions & 0 deletions ports/libsimpleservo/src/jniapi.rs
Expand Up @@ -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");
Expand Down
Expand Up @@ -151,4 +151,15 @@ public void onHistoryChanged(boolean canGoBack, boolean canGoForward) {
mFwdButton.setEnabled(canGoForward);
}

@Override
public void onPause() {
mServoView.onPause();
super.onPause();
}
@Override
public void onResume() {
mServoView.onResume();
super.onResume();
}

}
Expand Up @@ -35,6 +35,8 @@ public native void init(Activity activity,

public native void stop();

public native void refresh();

public native void goBack();

public native void goForward();
Expand Down
Expand Up @@ -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,
Expand Down Expand Up @@ -49,6 +50,10 @@ public void resize(int width, int height) {
mRunCallback.inGLThread(() -> mJNI.resize(width, height));
}

public void refresh() {
mRunCallback.inGLThread(() -> mJNI.refresh());
}

public void reload() {
mRunCallback.inGLThread(() -> mJNI.reload());
}
Expand Down Expand Up @@ -85,6 +90,10 @@ public void click(int x, int y) {
mRunCallback.inGLThread(() -> mJNI.click(x, y));
}

public void suspend(boolean suspended) {
mSuspended = suspended;
}

public interface Client {
void onLoadStarted();

Expand Down Expand Up @@ -122,7 +131,9 @@ private class Callbacks implements JNIServo.Callbacks, Client {
}

public void wakeup() {
mRunCallback.inGLThread(() -> mJNI.performUpdates());
if (!mSuspended) {
mRunCallback.inGLThread(() -> mJNI.performUpdates());
}
}

public void flush() {
Expand Down
Expand Up @@ -54,6 +54,7 @@ public ServoView(Context context, AttributeSet attrs) {
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);
Expand All @@ -80,9 +81,10 @@ public void stop() {
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();
}
}

Expand Down Expand Up @@ -143,7 +145,7 @@ public void doFrame(long frameTimeNanos) {

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.
Expand All @@ -164,7 +166,7 @@ public void doFrame(long frameTimeNanos) {
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();
Expand Down Expand Up @@ -205,7 +207,7 @@ public boolean onTouchEvent(final MotionEvent e) {
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):
Expand All @@ -215,7 +217,7 @@ public boolean onTouchEvent(final MotionEvent e) {
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;
Expand All @@ -225,7 +227,7 @@ public boolean onTouchEvent(final 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;
}

Expand All @@ -239,6 +241,22 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
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;
Expand All @@ -254,9 +272,9 @@ public void onSurfaceCreated(GL10 unused, EGLConfig config) {
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);
}
}
}

0 comments on commit ae407e9

Please sign in to comment.