Skip to content

Commit

Permalink
fix the stay alive blunder
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 26, 2024
1 parent 36626cc commit 1ce0334
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 293 deletions.
7 changes: 2 additions & 5 deletions app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import androidx.compose.ui.text.style.TextAlign
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFileInputStream
import org.andbootmgr.app.util.SDUtils
import org.andbootmgr.app.util.Terminal
import java.io.File
import java.io.IOException

Expand Down Expand Up @@ -118,7 +117,7 @@ private fun SelectDroidBoot(c: CreateBackupDataHolder) {

@Composable
private fun Flash(c: CreateBackupDataHolder) {
Terminal(logFile = "flash_${System.currentTimeMillis()}.txt") { terminal ->
WizardTerminalWork(c.vm, logFile = "flash_${System.currentTimeMillis()}.txt") { terminal ->
c.vm.logic.extractToolkit(terminal)
terminal.add(c.vm.activity.getString(R.string.term_starting))
val p = c.meta!!.dumpKernelPartition(c.pi)
Expand All @@ -143,13 +142,11 @@ private fun Flash(c: CreateBackupDataHolder) {
).to(terminal).exec()
if (!result2.isSuccess) {
terminal.add(c.vm.activity.getString(R.string.term_failure))
return@Terminal
return@WizardTerminalWork
}
} 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)
c.vm.onNext = { it.finish() }
}
}
17 changes: 6 additions & 11 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import org.andbootmgr.app.CreatePartDataHolder.Part
import org.andbootmgr.app.util.ConfigFile
import org.andbootmgr.app.util.SDUtils
import org.andbootmgr.app.util.SOUtils
import org.andbootmgr.app.util.Terminal
import org.json.JSONObject
import org.json.JSONTokener
import java.io.File
Expand Down Expand Up @@ -652,7 +651,7 @@ private fun Os(c: CreatePartDataHolder) {
@Composable
private fun Flash(c: CreatePartDataHolder) {
val vm = c.vm
Terminal(logFile = "install_${System.currentTimeMillis()}.txt") { terminal ->
WizardTerminalWork(vm, logFile = "install_${System.currentTimeMillis()}.txt") { terminal ->
c.vm.logic.extractToolkit(terminal)
c.vm.downloadRemainingFiles(terminal)
if (c.partitionName == null) { // OS install
Expand Down Expand Up @@ -695,15 +694,15 @@ private fun Flash(c: CreatePartDataHolder) {
terminal.add(vm.activity.getString(R.string.term_created_part))
} else {
terminal.add(vm.activity.getString(R.string.term_failure))
return@Terminal
return@WizardTerminalWork
}
}
terminal.add(vm.activity.getString(R.string.term_created_pt))
vm.logic.mountBootset(vm.deviceInfo)
val meta = SDUtils.generateMeta(vm.deviceInfo)
if (meta == null) {
terminal.add(vm.activity.getString(R.string.term_cant_get_meta))
return@Terminal
return@WizardTerminalWork
}
terminal.add(vm.activity.getString(R.string.term_building_cfg))

Expand All @@ -722,7 +721,7 @@ private fun Flash(c: CreatePartDataHolder) {
entry.exportToFile(File(vm.logic.abmEntries, "$fn.conf"))
if (!SuFile.open(File(vm.logic.abmBootset, fn).toURI()).mkdir()) {
terminal.add(vm.activity.getString(R.string.term_mkdir_failed))
return@Terminal
return@WizardTerminalWork
}

terminal.add(vm.activity.getString(R.string.term_flashing_imgs))
Expand All @@ -741,7 +740,7 @@ private fun Flash(c: CreatePartDataHolder) {
f.delete()
if (!result2.isSuccess) {
terminal.add(vm.activity.getString(R.string.term_failure))
return@Terminal
return@WizardTerminalWork
}
} else {
c.vm.copyPriv(f.openInputStream(c.vm), tp)
Expand All @@ -760,12 +759,10 @@ private fun Flash(c: CreatePartDataHolder) {
val result = vm.logic.runShFileWithArgs(cmd).to(terminal).exec()
if (!result.isSuccess) {
terminal.add(vm.activity.getString(R.string.term_failure))
return@Terminal
return@WizardTerminalWork
}

terminal.add(vm.activity.getString(R.string.term_success))
vm.nextText = vm.activity.getString(R.string.finish)
vm.onNext = { it.finish() }
} else { // Portable partition
terminal.add(vm.activity.getString(R.string.term_create_part))
vm.logic.unmountBootset()
Expand All @@ -779,8 +776,6 @@ private fun Flash(c: CreatePartDataHolder) {
terminal.add(vm.activity.getString(R.string.term_reboot_asap))
}
if (r.isSuccess) {
vm.nextText = c.vm.activity.getString(R.string.finish)
vm.onNext = { it.finish() }
terminal.add(vm.activity.getString(R.string.term_success))
} else {
terminal.add(vm.activity.getString(R.string.term_failure))
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/DeviceLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DeviceLogic(private val ctx: Context) {
val fileDir = File(rootDir, "files")
val cacheDir = File(rootDir, "cache")
val toolkitDir = File(toolkit.targetPath, "Toolkit") // will occasionally be pruned by OS, but it's fine
val rootTmpDir = File("/data/local/tmp")
private val rootTmpDir = File("/data/local/tmp")
val abmBootset = File(rootTmpDir, ".abm_bootset")
val abmDb = File(abmBootset, "db")
val abmEntries = File(abmDb, "entries")
Expand Down
40 changes: 14 additions & 26 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import com.topjohnwu.superuser.io.SuFileInputStream
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.andbootmgr.app.util.ConfigFile
import org.andbootmgr.app.util.SDUtils
import org.andbootmgr.app.util.Terminal
import org.json.JSONObject
import org.json.JSONTokener
import java.io.File
Expand Down Expand Up @@ -184,32 +182,32 @@ private fun Input(d: DroidBootFlowDataHolder) {
@Composable
private fun Flash(d: DroidBootFlowDataHolder) {
val vm = d.vm
Terminal(logFile = "blflash_${System.currentTimeMillis()}.txt") { terminal ->
WizardTerminalWork(d.vm, logFile = "blflash_${System.currentTimeMillis()}.txt") { terminal ->
vm.logic.extractToolkit(terminal)
vm.downloadRemainingFiles(terminal)
terminal.add(vm.activity.getString(R.string.term_preparing_fs))
if (vm.logic.checkMounted()) {
terminal.add(vm.activity.getString(R.string.term_mount_state_bad))
return@Terminal
return@WizardTerminalWork
}
if (!SuFile.open(vm.logic.abmBootset.toURI()).exists()) {
if (!SuFile.open(vm.logic.abmBootset.toURI()).mkdir()) {
terminal.add(vm.activity.getString(R.string.term_cant_create_mount_point))
return@Terminal
return@WizardTerminalWork
}
}
if (!SuFile.open(File(vm.logic.abmBootset, ".NOT_MOUNTED").toURI()).exists()) {
if (!SuFile.open(File(vm.logic.abmBootset, ".NOT_MOUNTED").toURI()).createNewFile()) {
terminal.add(vm.activity.getString(R.string.term_cant_create_placeholder))
return@Terminal
return@WizardTerminalWork
}
}

if (vm.deviceInfo.metaonsd) {
var meta = SDUtils.generateMeta(vm.deviceInfo)
if (meta == null) {
terminal.add(vm.activity.getString(R.string.term_cant_get_meta))
return@Terminal
return@WizardTerminalWork
}
if (!Shell.cmd(SDUtils.umsd(meta)).to(terminal).exec().isSuccess) {
terminal.add(vm.activity.getString(R.string.term_failed_umount_drive))
Expand All @@ -218,12 +216,12 @@ private fun Flash(d: DroidBootFlowDataHolder) {
.exec().isSuccess
) {
terminal.add(vm.activity.getString(R.string.term_failed_create_pt))
return@Terminal
return@WizardTerminalWork
}
meta = SDUtils.generateMeta(vm.deviceInfo)
if (meta == null) {
terminal.add(vm.activity.getString(R.string.term_cant_get_meta))
return@Terminal
return@WizardTerminalWork
}
val r = vm.logic.create(meta.s[0] as SDUtils.Partition.FreeSpace,
0,
Expand All @@ -240,33 +238,33 @@ private fun Flash(d: DroidBootFlowDataHolder) {
terminal.add(vm.activity.getString(R.string.term_done))
} else {
terminal.add(vm.activity.getString(R.string.term_failed_create_meta))
return@Terminal
return@WizardTerminalWork
}
} else {
// TODO provision for sdless
}

if (!vm.logic.mountBootset(vm.deviceInfo)) {
terminal.add(vm.activity.getString(R.string.term_failed_mount))
return@Terminal
return@WizardTerminalWork
}
if (SuFile.open(File(vm.logic.abmBootset, ".NOT_MOUNTED").toURI()).exists()) {
terminal.add(vm.activity.getString(R.string.term_mount_failure_inconsist))
return@Terminal
return@WizardTerminalWork
}

if (!SuFile.open(vm.logic.abmDb.toURI()).exists()) {
if (!SuFile.open(vm.logic.abmDb.toURI()).mkdir()) {
terminal.add(vm.activity.getString(R.string.term_failed_create_db_dir))
vm.logic.unmountBootset()
return@Terminal
return@WizardTerminalWork
}
}
if (!SuFile.open(vm.logic.abmEntries.toURI()).exists()) {
if (!SuFile.open(vm.logic.abmEntries.toURI()).mkdir()) {
terminal.add(vm.activity.getString(R.string.term_failed_create_entries_dir))
vm.logic.unmountBootset()
return@Terminal
return@WizardTerminalWork
}
}
val tmpFile = if (vm.deviceInfo.postInstallScript) {
Expand Down Expand Up @@ -304,7 +302,7 @@ private fun Flash(d: DroidBootFlowDataHolder) {
terminal.add(vm.activity.getString(R.string.term_bl_failed))
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
return@WizardTerminalWork
}
}
if (vm.deviceInfo.postInstallScript) {
Expand All @@ -317,16 +315,6 @@ private fun Flash(d: DroidBootFlowDataHolder) {
}
terminal.add(vm.activity.getString(R.string.term_success))
vm.logic.unmountBootset()
withContext(Dispatchers.Main) {
vm.nextText = vm.activity.getString(R.string.finish)
vm.onNext = {
if (vm.deviceInfo.isBooted(vm.logic)) {
it.finish()
} else {
// TODO prompt user to reboot?
it.finish()
}
}
}
// TODO prompt user to reboot?
}
}
13 changes: 3 additions & 10 deletions app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import com.topjohnwu.superuser.io.SuFile
import com.topjohnwu.superuser.io.SuFileInputStream
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.andbootmgr.app.util.Terminal
import java.io.File
import java.io.IOException

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

@Composable
private fun Flash(vm: WizardState) {
Terminal(logFile = "blfix_${System.currentTimeMillis()}.txt") { terminal ->
WizardTerminalWork(vm, logFile = "blfix_${System.currentTimeMillis()}.txt") { terminal ->
vm.logic.extractToolkit(terminal)
vm.downloadRemainingFiles(terminal)
val tmpFile = if (vm.deviceInfo.postInstallScript) {
Expand All @@ -78,7 +77,7 @@ private fun Flash(vm: WizardState) {
terminal.add(vm.activity.getString(R.string.term_bl_failed))
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
return@WizardTerminalWork
}
if (vm.deviceInfo.postInstallScript) {
terminal.add(vm.activity.getString(R.string.term_device_setup))
Expand All @@ -89,11 +88,5 @@ private fun Flash(vm: WizardState) {
tmpFile.delete()
}
terminal.add(vm.activity.getString(R.string.term_success))
withContext(Dispatchers.Main) {
vm.nextText = vm.activity.getString(R.string.finish)
vm.onNext = {
it.finish()
}
}
}
}
59 changes: 34 additions & 25 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import org.andbootmgr.app.util.AbmTheme
import org.andbootmgr.app.util.ConfigFile
import org.andbootmgr.app.util.StayAliveService
import org.andbootmgr.app.util.Terminal
import org.andbootmgr.app.util.TerminalList
import java.util.concurrent.atomic.AtomicBoolean

class MainActivityState(val activity: MainActivity?) {
Expand All @@ -80,7 +81,7 @@ class MainActivityState(val activity: MainActivity?) {
var deviceInfo: DeviceInfo? = null
val theme = ThemeViewModel(this)
var defaultCfg = mutableStateMapOf<String, String>()
var isOk = false
var isOk by mutableStateOf(false)
var logic: DeviceLogic? = null

private fun loadDefaultCfg() {
Expand Down Expand Up @@ -117,17 +118,15 @@ class MainActivityState(val activity: MainActivity?) {

// This will be called on startup, and after StayAlive work completes.
fun init() {
if (!StayAliveService.isRunning) {
val installed = deviceInfo?.isInstalled(logic!!)
if (installed == true) {
mountBootset()
} else {
Log.i("ABM", "not installed, not trying to mount")
}
if (deviceInfo != null) {
isOk = installed!! && deviceInfo!!.isBooted(logic!!) &&
!(!logic!!.mounted || deviceInfo!!.isCorrupt(logic!!))
}
val installed = deviceInfo?.isInstalled(logic!!)
if (installed == true) {
mountBootset()
} else {
Log.i("ABM", "not installed, not trying to mount")
}
if (deviceInfo != null) {
isOk = installed!! && deviceInfo!!.isBooted(logic!!) &&
!(!logic!!.mounted || deviceInfo!!.isCorrupt(logic!!))
}
}

Expand Down Expand Up @@ -247,31 +246,41 @@ class MainActivity : ComponentActivity() {
// == temp migration code end ==
}
vm.deviceInfo = di.await() // blocking
vm.init()
if (StayAliveService.instance == null) {
vm.init()
}
withContext(Dispatchers.Main) {
setContent {
// TODO allow rotating device while viewing logs without loosing logs (will require rememberSavable)
AbmTheme {
var showTerminal by remember { mutableStateOf(StayAliveService.isRunning) }
if (showTerminal) {
var canFinish by remember { mutableStateOf(false) }
if (StayAliveService.instance != null &&
StayAliveService.instance!!.workExtra is TerminalList) {
val i = StayAliveService.instance!!
val we = i.workExtra as TerminalList
DisposableEffect(Unit) {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
onDispose { window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) }
}
BackHandler {}
Column(modifier = Modifier.fillMaxSize()) {
Box(modifier = Modifier.fillMaxWidth().weight(1.0f)) {
Terminal(null, { canFinish = true }, null)
Terminal(we)
}
Box(modifier = Modifier.fillMaxWidth()) {
BasicButtonRow("", {}, if (canFinish)
stringResource(R.string.finish) else "") {
if (canFinish)
CoroutineScope(Dispatchers.IO).launch {
vm.init()
showTerminal = false
}
if (we.isCancelled == false || i.isWorkDone) {
BasicButtonRow(
if (we.isCancelled == false)
stringResource(R.string.cancel) else "", {
if (we.isCancelled == false)
we.cancel?.invoke()
}, if (i.isWorkDone)
stringResource(R.string.finish) else ""
) {
if (i.isWorkDone)
CoroutineScope(Dispatchers.IO).launch {
vm.init()
StayAliveService.instance!!.stopSelf()
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/andbootmgr/app/Start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ fun Start(vm: MainActivityState) {
PartTool(vm)
} else {
Text(stringResource(R.string.invalid), textAlign = TextAlign.Center)
Text("metaOnSd:$metaOnSd isOk:${vm.isOk}")
}
}
}
Expand Down
Loading

0 comments on commit 1ce0334

Please sign in to comment.