Skip to content

Commit

Permalink
refactoring & remove bitrot from updater
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 23, 2024
1 parent bca79b6 commit 7ec02e5
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 167 deletions.
83 changes: 40 additions & 43 deletions app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.io.File
import java.io.IOException

class BackupRestoreFlow(private val partitionId: Int): WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
override fun get(vm: WizardState): List<IWizardPage> {
val c = CreateBackupDataHolder(vm, partitionId)
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
Expand All @@ -45,7 +45,7 @@ class BackupRestoreFlow(private val partitionId: Int): WizardFlow() {
}
}

private class CreateBackupDataHolder(val vm: WizardActivityState, val pi: Int) {
private class CreateBackupDataHolder(val vm: WizardState, val pi: Int) {
var action: Int = 0
var path: Uri? = null
var meta: SDUtils.SDPartitionMeta? = null
Expand Down Expand Up @@ -93,17 +93,22 @@ private fun SelectDroidBoot(c: CreateBackupDataHolder) {
else -> ""
}
)
val next = { it: Uri ->
c.path = it
nextButtonAvailable = true
c.vm.nextText = c.vm.activity.getString(R.string.next)
c.vm.onNext = { i -> i.navigate("go") }
}
val next =
Button(onClick = {
if (c.action != 1) {
c.vm.activity.chooseFile("*/*", next)
c.vm.activity.chooseFile("*/*") {
c.vm.chosen["file"] = WizardState.DownloadedFile(it, null)
nextButtonAvailable = true
c.vm.nextText = c.vm.activity.getString(R.string.next)
c.vm.onNext = { i -> i.navigate("go") }
}
} else {
c.vm.activity.createFile("${c.meta!!.dumpKernelPartition(c.pi).name}.img", next)
c.vm.activity.createFile("${c.meta!!.dumpKernelPartition(c.pi).name}.img") {
c.path = it
nextButtonAvailable = true
c.vm.nextText = c.vm.activity.getString(R.string.next)
c.vm.onNext = { i -> i.navigate("go") }
}
}
}) {
Text(stringResource(if (c.action != 1) R.string.choose_file else R.string.create_file))
Expand All @@ -117,40 +122,32 @@ private fun Flash(c: CreateBackupDataHolder) {
Terminal(logFile = "flash_${System.currentTimeMillis()}.txt") { terminal ->
c.vm.logic.extractToolkit(terminal)
terminal.add(c.vm.activity.getString(R.string.term_starting))
try {
val p = c.meta!!.dumpKernelPartition(c.pi)
if (!c.vm.logic.unmount(p).to(terminal).exec().isSuccess)
throw IOException(c.vm.activity.getString(R.string.term_cant_umount))
if (c.action == 1) {
c.vm.copy(
SuFileInputStream.open(File(p.path)),
c.vm.activity.contentResolver.openOutputStream(c.path!!)!!
)
} else if (c.action == 2) {
c.vm.copyPriv(
c.vm.activity.contentResolver.openInputStream(c.path!!)!!,
File(p.path)
)
} else if (c.action == 3) {
val f = File(c.vm.logic.cacheDir, System.currentTimeMillis().toString())
c.vm.copyUnpriv(c.vm.activity.contentResolver.openInputStream(c.path!!)!!, f)
val result2 = Shell.cmd(
File(
c.vm.logic.toolkitDir,
"simg2img"
).absolutePath + " ${f.absolutePath} ${p.path}"
).to(terminal).exec()
if (!result2.isSuccess) {
terminal.add(c.vm.activity.getString(R.string.term_failure))
return@Terminal
}
} else {
throw IOException(c.vm.activity.getString(R.string.term_invalid_action))
val p = c.meta!!.dumpKernelPartition(c.pi)
if (!c.vm.logic.unmount(p).to(terminal).exec().isSuccess)
throw IOException(c.vm.activity.getString(R.string.term_cant_umount))
if (c.action == 1) {
c.vm.copy(
SuFileInputStream.open(File(p.path)),
c.vm.activity.contentResolver.openOutputStream(c.path!!)!!
)
} else if (c.action == 2) {
c.vm.copyPriv(
c.vm.chosen["file"]!!.openInputStream(c.vm),
File(p.path)
)
} else if (c.action == 3) {
val result2 = Shell.cmd(
File(
c.vm.logic.toolkitDir,
"simg2img"
).absolutePath + " ${c.vm.chosen["file"]!!.toFile(c.vm).absolutePath} ${p.path}"
).to(terminal).exec()
if (!result2.isSuccess) {
terminal.add(c.vm.activity.getString(R.string.term_failure))
return@Terminal
}
} catch (e: IOException) {
terminal.add(c.vm.activity.getString(R.string.term_backup_restore_fail))
terminal.add(if (e.message != null) e.message!! else "(null)")
terminal.add(c.vm.activity.getString(R.string.term_contact_support))
} else {
throw IOException(c.vm.activity.getString(R.string.term_invalid_action))
}
terminal.add(c.vm.activity.getString(R.string.term_success))
c.vm.nextText = c.vm.activity.getString(R.string.finish)
Expand Down
28 changes: 13 additions & 15 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import java.math.BigDecimal
import java.net.URL

class CreatePartFlow(private val desiredStartSector: Long): WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
override fun get(vm: WizardState): List<IWizardPage> {
val c = CreatePartDataHolder(vm, desiredStartSector)
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
Expand Down Expand Up @@ -92,7 +92,7 @@ class CreatePartFlow(private val desiredStartSector: Long): WizardFlow() {
}
}

private class CreatePartDataHolder(val vm: WizardActivityState, val desiredStartSector: Long) {
private class CreatePartDataHolder(val vm: WizardState, val desiredStartSector: Long) {
var meta by mutableStateOf<SDUtils.SDPartitionMeta?>(null)
lateinit var p: SDUtils.Partition.FreeSpace
var startSectorRelative = 0L
Expand Down Expand Up @@ -376,17 +376,17 @@ private fun Shop(c: CreatePartDataHolder) {
while (i < inets.length()) {
val l = inets.getJSONObject(i)
vm.inetAvailable[l.getString("id")] =
WizardActivityState.Downloadable(
WizardState.Downloadable(
l.getString("url"),
l.optString("hash"),
l.getStringOrNull("hash"),
l.getString("desc")
)
i++
}
vm.idNeeded.add("_install.sh_")
vm.inetAvailable["_install.sh_"] = WizardActivityState.Downloadable(
vm.inetAvailable["_install.sh_"] = WizardState.Downloadable(
o.getString("scriptname"),
o.optString("scriptSha256"),
o.getStringOrNull("scriptSha256"),
vm.activity.getString(R.string.installer_sh)
)

Expand Down Expand Up @@ -655,7 +655,7 @@ private fun Flash(c: CreatePartDataHolder) {
Terminal(logFile = "install_${System.currentTimeMillis()}.txt") { terminal ->
c.vm.logic.extractToolkit(terminal)
if (c.partitionName == null) { // OS install
val createdParts = ArrayMap<Part, Int>() // order is important
val createdParts = mutableListOf<Pair<Part, Int>>() // order is important
val fn = c.romFolderName
terminal.add(vm.activity.getString(R.string.term_f_name, fn))
terminal.add(vm.activity.getString(R.string.term_g_name, c.romDisplayName))
Expand Down Expand Up @@ -683,7 +683,7 @@ private fun Flash(c: CreatePartDataHolder) {
if (r.out.joinToString("\n").contains("kpartx")) {
terminal.add(vm.activity.getString(R.string.term_reboot_asap))
}
createdParts[part] = c.meta!!.nid
createdParts.add(Pair(part, c.meta!!.nid))
c.meta = SDUtils.generateMeta(c.vm.deviceInfo)
// do not assert there is leftover space if we just created the last partition we want to create
if (index < c.parts.size - 1) {
Expand Down Expand Up @@ -715,7 +715,7 @@ private fun Flash(c: CreatePartDataHolder) {
entry["dtbo"] = "$fn/dtbo.dtbo"
entry["options"] = c.cmdline
entry["xtype"] = c.rtype
entry["xpart"] = createdParts.values.joinToString(":")
entry["xpart"] = createdParts.map { it.second }.joinToString(":")
if (c.dmaMeta.contains("updateJson") && c.dmaMeta["updateJson"] != null)
entry["xupdate"] = c.dmaMeta["updateJson"]!!
entry.exportToFile(File(vm.logic.abmEntries, "$fn.conf"))
Expand All @@ -729,23 +729,21 @@ private fun Flash(c: CreatePartDataHolder) {
if (!c.vm.idNeeded.contains(part.id)) continue
terminal.add(vm.activity.getString(R.string.term_flashing_s, part.id))
val f = c.vm.chosen[part.id]!!
val tp = File(meta.dumpKernelPartition(createdParts[part]!!).path)
val tp = File(meta.dumpKernelPartition(createdParts.find { it.first == part }!!.second).path)
if (part.sparse) {
val f2 = f.toFile(c.vm)
val result2 = Shell.cmd(
File(
c.vm.logic.toolkitDir,
"simg2img"
).absolutePath + " ${f2.absolutePath} ${tp.absolutePath}"
).absolutePath + " ${f.toFile(c.vm).absolutePath} ${tp.absolutePath}"
).to(terminal).exec()
f.delete()
if (!result2.isSuccess) {
terminal.add(vm.activity.getString(R.string.term_failure))
return@Terminal
}
} else {
val f2 = f.openInputStream(c.vm)
c.vm.copyPriv(f2, tp)
c.vm.copyPriv(f.openInputStream(c.vm), tp)
}
terminal.add(vm.activity.getString(R.string.term_done))
}
Expand All @@ -756,7 +754,7 @@ private fun Flash(c: CreatePartDataHolder) {
cmd += " " + c.vm.chosen[i]!!.toFile(vm).absolutePath
}
for (i in c.parts) {
cmd += " " + createdParts[i]
cmd += " " + createdParts.find { it.first == i }!!.second
}
val result = vm.logic.runShFileWithArgs(cmd).to(terminal).exec()
if (!result.isSuccess) {
Expand Down
23 changes: 11 additions & 12 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import java.io.IOException
import java.net.URL

class DroidBootFlow : WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
override fun get(vm: WizardState): List<IWizardPage> {
val d = DroidBootFlowDataHolder(vm)
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
Expand All @@ -65,12 +65,12 @@ class DroidBootFlow : WizardFlow() {
}
}

class DroidBootFlowDataHolder(val vm: WizardActivityState) {
class DroidBootFlowDataHolder(val vm: WizardState) {
var osName by mutableStateOf(vm.activity.getString(R.string.android))
}

@Composable
private fun Start(vm: WizardActivityState) {
private fun Start(vm: WizardState) {
Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
Expand All @@ -91,7 +91,7 @@ private fun Start(vm: WizardActivityState) {

// shared across DroidBootFlow, UpdateDroidBootFlow, FixDroidBootFlow
@Composable
fun LoadDroidBootJson(vm: WizardActivityState, content: @Composable () -> Unit) {
fun LoadDroidBootJson(vm: WizardState, content: @Composable () -> Unit) {
var loading by remember { mutableStateOf(!vm.deviceInfo.isBooted(vm.logic) || vm.deviceInfo.postInstallScript) }
var error by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
Expand All @@ -106,17 +106,17 @@ fun LoadDroidBootJson(vm: WizardActivityState, content: @Composable () -> Unit)
if (!vm.deviceInfo.isBooted(vm.logic)) {
val bl = json.getJSONObject("bootloader")
val url = bl.getString("url")
val sha = bl.optString("sha256")
vm.inetAvailable["droidboot"] = WizardActivityState.Downloadable(
val sha = bl.getStringOrNull("sha256")
vm.inetAvailable["droidboot"] = WizardState.Downloadable(
url, sha, vm.activity.getString(R.string.droidboot_online)
)
vm.idNeeded.add("droidboot")
}
if (vm.deviceInfo.postInstallScript) {
val i = json.getJSONObject("installScript")
val url = i.getString("url")
val sha = i.optString("sha256")
vm.inetAvailable["_install.sh_"] = WizardActivityState.Downloadable(
val sha = i.getStringOrNull("sha256")
vm.inetAvailable["_install.sh_"] = WizardState.Downloadable(
url, sha, vm.activity.getString(R.string.installer_sh)
)
vm.idNeeded.add("_install.sh_")
Expand Down Expand Up @@ -269,10 +269,9 @@ private fun Flash(d: DroidBootFlowDataHolder) {
}
}
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.chosen["_install.sh_"]!!.openInputStream(vm), tmpFile)
tmpFile.setExecutable(true)
tmpFile
vm.chosen["_install.sh_"]!!.toFile(vm).also {
it.setExecutable(true)
}
} else null

terminal.add(vm.activity.getString(R.string.term_building_cfg))
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.io.File
import java.io.IOException

class FixDroidBootFlow(): WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
override fun get(vm: WizardState): List<IWizardPage> {
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {})
Expand All @@ -39,7 +39,7 @@ class FixDroidBootFlow(): WizardFlow() {
}

@Composable
private fun Start(vm: WizardActivityState) {
private fun Start(vm: WizardState) {
LoadDroidBootJson(vm) {
LaunchedEffect(Unit) {
vm.nextText = vm.activity.getString(R.string.next)
Expand All @@ -57,14 +57,13 @@ private fun Start(vm: WizardActivityState) {
}

@Composable
private fun Flash(vm: WizardActivityState) {
private fun Flash(vm: WizardState) {
Terminal(logFile = "blfix_${System.currentTimeMillis()}.txt") { terminal ->
vm.logic.extractToolkit(terminal)
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.chosen["_install.sh_"]!!.openInputStream(vm), tmpFile)
tmpFile.setExecutable(true)
tmpFile
vm.chosen["_install.sh_"]!!.toFile(vm).also {
it.setExecutable(true)
}
} else null
terminal.add(vm.activity.getString(R.string.term_flashing_droidboot))
val backupLk = File(vm.logic.fileDir, "backup_lk.img")
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ class MainActivityState(val activity: MainActivity?) {
val cfg = withContext(Dispatchers.IO) {
ConfigFile.importFromFile(logic!!.abmDbConf).toMap()
}
withContext(Dispatchers.Main) {
defaultCfg.clear()
defaultCfg.putAll(cfg)
}
defaultCfg.clear()
defaultCfg.putAll(cfg)
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/Start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ private fun PartTool(vm: MainActivityState) {
var initrdT by remember { mutableStateOf(e["initrd"] ?: "") }
val initrdE by remember { derivedStateOf { !initrdT.matches(asciiRegex) } }
var dtbT by remember { mutableStateOf(e["dtb"] ?: "") }
val dtbE by remember { derivedStateOf { dtbT.matches(asciiRegex) } }
val dtbE by remember { derivedStateOf { !dtbT.matches(asciiRegex) } }
var optionsT by remember { mutableStateOf(e["options"] ?: "") }
val optionsE by remember { derivedStateOf { !optionsT.matches(asciiRegex) } }
var xtypeT by remember { mutableStateOf(e["xtype"] ?: "") }
Expand Down
13 changes: 6 additions & 7 deletions app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.io.File
import java.io.IOException

class UpdateDroidBootFlow: WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
override fun get(vm: WizardState): List<IWizardPage> {
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {})
Expand All @@ -39,7 +39,7 @@ class UpdateDroidBootFlow: WizardFlow() {
}

@Composable
private fun Start(vm: WizardActivityState) {
private fun Start(vm: WizardState) {
LoadDroidBootJson(vm) {
LaunchedEffect(Unit) {
vm.nextText = vm.activity.getString(R.string.next)
Expand All @@ -57,14 +57,13 @@ private fun Start(vm: WizardActivityState) {
}

@Composable
private fun Flash(vm: WizardActivityState) {
private fun Flash(vm: WizardState) {
Terminal(logFile = "blup_${System.currentTimeMillis()}.txt") { terminal ->
vm.logic.extractToolkit(terminal)
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.chosen["_install.sh_"]!!.openInputStream(vm), tmpFile)
tmpFile.setExecutable(true)
tmpFile
vm.chosen["_install.sh_"]!!.toFile(vm).also {
it.setExecutable(true)
}
} else null
terminal.add(vm.activity.getString(R.string.term_flashing_droidboot))
val backupLk = File(vm.logic.fileDir, "backup2_lk.img")
Expand Down
Loading

0 comments on commit 7ec02e5

Please sign in to comment.