Skip to content

Commit 8b2afe1

Browse files
committed
Refactor SlotInfo
1 parent f6748a1 commit 8b2afe1

File tree

2 files changed

+55
-43
lines changed

2 files changed

+55
-43
lines changed

app/src/main/java/com/github/capntrips/kernelflasher/ui/components/SlotCard.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ fun SlotCard(
4848
mutableMaxWidth = cardWidth
4949
)
5050
}
51-
AnimatedVisibility(!viewModel.isRefreshing.value && viewModel.bootInfo.kernelVersion != null) {
51+
AnimatedVisibility(!viewModel.isRefreshing.value && viewModel.slotInfo.bootImgInfo.kernelVersion != null) {
5252
DataRow(
5353
label = stringResource(R.string.kernel_version),
54-
value = if (viewModel.bootInfo.kernelVersion != null) viewModel.bootInfo.kernelVersion!! else "",
54+
value = viewModel.slotInfo.bootImgInfo.kernelVersion ?: "",
5555
mutableMaxWidth = cardWidth,
5656
clickable = true
5757
)
@@ -69,27 +69,27 @@ fun SlotCard(
6969
}
7070
DataRow(
7171
label = stringResource(R.string.boot_fmt),
72-
value = viewModel.bootInfo.bootFmt ?: stringResource(R.string.not_found),
72+
value = viewModel.slotInfo.bootImgInfo.bootFmt ?: stringResource(R.string.not_found),
7373
mutableMaxWidth = cardWidth
7474
)
7575
DataRow(
76-
label = if (viewModel.bootInfo.ramdiskLocation == "init_boot.img") stringResource(R.string.init_boot_fmt) else stringResource(R.string.ramdisk_fmt),
77-
value = viewModel.bootInfo.initBootFmt ?: stringResource(R.string.not_found),
76+
label = if (viewModel.slotInfo.ramdiskInfo.ramdiskLocation == "init_boot.img") stringResource(R.string.init_boot_fmt) else stringResource(R.string.ramdisk_fmt),
77+
value = viewModel.slotInfo.ramdiskInfo.ramdiskFmt ?: stringResource(R.string.not_found),
7878
mutableMaxWidth = cardWidth
7979
)
8080
if(isSlotScreen && viewModel.slotSuffix != "")
8181
{
8282
DataRow(
8383
label = "Unbootable",
84-
value = viewModel.bootSlotInfo.unbootable ?: stringResource(R.string.not_found),
84+
value = viewModel.slotInfo.bootSlotInfo.unbootable ?: stringResource(R.string.not_found),
8585
mutableMaxWidth = cardWidth,
86-
valueColor = if (viewModel.bootSlotInfo.unbootable == "Yes") Color.Red else Color.Unspecified
86+
valueColor = if (viewModel.slotInfo.bootSlotInfo.unbootable == "Yes") Color.Red else Color.Unspecified
8787
)
8888
DataRow(
8989
label = "Successful",
90-
value = viewModel.bootSlotInfo.successful ?: stringResource(R.string.not_found),
90+
value = viewModel.slotInfo.bootSlotInfo.successful ?: stringResource(R.string.not_found),
9191
mutableMaxWidth = cardWidth,
92-
valueColor = if (viewModel.bootSlotInfo.successful == "No") Color.Red else Color.Unspecified
92+
valueColor = if (viewModel.slotInfo.bootSlotInfo.successful == "No") Color.Red else Color.Unspecified
9393
)
9494
}
9595
if (!viewModel.isRefreshing.value && viewModel.hasError) {

app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/slot/SlotViewModel.kt

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,26 @@ class SlotViewModel(
6060
var successful: String? = null,
6161
)
6262

63-
data class BootInfo(
63+
data class BootImgInfo(
6464
var kernelVersion: String? = null,
6565
var bootFmt: String? = null,
6666
var headerVersion: String? = null,
67-
var initBootFmt: String? = null,
68-
var ramdiskLocation: String? = null
67+
)
68+
69+
data class RamdiskInfo(
70+
var headerVersion: String? = null,
71+
var ramdiskFmt: String? = null,
72+
var ramdiskLocation: String? = null,
73+
)
74+
75+
data class SlotInfo(
76+
var bootSlotInfo: BootSlotInfo,
77+
var bootImgInfo: BootImgInfo,
78+
var ramdiskInfo: RamdiskInfo,
6979
)
7080

7181
private var _sha1: String? = null
72-
private val _bootInfo: MutableState<BootInfo> = mutableStateOf(BootInfo())
73-
private val _bootSlotInfo: MutableState<BootSlotInfo> = mutableStateOf(BootSlotInfo())
82+
private val _slotInfo: MutableState<SlotInfo> = mutableStateOf(SlotInfo(BootSlotInfo(), BootImgInfo(), RamdiskInfo()))
7483
var hasVendorDlkm: Boolean = false
7584
var isVendorDlkmMapped: Boolean = false
7685
var isVendorDlkmMounted: Boolean = false
@@ -103,10 +112,8 @@ class SlotViewModel(
103112
get() = _error
104113
val showCautionDialog: Boolean
105114
get() = _showCautionDialog.value
106-
val bootInfo: BootInfo
107-
get() = _bootInfo.value
108-
val bootSlotInfo: BootSlotInfo
109-
get() = _bootSlotInfo.value
115+
val slotInfo: SlotInfo
116+
get() = _slotInfo.value
110117

111118
init {
112119
refresh(context)
@@ -120,7 +127,9 @@ class SlotViewModel(
120127
fun refresh(context: Context) {
121128
_error = null
122129
_sha1 = null
123-
_bootInfo.value = _bootInfo.value.copy(kernelVersion = null, bootFmt = null, headerVersion = null, initBootFmt = null, ramdiskLocation = null)
130+
_slotInfo.value.bootSlotInfo = _slotInfo.value.bootSlotInfo.copy(null, null)
131+
_slotInfo.value.bootImgInfo = _slotInfo.value.bootImgInfo.copy(null, null, null)
132+
_slotInfo.value.ramdiskInfo = _slotInfo.value.ramdiskInfo.copy(null, null, null)
124133

125134
if (!isActive) {
126135
inInit = true
@@ -136,25 +145,28 @@ class SlotViewModel(
136145

137146
if(slotSuffix != "") {
138147
val resCode1 = Shell.cmd("$bootctl is-slot-bootable " + if (slotSuffix == "_a") "0" else "1").exec().code
139-
_bootSlotInfo.value.unbootable = if(resCode1 == 0) "No" else "Yes"
148+
_slotInfo.value.bootSlotInfo.unbootable = if(resCode1 == 0) "No" else "Yes"
140149
val resCode2 = Shell.cmd("$bootctl is-slot-marked-successful " + if (slotSuffix == "_a") "0" else "1").exec().code
141-
_bootSlotInfo.value.successful = if(resCode2 == 0) "Yes" else "No"
150+
_slotInfo.value.bootSlotInfo.successful = if(resCode2 == 0) "Yes" else "No"
142151
}
143152

144-
_bootInfo.value.headerVersion = extractKernelValues(bootUnpackOp.trimIndent(), HEADER_VER)
145-
_bootInfo.value.bootFmt = extractKernelValues(bootUnpackOp.trimIndent(), KERNEL_FMT)
146-
_bootInfo.value.initBootFmt = extractKernelValues(bootUnpackOp.trimIndent(), RAMDISK_FMT)
147-
if (_bootInfo.value.initBootFmt != null)
148-
_bootInfo.value.ramdiskLocation = "boot.img"
153+
_slotInfo.value.bootImgInfo.headerVersion = extractKernelValues(bootUnpackOp.trimIndent(), HEADER_VER)
154+
_slotInfo.value.bootImgInfo.bootFmt = extractKernelValues(bootUnpackOp.trimIndent(), KERNEL_FMT)
155+
_slotInfo.value.ramdiskInfo.ramdiskFmt = extractKernelValues(bootUnpackOp.trimIndent(), RAMDISK_FMT)
156+
if (_slotInfo.value.ramdiskInfo.ramdiskFmt != null)
157+
{
158+
_slotInfo.value.ramdiskInfo.ramdiskLocation = "boot.img"
159+
_slotInfo.value.ramdiskInfo.headerVersion = _slotInfo.value.bootImgInfo.headerVersion
160+
}
161+
Log.d(TAG, _slotInfo.value.bootImgInfo.toString())
149162

150-
Log.d(TAG, _bootInfo.value.toString())
151-
if (initBoot != null && _bootInfo.value.initBootFmt == null) {
163+
if (initBoot != null && _slotInfo.value.ramdiskInfo.ramdiskFmt == null) {
152164
val unpackInitBootOutput = mutableListOf<String>()
153165
if(Shell.cmd("$magiskboot unpack $initBoot").to(unpackInitBootOutput, unpackInitBootOutput).exec().isSuccess)
154166
{
155167
val initBootUnpackOp = unpackInitBootOutput.joinToString("\n")
156-
_bootInfo.value.initBootFmt = extractKernelValues(initBootUnpackOp.trimIndent(), RAMDISK_FMT)
157-
_bootInfo.value.ramdiskLocation = "init_boot.img"
168+
_slotInfo.value.ramdiskInfo.ramdiskFmt = extractKernelValues(initBootUnpackOp.trimIndent(), RAMDISK_FMT)
169+
_slotInfo.value.ramdiskInfo.ramdiskLocation = "init_boot.img"
158170
}
159171
}
160172

@@ -184,16 +196,16 @@ class SlotViewModel(
184196
}
185197
} else if (kernel.exists()) {
186198
_sha1 = Shell.cmd("$magiskboot sha1 $boot").exec().out.firstOrNull()
187-
if(_bootInfo.value.headerVersion.equals("4") && _bootInfo.value.ramdiskLocation.equals(null))
199+
if(_slotInfo.value.bootImgInfo.headerVersion.equals("4") && _slotInfo.value.ramdiskInfo.ramdiskLocation.equals(null))
188200
{
189-
_bootInfo.value.ramdiskLocation = "boot.img"
190-
_bootInfo.value.initBootFmt = "lz4_legacy"
201+
_slotInfo.value.ramdiskInfo.ramdiskLocation = "boot.img"
202+
_slotInfo.value.ramdiskInfo.ramdiskFmt = "lz4_legacy"
191203
}
192204
} else {
193-
if(_bootInfo.value.headerVersion.equals("4") && _bootInfo.value.ramdiskLocation.equals(null))
205+
if(_slotInfo.value.bootImgInfo.headerVersion.equals("4") && _slotInfo.value.ramdiskInfo.ramdiskLocation.equals(null))
194206
{
195-
_bootInfo.value.ramdiskLocation = "boot.img"
196-
_bootInfo.value.initBootFmt = "lz4_legacy"
207+
_slotInfo.value.ramdiskInfo.ramdiskLocation = "boot.img"
208+
_slotInfo.value.ramdiskInfo.ramdiskFmt = "lz4_legacy"
197209
}
198210
_error = "Unable to generate SHA1 hash. Invalid boot.img or magiskboot unpack failed!"
199211
}
@@ -203,7 +215,7 @@ class SlotViewModel(
203215
_backupPartitions[partitionName] = true
204216
}
205217

206-
_bootInfo.value.kernelVersion = null
218+
_slotInfo.value.bootImgInfo.kernelVersion = null
207219
inInit = false
208220
}
209221

@@ -323,7 +335,7 @@ class SlotViewModel(
323335
if (kernel.exists()) {
324336
val result = Shell.cmd("strings kernel | grep -E -m1 'Linux version.*#' | cut -d\\ -f3-").exec().out
325337
if (result.isNotEmpty()) {
326-
_bootInfo.value.kernelVersion = result[0].replace("""\(.+\)""".toRegex(), "").replace("""\s+""".toRegex(), " ")
338+
_slotInfo.value.bootImgInfo.kernelVersion = result[0].replace("""\(.+\)""".toRegex(), "").replace("""\s+""".toRegex(), " ")
327339
}
328340
}
329341
Shell.cmd("$magiskboot cleanup").exec()
@@ -450,13 +462,13 @@ class SlotViewModel(
450462
fun backup(context: Context) {
451463
launch {
452464
_clearFlash()
453-
val currentKernelVersion = if (_bootInfo.value.kernelVersion != null) {
454-
_bootInfo.value.kernelVersion
465+
val currentKernelVersion = if (_slotInfo.value.bootImgInfo.kernelVersion != null) {
466+
_slotInfo.value.bootImgInfo.kernelVersion
455467
} else if (isActive) {
456468
System.getProperty("os.version")!!
457469
} else {
458470
_getKernel(context)
459-
_bootInfo.value.kernelVersion
471+
_slotInfo.value.bootImgInfo.kernelVersion
460472
}
461473
val now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd--HH-mm"))
462474
val backupDir = createBackupDir(context, now)
@@ -483,7 +495,7 @@ class SlotViewModel(
483495
val now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd--HH-mm"))
484496
val backupDir = createBackupDir(context, now)
485497
val jsonFile = backupDir.getChildFile("backup.json")
486-
val backup = Backup(now, "ak3", _bootInfo.value.kernelVersion!!, null, flashFilename)
498+
val backup = Backup(now, "ak3", _slotInfo.value.bootImgInfo.kernelVersion!!, null, flashFilename)
487499
val indentedJson = Json { prettyPrint = true }
488500
jsonFile.outputStream().use { it.write(indentedJson.encodeToString(backup).toByteArray(Charsets.UTF_8)) }
489501
val destination = backupDir.getChildFile(flashFilename!!)
@@ -703,7 +715,7 @@ class SlotViewModel(
703715
if (driver.exists()) {
704716
addMessage("Copied $flashFilename")
705717
_wasFlashSuccess.value = false
706-
val partitionName = bootInfo.ramdiskLocation?.removeSuffix(".img") ?: "boot"
718+
val partitionName = _slotInfo.value.ramdiskInfo.ramdiskLocation?.removeSuffix(".img") ?: "boot"
707719
val magiskboot = File(context.filesDir, "magiskboot")
708720
val ksuinit = File(context.filesDir, "ksuinit")
709721
if(partitionName == "boot")

0 commit comments

Comments
 (0)