Add app/src/main/java/space/bloggin/scribe/ModelFiles.kt
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package space.bloggin.scribe
|
||||
|
||||
import android.content.Context
|
||||
import java.io.File
|
||||
|
||||
object ModelFiles {
|
||||
private const val MARK = "unpacked-v1"
|
||||
|
||||
/** Copies the bundled speech model out of the APK on first run. Returns the folder to load. */
|
||||
fun ensure(ctx: Context): File {
|
||||
val dest = File(ctx.filesDir, "model")
|
||||
val mark = File(dest, MARK)
|
||||
if (mark.exists()) return dest
|
||||
dest.deleteRecursively()
|
||||
dest.mkdirs()
|
||||
copyDir(ctx, "model", dest)
|
||||
mark.writeText("ok")
|
||||
return dest
|
||||
}
|
||||
|
||||
private fun copyDir(ctx: Context, assetPath: String, out: File) {
|
||||
val kids = ctx.assets.list(assetPath) ?: emptyArray()
|
||||
if (kids.isEmpty()) {
|
||||
ctx.assets.open(assetPath).use { i -> out.outputStream().use { o -> i.copyTo(o, 64 * 1024) } }
|
||||
return
|
||||
}
|
||||
out.mkdirs()
|
||||
for (k in kids) copyDir(ctx, "$assetPath/$k", File(out, k))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user