Skip to content
This repository was archived by the owner on Jul 28, 2021. It is now read-only.

Don't show document-related menu items until document loaded #9

Open
wants to merge 4 commits into
base: devel
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
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:versionCode="4"
android:versionName="1.2.1" >
<uses-sdk android:minSdkVersion="23"
android:targetSdkVersion="25" />
android:targetSdkVersion="26" />
<application android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Expand Down
1 change: 1 addition & 0 deletions app/src/main/assets/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ function onGetDocument() {
channel.setNumPages(pdfDoc.numPages);
scale = zoomLevels[channel.getZoomLevel()] / 100;
renderPage(channel.getPage());
channel.onDocumentLoaded();
});
}
32 changes: 27 additions & 5 deletions app/src/main/java/co/copperhead/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class PdfViewer extends Activity {
private WebView mWebView;
private Uri mUri;
private Channel mChannel;
private boolean documentLoaded;

private class Channel {
private int mPage;
Expand Down Expand Up @@ -73,6 +74,9 @@ public int getZoomLevel() {
public void setNumPages(int numPages) {
mNumPages = numPages;
}

@JavascriptInterface
public void onDocumentLoaded() { documentLoaded = true; invalidateOptionsMenu(); }
}

@Override
Expand Down Expand Up @@ -184,6 +188,12 @@ private void openDocument() {
startActivityForResult(intent, ACTION_OPEN_DOCUMENT_REQUEST_CODE);
}

private void renderDocument() {
documentLoaded = false;
invalidateOptionsMenu();
mWebView.evaluateJavascript("onRenderPage()", null);
}

private void closeDocument() {
if (mWebView != null) {
runOnUiThread(new Runnable() {
Expand Down Expand Up @@ -224,6 +234,18 @@ public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.pdf_viewer, menu);

if (documentLoaded) {
for (int itemId : new int[] { R.id.action_previous,
R.id.action_next,
R.id.action_close,
R.id.action_zoom_out,
R.id.action_zoom_in,
R.id.action_print }) {
menu.findItem(itemId).setVisible(true);
}
}

return true;
}

Expand All @@ -233,14 +255,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_previous:
if (mChannel.mPage > 1) {
mChannel.mPage--;
mWebView.evaluateJavascript("onRenderPage()", null);
renderDocument();
}
return super.onOptionsItemSelected(item);

case R.id.action_next:
if (mChannel.mPage < mChannel.mNumPages) {
mChannel.mPage++;
mWebView.evaluateJavascript("onRenderPage()", null);
renderDocument();
}
return super.onOptionsItemSelected(item);

Expand All @@ -256,14 +278,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_zoom_out:
if (mChannel.mZoomLevel > 0) {
mChannel.mZoomLevel--;
mWebView.evaluateJavascript("onRenderPage()", null);
renderDocument();
}
return super.onOptionsItemSelected(item);

case R.id.action_zoom_in:
if (mChannel.mZoomLevel < MAX_ZOOM_LEVEL) {
mChannel.mZoomLevel++;
mWebView.evaluateJavascript("onRenderPage()", null);
renderDocument();
}
return super.onOptionsItemSelected(item);

Expand All @@ -287,7 +309,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
int page = picker.getValue();
if (page >= 1 && page <= mChannel.mNumPages) {
mChannel.mPage = page;
mWebView.evaluateJavascript("onRenderPage()", null);
renderDocument();
}
}
})
Expand Down
23 changes: 15 additions & 8 deletions app/src/main/res/menu/pdf_viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,55 @@
android:id="@+id/action_previous"
android:icon="@drawable/ic_navigate_before_white_24dp"
android:title="@string/action_previous"
android:visible="false"
android:showAsAction="ifRoom" />

<item
android:id="@+id/action_next"
android:icon="@drawable/ic_navigate_next_white_24dp"
android:title="@string/action_next"
android:visible="false"
android:showAsAction="ifRoom" />

<item
android:id="@+id/action_open"
android:icon="@drawable/ic_insert_drive_file_white_24dp"
android:title="@string/action_open"
android:showAsAction="ifRoom" />
android:id="@+id/action_open"
android:icon="@drawable/ic_insert_drive_file_white_24dp"
android:title="@string/action_open"
android:showAsAction="ifRoom" />

<item
android:id="@+id/action_close"
android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:title="@string/action_close"
android:showAsAction="ifRoom" />
android:id="@+id/action_close"
android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:title="@string/action_close"
android:visible="false"
android:showAsAction="ifRoom" />

<item
android:id="@+id/action_zoom_out"
android:icon="@drawable/ic_zoom_out_white_24dp"
android:title="@string/action_zoom_out"
android:visible="false"
android:showAsAction="ifRoom" />

<item
android:id="@+id/action_zoom_in"
android:icon="@drawable/ic_zoom_in_white_24dp"
android:title="@string/action_zoom_in"
android:visible="false"
android:showAsAction="ifRoom" />

<item
android:id="@+id/action_jump_to_page"
android:icon="@drawable/ic_pageview_white_24dp"
android:title="@string/action_jump_to_page"
android:visible="false"
android:showAsAction="ifRoom" />

<item
android:id="@+id/action_print"
android:icon="@drawable/ic_print_white_24dp"
android:title="@string/action_print"
android:visible="false"
android:showAsAction="ifRoom" />

</menu>