Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.
Rust androiddocument-scannerdocximage-editorjetpack-composekotlinliquid-glassofflineopen-sourcepdfpdf-compressorpdf-editorpdf-mergerpdf-readerpdf-splitterpdf-toolspdf-viewerpptxspreadsheet-editorxlsx
fix: share on a full phone says so instead of closing the app (#19)
Joshd644fc507761
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/engine/DocumentFiles.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/engine/DocumentFiles.ktindex 7a97b60..9f7a842 100644--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/engine/DocumentFiles.kt+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/engine/DocumentFiles.kt@@ -10,6 +10,7 @@ import android.provider.OpenableColumnsimport androidx.core.content.FileProviderimport io.github.artcraftandroid.pdfcraft.Rimport java.io.File+import java.io.IOExceptionimport java.security.MessageDigest/**@@ -121,22 +122,27 @@ class DocumentFiles(private val context: Context) {}/** A copy of [file] under [name] that other apps may read, and its URI. */- fun shareable(file: File, name: String = file.name): Uri {- shareDir.listFiles()?.forEach { it.deleteRecursively() }- val dir = File(shareDir, System.nanoTime().toString()).apply { mkdirs() }- val copy = File(dir, safeName(name))- file.copyTo(copy, overwrite = true)- return FileProvider.getUriForFile(context, "${context.packageName}.files", copy)- }+ fun shareable(file: File, name: String = file.name): Uri = shareableAll(listOf(file to safeName(name))).first()- /** Several files to hand to another app at once (pictures of pages), each under its own name. */+ /**+ * Copies of [files] that other apps may read (a document, pictures of pages), each under its+ * own name, and their URIs. The last ones handed out are cleared first. When the copies can't+ * be made (a full phone has no room for them), the error says so in words.+ */fun shareableAll(files: List<Pair<File, String>>): List<Uri> {shareDir.listFiles()?.forEach { it.deleteRecursively() }- val dir = File(shareDir, System.nanoTime().toString()).apply { mkdirs() }- return files.map { (file, name) ->- val copy = File(dir, name.replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_").ifBlank { file.name })- file.copyTo(copy, overwrite = true)- FileProvider.getUriForFile(context, "${context.packageName}.files", copy)+ val dir = File(shareDir, System.nanoTime().toString())+ try {+ if (!dir.mkdirs()) throw IOException("can't make $dir")+ return files.map { (file, name) ->+ val copy = File(dir, name.replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_").ifBlank { file.name })+ file.copyTo(copy, overwrite = true)+ FileProvider.getUriForFile(context, "${context.packageName}.files", copy)+ }+ } catch (e: IOException) {+ dir.deleteRecursively()+ val full = shareDir.usableSpace < files.sumOf { it.first.length() } + 1024 * 1024+ throw IOException(context.getString(if (full) R.string.files_error_share_no_space else R.string.files_error_share), e)}}diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.ktindex 9539e07..980c701 100644--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/AppViewModel.kt@@ -16,6 +16,7 @@ import io.github.artcraftandroid.pdfcraft.ui.viewer.TextPromptimport io.github.artcraftandroid.pdfcraft.ui.viewer.ToolPrefsimport io.github.artcraftandroid.pdfcraft.ui.viewer.engineTypeimport io.github.artcraftandroid.pdfcraft.ui.viewer.hex+import android.content.ClipDataimport android.content.Intentimport androidx.compose.runtime.snapshotFlowimport io.github.artcraftandroid.pdfcraft.ui.viewer.LinkFlash@@ -492,20 +493,43 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {}}- /** Share a copy of [doc] through the system's share sheet (saved first). */+ /** Share a copy of [doc] through the system's share sheet (saved first, with the ink being drawn). */fun share(doc: OpenDocument) {- viewModelScope.launch {+ if (doc.controller.tool == ViewerTool.Draw) doc.commitTool?.invoke()+ handOut {+ // the ink is queued in the engine: reading the document back waits for it, so the save has it+ refresh(doc)save(doc)val uri = withContext(Dispatchers.IO) { files.shareable(doc.file, doc.name) }- val send = Intent(Intent.ACTION_SEND)- .setType("application/pdf")- .putExtra(Intent.EXTRA_STREAM, uri)- .putExtra(Intent.EXTRA_SUBJECT, text(R.string.msg_share_subject, doc.title))- .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)- intents.trySend(Intent.createChooser(send, null))+ sendIntent(listOf(uri)).putExtra(Intent.EXTRA_SUBJECT, text(R.string.msg_share_subject, doc.title))+ }+ }++ /**+ * Hand files to another app through the system's chooser: [make] copies them where other apps+ * may read them and returns the intent. A failure on the way, such as a file gone or a phone+ * too full for the copy, is told in a snackbar instead of closing the app.+ */+ private fun handOut(make: suspend () -> Intent) {+ viewModelScope.launch {+ val intent = try {+ make()+ } catch (e: Exception) {+ tell(e.userMessage)+ null+ } ?: return@launch+ intents.trySend(Intent.createChooser(intent, null))}}+ /** A send intent for [uris], with them as its clip too so the share sheet can show them. */+ private fun sendIntent(uris: List<Uri>, mime: String = "application/pdf"): Intent {+ val send = if (uris.size == 1) Intent(Intent.ACTION_SEND).putExtra(Intent.EXTRA_STREAM, uris.first())+ else Intent(Intent.ACTION_SEND_MULTIPLE).putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(uris))+ send.clipData = ClipData(null, arrayOf(mime), ClipData.Item(uris.first())).apply { uris.drop(1).forEach { addItem(ClipData.Item(it)) } }+ return send.setType(mime).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)+ }+/*** Keys in the viewer, as the reference takes them: N or Page Down for the next page, P or Page* Up for the previous one, the arrows to scroll, Ctrl+P to print, Ctrl and + or − to zoom.@@ -2000,14 +2024,12 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {organizing = falsedoc.controller.pagesOpen = falseif (share) {- val uris = withContext(Dispatchers.IO) { files.shareableAll(made) }- made.forEach { it.first.delete() }- val send = if (uris.size == 1) {- Intent(Intent.ACTION_SEND).setType(mime).putExtra(Intent.EXTRA_STREAM, uris.first())- } else {- Intent(Intent.ACTION_SEND_MULTIPLE).setType(mime).putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(uris))+ val uris = try {+ withContext(Dispatchers.IO) { files.shareableAll(made) }+ } finally {+ made.forEach { it.first.delete() }}- intents.trySend(Intent.createChooser(send.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), null))+ intents.trySend(Intent.createChooser(sendIntent(uris, mime), null))} else {withContext(Dispatchers.IO) {for ((f, name) in made) {@@ -2368,13 +2390,10 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {/** Several PDFs to the share sheet at once. */fun shareFiles(rows: List<io.github.artcraftandroid.pdfcraft.ui.home.FileRow>) {- viewModelScope.launch {+ handOut {val local = rows.mapNotNull { r -> localFileOf(r)?.let { it to r.name } }- if (local.isEmpty()) return@launch tell(text(R.string.msg_files_unreadable))- val uris = withContext(Dispatchers.IO) { files.shareableAll(local) }- val send = if (uris.size == 1) Intent(Intent.ACTION_SEND).putExtra(Intent.EXTRA_STREAM, uris.first())- else Intent(Intent.ACTION_SEND_MULTIPLE).putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(uris))- intents.trySend(Intent.createChooser(send.setType("application/pdf").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), null))+ if (local.isEmpty()) error(text(R.string.msg_files_unreadable))+ sendIntent(withContext(Dispatchers.IO) { files.shareableAll(local) })}}@@ -2393,10 +2412,9 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {}fun shareFile(row: io.github.artcraftandroid.pdfcraft.ui.home.FileRow) {- viewModelScope.launch {- val f = localFileOf(row) ?: return@launch tell(text(R.string.common_file_unreadable))- val uri = withContext(Dispatchers.IO) { files.shareable(f, row.name) }- intents.trySend(Intent.createChooser(Intent(Intent.ACTION_SEND).setType("application/pdf").putExtra(Intent.EXTRA_STREAM, uri).addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), null))+ handOut {+ val f = localFileOf(row) ?: error(text(R.string.common_file_unreadable))+ sendIntent(listOf(withContext(Dispatchers.IO) { files.shareable(f, row.name) }))}}@@ -2408,10 +2426,10 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {}fun openWith(row: io.github.artcraftandroid.pdfcraft.ui.home.FileRow) {- viewModelScope.launch {- val f = localFileOf(row) ?: return@launch tell(text(R.string.common_file_unreadable))+ handOut {+ val f = localFileOf(row) ?: error(text(R.string.common_file_unreadable))val uri = withContext(Dispatchers.IO) { files.shareable(f, row.name) }- intents.trySend(Intent.createChooser(Intent(Intent.ACTION_VIEW).setDataAndType(uri, "application/pdf").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), null))+ Intent(Intent.ACTION_VIEW).setDataAndType(uri, "application/pdf").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)}}diff --git a/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml b/apps/pdfcraft-android/android/app/src/main/res/values/strings.xmlindex 7dc135c..94164aa 100644--- a/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml+++ b/apps/pdfcraft-android/android/app/src/main/res/values/strings.xml@@ -1124,6 +1124,8 @@<!-- Translator note: a file handed to the app has no location on the phone --><string name="files_error_no_path">The file has no path.</string><string name="files_error_write">The file couldn\'t be written.</string>+ <string name="files_error_share">The file couldn\'t be shared.</string>+ <string name="files_error_share_no_space">Not enough free storage to share. Free up some space and try again.</string><!-- Translator note: Downloads is the phone's Downloads folder --><string name="files_error_save_downloads">The file couldn\'t be saved to Downloads.</string><string name="files_error_save_image">The image couldn\'t be saved.</string>