Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

fix: closing a PDF another app opened goes back to that app (#47)

Joshf2912921f3a0
diff --git a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/MainActivity.kt b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/MainActivity.kt
index cc34b3f..cdd1c1e 100644
--- a/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/MainActivity.kt
+++ b/apps/pdfcraft-android/android/app/src/main/java/io/github/artcraftandroid/pdfcraft/MainActivity.kt
@@ -76,6 +76,8 @@ class MainActivity : ComponentActivity() {
/** The view model's requests that need the activity: other apps, pickers, the camera, printing. */
private fun collectRequests() {
lifecycleScope.launch { for (i in vm.intents) launchSafely { startActivity(i) } }
+ // a document another app opened was closed: back to that app, not Home (#47)
+ lifecycleScope.launch { for (u in vm.leave) finish() }
lifecycleScope.launch { for ((file, name) in vm.prints) print(file, name) }
lifecycleScope.launch { for (name in vm.saveCopyRequests) launchSafely { saveCopy.launch(name) } }
lifecycleScope.launch {
@@ -164,7 +166,7 @@ class MainActivity : ComponentActivity() {
Intent.ACTION_SEND_MULTIPLE -> IntentCompat.getParcelableArrayListExtra(intent, Intent.EXTRA_STREAM, Uri::class.java)?.firstOrNull()
else -> null
}
- uri?.let(vm::open)
+ uri?.let { vm.open(it, fromOtherApp = true) }
}
/** Hand [file] to Android's print framework. */
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.kt
index 979dea4..4dec64f 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
@@ -131,8 +131,19 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
}
}
- /** Open the PDF at [uri] (picked, shared, or from Recent), asking for its password if it has one. */
- fun open(uri: Uri, password: String? = null) {
+ /** The document another app is opening in PdfCraft, until it opens. */
+ private var otherAppUri: Uri? = null
+
+ /** Fires when a document another app opened is closed: the activity finishes, back to that app. */
+ val leave = Channel<Unit>(Channel.CONFLATED)
+
+ /**
+ * Open the PDF at [uri] (picked, shared, or from Recent), asking for its password if it has one.
+ * [fromOtherApp]: another app opened it, so closing it goes back to that app rather than Home,
+ * as the reference does (#47).
+ */
+ fun open(uri: Uri, password: String? = null, fromOtherApp: Boolean = false) {
+ otherAppUri = uri.takeIf { fromOtherApp }
// a file opened from another app comes to the front, not under Settings
settingsOpen = false
showLicences = false
@@ -206,6 +217,7 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
val images = PageImages(eng, info.id, viewModelScope, imageBudget)
val source = uri.takeIf { it.scheme == "content" || it.scheme == "file" }
val doc = OpenDocument(info.id, info, source, file, images, password, eng, viewModelScope)
+ doc.fromOtherApp = otherAppUri == uri
val (recent, writable) = withContext(Dispatchers.IO) {
recents.list().firstOrNull { it.uri == uri.toString() } to (source == null || files.canWrite(source))
}
@@ -264,6 +276,7 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
saveAndClose(eng, doc)
readingMode = false
refreshRecents()
+ if (doc.fromOtherApp) leave.trySend(Unit)
}
}
@@ -279,6 +292,7 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
viewModelScope.launch {
closeQuietly(eng, doc)
refreshRecents()
+ if (doc.fromOtherApp) leave.trySend(Unit)
}
})
false -> {
@@ -286,6 +300,7 @@ class AppViewModel(app: Application, private val saved: SavedStateHandle) : Andr
viewModelScope.launch {
closeQuietly(eng, doc)
refreshRecents()
+ if (doc.fromOtherApp) leave.trySend(Unit)
}
}
}
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.kt
index 9cc8a63..4abf943 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
@@ -46,6 +46,9 @@ class OpenDocument(
/** A link's area flashing after it was tapped. */
var linkFlash by mutableStateOf<LinkFlash?>(null)
+ /** Opened by another app (a file manager's Open with, a share): closing it goes back there. */
+ var fromOtherApp = false
+
/** Whether saving can write back to [source] (else changes need Save a copy). */
var writable by mutableStateOf(true)