Skip to content

Commit e641907

Browse files
committed
Refactor code
1. Remove some warnings. 2. Remove some unused codes. 3. Add Page Refresh enable where refresh was required.
1 parent e70afa7 commit e641907

File tree

11 files changed

+156
-155
lines changed

11 files changed

+156
-155
lines changed

app/src/main/java/com/github/capntrips/kernelflasher/MainActivity.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import java.io.File
7575
import kotlin.system.exitProcess
7676

7777
object SharedViewModels {
78+
@OptIn(ExperimentalSerializationApi::class)
7879
lateinit var mainViewModel: MainViewModel
7980
}
8081

@@ -202,10 +203,14 @@ class MainActivity : ComponentActivity() {
202203
val action = intent?.action ?: return
203204
val uri = when (action) {
204205
Intent.ACTION_VIEW -> intent.data
205-
Intent.ACTION_SEND -> intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
206+
Intent.ACTION_SEND -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
207+
intent.getParcelableExtra(Intent.EXTRA_STREAM, Uri::class.java)
208+
} else {
209+
@Suppress("DEPRECATION")
210+
intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
211+
}
206212
else -> null
207213
} ?: return
208-
Log.d(MainViewModel.Companion.TAG, intent?.data.toString())
209214

210215
if (intent.action == Intent.ACTION_VIEW || intent.action == Intent.ACTION_SEND) {
211216
if(uri.scheme == "content" && DocumentsContract.isDocumentUri(this, uri)) {
@@ -339,7 +344,7 @@ class MainActivity : ComponentActivity() {
339344
val slotContentA: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit = { backStackEntry ->
340345
val slotSuffix = "_a"
341346
val slotViewModel = slotViewModelA
342-
if (slotViewModel!!.wasFlashSuccess.value != null && listOf("slot{slotSuffix}", "slot").any { navController.currentDestination!!.route.equals(it) }) {
347+
if (slotViewModel.wasFlashSuccess.value != null && listOf("slot{slotSuffix}", "slot").any { navController.currentDestination!!.route.equals(it) }) {
343348
slotViewModel.clearFlash(this@MainActivity)
344349
}
345350
RefreshableScreen(mainViewModel, navController, swipeEnabled = true) {
@@ -361,7 +366,7 @@ class MainActivity : ComponentActivity() {
361366
val slotContent: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit = { backStackEntry ->
362367
val slotSuffix = ""
363368
val slotViewModel = slotViewModelA
364-
if (slotViewModel!!.wasFlashSuccess.value != null && listOf("slot{slotSuffix}", "slot").any { navController.currentDestination!!.route.equals(it) }) {
369+
if (slotViewModel.wasFlashSuccess.value != null && listOf("slot{slotSuffix}", "slot").any { navController.currentDestination!!.route.equals(it) }) {
365370
slotViewModel.clearFlash(this@MainActivity)
366371
}
367372
RefreshableScreen(mainViewModel, navController, swipeEnabled = true) {
@@ -373,7 +378,7 @@ class MainActivity : ComponentActivity() {
373378
val slotSuffix = "_a"
374379
val slotViewModel = slotViewModelA
375380
RefreshableScreen(mainViewModel, navController) {
376-
SlotFlashContent(slotViewModel!!, slotSuffix, navController)
381+
SlotFlashContent(slotViewModel, slotSuffix, navController)
377382
}
378383
}
379384
val slotFlashContentB: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit = { backStackEntry ->
@@ -387,7 +392,7 @@ class MainActivity : ComponentActivity() {
387392
val slotSuffix = ""
388393
val slotViewModel = slotViewModelA
389394
RefreshableScreen(mainViewModel, navController) {
390-
SlotFlashContent(slotViewModel!!, slotSuffix, navController)
395+
SlotFlashContent(slotViewModel, slotSuffix, navController)
391396
}
392397
}
393398
val slotBackupsContentA: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit = { backStackEntry ->
@@ -399,7 +404,7 @@ class MainActivity : ComponentActivity() {
399404
backupsViewModel.clearCurrent()
400405
}
401406
RefreshableScreen(mainViewModel, navController) {
402-
SlotBackupsContent(slotViewModel!!, backupsViewModel, slotSuffix, navController)
407+
SlotBackupsContent(slotViewModel, backupsViewModel, slotSuffix, navController)
403408
}
404409
}
405410
val slotBackupsContentB: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit = { backStackEntry ->
@@ -423,7 +428,7 @@ class MainActivity : ComponentActivity() {
423428
backupsViewModel.clearCurrent()
424429
}
425430
RefreshableScreen(mainViewModel, navController) {
426-
SlotBackupsContent(slotViewModel!!, backupsViewModel, slotSuffix, navController)
431+
SlotBackupsContent(slotViewModel, backupsViewModel, slotSuffix, navController)
427432
}
428433
}
429434
val slotBackupFlashContentA: @Composable AnimatedVisibilityScope.(NavBackStackEntry) -> Unit = { backStackEntry ->
@@ -432,7 +437,7 @@ class MainActivity : ComponentActivity() {
432437
backupsViewModel.currentBackup = backStackEntry.arguments?.getString("backupId")
433438
if (backupsViewModel.backups.containsKey(backupsViewModel.currentBackup)) {
434439
RefreshableScreen(mainViewModel, navController) {
435-
SlotFlashContent(slotViewModel!!, slotSuffix, navController)
440+
SlotFlashContent(slotViewModel, slotSuffix, navController)
436441
}
437442
}
438443

@@ -454,7 +459,7 @@ class MainActivity : ComponentActivity() {
454459
backupsViewModel.currentBackup = backStackEntry.arguments?.getString("backupId")
455460
if (backupsViewModel.backups.containsKey(backupsViewModel.currentBackup)) {
456461
RefreshableScreen(mainViewModel, navController) {
457-
SlotFlashContent(slotViewModel!!, slotSuffix, navController)
462+
SlotFlashContent(slotViewModel, slotSuffix, navController)
458463
}
459464
}
460465

@@ -570,22 +575,22 @@ class MainActivity : ComponentActivity() {
570575
onDismissRequest = { viewModel!!.hideUpdateDialog() },
571576
title = {
572577
Text(
573-
dialogData!!.title,
578+
dialogData.title,
574579
style = MaterialTheme.typography.titleLarge,
575580
fontWeight = FontWeight.Bold
576581
)
577582
},
578583
text = {
579584
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
580-
dialogData!!.changelog.forEach {
585+
dialogData.changelog.forEach {
581586
Text(it, fontWeight = FontWeight.Bold)
582587
}
583588
}
584589
},
585590
confirmButton = {
586591
DialogButton("Update APK") {
587592
viewModel!!.hideUpdateDialog()
588-
dialogData!!.onConfirm()
593+
dialogData.onConfirm()
589594
}
590595
},
591596
dismissButton = {

app/src/main/java/com/github/capntrips/kernelflasher/common/PartitionUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ object PartitionUtil {
101101
throw Error("Partition ${blockDevice.name} is smaller than image")
102102
}
103103
if (partitionSize > imageSize) {
104-
Shell.cmd("dd bs=4096 if=/dev/zero of=$blockDevice").exec()
104+
Shell.cmd("dd bs=4096 if=/dev/zero of=$blockDevice && sync").exec()
105105
}
106106
val messageDigest = MessageDigest.getInstance(hashAlgorithm)
107107
image.newInputStream().use { inputStream ->

app/src/main/java/com/github/capntrips/kernelflasher/common/types/partitions/Partitions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ data class Partitions(
3030
}.partitions
3131
}
3232

33-
fun get(partition: String): String? {
33+
operator fun get(partition: String): String? {
3434
return when (partition) {
3535
"boot" -> boot
3636
"dtbo" -> dtbo

app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/backups/BackupsContent.kt

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,39 @@ fun ColumnScope.BackupsContent(
3737
navController: NavController
3838
) {
3939
val context = LocalContext.current
40+
41+
val monoStyle = MaterialTheme.typography.titleSmall.copy(
42+
fontFamily = FontFamily.Monospace,
43+
fontWeight = FontWeight.Medium
44+
)
45+
4046
if (viewModel.currentBackup != null && viewModel.backups.containsKey(viewModel.currentBackup)) {
4147
DataCard (viewModel.currentBackup!!) {
4248
val cardWidth = remember { mutableIntStateOf(0) }
43-
val currentBackup = viewModel.backups.getValue(viewModel.currentBackup!!)
49+
val backupId = viewModel.currentBackup!!
50+
val currentBackup = viewModel.backups[backupId]
51+
if(currentBackup == null) return@DataCard
4452
DataRow(stringResource(R.string.backup_type), currentBackup.type, mutableMaxWidth = cardWidth)
4553
DataRow(stringResource(R.string.kernel_version), currentBackup.kernelVersion, mutableMaxWidth = cardWidth, clickable = true)
4654
if (currentBackup.type == "raw") {
47-
if (!currentBackup.bootSha1.isNullOrEmpty()) {
55+
currentBackup.bootSha1?.takeIf { it.length >= 8 }?.let { sha1 ->
4856
DataRow(
4957
label = stringResource(R.string.boot_sha1),
50-
value = currentBackup.bootSha1.substring(0, 8),
51-
valueStyle = MaterialTheme.typography.titleSmall.copy(
52-
fontFamily = FontFamily.Monospace,
53-
fontWeight = FontWeight.Medium
54-
),
58+
value = sha1.substring(0, 8),
59+
valueStyle = monoStyle,
5560
mutableMaxWidth = cardWidth
5661
)
5762
}
5863
if (currentBackup.hashes != null) {
5964
val hashWidth = remember { mutableIntStateOf(0) }
6065
DataSet(stringResource(R.string.hashes)) {
6166
for (partitionName in PartitionUtil.PartitionNames) {
62-
val hash = currentBackup.hashes.get(partitionName)
67+
val hash = currentBackup.hashes[partitionName]
6368
if (hash != null) {
6469
DataRow(
6570
label = partitionName,
6671
value = hash.takeIf { it.isNotEmpty() }?.substring(0, 8) ?: "Hash not found!",
67-
valueStyle = MaterialTheme.typography.titleSmall.copy(
68-
fontFamily = FontFamily.Monospace,
69-
fontWeight = FontWeight.Medium
70-
),
72+
valueStyle = monoStyle,
7173
mutableMaxWidth = hashWidth
7274
)
7375
}
@@ -121,16 +123,15 @@ fun ColumnScope.BackupsContent(
121123
}
122124
) {
123125
val cardWidth = remember { mutableIntStateOf(0) }
124-
if (currentBackup.type == "raw" && !currentBackup.bootSha1.isNullOrEmpty()) {
125-
DataRow(
126-
label = stringResource(R.string.boot_sha1),
127-
value = currentBackup.bootSha1.substring(0, 8),
128-
valueStyle = MaterialTheme.typography.titleSmall.copy(
129-
fontFamily = FontFamily.Monospace,
130-
fontWeight = FontWeight.Medium
131-
),
132-
mutableMaxWidth = cardWidth
133-
)
126+
if (currentBackup.type == "raw") {
127+
currentBackup.bootSha1?.takeIf { it.length >= 8 }?.let { sha1 ->
128+
DataRow(
129+
label = stringResource(R.string.boot_sha1),
130+
value = sha1.substring(0, 8),
131+
valueStyle = monoStyle,
132+
mutableMaxWidth = cardWidth
133+
)
134+
}
134135
}
135136
DataRow(stringResource(R.string.kernel_version), currentBackup.kernelVersion, mutableMaxWidth = cardWidth, clickable = true)
136137
}

app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/backups/BackupsViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import androidx.compose.runtime.snapshots.SnapshotStateMap
1313
import androidx.lifecycle.ViewModel
1414
import androidx.lifecycle.viewModelScope
1515
import androidx.navigation.NavController
16+
import com.github.capntrips.kernelflasher.SharedViewModels
1617
import com.github.capntrips.kernelflasher.common.PartitionUtil
1718
import com.github.capntrips.kernelflasher.common.extensions.ExtendedFile.outputStream
1819
import com.github.capntrips.kernelflasher.common.extensions.ExtendedFile.readText
@@ -25,6 +26,7 @@ import kotlin.DeprecationLevel
2526
import kotlinx.coroutines.Dispatchers
2627
import kotlinx.coroutines.launch
2728
import kotlinx.coroutines.withContext
29+
import kotlinx.serialization.ExperimentalSerializationApi
2830
import kotlinx.serialization.json.Json
2931
import java.io.File
3032
import java.io.FileInputStream
@@ -49,7 +51,7 @@ class BackupsViewModel(
4951
if (value != field) {
5052
if (_backups[value]?.hashes != null) {
5153
PartitionUtil.AvailablePartitions.forEach { partitionName ->
52-
if (_backups[value]!!.hashes!!.get(partitionName) != null) {
54+
if (_backups[value]!!.hashes!![partitionName] != null) {
5355
_backupPartitions[partitionName] = true
5456
}
5557
}
@@ -236,6 +238,7 @@ class BackupsViewModel(
236238
}
237239
}
238240

241+
@OptIn(ExperimentalSerializationApi::class)
239242
@SuppressLint("SdCardPath")
240243
@Deprecated("Backup migration will be removed in the first stable release")
241244
fun migrate(context: Context) {
@@ -290,6 +293,7 @@ class BackupsViewModel(
290293
}
291294
oldBackupsDir.delete()
292295
}
296+
SharedViewModels.mainViewModel.markRefreshNeeded()
293297
refresh(context)
294298
}
295299
}

app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/backups/SlotBackupsContent.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ fun ColumnScope.SlotBackupsContent(
5151
navController: NavController
5252
) {
5353
val context = LocalContext.current
54-
if (!navController.currentDestination!!.route!!.contains("/backups/{backupId}/restore")) {
54+
55+
val monoStyle = MaterialTheme.typography.titleSmall.copy(
56+
fontFamily = FontFamily.Monospace,
57+
fontWeight = FontWeight.Medium
58+
)
59+
val currentRoute = navController.currentDestination?.route.orEmpty()
60+
61+
if (!currentRoute.contains("/backups/{backupId}/restore")) {
5562
SlotCard(
5663
title = stringResource(if (slotSuffix == "_a") R.string.slot_a else if (slotSuffix == "_b") R.string.slot_b else R.string.slot),
5764
viewModel = slotViewModel,
@@ -67,31 +74,24 @@ fun ColumnScope.SlotBackupsContent(
6774
DataRow(stringResource(R.string.backup_type), currentBackup.type, mutableMaxWidth = cardWidth)
6875
DataRow(stringResource(R.string.kernel_version), currentBackup.kernelVersion, mutableMaxWidth = cardWidth, clickable = true)
6976
if (currentBackup.type == "raw") {
70-
if(!currentBackup.bootSha1.isNullOrEmpty())
71-
{
77+
currentBackup.bootSha1?.takeIf { it.length >= 8 }?.let { sha1 ->
7278
DataRow(
7379
label = stringResource(R.string.boot_sha1),
74-
value = currentBackup.bootSha1.substring(0, 8),
75-
valueStyle = MaterialTheme.typography.titleSmall.copy(
76-
fontFamily = FontFamily.Monospace,
77-
fontWeight = FontWeight.Medium
78-
),
80+
value = sha1.substring(0, 8),
81+
valueStyle = monoStyle,
7982
mutableMaxWidth = cardWidth
8083
)
8184
}
8285
if (currentBackup.hashes != null) {
8386
val hashWidth = remember { mutableIntStateOf(0) }
8487
DataSet(stringResource(R.string.hashes)) {
8588
for (partitionName in PartitionUtil.PartitionNames) {
86-
val hash = currentBackup.hashes.get(partitionName)
89+
val hash = currentBackup.hashes[partitionName]
8790
if (hash != null) {
8891
DataRow(
8992
label = partitionName,
90-
value = hash.takeIf { it.isNotEmpty() }?.substring(0, 8) ?: "Hash not found!",
91-
valueStyle = MaterialTheme.typography.titleSmall.copy(
92-
fontFamily = FontFamily.Monospace,
93-
fontWeight = FontWeight.Medium
94-
),
93+
value = hash.takeIf { it.isNotEmpty() && it.length >= 8 }?.substring(0, 8) ?: "Hash not found!",
94+
valueStyle = monoStyle,
9595
mutableMaxWidth = hashWidth
9696
)
9797
}
@@ -193,7 +193,7 @@ fun ColumnScope.SlotBackupsContent(
193193
val currentBackup = backupsViewModel.backups.getValue(backupsViewModel.currentBackup!!)
194194
if (currentBackup.hashes != null) {
195195
for (partitionName in PartitionUtil.PartitionNames) {
196-
val hash = currentBackup.hashes.get(partitionName)
196+
val hash = currentBackup.hashes[partitionName]
197197
if (hash != null) {
198198
OutlinedButton(
199199
modifier = Modifier
@@ -235,7 +235,9 @@ fun ColumnScope.SlotBackupsContent(
235235
popUpTo("slot$slotSuffix")
236236
}
237237
},
238-
enabled = currentBackup.hashes == null || (PartitionUtil.PartitionNames.none { currentBackup.hashes.get(it) != null && backupsViewModel.backupPartitions[it] == null } && backupsViewModel.backupPartitions.filter { it.value }.isNotEmpty())
238+
enabled = currentBackup.hashes == null || (PartitionUtil.PartitionNames.none {
239+
currentBackup.hashes[it] != null && backupsViewModel.backupPartitions[it] == null
240+
} && backupsViewModel.backupPartitions.filter { it.value }.isNotEmpty())
239241
) {
240242
Text(stringResource(R.string.restore))
241243
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,14 @@ fun ColumnScope.MainContent(
3737
DataRow(stringResource(R.string.model), "${Build.MODEL} (${Build.DEVICE})", mutableMaxWidth = cardWidth)
3838
DataRow(stringResource(R.string.build_number), Build.ID, mutableMaxWidth = cardWidth)
3939
DataRow(stringResource(R.string.kernel_version), viewModel.kernelVersion, mutableMaxWidth = cardWidth, clickable = true)
40-
if (viewModel.isAb) {
40+
if (viewModel.isAb)
4141
DataRow(stringResource(R.string.slot_suffix), viewModel.slotSuffix, mutableMaxWidth = cardWidth)
42-
}
42+
4343
if(viewModel.susfsVersion != "v0.0.0" && viewModel.susfsVersion != "Invalid")
44-
{
4544
DataRow(stringResource(R.string.susfs_version), viewModel.susfsVersion, mutableMaxWidth = cardWidth)
46-
}
45+
4746
if(viewModel.halInfo != "")
48-
{
4947
DataRow("Boot HAL version", viewModel.halInfo, mutableMaxWidth = cardWidth)
50-
}
5148
}
5249
Spacer(Modifier.height(16.dp))
5350
SlotCard(

0 commit comments

Comments
 (0)