Skip to content

Update to new pagination API #40

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: develop
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 @@ -87,10 +87,15 @@ ReadiumSDK.HostAppFeedback = function() {

}, this);

this.onPaginationChanged = function(paginationInfo) {
this.onPaginationChanged = function(pageChangeData) {

if (window.LauncherUI) {
window.LauncherUI.onPaginationChanged(JSON.stringify(paginationInfo.paginationInfo));
var staticPaginationInfo = pageChangeData.paginationInfo;
staticPaginationInfo.canGoLeft = staticPaginationInfo.canGoLeft();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename the canGoXX field names, so that they do not overwrite the canGoXX() method names in the PaginationInfo object?
In iOS launcher we append '_':
https://github.com/readium/SDKLauncher-iOS/blob/develop/Resources/host_app_feedback.js#L94

staticPaginationInfo.canGoRight = staticPaginationInfo.canGoRight();
staticPaginationInfo.canGoNext = staticPaginationInfo.canGoNext();
staticPaginationInfo.canGoPrev = staticPaginationInfo.canGoPrev();
window.LauncherUI.onPaginationChanged(JSON.stringify(staticPaginationInfo));

if(window.LauncherUI.onIsMediaOverlayAvailable){
window.LauncherUI.onIsMediaOverlayAvailable(ReadiumSDK.reader.isMediaOverlayAvailable());
Expand Down
1 change: 1 addition & 0 deletions SDKLauncher-Android/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<string name="metadata_title">Title: %1$s</string>
<string name="page_list">Page List</string>
<string name="page_x_of_y">Page %1$d of %2$d</string>
<string name="page_x_dash_y_of_z">Page %1$d-%2$d of %3$d</string>
<string name="spine_items">Spine Items</string>
<string name="table_of_contents">Table of Contents</string>
<string name="title_activity_web_view">WebViewActivity</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
Expand Down Expand Up @@ -133,6 +134,10 @@ public class WebViewActivity extends FragmentActivity implements ViewerSettingsD
private ViewerSettings mViewerSettings;
private ReadiumJSApi mReadiumJSApi;
private EpubServer mServer;

private Button mLeftButton;
private Button mRightButton;


private boolean mIsMoAvailable;
private boolean mIsMoPlaying;
Expand All @@ -148,7 +153,10 @@ protected void onCreate(Bundle savedInstanceState) {
&& 0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
mWebview.setWebContentsDebuggingEnabled(true);
}


mLeftButton = (Button) findViewById(R.id.left);
mRightButton = (Button) findViewById(R.id.right);

mPageInfo = (TextView) findViewById(R.id.page_info);
initWebView();

Expand Down Expand Up @@ -497,19 +505,31 @@ public class EpubInterface {
public void onPaginationChanged(String currentPagesInfo) {
Log.d(TAG, "onPaginationChanged: "+currentPagesInfo);
try {
PaginationInfo paginationInfo = PaginationInfo.fromJson(currentPagesInfo);
List<Page> openPages = paginationInfo.getOpenPages();
final PaginationInfo paginationInfo = PaginationInfo.fromJson(currentPagesInfo);
final List<Page> openPages = paginationInfo.getOpenPages();
if (!openPages.isEmpty()) {
final Page page = openPages.get(0);

final Page firstVisiblePage = openPages.get(0);
runOnUiThread(new Runnable() {
public void run() {
if(openPages.size() == 1){
mPageInfo.setText(getString(R.string.page_x_of_y,
page.getSpineItemPageIndex() + 1,
page.getSpineItemPageCount()));
SpineItem spineItem = mPackage.getSpineItem(page.getIdref());
firstVisiblePage.getSpineItemPageIndex() + 1,
firstVisiblePage.getSpineItemPageCount()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When isFixedLayout is true in PaginationInfo, the total page count is assumed to be getSpineItemCount(), rather than Page.getSpineItemPageCount() ... well, to be precise this works okay with most commercial EPUBs that are either reflow or fixed-layout, of course this calculation breaks when an EPUB contains mixed reflow-fxl spine items (known caveat). Obviously, the page index itself needs adjusting as well, so that when isFixedLayout is true in PaginationInfo, spineItemIndex is used instead of Page.getSpineItemPageIndex()

References:
https://github.com/readium/SDKLauncher-iOS/blob/develop/Classes/EPubViewController.m#L474
https://github.com/readium/readium-shared-js/blob/develop/js/models/current_pages_info.js#L45

} else {
Page lastVisiblePage = openPages.get(openPages.size() - 1);
mPageInfo.setText(getString(R.string.page_x_dash_y_of_z,
firstVisiblePage.getSpineItemPageIndex() + 1,
lastVisiblePage.getSpineItemPageIndex() + 1,
firstVisiblePage.getSpineItemPageCount()));
}

SpineItem spineItem = mPackage.getSpineItem(firstVisiblePage.getIdref());
boolean isFixedLayout = spineItem.isFixedLayout();
mWebview.getSettings().setBuiltInZoomControls(isFixedLayout);
mWebview.getSettings().setDisplayZoomControls(false);
mLeftButton.setEnabled(paginationInfo.canGoLeft());
mRightButton.setEnabled(paginationInfo.canGoRight());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,29 @@

public class PaginationInfo {

private final String pageProgressionDirection;
private final boolean isRightToLeft;
private final boolean isFixedLayout;
private final int spineItemCount;
private final List<Page> openPages;

public PaginationInfo(String pageProgressionDirection,
boolean isFixedLayout, int spineItemCount) {
this.pageProgressionDirection = pageProgressionDirection;
this.isFixedLayout = isFixedLayout;
this.spineItemCount = spineItemCount;
this.openPages = new ArrayList<Page>();
}
private final boolean canGoLeft;
private final boolean canGoRight;
private final boolean canGoPrev;
private final boolean canGoNext;

public PaginationInfo(boolean isRightToLeft, boolean isFixedLayout, int spineItemCount, boolean canGoLeft, boolean canGoRight, boolean canGoPrev, boolean canGoNext) {

public String getPageProgressionDirection() {
return pageProgressionDirection;
this.isRightToLeft = isRightToLeft;
this.isFixedLayout = isFixedLayout;
this.spineItemCount = spineItemCount;
this.openPages = new ArrayList<Page>();
this.canGoLeft = canGoLeft;
this.canGoRight = canGoRight;
this.canGoPrev = canGoPrev;
this.canGoNext = canGoNext;
}

public boolean isRightToLeft() {
return isRightToLeft;
}

public boolean isFixedLayout() {
Expand All @@ -68,12 +76,32 @@ public int getSpineItemCount() {
public List<Page> getOpenPages() {
return openPages;
}

public static PaginationInfo fromJson(String jsonString) throws JSONException {

public boolean canGoLeft() {
return canGoLeft;
}

public boolean canGoRight() {
return canGoRight;
}

public boolean canGoPrev() {
return canGoPrev;
}

public boolean canGoNext() {
return canGoNext;
}

public static PaginationInfo fromJson(String jsonString) throws JSONException {
JSONObject json = new JSONObject(jsonString);
PaginationInfo paginationInfo = new PaginationInfo(json.optString("pageProgressionDirection", "ltr"),
json.optBoolean("isFixedLayout"),
json.optInt("spineItemCount"));
PaginationInfo paginationInfo = new PaginationInfo(json.optBoolean("isRightToLeft"),
json.optBoolean("isFixedLayout"),
json.optInt("spineItemCount"),
json.optBoolean("canGoLeft"),
json.optBoolean("canGoRight"),
json.optBoolean("canGoPrev"),
json.optBoolean("canGoNext"));
JSONArray openPages = json.getJSONArray("openPages");
for (int i = 0; i < openPages.length(); i++) {
JSONObject p = openPages.getJSONObject(i);
Expand Down