Skip to content

Commit

Permalink
wip refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 11, 2024
1 parent c9c27b1 commit 4d58107
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 81 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFile
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.*
import okio.*
import org.andbootmgr.app.util.AbmTheme
Expand Down Expand Up @@ -351,7 +355,7 @@ private fun Shop(c: CreatePartDataHolder) {
val ctx = LocalContext.current
LaunchedEffect(Unit) {
c.run {
Thread {
CoroutineScope(Dispatchers.IO).launch {
try {
val jsonText = try {
ctx.assets.open("abm.json").readBytes().toString(Charsets.UTF_8)
Expand Down Expand Up @@ -838,7 +842,7 @@ private fun Download(c: CreatePartDataHolder) {
SOUtils.humanReadableByteCountBin(bytesRead), SOUtils.humanReadableByteCountBin(contentLength))
}
}
Thread {
CoroutineScope(Dispatchers.IO).launch {
try {
val downloadedFile = File(c.vm.logic.cacheDir, i)
val request =
Expand Down Expand Up @@ -868,7 +872,7 @@ private fun Download(c: CreatePartDataHolder) {
c.chosen[i] = DledFile(null, downloadedFile)
} catch (e: Exception) {
Log.e("ABM", Log.getStackTraceString(e))
c.vm.activity.runOnUiThread {
withContext(Dispatchers.Main) {
Toast.makeText(
c.vm.activity,
c.vm.activity.getString(R.string.dl_error),
Expand All @@ -878,7 +882,7 @@ private fun Download(c: CreatePartDataHolder) {
}
c.pl = null
downloading = false
}.start()
}
}) {
Text(stringResource(R.string.download))
}
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import androidx.compose.ui.unit.dp
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFile
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.AbmTheme
import org.andbootmgr.app.util.ConfigFile
import org.andbootmgr.app.util.SDUtils
Expand Down Expand Up @@ -142,7 +146,7 @@ fun SelectDroidBoot(vm: WizardActivityState) {
}
val ctx = LocalContext.current
Button(onClick = {
Thread {
CoroutineScope(Dispatchers.Default).launch {
try {
val jsonText =
URL("https://raw.githubusercontent.com/Android-Boot-Manager/ABM-json/master/devices/" + vm.codename + ".json").readText()
Expand All @@ -160,7 +164,7 @@ fun SelectDroidBoot(vm: WizardActivityState) {
}
Log.e("ABM droidboot json", Log.getStackTraceString(e))
}
}.start()
}
}) {
Text(stringResource(id = R.string.download))
}
Expand Down Expand Up @@ -193,7 +197,7 @@ fun SelectInstallSh(vm: WizardActivityState, update: Boolean = false) {
}
val ctx = LocalContext.current
Button(onClick = {
Thread {
CoroutineScope(Dispatchers.IO).launch {
try {
val jsonText =
URL("https://raw.githubusercontent.com/Android-Boot-Manager/ABM-json/master/devices/" + vm.codename + ".json").readText()
Expand All @@ -211,7 +215,7 @@ fun SelectInstallSh(vm: WizardActivityState, update: Boolean = false) {
}
Log.e("ABM install json", Log.getStackTraceString(e))
}
}.start()
}
}) {
Text(stringResource(id = R.string.download))
}
Expand Down Expand Up @@ -354,7 +358,7 @@ private fun Flash(vm: WizardActivityState) {
}
terminal.add(vm.activity.getString(R.string.term_success))
vm.logic.unmountBootset()
vm.activity.runOnUiThread {
withContext(Dispatchers.Main) {
vm.btnsOverride = true
vm.nextText.value = vm.activity.getString(R.string.finish)
vm.onNext.value = {
Expand Down
63 changes: 32 additions & 31 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class MainActivity : ComponentActivity() {

val toast =
Toast.makeText(this, getString(R.string.toolkit_extracting), Toast.LENGTH_LONG)
Thread {
CoroutineScope(Dispatchers.IO).launch {
if (Shell.getCachedShell() == null) {
Shell.enableVerboseLogging = BuildConfig.DEBUG
Shell.setDefaultBuilder(
Expand All @@ -186,17 +186,39 @@ class MainActivity : ComponentActivity() {
.setTimeout(30)
)
}
Toolkit(this).copyAssets({
runOnUiThread {
Toolkit(this@MainActivity).copyAssets({
withContext(Dispatchers.Main) {
toast.show()
}
}) { fail ->
runOnUiThread {
withContext(Dispatchers.Main) {
toast.cancel()
if (fail) {
setContent {
AlertDialog(
onDismissRequest = {},
title = {
Text(text = getString(R.string.error))
},
text = {
Text(getString(R.string.toolkit_error))
},
confirmButton = {
Button(
onClick = {
finish()
}) {
Text(getString(R.string.quit))
}
}
)
}
vm.isReady = true
}
}
if (!fail) {
Shell.getShell { shell ->
Thread {
CoroutineScope(Dispatchers.IO).launch {
vm.root = shell.isRoot
vm.deviceInfo = JsonDeviceInfoFactory(vm.activity!!).get(Build.DEVICE)
// == temp migration code start ==
Expand All @@ -218,7 +240,7 @@ class MainActivity : ComponentActivity() {
vm.deviceInfo!!.isBooted(vm.logic!!) &&
!(!vm.logic!!.mounted || vm.deviceInfo!!.isCorrupt(vm.logic!!)))
}
runOnUiThread {
withContext(Dispatchers.Main) {
setContent {
val navController = rememberNavController()
val drawerState = rememberDrawerState(DrawerValue.Closed)
Expand All @@ -235,32 +257,11 @@ class MainActivity : ComponentActivity() {
}
vm.isReady = true
}
}.start()
}
} else {
setContent {
AlertDialog(
onDismissRequest = {},
title = {
Text(text = getString(R.string.error))
},
text = {
Text(getString(R.string.toolkit_error))
},
confirmButton = {
Button(
onClick = {
finish()
}) {
Text(getString(R.string.quit))
}
}
)
}
}
vm.isReady = true
}
}
}.start()
}
}
}

Expand Down Expand Up @@ -1076,7 +1077,7 @@ private fun PartTool(vm: MainActivityState) {
Button(onClick = {
processing = true
delete = false
Thread {
CoroutineScope(Dispatchers.Default).launch {
var tresult = ""
if (e.has("xpart") && !e["xpart"].isNullOrBlank()) {
val allp = e["xpart"]!!.split(":")
Expand All @@ -1100,7 +1101,7 @@ private fun PartTool(vm: MainActivityState) {
processing = false
parts = SDUtils.generateMeta(vm.deviceInfo!!)
result = tresult
}.start()
}
}) {
Text(stringResource(R.string.delete))
}
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
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.AbmTheme
import java.io.File
import java.io.IOException
Expand Down Expand Up @@ -82,7 +84,7 @@ private fun Flash(vm: WizardActivityState) {
tmpFile.delete()
}
terminal.add(vm.activity.getString(R.string.term_success))
vm.activity.runOnUiThread {
withContext(Dispatchers.Main) {
vm.btnsOverride = true
vm.nextText.value = vm.activity.getString(R.string.finish)
vm.onNext.value = {
Expand Down
19 changes: 10 additions & 9 deletions app/src/main/java/org/andbootmgr/app/WizardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.andbootmgr.app.util.AbmTheme
import java.io.File
import java.io.FileInputStream
Expand Down Expand Up @@ -320,7 +321,7 @@ fun Terminal(vm: WizardActivityState, logFile: String? = null, action: suspend (
val scope = rememberCoroutineScope()
val text = remember { mutableStateOf("") }
val ctx = LocalContext.current
DisposableEffect(Unit) {
DisposableEffect(Unit) { // TODO don't run again on UI recreation
val log = logFile?.let { FileOutputStream(File(ctx.externalCacheDir, it)) }
// Budget CallbackList
val s = object : MutableList<String> {
Expand Down Expand Up @@ -417,21 +418,21 @@ fun Terminal(vm: WizardActivityState, logFile: String? = null, action: suspend (
}

fun onAdd(element: String) {
vm.activity.runOnUiThread {
scope.launch {
text.value += element + "\n"
scope.launch {
delay(200) // Give it time to re-measure
scrollV.animateScrollTo(scrollV.maxValue)
scrollH.animateScrollTo(0)
}
delay(200) // Give it time to re-measure
scrollV.animateScrollTo(scrollV.maxValue)
scrollH.animateScrollTo(0)
}
log?.write((element + "\n").encodeToByteArray())
}

}
CoroutineScope(Dispatchers.Default).launch {
CoroutineScope(Dispatchers.Default).launch { // do not bind to composable, we must never die
try {
action(s)
withContext(Dispatchers.Default) {
action(s)
}
} catch (e: Throwable) {
s.add(vm.activity.getString(R.string.term_failure))
s.add(vm.activity.getString(R.string.dev_details))
Expand Down
34 changes: 18 additions & 16 deletions app/src/main/java/org/andbootmgr/app/themes/Themes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fun Themes(vm: ThemeViewModel) {
label = { Text(stringResource(id = cfg.text)) },
isError = error,
keyboardOptions = KeyboardOptions(
keyboardType = if (cfg is NumberConfig)
keyboardType = if (cfg is IntConfig || cfg is ShortConfig)
KeyboardType.Decimal else KeyboardType.Text,
capitalization = KeyboardCapitalization.None,
imeAction = ImeAction.Next,
Expand All @@ -256,20 +256,22 @@ class ColorConfig(text: Int, configKey: String, default: String) : Config(text,
(it.substring(2).toIntOrNull(16) ?: -1) in 0..0xffffff })
class BoolConfig(text: Int, configKey: String, default: String) : Config(text, configKey,
default, { it.toBooleanStrictOrNull() != null})
class NumberConfig(text: Int, configKey: String, default: String, validate: (String) -> Boolean)
: Config(text, configKey, default, validate)
class IntConfig(text: Int, configKey: String, default: String)
: Config(text, configKey, default, { it.toIntOrNull() != null })
class ShortConfig(text: Int, configKey: String, default: String)
: Config(text, configKey, default, { it.toShortOrNull() != null })

class ThemeViewModel(val mvm: MainActivityState) : ViewModel() {
val configs = listOf(
ColorConfig(R.string.win_bg_color, "win_bg_color", "0x000000"),
NumberConfig(R.string.win_radius, "win_radius", "0") { it.toShortOrNull() != null },
NumberConfig(R.string.win_border_size, "win_border_size", "0") { it.toShortOrNull() != null },
ShortConfig(R.string.win_radius, "win_radius", "0"),
ShortConfig(R.string.win_border_size, "win_border_size", "0"),
ColorConfig(R.string.win_border_color, "win_border_color", "0xffffff"),
ColorConfig(R.string.list_bg_color, "list_bg_color", "0x000000"),
NumberConfig(R.string.list_radius, "list_radius", "0") { it.toShortOrNull() != null },
NumberConfig(R.string.list_border_size, "list_border_size", "0") { it.toShortOrNull() != null },
ShortConfig(R.string.list_radius, "list_radius", "0"),
ShortConfig(R.string.list_border_size, "list_border_size", "0"),
ColorConfig(R.string.list_border_color, "list_border_color", "0xffffff"),
NumberConfig(R.string.global_font_size, "global_font_size", "0") { it.toIntOrNull() != null },
IntConfig(R.string.global_font_size, "global_font_size", "0"),
Config(
R.string.global_font_name,
"global_font_name",
Expand All @@ -281,18 +283,18 @@ class ThemeViewModel(val mvm: MainActivityState) : ViewModel() {
"button_unselected_text_color",
"0xffffff"
),
NumberConfig(
ShortConfig(
R.string.button_unselected_radius,
"button_unselected_radius",
"0"
) { it.toShortOrNull() != null },
),
ColorConfig(R.string.button_selected_color, "button_selected_color", "0xff9800"),
ColorConfig(R.string.button_selected_text_color, "button_selected_text_color", "0x000000"),
NumberConfig(
ShortConfig(
R.string.button_selected_radius,
"button_selected_radius",
"0"
) { it.toShortOrNull() != null },
),
BoolConfig(
R.string.button_grow_default,
"button_grow_default",
Expand All @@ -303,21 +305,21 @@ class ThemeViewModel(val mvm: MainActivityState) : ViewModel() {
"button_border_unselected_color",
"0xffffff"
),
NumberConfig(
IntConfig(
R.string.button_border_unselected_size,
"button_border_unselected_size",
"1"
) { it.toIntOrNull() != null },
),
ColorConfig(
R.string.button_border_selected_color,
"button_border_selected_color",
"0xffffff"
),
NumberConfig(
IntConfig(
R.string.button_border_selected_size,
"button_border_selected_size",
"1"
) { it.toIntOrNull() != null }
)
)
}

Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/org/andbootmgr/app/util/StayAliveService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.andbootmgr.app.util

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder

class StayAliveService : Service() {
override fun onCreate() {
super.onCreate()
startForeground(FG_SERVICE_ID, notif) // TODO
}

override fun onBind(intent: Intent?): IBinder {
return object : Binder(), Provider {
override val service
get() = this@StayAliveService
}
}

interface Provider {
val service: StayAliveService
}
}
Loading

0 comments on commit 4d58107

Please sign in to comment.