Skip to content

Save view state with onSaveInstanceState #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import android.net.Uri;
import android.os.AsyncTask;
import android.os.HandlerThread;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
Expand Down Expand Up @@ -68,6 +71,7 @@

import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -124,6 +128,9 @@ enum ScrollDir {

PdfFile pdfFile;

private PdfViewState restoredState;
private boolean hasRestoredFromState;

/** The index of the current sequence */
private int currentPage;

Expand Down Expand Up @@ -261,12 +268,28 @@ public PDFView(Context context, AttributeSet set) {
setWillNotDraw(false);
}

@Nullable
@Override
protected Parcelable onSaveInstanceState() {
SavedState state = new SavedState(super.onSaveInstanceState());
if (pdfFile != null) {
state.viewState = getCurrentViewState();
}
return state;
}

@Override
protected void onRestoreInstanceState(Parcelable parcelable) {
SavedState state = (SavedState) parcelable;
restoredState = state.viewState;
super.onRestoreInstanceState(state.getSuperState());
}

private void load(DocumentSource docSource, String password) {
load(docSource, password, null);
}

private void load(DocumentSource docSource, String password, int[] userPages) {

if (!recycled) {
throw new IllegalStateException("Don't call load on a PDF View without recycling it first.");
}
Expand All @@ -277,6 +300,69 @@ private void load(DocumentSource docSource, String password, int[] userPages) {
decodingAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

/**
* Gets the current position and zoom state of the view.
* This state can be saved or persisted and later restored with {@link #restoreViewState(PdfViewState)}.
*/
public PdfViewState getCurrentViewState() {
if (pdfFile == null) {
throw new IllegalStateException("Failed to save current view state (no file loaded)");
}
PdfViewState state = new PdfViewState();
state.zoom = getZoom();
state.currentPage = getCurrentPage();
float centerX = -currentXOffset + (getWidth() / 2f);
float centerY = -currentYOffset + (getHeight() / 2f);
SizeF pageSize = pdfFile.getScaledPageSize(state.currentPage, state.zoom);
if (swipeVertical) {
state.pageFocusX = (centerX - pdfFile.getSecondaryPageOffset(state.currentPage, zoom)) / pageSize.getWidth();
state.pageFocusY = (centerY - pdfFile.getPageOffset(state.currentPage, zoom)) / pageSize.getHeight();
} else {
state.pageFocusX = (centerX - pdfFile.getPageOffset(state.currentPage, zoom)) / pageSize.getWidth();
state.pageFocusY = (centerY - pdfFile.getSecondaryPageOffset(state.currentPage, zoom)) / pageSize.getHeight();
}
return state;
}

/**
* <p>Restores a view state previously gotten with {@link #getCurrentViewState()} or
* {@link #onSaveInstanceState()}. </p>
* <p>The view handles restoring state with onSaveInstanceState for configuration changes or
* activity destruction, but the host application has to make sure the same pdf is loaded.
* This view has no way of knowing if the pdf loaded after restoring is the same as before.
* If the pdf content changed or a different pdf is loaded, it may restore to a incorrect position.
* To disable the automatic restoration use {@link #setSaveEnabled(boolean)}.</p>
* <p>Note: This must be called after a pdf file has been loaded. A good place to call this is
* in the {@link OnLoadCompleteListener} callback.</p>
* @throws IllegalStateException If pdf file is not loaded
*/
public void restoreViewState(PdfViewState state) {
if (pdfFile == null) {
throw new IllegalStateException("Pdf file has not been loaded. Call restoreViewState from the loadCompleted callback.");
}
zoom = state.zoom;
int maxPageCount = pdfFile.getPagesCount() - 1;
currentPage = Math.min(maxPageCount, state.currentPage);

SizeF pageSize = pdfFile.getScaledPageSize(currentPage, zoom);
float pageX = pageSize.getWidth() * state.pageFocusX;
float pageY = pageSize.getHeight() * state.pageFocusY;
float mainOffset = pdfFile.getPageOffset(currentPage, zoom);
float secondaryOffset = pdfFile.getSecondaryPageOffset(currentPage, zoom);
float x, y;
if (swipeVertical) {
x = secondaryOffset + pageX - (getWidth() / 2f);
y = mainOffset + pageY - (getHeight() / 2f);
} else {
x = mainOffset + pageX - (getWidth() / 2f);
y = secondaryOffset + pageY - (getHeight() / 2f);
}
moveTo(-x, -y);
showPage(currentPage);
performPageSnap(false);
hasRestoredFromState = true;
}

/**
* Go to the given page.
*
Expand Down Expand Up @@ -735,9 +821,17 @@ void loadComplete(PdfFile pdfFile) {

dragPinchManager.enable();

hasRestoredFromState = false;
callbacks.callOnLoadComplete(pdfFile.getPagesCount());

jumpTo(defaultPage, false);
if (!hasRestoredFromState && restoredState != null) {
restoreViewState(restoredState);
restoredState = null;
}

if (!hasRestoredFromState) {
jumpTo(defaultPage, false);
}
}

void loadError(Throwable t) {
Expand Down Expand Up @@ -897,6 +991,13 @@ void loadPageByOffset() {
* Animate to the nearest snapping position for the current SnapPolicy
*/
public void performPageSnap() {
performPageSnap(true);
}

/**
* Snap to the nearest snapping position for the current SnapPolicy
*/
public void performPageSnap(boolean animated) {
if (!pageSnap || pdfFile == null || pdfFile.getPagesCount() == 0) {
return;
}
Expand All @@ -907,10 +1008,18 @@ public void performPageSnap() {
}

float offset = snapOffsetForPage(centerPage, edge);
if (swipeVertical) {
animationManager.startYAnimation(currentYOffset, -offset);
if (animated) {
if (swipeVertical) {
animationManager.startYAnimation(currentYOffset, -offset);
} else {
animationManager.startXAnimation(currentXOffset, -offset);
}
} else {
animationManager.startXAnimation(currentXOffset, -offset);
if (swipeVertical) {
moveTo(currentXOffset, -offset);
} else {
moveTo(-offset, currentYOffset);
}
}
}

Expand Down Expand Up @@ -1280,6 +1389,7 @@ public Configurator fromSource(DocumentSource docSource) {

private enum State {DEFAULT, LOADED, SHOWN, ERROR}

@SuppressWarnings("unused")
public class Configurator {

private final DocumentSource documentSource;
Expand Down Expand Up @@ -1508,4 +1618,90 @@ public void load() {
}
}
}

/**
* Holds the zoom and position state for PdfView
*/
public static class PdfViewState implements Serializable, Parcelable {

int currentPage;
float zoom = 1f;
// relative point of the page (0 to 1) that is in the center of the view
float pageFocusX;
float pageFocusY;

public PdfViewState() {
}

private PdfViewState(Parcel in) {
currentPage = in.readInt();
zoom = in.readFloat();
pageFocusX = in.readFloat();
pageFocusY = in.readFloat();
}

@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(currentPage);
out.writeFloat(zoom);
out.writeFloat(pageFocusX);
out.writeFloat(pageFocusY);
}

@Override
public int describeContents() {
return 0;
}

public static final Parcelable.Creator<PdfViewState> CREATOR =
new Parcelable.Creator<PdfViewState>() {
public PdfViewState createFromParcel(Parcel in) {
return new PdfViewState(in);
}

public PdfViewState[] newArray(int size) {
return new PdfViewState[size];
}
};
}

/**
* Wrapper around {@link PdfViewState} to not leak super state in public API.
*/
static class SavedState extends BaseSavedState {

@Nullable PdfViewState viewState;

public SavedState(Parcelable superState) {
super(superState);
}

private SavedState(Parcel in) {
super(in);
if (in.readByte() == 1) {
viewState = in.readParcelable(getClass().getClassLoader());
}
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeByte((byte) (viewState == null ? 0 : 1));
if (viewState != null) {
out.writeParcelable(viewState, 0);
}
}

public static final Parcelable.Creator<SavedState> CREATOR =
new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

public SavedState[] newArray(int size) {
return new SavedState[size];
}
};

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ public class PDFViewActivity extends AppCompatActivity implements OnPageChangeLi
@NonConfigurationInstance
Uri uri;

@NonConfigurationInstance
Integer pageNumber = 0;

String pdfFileName;

@OptionsItem(R.id.pickFile)
Expand Down Expand Up @@ -115,7 +112,6 @@ private void displayFromAsset(String assetFileName) {
pdfFileName = assetFileName;

pdfView.fromAsset(SAMPLE_FILE)
.defaultPage(pageNumber)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
Expand All @@ -130,7 +126,6 @@ private void displayFromUri(Uri uri) {
pdfFileName = getFileName(uri);

pdfView.fromUri(uri)
.defaultPage(pageNumber)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
Expand All @@ -150,7 +145,6 @@ public void onResult(int resultCode, Intent intent) {

@Override
public void onPageChanged(int page, int pageCount) {
pageNumber = page;
setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
}

Expand Down