From 31fe21007d09d0668bce6d10c85cea2c7f70183f Mon Sep 17 00:00:00 2001 From: seven332 Date: Sun, 9 Aug 2015 01:34:21 +0800 Subject: [PATCH] Add GLView save state --- .../hippo/ehviewer/gallery/ui/GLRootView.java | 24 +++++ .../com/hippo/ehviewer/gallery/ui/GLView.java | 93 ++++++++++++++++++- .../ehviewer/gallery/ui/GalleryLayout.java | 4 +- .../ehviewer/gallery/ui/GalleryPanel.java | 25 ++++- .../hippo/ehviewer/ui/GalleryActivity.java | 20 ++-- app/src/main/res/values/id.xml | 3 + vectorold | 2 +- yorozuya | 2 +- 8 files changed, 157 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLRootView.java b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLRootView.java index 2354a0ea4..be63d6eb1 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLRootView.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLRootView.java @@ -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 container) { + super.dispatchSaveInstanceState(container); + if (mContentView != null) { + mContentView.saveHierarchyState(container); + } + } + + /** + * {@inheritDoc} + */ + @Override + protected void dispatchRestoreInstanceState(@NonNull SparseArray container) { + super.dispatchRestoreInstanceState(container); + if (mContentView != null) { + mContentView.restoreHierarchyState(container); + } + } + @Override protected void finalize() throws Throwable { try { diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLView.java b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLView.java index 253935791..4d78f7513 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLView.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GLView.java @@ -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 container) { + dispatchSaveInstanceState(container); + } + + protected void dispatchSaveInstanceState(SparseArray 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 container) { + dispatchRestoreInstanceState(container); + } + + protected void dispatchRestoreInstanceState(SparseArray 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); } diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryLayout.java b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryLayout.java index 470559bb5..09ef119c8 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryLayout.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryLayout.java @@ -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(); } diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryPanel.java b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryPanel.java index 48ca2d425..5313b9c39 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryPanel.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/ui/GalleryPanel.java @@ -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); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java index 98730655e..1697b072b 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java @@ -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 sGalleryProviderMap = new SparseArray<>(); public static final SparseArray 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 diff --git a/app/src/main/res/values/id.xml b/app/src/main/res/values/id.xml index b29a98451..8b9fe5641 100644 --- a/app/src/main/res/values/id.xml +++ b/app/src/main/res/values/id.xml @@ -19,4 +19,7 @@ + + + diff --git a/vectorold b/vectorold index 82fa5be4a..34d314b58 160000 --- a/vectorold +++ b/vectorold @@ -1 +1 @@ -Subproject commit 82fa5be4abab7ae497b72676f2f7c83f154a521e +Subproject commit 34d314b584741a703ba7e17cf3f7f123bd3ade40 diff --git a/yorozuya b/yorozuya index e168b8d76..78bcf1c6b 160000 --- a/yorozuya +++ b/yorozuya @@ -1 +1 @@ -Subproject commit e168b8d76b385485de465f014bb8906266eb4245 +Subproject commit 78bcf1c6b7c545d4880a5382c59e6a89272619c9