add basic support to share documents via an additional item in options menu, add share icon

Signed-off-by: emschu <emschu@mailbox.org>
This commit is contained in:
emschu 2022-03-05 11:55:45 +01:00 committed by Daniel Micay
parent cb4a0e8684
commit 0c21e13459
4 changed files with 34 additions and 1 deletions

View File

@ -399,6 +399,18 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
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);
@ -506,7 +518,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
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);
@ -527,6 +539,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
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);
@ -572,6 +585,9 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
new JumpToPageFragment()
.show(getSupportFragmentManager(), JumpToPageFragment.TAG);
return true;
} else if (itemId == R.id.action_share) {
shareDocument();
return true;
}
return super.onOptionsItemSelected(item);

View File

@ -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>

View File

@ -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"

View File

@ -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>