added feature to save document

This commit is contained in:
Pratyush 2022-04-22 11:04:05 +05:30 committed by Daniel Micay
parent c1a1263bd5
commit eb6eb8046c
5 changed files with 93 additions and 2 deletions

View File

@ -0,0 +1,25 @@
package app.grapheneos.pdfviewer
import android.content.Context
import android.net.Uri
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
@Throws(FileNotFoundException::class, IOException::class)
fun saveAs(context: Context, existingUri: Uri, saveAs: Uri) {
context.asInputStream(existingUri)?.use { inputStream ->
context.asOutputStream(saveAs)?.use { outputStream ->
outputStream.write(inputStream.readBytes())
}
}
}
@Throws(FileNotFoundException::class)
private fun Context.asInputStream(uri: Uri): InputStream? = contentResolver.openInputStream(uri)
@Throws(FileNotFoundException::class)
private fun Context.asOutputStream(uri: Uri): OutputStream? = contentResolver.openOutputStream(uri)

View File

@ -132,6 +132,19 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
}
});
private final ActivityResultLauncher<Intent> saveAsLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), result -> {
if (result == null) return;
if (result.getResultCode() != RESULT_OK) return;
Intent resultData = result.getData();
if (resultData != null) {
Uri path = resultData.getData();
if (path != null) {
saveDocumentAs(path);
}
}
});
private class Channel {
@JavascriptInterface
public int getPage() {
@ -514,10 +527,10 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final int[] ids = { R.id.action_zoom_in, R.id.action_zoom_out, R.id.action_jump_to_page,
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_share };
R.id.action_view_document_properties, R.id.action_share, R.id.action_save_as};
if (mDocumentState < STATE_LOADED) {
for (final int id : ids) {
final MenuItem item = menu.findItem(id);
@ -541,6 +554,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
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);
enableDisableMenuItem(menu.findItem(R.id.action_save_as), mUri != null);
return true;
}
@ -587,8 +601,42 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
} else if (itemId == R.id.action_share) {
shareDocument();
return true;
} else if (itemId == R.id.action_save_as) {
saveDocument();
}
return super.onOptionsItemSelected(item);
}
private void saveDocument() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_TITLE, getCurrentDocumentName());
saveAsLauncher.launch(intent);
}
private String getCurrentDocumentName() {
if (mDocumentProperties == null || mDocumentProperties.isEmpty()) return "";
String fileName = "";
String title = "";
for (CharSequence property : mDocumentProperties) {
if (property.toString().startsWith("File name:")) {
fileName = property.toString().replace("File name:", "");
}
if (property.toString().startsWith("Title:-")) {
title = property.toString().replace("Title:-", "");
}
}
return fileName.length() > 2 ? fileName : title;
}
private void saveDocumentAs(Uri uri) {
try {
KtUtilsKt.saveAs(this, mUri, uri);
} catch (IOException e) {
e.printStackTrace();
snackbar.setText(R.string.error_while_saving).show();
}
}
}

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="M17,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,7l-4,-4zM19,19L5,19L5,5h11.17L19,7.83L19,19zM12,12c-1.66,0 -3,1.34 -3,3s1.34,3 3,3 3,-1.34 3,-3 -1.34,-3 -3,-3zM6,6h9v4L6,10z"/>
</vector>

View File

@ -73,6 +73,12 @@
android:title="@string/action_rotate_counterclockwise"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_save_as"
android:icon="@drawable/ic_save_24dp"
android:title="@string/action_save_as"
app:showAsAction="never" />
<item
android:id="@+id/action_view_document_properties"
android:title="@string/action_view_document_properties"

View File

@ -20,8 +20,10 @@
<string name="invalid_mime_type">Cannot open file with invalid MIME type</string>
<string name="legacy_file_uri">Cannot open legacy file paths from insecure apps</string>
<string name="io_error">Received I/O error trying to open content</string>
<string name="error_while_saving">Error encountered while saving</string>
<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>
<string name="action_save_as">Save as</string>
</resources>