Skip to content

Commit 934a281

Browse files
committed
finish theoretical sdless refactor
1 parent 5562cc9 commit 934a281

File tree

8 files changed

+100
-20
lines changed

8 files changed

+100
-20
lines changed

app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.andbootmgr.app.util.SDUtils
2222
import java.io.File
2323
import java.io.IOException
2424

25-
class BackupRestoreFlow(private val partitionId: Int, private val partFile: File?): WizardFlow() {
25+
class BackupRestoreFlow(private val partitionId: Int?, private val partFile: File?): WizardFlow() {
2626
override fun get(vm: WizardState): List<IWizardPage> {
2727
val c = CreateBackupDataHolder(vm, partitionId, partFile)
2828
return listOf(WizardPage("start",

app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,6 @@ private fun Flash(c: CreatePartDataHolder) {
812812
val id = i++
813813
val img = File(imgFolder, id.toString())
814814
val map = File(entryFolder, "$id.map")
815-
val mappedName = "abm_${fn}_$id"
816815
val bytes = part.resolveBytesSize(c, space)
817816
space -= bytes
818817
if (space < 0)
@@ -825,14 +824,11 @@ private fun Flash(c: CreatePartDataHolder) {
825824
terminal.add(vm.activity.getString(R.string.term_failed_uncrypt))
826825
return@WizardTerminalWork
827826
}
828-
if (!SDLessUtils.unmap(vm.logic, mappedName, false, terminal))
829-
throw IllegalStateException("failed to unmap $mappedName which shouldn't even exist?")
830-
if (!SDLessUtils.map(vm.logic, mappedName, map, terminal)) {
827+
if (!vm.logic.map(fn, id, terminal)) {
831828
terminal.add(vm.activity.getString(R.string.term_failed_map_other))
832829
return@WizardTerminalWork
833830
}
834-
val mapped = File(vm.logic.dmBase, mappedName)
835-
createdParts.add(part to (i to mapped))
831+
createdParts.add(part to (i to vm.logic.getDmFile(fn, id)))
836832
terminal.add(vm.activity.getString(R.string.term_created_part))
837833
}
838834
}

app/src/main/java/org/andbootmgr/app/DeviceLogic.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,23 @@ class DeviceLogic(private val ctx: Context) {
7777
}
7878
return mounted
7979
}
80-
fun mapBootset(): Boolean {
81-
return SDLessUtils.map(this, dmName, metadataMap)
80+
fun mapBootset(terminal: MutableList<String>? = null): Boolean {
81+
return SDLessUtils.map(this, dmName, metadataMap, terminal)
8282
}
8383
private fun unmapBootset(): Boolean {
8484
return SDLessUtils.unmap(this, dmName, true)
8585
}
86+
fun getDmName(fn: String, id: Int) = "abm_${fn}_$id"
87+
fun getDmFile(fn: String, id: Int) = File(dmBase, getDmName(fn, id))
88+
fun map(fn: String, id: Int, terminal: MutableList<String>? = null): Boolean {
89+
if (!unmap(fn, id, terminal))
90+
throw IllegalStateException("failed to unmap part which shouldn't even exist?")
91+
val map = File(File(abmBootset, fn), "$id.map")
92+
return SDLessUtils.map(this, getDmName(fn, id), map, terminal)
93+
}
94+
fun unmap(fn: String, id: Int, terminal: MutableList<String>? = null): Boolean {
95+
return SDLessUtils.unmap(this, getDmName(fn, id), false, terminal)
96+
}
8697
fun mount(p: SDUtils.Partition): Shell.Job {
8798
return Shell.cmd(p.mount())
8899
}

app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,11 @@ private fun Flash(d: DroidBootFlowDataHolder) {
274274
terminal.add(vm.activity.getString(R.string.term_failed_unmap))
275275
return@WizardTerminalWork
276276
}
277-
if (!SDLessUtils.map(vm.logic, vm.logic.dmName, vm.logic.metadataMap, terminal)) {
277+
if (!vm.logic.mapBootset(terminal)) {
278278
terminal.add(vm.activity.getString(R.string.term_failed_map))
279279
return@WizardTerminalWork
280280
}
281-
if (!Shell.cmd("mkfs.ext4 ${vm.logic.dmName}").to(terminal).exec().isSuccess) {
281+
if (!Shell.cmd("mkfs.ext4 $ast").to(terminal).exec().isSuccess) {
282282
terminal.add(vm.activity.getString(R.string.term_failed_bootset_mkfs))
283283
return@WizardTerminalWork
284284
}

app/src/main/java/org/andbootmgr/app/Start.kt

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.compose.material3.Button
1717
import androidx.compose.material3.Card
1818
import androidx.compose.material3.CardDefaults
1919
import androidx.compose.material3.Icon
20+
import androidx.compose.material3.ListItem
2021
import androidx.compose.material3.Text
2122
import androidx.compose.material3.TextField
2223
import androidx.compose.runtime.Composable
@@ -45,7 +46,9 @@ import kotlinx.coroutines.Dispatchers
4546
import kotlinx.coroutines.launch
4647
import kotlinx.coroutines.withContext
4748
import org.andbootmgr.app.util.ConfigFile
49+
import org.andbootmgr.app.util.SDLessUtils
4850
import org.andbootmgr.app.util.SDUtils
51+
import org.andbootmgr.app.util.SOUtils
4952
import java.io.File
5053
import kotlin.collections.set
5154
import kotlin.io.nameWithoutExtension
@@ -363,6 +366,7 @@ private fun OsEditor(vm: MainActivityState, parts: SDUtils.SDPartitionMeta?, e:
363366
onClose: (newPt: Boolean) -> Unit, onOpenUpdater: () -> Unit) {
364367
var processing by remember { mutableStateOf(false) }
365368
var delete by remember { mutableStateOf(false) }
369+
var backupAskPart by remember { mutableStateOf(false) }
366370
var result by remember { mutableStateOf<Pair<String, (() -> Unit)?>?>(null) }
367371
AlertDialog(
368372
onDismissRequest = {
@@ -380,8 +384,11 @@ private fun OsEditor(vm: MainActivityState, parts: SDUtils.SDPartitionMeta?, e:
380384
enabled = e.has("xupdate") && !e["xupdate"].isNullOrBlank()) {
381385
Text(stringResource(R.string.update))
382386
}
383-
// TODO add button to open backup & restore tool (by asking which partition should
384-
// be backed up / restored)
387+
Button(onClick = {
388+
backupAskPart = true
389+
}, enabled = e.has("xparts") && !e["xparts"].isNullOrBlank() && e["xparts"] != "real") {
390+
Text(stringResource(R.string.backupnrestore))
391+
}
385392
Button(
386393
onClick = {
387394
delete = true
@@ -400,6 +407,45 @@ private fun OsEditor(vm: MainActivityState, parts: SDUtils.SDPartitionMeta?, e:
400407
}
401408
)
402409

410+
if (backupAskPart) {
411+
AlertDialog(
412+
onDismissRequest = {
413+
backupAskPart = false
414+
},
415+
title = {
416+
Text(stringResource(R.string.choose_part_to_backup))
417+
},
418+
text = {
419+
for (i in e["xparts"]!!.split(":")) {
420+
val part = parts?.dumpKernelPartition(i.toInt())
421+
val file = if (!vm.deviceInfo!!.metaonsd)
422+
File(File(vm.logic!!.abmSdLessBootset, f.nameWithoutExtension), i) else null
423+
val size = part?.let { it.size * it.meta.logicalSectorSizeBytes }
424+
?: SuFile.open(file!!.toURI()).length()
425+
ListItem(headlineContent = {
426+
Text(stringResource(R.string.entry_space_usage,
427+
if (part != null)
428+
stringResource(R.string.part_item, i, part.name)
429+
else
430+
stringResource(R.string.part_title, i),
431+
SOUtils.humanReadableByteCountBin(size)
432+
))
433+
}, modifier = Modifier.clickable {
434+
vm.currentWizardFlow = if (file != null)
435+
BackupRestoreFlow(null, file)
436+
else
437+
BackupRestoreFlow(i.toInt(), null)
438+
})
439+
}
440+
},
441+
confirmButton = {
442+
Button(onClick = { backupAskPart = false }) {
443+
Text(stringResource(id = R.string.cancel))
444+
}
445+
}
446+
)
447+
}
448+
403449
if (delete) {
404450
AlertDialog(
405451
onDismissRequest = {
@@ -724,15 +770,17 @@ private fun EntryEditor(vm: MainActivityState, e: ConfigFile, f: File?, onClose:
724770
val initrdE by remember { derivedStateOf { !initrdT.matches(asciiRegex) } }
725771
var dtbT by remember { mutableStateOf(e["dtb"] ?: "") }
726772
val dtbE by remember { derivedStateOf { !dtbT.matches(asciiRegex) } }
773+
var dtboT by remember { mutableStateOf(e["dtbo"] ?: "") }
774+
val dtboE by remember { derivedStateOf { if (vm.deviceInfo!!.havedtbo)
775+
!dtboT.matches(asciiRegex) else false } }
727776
var optionsT by remember { mutableStateOf(e["options"] ?: "") }
728777
val optionsE by remember { derivedStateOf { !optionsT.matches(asciiRegex) } }
729778
var xtypeT by remember { mutableStateOf(e["xtype"] ?: "") }
730779
val xtypeE by remember { derivedStateOf { !xtypeValidValues.contains(xtypeT) } }
731780
var xpartT by remember { mutableStateOf(e["xpart"] ?: "") }
732781
val xpartE by remember { derivedStateOf { !xpartT.matches(xpartValidValues) } }
733782
var xupdateT by remember { mutableStateOf(e["xupdate"] ?: "") }
734-
// TODO dtbo editing if havedtbo
735-
val isOk = !(newFileNameErr || titleE || linuxE || initrdE || dtbE || optionsE || xtypeE || xpartE)
783+
val isOk = !(newFileNameErr || titleE || linuxE || initrdE || dtbE || dtboE || optionsE || xtypeE || xpartE)
736784
AlertDialog(
737785
onDismissRequest = {
738786
onClose()
@@ -776,6 +824,13 @@ private fun EntryEditor(vm: MainActivityState, e: ConfigFile, f: File?, onClose:
776824
Text(stringResource(R.string.dtb))
777825
})
778826

827+
if (vm.deviceInfo!!.havedtbo)
828+
TextField(value = dtbT, onValueChange = {
829+
dtbT = it
830+
}, isError = dtbE, label = {
831+
Text(stringResource(R.string.dtbo))
832+
})
833+
779834
TextField(value = optionsT, onValueChange = {
780835
optionsT = it
781836
}, isError = optionsE, label = {
@@ -828,6 +883,8 @@ private fun EntryEditor(vm: MainActivityState, e: ConfigFile, f: File?, onClose:
828883
e["linux"] = linuxT
829884
e["initrd"] = initrdT
830885
e["dtb"] = dtbT
886+
if (vm.deviceInfo!!.havedtbo)
887+
e["dtbo"] = dtboT
831888
e["options"] = optionsT
832889
e["xtype"] = xtypeT
833890
e["xpart"] = xpartT
@@ -879,7 +936,8 @@ private fun BootsetTool(vm: MainActivityState) {
879936
MyInfoCard(stringResource(R.string.click2inspect), padding = 5.dp)
880937
if (entries != null) {
881938
for (e in entries!!.keys) {
882-
val spaceUsage = null // TODO compute space usage of installed OS
939+
val spaceUsage = SDLessUtils.getSpaceUsageBytes(vm.logic!!,
940+
entries!![e]!!.nameWithoutExtension)
883941
Row(horizontalArrangement = Arrangement.SpaceEvenly,
884942
verticalAlignment = Alignment.CenterVertically,
885943
modifier = Modifier
@@ -926,7 +984,6 @@ private fun BootsetTool(vm: MainActivityState) {
926984
}
927985
}
928986
}
929-
// TODO we eventually want portable partitions for !metaonsd, but not supported yet
930987
if (editEntryID != null && filterEntryView) {
931988
EntryEditor(
932989
vm, editEntryID!!, entries!![editEntryID!!],

app/src/main/java/org/andbootmgr/app/UpdateFlow.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.topjohnwu.superuser.io.SuFile
2727
import kotlinx.coroutines.Dispatchers
2828
import kotlinx.coroutines.launch
2929
import org.andbootmgr.app.util.ConfigFile
30+
import org.andbootmgr.app.util.SDLessUtils
3031
import org.andbootmgr.app.util.SDUtils
3132
import org.json.JSONObject
3233
import org.json.JSONTokener
@@ -210,8 +211,9 @@ private fun Flash(u: UpdateFlowDataHolder) {
210211
u.vm.logic.extractToolkit(terminal)
211212
u.vm.downloadRemainingFiles(terminal)
212213
val sp = u.e!!["xpart"]!!.split(":")
213-
val meta = SDUtils.generateMeta(u.vm.deviceInfo.asMetaOnSdDeviceInfo())!! // TODO !metaonsd
214-
Shell.cmd(SDUtils.umsd(meta)).exec()
214+
val meta = if (u.vm.deviceInfo.metaonsd)
215+
SDUtils.generateMeta(u.vm.deviceInfo.asMetaOnSdDeviceInfo())!! else null
216+
meta?.let { Shell.cmd(SDUtils.umsd(it)).exec() }
215217
val tmpFile = if (u.vm.idNeeded.contains("_install.sh_")) {
216218
u.vm.chosen["_install.sh_"]!!.toFile(u.vm).also {
217219
it.setExecutable(true)
@@ -221,7 +223,13 @@ private fun Flash(u: UpdateFlowDataHolder) {
221223
val physicalId = sp[p.toInt()].toInt()
222224
terminal.add(u.vm.activity.getString(R.string.term_flashing_p, p))
223225
val f2 = u.vm.chosen["part$p"]!!
224-
val tp = File(meta.dumpKernelPartition(physicalId).path)
226+
val tp = if (u.vm.deviceInfo.metaonsd)
227+
File(meta!!.dumpKernelPartition(physicalId).path)
228+
else {
229+
if (!u.vm.logic.map(u.ef!!.nameWithoutExtension, physicalId, terminal))
230+
throw IllegalStateException("failed to map $physicalId")
231+
u.vm.logic.getDmFile(u.ef!!.nameWithoutExtension, physicalId)
232+
}
225233
if (u.sparse.contains(p.toInt())) {
226234
val result2 = Shell.cmd(
227235
File(

app/src/main/java/org/andbootmgr/app/util/SDLessUtils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ object SDLessUtils {
1010
return 4L * 1024L * 1024L * 1024L // TODO
1111
}
1212

13+
fun getSpaceUsageBytes(logic: DeviceLogic, fn: String): Long? {
14+
return SuFile.open(logic.abmSdLessBootset, fn).listFiles()?.let {
15+
it.sumOf { it.length() }
16+
}
17+
}
18+
1319
fun map(logic: DeviceLogic, name: String, mapFile: File, terminal: MutableList<String>? = null): Boolean {
1420
val dmPath = File(logic.dmBase, name)
1521
if (SuFile.open(dmPath.toURI()).exists())

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,6 @@
273273
<string name="term_failed_bootset_mkfs">-- failed to create file system in bootset</string>
274274
<string name="term_failed_unmap">-- failed to unmap bootset.img</string>
275275
<string name="install_bl_first">You need to install the bootloader manually first. Please check the ABM wiki for more infos.</string>
276+
<string name="dtbo">Dtbo</string>
277+
<string name="choose_part_to_backup">Choose partition to backup</string>
276278
</resources>

0 commit comments

Comments
 (0)