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: changes to a file that can't be written are offered as a copy instead of lost
Josh25d6017b095b
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 43a175a..4f450a0 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@@ -94,10 +94,18 @@ class DocumentFiles(private val context: Context) {/** Whether the app may write to [uri] (a document the user picked with write access). */fun canWrite(uri: Uri): Boolean {if (uri.scheme == ContentResolver.SCHEME_FILE) return uri.path?.let { File(it).canWrite() } == true+ // a provider that says its document can't be written refuses whatever access the picker+ // granted (Android's media documents, which the picker's Recent shows: "Media is read-only")+ if (DocumentsContract.isDocumentUri(context, uri) && documentFlags(uri)?.let { it and DocumentsContract.Document.FLAG_SUPPORTS_WRITE == 0 } == true) return falseval granted = resolver.persistedUriPermissions.any { it.uri == uri && it.isWritePermission }return granted || runCatching { resolver.openFileDescriptor(uri, "wa")?.close(); true }.getOrDefault(false)}+ /** A document's flags (DocumentsContract.Document.FLAG_*), when its provider says. */+ private fun documentFlags(uri: Uri): Int? = runCatching {+ resolver.query(uri, arrayOf(DocumentsContract.Document.COLUMN_FLAGS), null, null, null)?.use { c -> if (c.moveToFirst() && !c.isNull(0)) c.getInt(0) else null }+ }.getOrNull()+/** Write [file] over [uri] (truncating it). */fun writeBack(file: File, uri: Uri) {val out = resolver.openOutputStream(uri, "wt") ?: error("The file couldn't be written.")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 32ebe09..28e43ff 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@@ -216,7 +216,8 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {val eng = engine ?: returnif (doc.controller.tool != ViewerTool.None) leaveTool(doc)viewModelScope.launch {- if (doc.info.dirty && !doc.writable) {+ // changes the file's place can't take (known read-only, or found so now) are offered as a copy+ if ((doc.info.dirty || doc.notWrittenBack) && (!doc.writable || !save(doc, quiet = true))) {readOnlyPrompt = docreturn@launch}@@ -291,26 +292,31 @@ class AppViewModel(app: Application) : AndroidViewModel(app) {/*** Save [doc]'s changes: into its working file (incrementally, so earlier signatures stay* valid), then back over the file it came from when the app may write there. Returns- * whether the changes reached the original.+ * whether the changes reached the original; [quiet] leaves a failure's message to the caller.*/- suspend fun save(doc: OpenDocument): Boolean {+ suspend fun save(doc: OpenDocument, quiet: Boolean = false): Boolean {val eng = engine ?: return false- if (!doc.info.dirty) return true+ if (!doc.info.dirty && !doc.notWrittenBack) return truereturn try {- eng.tool("doc_save", mapOf("doc" to doc.id))+ if (doc.info.dirty) eng.tool("doc_save", mapOf("doc" to doc.id))refresh(doc)val src = doc.sourceif (src != null && src.scheme == "content") {- if (!doc.writable) return false+ if (!doc.writable) {+ doc.notWrittenBack = true+ return false+ }withContext(Dispatchers.IO) { files.writeBack(doc.file, src) }} else if (src != null && src.scheme == "file") {val target = File(requireNotNull(src.path))if (target.canonicalPath != doc.file.canonicalPath) withContext(Dispatchers.IO) { doc.file.copyTo(target, overwrite = true) }}+ doc.notWrittenBack = falsetrue} catch (e: Exception) {doc.writable = false- tell(e.userMessage)+ doc.notWrittenBack = true+ if (!quiet) tell(e.userMessage)false}}diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/OpenDocument.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/OpenDocument.ktindex fe31871..5934a5f 100644--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/OpenDocument.kt+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/ui/OpenDocument.kt@@ -49,6 +49,9 @@ class OpenDocument(/** Whether saving can write back to [source] (else changes need Save a copy). */var writable by mutableStateOf(true)+ /** Changes saved into [file] that [source] doesn't have (it couldn't be written): closing offers a copy. */+ var notWrittenBack = false+/** The links of each page (0-based), refreshed when the document changes. */var links: Map<Int, List<io.github.artcraftandroid.pdfcraft.core.LinkInfo>> = emptyMap()