Skip to content

Commit

Permalink
migrate to new download system in legacy flows part3
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 23, 2024
1 parent aa590e6 commit bca79b6
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 303 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CreatePartFlow(private val desiredStartSector: Long): WizardFlow() {
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {}
) {
WizardDownloader(c.vm)
WizardDownloader(c.vm, "flash")
}, WizardPage("flash",
NavButton("") {},
NavButton("") {}
Expand Down
52 changes: 23 additions & 29 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package org.andbootmgr.app

import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
Expand All @@ -23,8 +19,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
Expand All @@ -46,7 +40,7 @@ import java.net.URL

class DroidBootFlow : WizardFlow() {
override fun get(vm: WizardActivityState): List<IWizardPage> {
val booted = vm.deviceInfo.isBooted(vm.logic)
val d = DroidBootFlowDataHolder(vm)
return listOf(WizardPage("start",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton(vm.activity.getString(R.string.next)) { it.navigate("input") })
Expand All @@ -56,21 +50,25 @@ class DroidBootFlow : WizardFlow() {
NavButton(vm.activity.getString(R.string.prev)) { it.navigate("start") },
NavButton("") {}
) {
Input(vm)
Input(d)
}, WizardPage("dload",
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {}
) {
WizardDownloader(vm)
WizardDownloader(vm, "flash")
}, WizardPage("flash",
NavButton("") {},
NavButton("") {}
) {
Flash(vm)
Flash(d)
})
}
}

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

@Composable
private fun Start(vm: WizardActivityState) {
Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center,
Expand Down Expand Up @@ -118,10 +116,10 @@ fun LoadDroidBootJson(vm: WizardActivityState, content: @Composable () -> Unit)
val i = json.getJSONObject("installScript")
val url = i.getString("url")
val sha = i.optString("sha256")
vm.inetAvailable["install"] = WizardActivityState.Downloadable(
vm.inetAvailable["_install.sh_"] = WizardActivityState.Downloadable(
url, sha, vm.activity.getString(R.string.installer_sh)
)
vm.idNeeded.add("install")
vm.idNeeded.add("_install.sh_")
}
loading = false
} catch (e: Exception) {
Expand All @@ -143,28 +141,24 @@ fun LoadDroidBootJson(vm: WizardActivityState, content: @Composable () -> Unit)
}

@Composable
private fun Input(vm: WizardActivityState) {
LoadDroidBootJson(vm) {
private fun Input(d: DroidBootFlowDataHolder) {
LoadDroidBootJson(d.vm) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxSize()
) {
LaunchedEffect(Unit) { // TODO can't I do this better?
if (vm.texts.isBlank())
vm.texts = vm.activity.getString(R.string.android)
}
val e = vm.texts.isBlank() || !vm.texts.matches(Regex("[\\dA-Za-z]+"))
val e = d.osName.isBlank() || !d.osName.matches(Regex("[\\dA-Za-z]+"))

Text(
stringResource(R.string.enter_name_for_current),
textAlign = TextAlign.Center,
modifier = Modifier.padding(vertical = 5.dp)
)
TextField(
value = vm.texts,
value = d.osName,
onValueChange = {
vm.texts = it
d.osName = it
},
label = { Text(stringResource(R.string.os_name)) },
isError = e
Expand All @@ -176,20 +170,20 @@ private fun Input(vm: WizardActivityState) {
}
LaunchedEffect(e) {
if (e) {
vm.nextText = ""
vm.onNext = {}
d.vm.nextText = ""
d.vm.onNext = {}
} else {
vm.nextText = vm.activity.getString(R.string.next)
vm.onNext = { it.navigate(if (vm.idNeeded.isNotEmpty()) "dload" else "flash") }
d.vm.nextText = d.vm.activity.getString(R.string.next)
d.vm.onNext = { it.navigate(if (d.vm.idNeeded.isNotEmpty()) "dload" else "flash") }
}
}
}
}
}

@Composable
private fun Flash(vm: WizardActivityState) {
val flashType = "DroidBootFlashType"
private fun Flash(d: DroidBootFlowDataHolder) {
val vm = d.vm
Terminal(logFile = "blflash_${System.currentTimeMillis()}.txt") { terminal ->
vm.logic.extractToolkit(terminal)
terminal.add(vm.activity.getString(R.string.term_preparing_fs))
Expand Down Expand Up @@ -276,7 +270,7 @@ private fun Flash(vm: WizardActivityState) {
}
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.chosen["install"]!!.openInputStream(vm), tmpFile)
vm.copyPriv(vm.chosen["_install.sh_"]!!.openInputStream(vm), tmpFile)
tmpFile.setExecutable(true)
tmpFile
} else null
Expand All @@ -287,7 +281,7 @@ private fun Flash(vm: WizardActivityState) {
db["timeout"] = "5"
db.exportToFile(File(vm.logic.abmDb, "db.conf"))
val entry = ConfigFile()
entry["title"] = vm.texts.trim()
entry["title"] = d.osName.trim()
entry["linux"] = "null"
entry["initrd"] = "null"
entry["dtb"] = "null"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FixDroidBootFlow(): WizardFlow() {
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {}
) {
WizardDownloader(vm)
WizardDownloader(vm, "flash")
}, WizardPage("flash",
NavButton("") {},
NavButton("") {}
Expand Down Expand Up @@ -62,7 +62,7 @@ private fun Flash(vm: WizardActivityState) {
vm.logic.extractToolkit(terminal)
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.chosen["install"]!!.openInputStream(vm), tmpFile)
vm.copyPriv(vm.chosen["_install.sh_"]!!.openInputStream(vm), tmpFile)
tmpFile.setExecutable(true)
tmpFile
} else null
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.andbootmgr.app.themes.ThemeViewModel
import org.andbootmgr.app.themes.Themes
Expand All @@ -81,8 +82,10 @@ class MainActivityState(val activity: MainActivity?) {
var logic: DeviceLogic? = null

private fun loadDefaultCfg() {
CoroutineScope(Dispatchers.IO).launch {
val cfg = ConfigFile.importFromFile(logic!!.abmDbConf).toMap()
runBlocking {
val cfg = withContext(Dispatchers.IO) {
ConfigFile.importFromFile(logic!!.abmDbConf).toMap()
}
withContext(Dispatchers.Main) {
defaultCfg.clear()
defaultCfg.putAll(cfg)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class UpdateDroidBootFlow: WizardFlow() {
NavButton(vm.activity.getString(R.string.cancel)) { it.finish() },
NavButton("") {}
) {
WizardDownloader(vm)
WizardDownloader(vm, "flash")
}, WizardPage("flash",
NavButton("") {},
NavButton("") {}
Expand Down Expand Up @@ -62,7 +62,7 @@ private fun Flash(vm: WizardActivityState) {
vm.logic.extractToolkit(terminal)
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.chosen["install"]!!.openInputStream(vm), tmpFile)
vm.copyPriv(vm.chosen["_install.sh_"]!!.openInputStream(vm), tmpFile)
tmpFile.setExecutable(true)
tmpFile
} else null
Expand Down
Loading

0 comments on commit bca79b6

Please sign in to comment.