Android: CheckedDownload shared by the engine's network and Maia's weights
The system download with SHA-256 check moves out of EngineFiles into a class for any such file; EngineFiles keeps its behaviour (same preferences key); MaiaFiles fetches the small model of the pinned revision on request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e33c4f3044
commit
a7c832b8c9
13 changed files with 341 additions and 248 deletions
|
|
@ -9,6 +9,7 @@ import android.provider.OpenableColumns
|
|||
import io.github.vrubelroman.chesspenguin.data.AppUpdates
|
||||
import io.github.vrubelroman.chesspenguin.data.DbEntry
|
||||
import io.github.vrubelroman.chesspenguin.data.EngineFiles
|
||||
import io.github.vrubelroman.chesspenguin.data.MaiaFiles
|
||||
import io.github.vrubelroman.chesspenguin.data.Library
|
||||
import io.github.vrubelroman.chesspenguin.data.OnlineDownloads
|
||||
import io.github.vrubelroman.chesspenguin.data.Settings
|
||||
|
|
@ -30,6 +31,8 @@ class ChessPenguinApp : Application() {
|
|||
val library by lazy { Library(this) }
|
||||
val settings by lazy { Settings(this) }
|
||||
val engineFiles by lazy { EngineFiles(this, scope) }
|
||||
/// Maia's weights, downloaded on request (android-play.md §31).
|
||||
val maiaFiles by lazy { MaiaFiles(this, scope) }
|
||||
/// Games from Lichess and Chess.com being downloaded (android-ui.md §23).
|
||||
val downloads by lazy { OnlineDownloads(library, scope) }
|
||||
/// The app's own updates (android-ui.md §25).
|
||||
|
|
|
|||
|
|
@ -0,0 +1,282 @@
|
|||
package io.github.vrubelroman.chesspenguin.data
|
||||
|
||||
import android.app.DownloadManager
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.Uri
|
||||
import android.os.storage.StorageManager
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.security.MessageDigest
|
||||
|
||||
/// Why a downloaded file is not there.
|
||||
enum class NetProblem { NoInternet, Interrupted, NoSpace, Corrupt }
|
||||
|
||||
sealed interface NetState {
|
||||
data object Missing : NetState
|
||||
data class Loading(val done: Long, val total: Long) : NetState
|
||||
data class Failed(val problem: NetProblem) : NetState
|
||||
data object Ready : NetState
|
||||
}
|
||||
|
||||
/// A file of known size and SHA-256 fetched on request into the app's storage: the
|
||||
/// engine's network, Maia's weights.
|
||||
data class FileSpec(
|
||||
/// Folder in the app's files, and the file's name.
|
||||
val dir: String,
|
||||
val name: String,
|
||||
val url: String,
|
||||
val size: Long,
|
||||
val sha256: String,
|
||||
/// The download's title in the system's notification.
|
||||
val title: String,
|
||||
/// The preferences that keep the system's download id (one file each).
|
||||
val prefs: String,
|
||||
)
|
||||
|
||||
/// The download is the system's (DownloadManager): it goes on with the app closed,
|
||||
/// waits for the network and resumes, and shows its progress in a notification. Its id
|
||||
/// is kept, so the app picks the download up again when it starts. The file is checked
|
||||
/// by its SHA-256 as it is moved into place.
|
||||
class CheckedDownload(private val context: Context, private val scope: CoroutineScope, private val spec: FileSpec) {
|
||||
private val dir = File(context.filesDir, spec.dir)
|
||||
val file = File(dir, spec.name)
|
||||
private val part = File(dir, spec.name + ".part")
|
||||
private val prefs = context.getSharedPreferences(spec.prefs, Context.MODE_PRIVATE)
|
||||
private val downloads get() = context.getSystemService(DownloadManager::class.java)
|
||||
|
||||
private val _state = MutableStateFlow<NetState>(NetState.Missing)
|
||||
val state: StateFlow<NetState> = _state.asStateFlow()
|
||||
private var job: Job? = null
|
||||
|
||||
init {
|
||||
// Files of other versions (kept before an update) are of no use to this one.
|
||||
dir.listFiles()?.filter { it.name != file.name && it.name != part.name }?.forEach { it.delete() }
|
||||
when {
|
||||
installed() -> _state.value = NetState.Ready
|
||||
downloadId() != NO_DOWNLOAD -> {
|
||||
_state.value = NetState.Loading(0, spec.size)
|
||||
watch(downloadId())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A file checked when it was written: the right size is enough later.
|
||||
fun installed() = file.length() == spec.size
|
||||
|
||||
/// True when the phone is on a metered (mobile) connection right now.
|
||||
fun metered(): Boolean {
|
||||
val cm = context.getSystemService(ConnectivityManager::class.java) ?: return false
|
||||
val caps = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false
|
||||
return !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
|
||||
}
|
||||
|
||||
/// Fetches the file from the internet.
|
||||
fun download() {
|
||||
if (_state.value == NetState.Ready || _state.value is NetState.Loading) return
|
||||
if (!online()) {
|
||||
_state.value = NetState.Failed(NetProblem.NoInternet)
|
||||
return
|
||||
}
|
||||
// The file is downloaded next to the app's files, then checked and moved in.
|
||||
if (freeSpace() < 2 * spec.size) {
|
||||
_state.value = NetState.Failed(NetProblem.NoSpace)
|
||||
return
|
||||
}
|
||||
target()?.delete()
|
||||
val request = DownloadManager.Request(Uri.parse(spec.url))
|
||||
.setTitle(spec.title)
|
||||
.setDestinationInExternalFilesDir(context, null, spec.name)
|
||||
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
|
||||
.setAllowedOverMetered(true)
|
||||
val id = try {
|
||||
downloads.enqueue(request)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
// No shared storage for the download (or the download service is off).
|
||||
_state.value = NetState.Failed(NetProblem.Interrupted)
|
||||
return
|
||||
}
|
||||
prefs.edit().putLong(KEY, id).apply()
|
||||
_state.value = NetState.Loading(0, spec.size)
|
||||
watch(id)
|
||||
}
|
||||
|
||||
/// Writes the file from `open()` (inside the APK) into place, checked, in the
|
||||
/// background, the progress shown as a download's.
|
||||
fun installFrom(open: () -> InputStream) {
|
||||
if (_state.value == NetState.Ready || _state.value is NetState.Loading) return
|
||||
_state.value = NetState.Loading(0, spec.size)
|
||||
job = scope.launch(Dispatchers.IO) {
|
||||
val result = try {
|
||||
open().use { install(it, progress = true) }
|
||||
NetState.Ready
|
||||
} catch (e: Failure) {
|
||||
NetState.Failed(e.problem)
|
||||
} catch (e: IOException) {
|
||||
NetState.Failed(if (freeSpace() < spec.size) NetProblem.NoSpace else NetProblem.Interrupted)
|
||||
}
|
||||
if (isActive) _state.value = result
|
||||
}
|
||||
}
|
||||
|
||||
/// The same, at once (the caller waits): false when it could not (no space).
|
||||
suspend fun installNow(open: () -> InputStream): Boolean = withContext(Dispatchers.IO) {
|
||||
if (installed()) return@withContext true
|
||||
if (freeSpace() < 2 * spec.size) return@withContext false
|
||||
try {
|
||||
open().use { install(it, progress = false) }
|
||||
_state.value = NetState.Ready
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
part.delete()
|
||||
if (!installed()) _state.value = NetState.Missing
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Stops a download and throws away what came.
|
||||
fun cancel() {
|
||||
job?.cancel()
|
||||
val id = downloadId()
|
||||
if (id != NO_DOWNLOAD) downloads.remove(id)
|
||||
forget()
|
||||
part.delete()
|
||||
if (!installed()) _state.value = NetState.Missing
|
||||
}
|
||||
|
||||
/// Removes the file.
|
||||
fun delete() {
|
||||
cancel()
|
||||
file.delete()
|
||||
_state.value = NetState.Missing
|
||||
}
|
||||
|
||||
private class Failure(val problem: NetProblem) : Exception()
|
||||
|
||||
private fun downloadId(): Long = prefs.getLong(KEY, NO_DOWNLOAD)
|
||||
|
||||
private fun forget() = prefs.edit().remove(KEY).apply()
|
||||
|
||||
/// Where the system puts the downloaded file (the app's own external folder).
|
||||
private fun target(): File? = context.getExternalFilesDir(null)?.let { File(it, spec.name) }
|
||||
|
||||
/// Follows a download of the system until it ends. The state is set on the main
|
||||
/// thread, as "Cancel" sets it: after a cancel the watcher sets nothing more.
|
||||
private fun watch(id: Long) {
|
||||
job = scope.launch(Dispatchers.Main.immediate) {
|
||||
while (isActive) {
|
||||
val row = withContext(Dispatchers.IO) { progress(id) }
|
||||
if (row == null) {
|
||||
// Removed in the system's list of downloads.
|
||||
forget()
|
||||
_state.value = NetState.Missing
|
||||
return@launch
|
||||
}
|
||||
when (row.status) {
|
||||
DownloadManager.STATUS_SUCCESSFUL -> {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val f = target() ?: throw IOException("no external folder")
|
||||
f.inputStream().use { install(it, progress = false) }
|
||||
NetState.Ready
|
||||
} catch (e: Failure) {
|
||||
NetState.Failed(e.problem)
|
||||
} catch (e: IOException) {
|
||||
NetState.Failed(if (freeSpace() < spec.size) NetProblem.NoSpace else NetProblem.Interrupted)
|
||||
}
|
||||
}
|
||||
downloads.remove(id)
|
||||
forget()
|
||||
_state.value = result
|
||||
return@launch
|
||||
}
|
||||
DownloadManager.STATUS_FAILED -> {
|
||||
downloads.remove(id)
|
||||
forget()
|
||||
_state.value = NetState.Failed(
|
||||
if (row.reason == DownloadManager.ERROR_INSUFFICIENT_SPACE) NetProblem.NoSpace else NetProblem.Interrupted,
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
else -> _state.value = NetState.Loading(row.done.coerceAtLeast(0), spec.size)
|
||||
}
|
||||
delay(500)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Row(val status: Int, val reason: Int, val done: Long)
|
||||
|
||||
private fun progress(id: Long): Row? =
|
||||
downloads.query(DownloadManager.Query().setFilterById(id))?.use { c ->
|
||||
if (!c.moveToFirst()) return null
|
||||
Row(
|
||||
c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)),
|
||||
c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)),
|
||||
c.getLong(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Writes the file into place, checking its SHA-256 on the way; a wrong file is
|
||||
/// thrown away.
|
||||
private suspend fun install(input: InputStream, progress: Boolean) {
|
||||
dir.mkdirs()
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
var done = 0L
|
||||
part.outputStream().use { out ->
|
||||
val buf = ByteArray(256 * 1024)
|
||||
var last = 0L
|
||||
while (true) {
|
||||
kotlin.coroutines.coroutineContext.ensureActive()
|
||||
val n = input.read(buf)
|
||||
if (n < 0) break
|
||||
out.write(buf, 0, n)
|
||||
digest.update(buf, 0, n)
|
||||
done += n
|
||||
if (progress && done - last > 512 * 1024) {
|
||||
last = done
|
||||
_state.value = NetState.Loading(done, spec.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
val sum = digest.digest().joinToString("") { "%02x".format(it) }
|
||||
if (sum != spec.sha256 || done != spec.size) {
|
||||
part.delete()
|
||||
throw Failure(NetProblem.Corrupt)
|
||||
}
|
||||
if (!part.renameTo(file)) throw IOException("cannot move the file into place")
|
||||
}
|
||||
|
||||
/// The space the app can take, with the caches the system would clear for it. Asked
|
||||
/// of filesDir, which always exists: a folder not made yet answers 0.
|
||||
private fun freeSpace(): Long = try {
|
||||
val storage = context.getSystemService(StorageManager::class.java)
|
||||
storage.getAllocatableBytes(storage.getUuidForPath(context.filesDir))
|
||||
} catch (e: IOException) {
|
||||
context.filesDir.usableSpace
|
||||
}
|
||||
|
||||
private fun online(): Boolean {
|
||||
val cm = context.getSystemService(ConnectivityManager::class.java) ?: return true
|
||||
val caps = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false
|
||||
return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val KEY = "download"
|
||||
const val NO_DOWNLOAD = -1L
|
||||
}
|
||||
}
|
||||
|
|
@ -1,280 +1,55 @@
|
|||
package io.github.vrubelroman.chesspenguin.data
|
||||
|
||||
import android.app.DownloadManager
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.Uri
|
||||
import android.os.storage.StorageManager
|
||||
import io.github.vrubelroman.chesspenguin.BuildConfig
|
||||
import io.github.vrubelroman.chesspenguin.R
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
import java.security.MessageDigest
|
||||
|
||||
/// Why the network of the engine is not there.
|
||||
enum class NetProblem { NoInternet, Interrupted, NoSpace, Corrupt }
|
||||
|
||||
sealed interface NetState {
|
||||
data object Missing : NetState
|
||||
data class Loading(val done: Long, val total: Long) : NetState
|
||||
data class Failed(val problem: NetProblem) : NetState
|
||||
data object Ready : NetState
|
||||
}
|
||||
|
||||
/// Stockfish and its network (android-ui.md §8). The executable is a "library" of the
|
||||
/// APK; the network is a file in the app's storage: copied from the APK in the "full"
|
||||
/// build, downloaded on request in the "light" one, checked by its SHA-256 either way.
|
||||
///
|
||||
/// The download is the system's (DownloadManager): it goes on with the app closed,
|
||||
/// waits for the network and resumes, and shows its progress in a notification. Its
|
||||
/// id is kept, so the app picks the download up again when it starts.
|
||||
class EngineFiles(private val context: Context, private val scope: CoroutineScope) {
|
||||
private val dir = File(context.filesDir, "engine")
|
||||
val net = File(dir, BuildConfig.NET_NAME)
|
||||
private val part = File(dir, BuildConfig.NET_NAME + ".part")
|
||||
private val prefs = context.getSharedPreferences("engine", Context.MODE_PRIVATE)
|
||||
private val downloads get() = context.getSystemService(DownloadManager::class.java)
|
||||
/// build, downloaded on request in the "light" one, checked by its SHA-256 either way
|
||||
/// ([CheckedDownload]).
|
||||
class EngineFiles(private val context: Context, scope: CoroutineScope) {
|
||||
private val download = CheckedDownload(
|
||||
context,
|
||||
scope,
|
||||
FileSpec(
|
||||
dir = "engine",
|
||||
name = BuildConfig.NET_NAME,
|
||||
url = BuildConfig.NET_URL,
|
||||
size = BuildConfig.NET_SIZE,
|
||||
sha256 = BuildConfig.NET_SHA256,
|
||||
title = context.getString(R.string.download_title),
|
||||
prefs = "engine",
|
||||
),
|
||||
)
|
||||
val net: File get() = download.file
|
||||
|
||||
/// The executable, unpacked by the installer (useLegacyPackaging).
|
||||
val executable: String get() = context.applicationInfo.nativeLibraryDir + "/libstockfish.so"
|
||||
|
||||
private val _state = MutableStateFlow<NetState>(NetState.Missing)
|
||||
val state: StateFlow<NetState> = _state.asStateFlow()
|
||||
private var job: Job? = null
|
||||
|
||||
init {
|
||||
// A network of another version of Stockfish (kept before an update, §25.6) is of
|
||||
// no use to this one.
|
||||
dir.listFiles()?.filter { it.name != net.name && it.name != part.name }?.forEach { it.delete() }
|
||||
when {
|
||||
installed() -> _state.value = NetState.Ready
|
||||
download() != NO_DOWNLOAD -> {
|
||||
_state.value = NetState.Loading(0, BuildConfig.NET_SIZE)
|
||||
watch(download())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A network checked when it was written: the right size is enough later.
|
||||
private fun installed() = net.length() == BuildConfig.NET_SIZE
|
||||
val state: StateFlow<NetState> get() = download.state
|
||||
|
||||
val builtIn: Boolean get() = BuildConfig.NET_BUILT_IN
|
||||
|
||||
/// True when the phone is on a metered (mobile) connection right now.
|
||||
fun metered(): Boolean {
|
||||
val cm = context.getSystemService(ConnectivityManager::class.java) ?: return false
|
||||
val caps = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false
|
||||
return !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
|
||||
}
|
||||
fun metered(): Boolean = download.metered()
|
||||
|
||||
/// Gets the network ready: from the APK, or from the internet.
|
||||
fun prepare() {
|
||||
if (_state.value == NetState.Ready || _state.value is NetState.Loading) return
|
||||
if (builtIn) {
|
||||
_state.value = NetState.Loading(0, BuildConfig.NET_SIZE)
|
||||
job = scope.launch(Dispatchers.IO) {
|
||||
val result = try {
|
||||
dir.mkdirs()
|
||||
context.assets.open(BuildConfig.NET_NAME).use { install(it) }
|
||||
NetState.Ready
|
||||
} catch (e: NetFailure) {
|
||||
NetState.Failed(e.problem)
|
||||
} catch (e: IOException) {
|
||||
NetState.Failed(if (freeSpace() < BuildConfig.NET_SIZE) NetProblem.NoSpace else NetProblem.Interrupted)
|
||||
}
|
||||
if (isActive) _state.value = result
|
||||
}
|
||||
return
|
||||
}
|
||||
if (!online()) {
|
||||
_state.value = NetState.Failed(NetProblem.NoInternet)
|
||||
return
|
||||
}
|
||||
// The file is downloaded next to the app's files, then checked and moved in.
|
||||
if (freeSpace() < 2 * BuildConfig.NET_SIZE) {
|
||||
_state.value = NetState.Failed(NetProblem.NoSpace)
|
||||
return
|
||||
}
|
||||
target()?.delete()
|
||||
val request = DownloadManager.Request(Uri.parse(BuildConfig.NET_URL))
|
||||
.setTitle(context.getString(R.string.download_title))
|
||||
.setDestinationInExternalFilesDir(context, null, BuildConfig.NET_NAME)
|
||||
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
|
||||
.setAllowedOverMetered(true)
|
||||
val id = try {
|
||||
downloads.enqueue(request)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
// No shared storage for the download (or the download service is off).
|
||||
_state.value = NetState.Failed(NetProblem.Interrupted)
|
||||
return
|
||||
}
|
||||
prefs.edit().putLong(KEY, id).apply()
|
||||
_state.value = NetState.Loading(0, BuildConfig.NET_SIZE)
|
||||
watch(id)
|
||||
if (builtIn) download.installFrom { context.assets.open(BuildConfig.NET_NAME) } else download.download()
|
||||
}
|
||||
|
||||
/// Keeps the network of the full build in the app's storage, so that it stays after
|
||||
/// the app is updated to the light build (android-ui.md §25.6). False when it could
|
||||
/// not (no space): the light build then offers the download, as always.
|
||||
suspend fun keepBuiltIn(): Boolean = withContext(Dispatchers.IO) {
|
||||
if (!builtIn || installed()) return@withContext true
|
||||
if (freeSpace() < 2 * BuildConfig.NET_SIZE) return@withContext false
|
||||
try {
|
||||
dir.mkdirs()
|
||||
context.assets.open(BuildConfig.NET_NAME).use { install(it) }
|
||||
_state.value = NetState.Ready
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
part.delete()
|
||||
if (!installed()) _state.value = NetState.Missing
|
||||
false
|
||||
}
|
||||
}
|
||||
suspend fun keepBuiltIn(): Boolean = !builtIn || download.installNow { context.assets.open(BuildConfig.NET_NAME) }
|
||||
|
||||
/// Stops a download and throws away what came.
|
||||
fun cancel() {
|
||||
job?.cancel()
|
||||
val id = download()
|
||||
if (id != NO_DOWNLOAD) downloads.remove(id)
|
||||
forget()
|
||||
part.delete()
|
||||
if (!installed()) _state.value = NetState.Missing
|
||||
}
|
||||
fun cancel() = download.cancel()
|
||||
|
||||
/// Removes a downloaded network (Settings → "Delete the engine").
|
||||
fun delete() {
|
||||
cancel()
|
||||
net.delete()
|
||||
_state.value = NetState.Missing
|
||||
}
|
||||
|
||||
private class NetFailure(val problem: NetProblem) : Exception()
|
||||
|
||||
private fun download(): Long = prefs.getLong(KEY, NO_DOWNLOAD)
|
||||
|
||||
private fun forget() = prefs.edit().remove(KEY).apply()
|
||||
|
||||
/// Where the system puts the downloaded file (the app's own external folder).
|
||||
private fun target(): File? = context.getExternalFilesDir(null)?.let { File(it, BuildConfig.NET_NAME) }
|
||||
|
||||
/// Follows a download of the system until it ends. The state is set on the main
|
||||
/// thread, as "Cancel" sets it: after a cancel the watcher sets nothing more.
|
||||
private fun watch(id: Long) {
|
||||
job = scope.launch(Dispatchers.Main.immediate) {
|
||||
while (isActive) {
|
||||
val row = withContext(Dispatchers.IO) { progress(id) }
|
||||
if (row == null) {
|
||||
// Removed in the system's list of downloads.
|
||||
forget()
|
||||
_state.value = NetState.Missing
|
||||
return@launch
|
||||
}
|
||||
when (row.status) {
|
||||
DownloadManager.STATUS_SUCCESSFUL -> {
|
||||
val result = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
dir.mkdirs()
|
||||
val file = target() ?: throw IOException("no external folder")
|
||||
file.inputStream().use { install(it) }
|
||||
NetState.Ready
|
||||
} catch (e: NetFailure) {
|
||||
NetState.Failed(e.problem)
|
||||
} catch (e: IOException) {
|
||||
NetState.Failed(if (freeSpace() < BuildConfig.NET_SIZE) NetProblem.NoSpace else NetProblem.Interrupted)
|
||||
}
|
||||
}
|
||||
downloads.remove(id)
|
||||
forget()
|
||||
_state.value = result
|
||||
return@launch
|
||||
}
|
||||
DownloadManager.STATUS_FAILED -> {
|
||||
downloads.remove(id)
|
||||
forget()
|
||||
_state.value = NetState.Failed(
|
||||
if (row.reason == DownloadManager.ERROR_INSUFFICIENT_SPACE) NetProblem.NoSpace else NetProblem.Interrupted,
|
||||
)
|
||||
return@launch
|
||||
}
|
||||
else -> _state.value = NetState.Loading(row.done.coerceAtLeast(0), BuildConfig.NET_SIZE)
|
||||
}
|
||||
delay(500)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Row(val status: Int, val reason: Int, val done: Long)
|
||||
|
||||
private fun progress(id: Long): Row? =
|
||||
downloads.query(DownloadManager.Query().setFilterById(id))?.use { c ->
|
||||
if (!c.moveToFirst()) return null
|
||||
Row(
|
||||
c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)),
|
||||
c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_REASON)),
|
||||
c.getLong(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)),
|
||||
)
|
||||
}
|
||||
|
||||
/// Writes the network into place, checking its SHA-256 on the way; a wrong file is
|
||||
/// thrown away.
|
||||
private suspend fun install(input: InputStream) {
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
var done = 0L
|
||||
part.outputStream().use { out ->
|
||||
val buf = ByteArray(256 * 1024)
|
||||
var last = 0L
|
||||
while (true) {
|
||||
kotlin.coroutines.coroutineContext.ensureActive()
|
||||
val n = input.read(buf)
|
||||
if (n < 0) break
|
||||
out.write(buf, 0, n)
|
||||
digest.update(buf, 0, n)
|
||||
done += n
|
||||
if (builtIn && done - last > 512 * 1024) {
|
||||
last = done
|
||||
_state.value = NetState.Loading(done, BuildConfig.NET_SIZE)
|
||||
}
|
||||
}
|
||||
}
|
||||
val sum = digest.digest().joinToString("") { "%02x".format(it) }
|
||||
if (sum != BuildConfig.NET_SHA256 || done != BuildConfig.NET_SIZE) {
|
||||
part.delete()
|
||||
throw NetFailure(NetProblem.Corrupt)
|
||||
}
|
||||
if (!part.renameTo(net)) throw IOException("cannot move the network into place")
|
||||
}
|
||||
|
||||
/// The space the app can take, with the caches the system would clear for it. Asked
|
||||
/// of filesDir, which always exists: a folder not made yet (the engine's, before the
|
||||
/// first download) answers 0.
|
||||
private fun freeSpace(): Long = try {
|
||||
val storage = context.getSystemService(StorageManager::class.java)
|
||||
storage.getAllocatableBytes(storage.getUuidForPath(context.filesDir))
|
||||
} catch (e: IOException) {
|
||||
context.filesDir.usableSpace
|
||||
}
|
||||
|
||||
private fun online(): Boolean {
|
||||
val cm = context.getSystemService(ConnectivityManager::class.java) ?: return true
|
||||
val caps = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false
|
||||
return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val KEY = "download"
|
||||
const val NO_DOWNLOAD = -1L
|
||||
}
|
||||
fun delete() = download.delete()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
package io.github.vrubelroman.chesspenguin.data
|
||||
|
||||
import android.content.Context
|
||||
import io.github.vrubelroman.chesspenguin.R
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
/// Maia's weights (android-play.md §31.2): the small model (`maia3-5m.pt`, 21 MB) of a
|
||||
/// pinned revision, downloaded on request from the authors' Hugging Face page — the
|
||||
/// file is theirs, not shipped in the APK. The same file as the desktop app's.
|
||||
class MaiaFiles(context: Context, scope: CoroutineScope) {
|
||||
val download = CheckedDownload(
|
||||
context,
|
||||
scope,
|
||||
FileSpec(
|
||||
dir = "maia",
|
||||
name = "maia3-5m.pt",
|
||||
url = "https://huggingface.co/UofTCSSLab/Maia3-5M/resolve/b6559de2398d7140b985f28fd2c19fb5e47ddabe/maia3-5m.pt",
|
||||
size = 20_968_049,
|
||||
sha256 = "ba14208b2992d85502f5fb501934abf6aaaeb355e9f3fdf90e326911f562524f",
|
||||
title = context.getString(R.string.maia_download_title),
|
||||
prefs = "maia",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
@ -460,4 +460,5 @@
|
|||
<item quantity="one">%1$d Durchgang</item>
|
||||
<item quantity="other">%1$d Durchgänge</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">Maia für ChessPenguin</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -468,4 +468,5 @@
|
|||
<item quantity="many">%1$d repasos</item>
|
||||
<item quantity="other">%1$d repasos</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">Maia para ChessPenguin</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -468,4 +468,5 @@
|
|||
<item quantity="many">%1$d passages</item>
|
||||
<item quantity="other">%1$d passages</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">Maia pour ChessPenguin</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -468,4 +468,5 @@
|
|||
<item quantity="many">%1$d ripassi</item>
|
||||
<item quantity="other">%1$d ripassi</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">Maia per ChessPenguin</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -461,4 +461,5 @@
|
|||
<item quantity="one">%1$d рет</item>
|
||||
<item quantity="other">%1$d рет</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">ChessPenguin үшін Maia</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -469,4 +469,5 @@
|
|||
<item quantity="many">%1$d passagens</item>
|
||||
<item quantity="other">%1$d passagens</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">Maia para o ChessPenguin</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -476,4 +476,5 @@
|
|||
<item quantity="many">прохождений: %1$d</item>
|
||||
<item quantity="other">прохождений: %1$d</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">Maia для ChessPenguin</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -459,4 +459,5 @@
|
|||
<item quantity="one">%1$d marta</item>
|
||||
<item quantity="other">%1$d marta</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">ChessPenguin uchun Maia</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -464,4 +464,5 @@
|
|||
<item quantity="one">%1$d run</item>
|
||||
<item quantity="other">%1$d runs</item>
|
||||
</plurals>
|
||||
<string name="maia_download_title">Maia for ChessPenguin</string>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue