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: PdfCraft no longer keeps a copy of every PDF it opens (#42, #50)
Josh737795e665e8
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 9f7a842..eff66e1 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@@ -63,6 +63,32 @@ class DocumentFiles(private val context: Context) {?: column(MediaStore.MediaColumns.DATE_MODIFIED)?.takeIf { it > 0 }?.times(1000)}+ /**+ * Throw away the working copy of [uri], its document closed, when the app keeps access to+ * [uri]: it is copied in again the next time it opens. A file another app lent for one visit+ * keeps its copy, which is what Recent opens once Android takes that access back.+ */+ fun dropCopyIfReachable(uri: Uri) {+ if (uri.scheme != ContentResolver.SCHEME_CONTENT) return+ if (resolver.persistedUriPermissions.none { it.uri == uri && it.isReadPermission }) return+ File(workDir, sha(uri.toString())).deleteRecursively()+ }++ /**+ * Remove the working copies no longer needed: those of documents that left [recent], and of+ * those in it whose original the app can read again. The [open] document's copy stays, and so+ * does one written in the last few minutes (a document being opened right now).+ */+ fun pruneCopies(recent: Collection<String>, open: String?) {+ val readable = resolver.persistedUriPermissions.filter { it.isReadPermission }.mapTo(HashSet()) { it.uri.toString() }+ val wanted = recent.filter { it !in readable }.mapTo(HashSet()) { sha(it) }+ open?.let { wanted += sha(it) }+ val settled = System.currentTimeMillis() - 10 * 60 * 1000L+ workDir.listFiles()?.forEach { dir ->+ if (dir.name !in wanted && dir.lastModified() < settled) dir.deleteRecursively()+ }+ }+/** The working copy made earlier for [uri], if there is one (for thumbnails). */fun existingCopy(uri: Uri): File? =File(workDir, sha(uri.toString())).listFiles()?.firstOrNull { it.isFile && !it.name.startsWith(".") }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 58e6b74..979dea4 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@@ -120,7 +120,15 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr}fun refreshRecents() {- viewModelScope.launch { recentList = withContext(Dispatchers.IO) { recents.list() } }+ viewModelScope.launch {+ val open = document?.source?.toString()+ recentList = withContext(Dispatchers.IO) {+ recents.list().also { list ->+ // copies of documents that left Recent go too (#42)+ runCatching { files.pruneCopies(list.map { it.uri }, open) }+ }+ }+ }}/** Open the PDF at [uri] (picked, shared, or from Recent), asking for its password if it has one. */@@ -138,10 +146,18 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr}openFile(eng, uri, file, password)} catch (e: java.io.FileNotFoundException) {- // moved or deleted since it was opened: it leaves Recent- withContext(Dispatchers.IO) { recents.remove(setOf(uri.toString())) }- refreshRecents()- tell(text(R.string.msg_file_moved))+ // Moved or deleted since it was opened, or a link a file manager lent that a+ // restart ended (#50; its provider no longer finds it): PdfCraft's own copy opens+ // if it kept one, as below; otherwise it leaves Recent.+ val copy = withContext(Dispatchers.IO) { files.existingCopy(uri) }+ if (copy != null) {+ openFile(eng, uri, copy, password)+ tell(text(R.string.msg_opened_own_copy))+ } else {+ withContext(Dispatchers.IO) { recents.remove(setOf(uri.toString())) }+ refreshRecents()+ tell(text(R.string.msg_file_moved))+ }} catch (e: SecurityException) {// A file another app lent for one visit (Files, Downloads: "Open with"): Android// takes that access back later, so Recent can't read it again. PdfCraft's own copy@@ -303,6 +319,8 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : AndrrunCatching { eng.command { close(doc.id, true) } }doc.images.close()doc.search.clear()+ // the working copy isn't kept when the original can be read again (#42)+ doc.source?.let { src -> withContext(Dispatchers.IO) { runCatching { files.dropCopyIfReachable(src) } } }}/** The app went to the background: save the open document, as the reference does. */