Open the saved sign-in while the first screen draws (from release 0.108.51)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-06 11:03:46 +00:00
co-authored by Claude Opus 5.5
parent edeb8e4490
commit c4efe3d16d
2 changed files with 23 additions and 4 deletions
@@ -58,6 +58,8 @@ class MainActivity : ComponentActivity() {
}
// Before anything is drawn, so a locked After Dark never shows for a moment.
AfterDarkLock.load(this)
// Opening the saved sign-in takes seconds on the box: start it now, while the first screen is drawn.
runCatching { Workspaces.current(this).connection.warm() }
// The phone page's server runs only while Orbit is open (Create your own from a phone).
PhoneLink.init(this)
Profiles.load(this)
@@ -38,7 +38,7 @@ internal class RememberedConnectionStore(path: File, private val alias: String,
/** The open server's store. */
fun get(context: Context): RememberedConnectionStore = Workspaces.current(context).connection
}
@Synchronized fun invalidate() { generation++ }
@Synchronized fun invalidate() { generation++; lastRead = null }
private val file = AtomicFile(path)
private var generation = 0L
@Synchronized fun ticket() = generation
@@ -52,13 +52,28 @@ internal class RememberedConnectionStore(path: File, private val alias: String,
.setKeySize(256).setRandomizedEncryptionRequired(true).build())
}.generateKey()
}
/** The last read, kept while the file and the generation stay as they were: opening it on the box takes seconds. */
private var lastRead: Triple<Long, Pair<Long, Long>, RememberedState>? = null
@Synchronized fun load(): RememberedState? {
if (!file.baseFile.exists() && !File(file.baseFile.path + ".bak").exists()) return null
val stamp = file.baseFile.lastModified() to file.baseFile.length()
lastRead?.let { (seen, at, state) -> if (seen == generation && at == stamp) return state }
val started = System.currentTimeMillis()
var keyMs = 0L; var openMs = 0L
return file.openRead().use { input ->
val bytes = input.readBytesLimited(RememberedStateCodec.MAX_BYTES + 29)
RememberedStateCodec.decode(ConnectionCipher.open(key(false), bytes))
}.also { require(it.origin == origin) { "A saved sign-in belongs to another server." } }
val key = key(false).also { keyMs = System.currentTimeMillis() - started }
val plain = ConnectionCipher.open(key, bytes).also { openMs = System.currentTimeMillis() - started - keyMs }
RememberedStateCodec.decode(plain)
}.also {
require(it.origin == origin) { "A saved sign-in belongs to another server." }
lastRead = Triple(generation, stamp, it)
// Times only, to see where opening spends them.
runCatching { android.util.Log.i("OrbitStartup", "saved sign-in keyMs=$keyMs openMs=$openMs decodeMs=${System.currentTimeMillis() - started - keyMs - openMs}") }
}
}
/** Opens the saved sign-in in the background as Orbit starts, so it's ready by the time the screen asks for it. */
fun warm() { Thread({ runCatching { load() } }, "orbit-warm-signin").apply { isDaemon = true }.start() }
@Synchronized fun save(state: RememberedState, ticket: Long): Boolean {
if (ticket != generation) return false
require(state.origin == origin) { "A sign-in can only be saved with its own server." }
@@ -66,6 +81,7 @@ internal class RememberedConnectionStore(path: File, private val alias: String,
// Validate the same bounded schema before committing it.
RememberedStateCodec.decode(plain)
val encrypted = ConnectionCipher.seal(key(true), plain)
lastRead = null
val output = file.startWrite()
try { output.write(encrypted); file.finishWrite(output) }
catch (failure: Exception) { file.failWrite(output); throw failure }
@@ -79,13 +95,14 @@ internal class RememberedConnectionStore(path: File, private val alias: String,
saved.matches, saved.approved, saved.episodes, saved.orbitOne, saved.toonami, saved.selectedNumber, saved.afterDark, saved.darkMovies,
saved.approvedDark, saved.extra, saved.origin))
val encrypted = ConnectionCipher.seal(key(true), plain)
lastRead = null
val output = file.startWrite()
try { output.write(encrypted); file.finishWrite(output) }
catch (failure: Exception) { file.failWrite(output); throw failure }
return true
}
@Synchronized fun clear() {
generation++
generation++; lastRead = null
file.delete()
KeyStore.getInstance("AndroidKeyStore").apply { load(null); if (containsAlias(alias)) deleteEntry(alias) }
}