Add GLView save state

This commit is contained in:
seven332 committed 2015-08-09 01:34:21 +08:00
1 parent 36424b11a9
commit 31fe21007d
8 files changed
+157 -16

No files matched your search

@@ -22,11 +22,13 @@ import android.graphics.Matrix;
import android.graphics.Rect;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Parcelable;
import android.os.Process;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
@@ -603,6 +605,28 @@ public class GLRootView extends GLSurfaceView
}
}
/**
* {@inheritDoc}
*/
@Override
protected void dispatchSaveInstanceState(@NonNull SparseArray<Parcelable> container) {
super.dispatchSaveInstanceState(container);
if (mContentView != null) {
mContentView.saveHierarchyState(container);
}
}
/**
* {@inheritDoc}
*/
@Override
protected void dispatchRestoreInstanceState(@NonNull SparseArray<Parcelable> container) {
super.dispatchRestoreInstanceState(container);
if (mContentView != null) {
mContentView.restoreHierarchyState(container);
}
}
@Override
protected void finalize() throws Throwable {
try {
@@ -18,8 +18,10 @@ package com.hippo.ehviewer.gallery.ui;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Parcelable;
import android.os.SystemClock;
import android.util.Log;
import android.util.SparseArray;
import android.view.MotionEvent;
import com.hippo.ehviewer.gallery.anim.CanvasAnimation;
@@ -44,12 +46,11 @@ import java.util.ArrayList;
// from main thread (like a Handler) in your GLView, you need to call
// lockRendering() if the rendering thread should not run at the same time.
//
// TODO Add store state
// TODO Add click listener
public class GLView {
private static final String TAG = "GLView";
private static final boolean DEBUG_DRAW_BOUNDS = true;
private static final boolean DEBUG_DRAW_BOUNDS = false;
public static final int VISIBLE = 0;
public static final int INVISIBLE = 1;
@@ -58,6 +59,11 @@ public class GLView {
private static final int FLAG_SET_MEASURED_SIZE = 2;
private static final int FLAG_LAYOUT_REQUESTED = 4;
/**
* Used to mark a GlView that has no ID.
*/
public static final int NO_ID = -1;
public interface OnClickListener {
void onClick(GLView v);
}
@@ -94,6 +100,14 @@ public class GLView {
protected int mScrollHeight = 0;
protected int mScrollWidth = 0;
/**
* The GlView's identifier.
*
* @see #setId(int)
* @see #getId()
*/
private int mID = NO_ID;
/**
* The minimum height of the view. We'll try our best to have the height
* of this view to at least this amount.
@@ -259,6 +273,27 @@ public class GLView {
component.mParent = null;
}
/**
* Sets the identifier for this GlView. The identifier does not have to be
* unique in this GlView's hierarchy. The identifier should be a positive
* number.
*
* @param id a number used to identify the view
*/
public void setId(int id) {
mID = id;
}
/**
* Returns this GlView's identifier.
*
* @return a positive integer used to identify the view or {@link #NO_ID}
* if the view has no ID
*/
public int getId() {
return mID;
}
public Rect bounds() {
return mBounds;
}
@@ -429,6 +464,60 @@ public class GLView {
return onTouch(event);
}
/**
* Called by {@link GLRootView#dispatchSaveInstanceState(SparseArray)} to
* store this GlView hierarchy's frozen state into the given container.
*
* @param container The SparseArray in which to save the view's state.
*/
public void saveHierarchyState(SparseArray<Parcelable> container) {
dispatchSaveInstanceState(container);
}
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
if (mID != NO_ID) {
Parcelable state = onSaveInstanceState();
if (state != null) {
container.put(mID, state);
}
}
for (int i = 0, n = getComponentCount(); i < n; ++i) {
getComponent(i).dispatchSaveInstanceState(container);
}
}
protected Parcelable onSaveInstanceState() {
return null;
}
/**
* Called by {@link GLRootView#dispatchRestoreInstanceState(SparseArray)} to
* restore this view hierarchy's frozen state from the given container.
*
* @param container The SparseArray which holds previously frozen states.
*/
public void restoreHierarchyState(SparseArray<Parcelable> container) {
dispatchRestoreInstanceState(container);
}
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
if (mID != NO_ID) {
Parcelable state = container.get(mID);
if (state != null) {
onRestoreInstanceState(state);
}
}
for (int i = 0, n = getComponentCount(); i < n; ++i) {
getComponent(i).dispatchRestoreInstanceState(container);
}
}
protected void onRestoreInstanceState(Parcelable state) {
if (state != null) {
throw new IllegalArgumentException("Please override onRestoreInstanceState");
}
}
public void setPaddings(int paddingLeft, int paddingTop, int paddingRight, int paddingBottom) {
mPaddings.set(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
@@ -33,10 +33,12 @@ public class GalleryLayout extends FrameLayout implements GalleryView.ActionList
setBackgroundColor(context.getResources().getColor(R.color.gallery_background));
mGalleryView = new GalleryView(context, 0, this);
mGalleryView.setId(R.id.gallery_view);
mGalleryView.setProgressSize(LayoutUtils.dp2pix(context, 56));
mGalleryView.setInterval(LayoutUtils.dp2pix(context, 48));
mGalleryPanel = new GalleryPanel(context, this);
mGalleryPanel.setId(R.id.gallery_panel);
addComponent(mGalleryView, new GravityLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addComponent(mGalleryPanel, new GravityLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
@@ -52,7 +54,7 @@ public class GalleryLayout extends FrameLayout implements GalleryView.ActionList
@Override
public void onTapCenter() {
mGalleryPanel.setShown(true);
mGalleryPanel.setShown(true, true);
mActionListener.onShowGalleryPanel();
}
@@ -17,6 +17,8 @@
package com.hippo.ehviewer.gallery.ui;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.MotionEvent;
import com.hippo.ehviewer.R;
@@ -30,6 +32,8 @@ import com.hippo.yorozuya.LayoutUtils;
public class GalleryPanel extends GLView implements FitPaddingImpl, Slider.OnSetProgressListener {
private static final String KEY_SHOWN = "shown_key";
private Context mContext;
private LinearLayout mRightView;
@@ -134,15 +138,15 @@ public class GalleryPanel extends GLView implements FitPaddingImpl, Slider.OnSet
mBottomView.offsetTopAndBottom(getHeight() - (int) (mBottomView.getMeasuredHeight() * mShowPercent) - mBottomView.bounds().top);
}
};
mAnimation.setInterpolator(AnimationUtils.FAST_SLOW_INTERPOLATOR);
}
protected void setShown(boolean shown) {
public void setShown(boolean shown, boolean animation) {
if (mShown != shown) {
mShown = shown;
mAnimation.setRange(mShowPercent, shown ? 1.0f : 0.0f);
mAnimation.setDuration(300); // TODO
mAnimation.setDuration(animation ? 300 : 0); // TODO
mAnimation.setInterpolator(shown ? AnimationUtils.FAST_SLOW_INTERPOLATOR : AnimationUtils.SLOW_FAST_INTERPOLATOR);
mAnimation.forceReset();
invalidate();
@@ -155,7 +159,7 @@ public class GalleryPanel extends GLView implements FitPaddingImpl, Slider.OnSet
@Override
protected boolean onTouch(MotionEvent event) {
if (mShown && event.getAction() == MotionEvent.ACTION_UP) {
setShown(false);
setShown(false, true);
}
return mShown;
}
@@ -220,6 +224,19 @@ public class GalleryPanel extends GLView implements FitPaddingImpl, Slider.OnSet
super.onRender(canvas);
}
@Override
protected Parcelable onSaveInstanceState() {
Bundle saved = new Bundle();
saved.putBoolean(KEY_SHOWN, mShown);
return saved;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
Bundle saved = (Bundle) state;
setShown(saved.getBoolean(KEY_SHOWN), false);
}
public interface ActionListener {
void onSetProgress(int newProgress, int oldProgress, boolean byUser);
@@ -65,6 +65,8 @@ public class GalleryActivity extends StatsActivity implements TiledTexture.OnFre
public static final String KEY_GALLERY_BASE = "gallery_base";
public static final String KEY_ARCHIVE_URI = "archive_uri";
public static final String KEY_SHOWING = "showing";
public static final String KEY_HASH_CODE = "hash_code";
public static final SparseArray<GalleryProvider> sGalleryProviderMap = new SparseArray<>();
public static final SparseArray<CloseGalleryProviderTask> sCloseGalleryProviderTaskMap = new SparseArray<>();
@@ -151,7 +153,11 @@ public class GalleryActivity extends StatsActivity implements TiledTexture.OnFre
mSystemUiHelper = new SystemUiHelper(this, SystemUiHelper.LEVEL_IMMERSIVE,
SystemUiHelper.FLAG_LAYOUT_IN_SCREEN_OLDER_DEVICES | SystemUiHelper.FLAG_IMMERSIVE_STICKY);
mSystemUiHelper.hide();
if (savedInstanceState == null || !savedInstanceState.getBoolean(KEY_SHOWING, false)) {
mSystemUiHelper.hide();
} else {
mSystemUiHelper.show();
}
mResources = getResources();
@@ -208,11 +214,6 @@ public class GalleryActivity extends StatsActivity implements TiledTexture.OnFre
mGalleryView.setMode(mode);
}
@Override
public void finish() {
super.finish();
}
private static void closeGalleryProvider(GalleryProvider galleryProvider) {
if (galleryProvider instanceof GallerySpider) {
GallerySpider.release((GallerySpider) galleryProvider);
@@ -225,6 +226,7 @@ public class GalleryActivity extends StatsActivity implements TiledTexture.OnFre
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_HASH_CODE, hashCode());
outState.putBoolean(KEY_SHOWING, mSystemUiHelper.isShowing());
}
@Override
@@ -266,7 +268,11 @@ public class GalleryActivity extends StatsActivity implements TiledTexture.OnFre
protected void onResume() {
super.onResume();
mSystemUiHelper.hide();
if (mSystemUiHelper.isShowing()) {
mSystemUiHelper.show();
} else {
mSystemUiHelper.hide();
}
}
@Override