Skip to content

Commit

Permalink
add basic support to share documents via an additional item in option…
Browse files Browse the repository at this point in the history
…s menu, add share icon

Signed-off-by: emschu <emschu@mailbox.org>
  • Loading branch information
emschu authored and thestinger committed Mar 20, 2022
1 parent cb4a0e8 commit 0c21e13
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
18 changes: 17 additions & 1 deletion app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,18 @@ private void openDocument() {
startActivityForResult(intent, ACTION_OPEN_DOCUMENT_REQUEST_CODE);
}

private void shareDocument() {
if (mUri != null) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setDataAndTypeAndNormalize(mUri, "application/pdf");
shareIntent.putExtra(Intent.EXTRA_STREAM, mUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.action_share)));
} else {
Log.w(TAG, "Cannot share unexpected null URI");
}
}

private void zoomIn(float value, boolean end) {
if (mZoomRatio < MAX_ZOOM_RATIO) {
mZoomRatio = Math.min(mZoomRatio + value, MAX_ZOOM_RATIO);
Expand Down Expand Up @@ -506,7 +518,7 @@ public boolean onPrepareOptionsMenu(Menu menu) {
final int[] ids = { R.id.action_zoom_in, R.id.action_zoom_out, R.id.action_jump_to_page,
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_view_document_properties, R.id.action_share };
if (mDocumentState < STATE_LOADED) {
for (final int id : ids) {
final MenuItem item = menu.findItem(id);
Expand All @@ -527,6 +539,7 @@ public boolean onPrepareOptionsMenu(Menu menu) {
enableDisableMenuItem(menu.findItem(R.id.action_open), getWebViewRelease() >= MIN_WEBVIEW_RELEASE);
enableDisableMenuItem(menu.findItem(R.id.action_zoom_in), mZoomRatio != MAX_ZOOM_RATIO);
enableDisableMenuItem(menu.findItem(R.id.action_zoom_out), mZoomRatio != MIN_ZOOM_RATIO);
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);

Expand Down Expand Up @@ -572,6 +585,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
new JumpToPageFragment()
.show(getSupportFragmentManager(), JumpToPageFragment.TAG);
return true;
} else if (itemId == R.id.action_share) {
shareDocument();
return true;
}

return super.onOptionsItemSelected(item);
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_share_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/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>
</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 @@ -37,6 +37,12 @@
android:title="@string/action_zoom_in"
app:showAsAction="ifRoom" />

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

<item
android:id="@+id/action_first"
android:icon="@drawable/ic_first_page_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 @@ -23,4 +23,5 @@

<string name="webview_out_of_date_title">WebView out-of-date</string>
<string name="webview_out_of_date_message">Your current WebView version is %d. The WebView should be at least version %d for the PDF Viewer to work.</string>
<string name="action_share">Share</string>
</resources>

0 comments on commit 0c21e13

Please sign in to comment.