Files
LLeMbas-Android/app/src/main/java/eu/llembas/android/InstanceGate.kt
T
HomerandClaude Fable 5.2 a249b516fd
CI / check (push) Canceled after 0s
A review of the file manager: nothing new is sent behind the lock, a share's addresses are not believed
1. One gate (InstanceGate) before every request that carries the account's
   files: uploads, downloads, the copies for Share and Open with, the share
   sheet's sends, a viewer's and a player's copy. The request under way
   finishes; the next waits until the app is in front of an unlocked phone,
   and the dialog says it is waiting.
2. The share target takes only content: addresses of other apps' providers,
   and refuses one that opens onto this app's own files; each file is shown
   with its provider's name and size; the instance is asked nothing before
   the person has chosen.
3. New file reads the directory first and never asks for a name it holds,
   besides create: true.
4. A PDF's renderer is closed off the main thread, and one made while the
   viewer was closing is closed too; at most 100 MB and 2000 pages.
5. Shared cache copies go by their age (an hour), not at every start; a
   name is capped by UTF-8 bytes and keeps its extension.
6. A model name the server said as a model_ref is looked up as a ref alone
   in the picker, the assistant's sheet and the schedule form.
7. A second "Save to Files" goes on from the file that failed.
8. A notebook is parsed off the main thread; a download given up cancels
   its call.

Also: "Open in Files" asks where the node is now (GET /files/node), and an
archive's entries are listed in the viewer.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 07:38:26 +00:00

77 lines
2.8 KiB
Kotlin

package eu.llembas.android
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withTimeoutOrNull
import java.util.concurrent.atomic.AtomicInteger
/**
* Whether a new request that carries the account's files may start: only while the app is in
* front and the phone is unlocked. Work that goes on by itself -- a queue of uploads, the copies
* for a share, a player's copy -- asks [pass] before each request. A request already on its way
* is not touched, so what the person started finishes; the next one waits here, and goes on by
* itself when the app is in front of an unlocked phone again.
*
* The process has one ([LembasApp.gate]). It hears of the app's activities starting and stopping
* and of the screen going off and the phone being unlocked; [locked] is asked each time.
*/
class InstanceGate(private val locked: () -> Boolean) {
private val started = AtomicInteger(0)
private val state = MutableStateFlow(false)
/** Whether a request may start now, as it was last looked at. */
val open: StateFlow<Boolean> = state.asStateFlow()
fun activityStarted() {
started.incrementAndGet()
look()
}
fun activityStopped() {
started.updateAndGet { (it - 1).coerceAtLeast(0) }
look()
}
/** Looks again (the screen went off, the phone was unlocked) and says whether a request may start. */
fun look(): Boolean {
val now = started.get() > 0 && !locked()
state.value = now
return now
}
/**
* Returns when a request may start. [waiting] hears `true` when that is not now, and `false`
* when the wait is over (also when it is given up), so a screen can say what it is waiting
* for. While waiting the lock is looked at again every [AGAIN_MS]: an unlock nobody
* announced must not leave a queue standing.
*/
suspend fun pass(waiting: (Boolean) -> Unit = {}) {
if (look()) return
waiting(true)
try {
while (!look()) withTimeoutOrNull(AGAIN_MS) { state.first { it } }
} finally {
waiting(false)
}
}
companion object {
const val AGAIN_MS = 1_000L
}
}
/**
* [send] for each of [items] from the one at [from], each after its [turn] at the gate, and
* [done] hears how many are through after each. A failure leaves the count where it was, so
* the same call with that count takes the queue up at the one that failed, not at the first.
*/
suspend fun <T> sendFrom(items: List<T>, from: Int, turn: suspend () -> Unit, done: (Int) -> Unit, send: suspend (T) -> Unit) {
for (i in from.coerceAtLeast(0) until items.size) {
turn()
send(items[i])
done(i + 1)
}
}