Skip to content
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
46 changes: 42 additions & 4 deletions app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private static final String STATE_ZOOM_RATIO = "zoomRatio";
private static final String STATE_DOCUMENT_ORIENTATION_DEGREES = "documentOrientationDegrees";
private static final String STATE_ENCRYPTED_DOCUMENT_PASSWORD = "encrypted_document_password";
private static final String STATE_VERTICAL_SCROLL_MODE = "vertical_scroll_mode";
private static final String KEY_PROPERTIES = "properties";
private static final int MIN_WEBVIEW_RELEASE = 133;

Expand Down Expand Up @@ -130,6 +131,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private String mEncryptedDocumentPassword;
private List<CharSequence> mDocumentProperties;
private InputStream mInputStream;
private boolean isVerticalScrollMode = false;

private PdfviewerBinding binding;
private TextView mTextView;
Expand Down Expand Up @@ -265,6 +267,22 @@ public void onLoaded() {
public String getPassword() {
return mEncryptedDocumentPassword != null ? mEncryptedDocumentPassword : "";
}

@JavascriptInterface
public boolean isVerticalScrollMode() {
return isVerticalScrollMode;
}

@JavascriptInterface
public void onPageChanged(final int pageNumber) {
if (isVerticalScrollMode) {
mPage = pageNumber;
runOnUiThread(() -> {
invalidateOptionsMenu();
showPageNumber();
});
}
}
}

private void showWebViewCrashed() {
Expand Down Expand Up @@ -502,6 +520,7 @@ public void onZoomEnd() {
mZoomRatio = savedInstanceState.getFloat(STATE_ZOOM_RATIO);
mDocumentOrientationDegrees = savedInstanceState.getInt(STATE_DOCUMENT_ORIENTATION_DEGREES);
mEncryptedDocumentPassword = savedInstanceState.getString(STATE_ENCRYPTED_DOCUMENT_PASSWORD);
isVerticalScrollMode = savedInstanceState.getBoolean(STATE_VERTICAL_SCROLL_MODE, false);
}

binding.webviewAlertReload.setOnClickListener(v -> {
Expand Down Expand Up @@ -675,12 +694,22 @@ private static void enableDisableMenuItem(MenuItem item, boolean enable) {
public void onJumpToPageInDocument(final int selected_page) {
if (selected_page >= 1 && selected_page <= mNumPages && mPage != selected_page) {
mPage = selected_page;
renderPage(0);
if (isVerticalScrollMode) {
binding.webview.evaluateJavascript("scrollToPageInDocument(" + selected_page + ")", null);
} else {
renderPage(0);
}
showPageNumber();
invalidateOptionsMenu();
}
}

private void toggleVerticalScrollMode() {
isVerticalScrollMode = !isVerticalScrollMode;
binding.webview.evaluateJavascript("setVerticalScrollMode(" + isVerticalScrollMode + ")", null);
invalidateOptionsMenu();
}

private void showSystemUi() {
ViewKt.showSystemUi(binding.getRoot(), getWindow());
getSupportActionBar().show();
Expand All @@ -700,6 +729,7 @@ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
savedInstanceState.putFloat(STATE_ZOOM_RATIO, mZoomRatio);
savedInstanceState.putInt(STATE_DOCUMENT_ORIENTATION_DEGREES, mDocumentOrientationDegrees);
savedInstanceState.putString(STATE_ENCRYPTED_DOCUMENT_PASSWORD, mEncryptedDocumentPassword);
savedInstanceState.putBoolean(STATE_VERTICAL_SCROLL_MODE, isVerticalScrollMode);
}

private void showPageNumber() {
Expand Down Expand Up @@ -731,7 +761,7 @@ public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
R.id.action_next, R.id.action_previous, R.id.action_first, R.id.action_last,
R.id.action_rotate_clockwise, R.id.action_rotate_counterclockwise,
R.id.action_view_document_properties, R.id.action_share, R.id.action_save_as,
R.id.action_outline));
R.id.action_outline, R.id.action_toggle_vertical_scroll));
if (BuildConfig.DEBUG) {
ids.add(R.id.debug_action_toggle_text_layer_visibility);
ids.add(R.id.debug_action_crash_webview);
Expand All @@ -757,8 +787,13 @@ public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
enableDisableMenuItem(menu.findItem(R.id.action_open),
!webViewCrashed && getWebViewRelease() >= MIN_WEBVIEW_RELEASE);
enableDisableMenuItem(menu.findItem(R.id.action_share), mUri != null);
enableDisableMenuItem(menu.findItem(R.id.action_next), mPage < mNumPages);
enableDisableMenuItem(menu.findItem(R.id.action_previous), mPage > 1);

// In vertical scroll mode, disable page navigation buttons
enableDisableMenuItem(menu.findItem(R.id.action_next), !isVerticalScrollMode && mPage < mNumPages);
enableDisableMenuItem(menu.findItem(R.id.action_previous), !isVerticalScrollMode && mPage > 1);
enableDisableMenuItem(menu.findItem(R.id.action_first), !isVerticalScrollMode);
enableDisableMenuItem(menu.findItem(R.id.action_last), !isVerticalScrollMode);

enableDisableMenuItem(menu.findItem(R.id.action_save_as), mUri != null);
enableDisableMenuItem(menu.findItem(R.id.action_view_document_properties),
mDocumentProperties != null);
Expand Down Expand Up @@ -822,6 +857,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
} else if (itemId == R.id.action_save_as) {
saveDocument();
} else if (itemId == R.id.action_toggle_vertical_scroll) {
toggleVerticalScrollMode();
return true;
} else if (itemId == R.id.debug_action_toggle_text_layer_visibility) {
binding.webview.evaluateJavascript("toggleTextLayerVisibility()", null);
return true;
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_view_agenda_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M20,13H3c-0.55,0 -1,0.45 -1,1v6c0,0.55 0.45,1 1,1h17c0.55,0 1,-0.45 1,-1v-6c0,-0.55 -0.45,-1 -1,-1zM20,3H3c-0.55,0 -1,0.45 -1,1v6c0,0.55 0.45,1 1,1h17c0.55,0 1,-0.45 1,-1V4c0,-0.55 -0.45,-1 -1,-1z"/>
</vector>
6 changes: 6 additions & 0 deletions app/src/main/res/menu/pdf_viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
android:title="@string/action_jump_to_page"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_toggle_vertical_scroll"
android:icon="@drawable/ic_view_agenda_24dp"
android:title="@string/action_toggle_vertical_scroll"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_rotate_clockwise"
android:icon="@drawable/ic_rotate_right_24dp"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<string name="action_outline">Outline</string>
<string name="action_view_document_properties">Properties</string>
<string name="action_close">Close</string>
<string name="action_toggle_vertical_scroll">Vertical scroll</string>

<string name="outline_child_button_description">View nested outline entries</string>
<string name="outline_not_available">No outline available</string>
Expand Down
Loading