Skip to content

Commit

Permalink
More refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 13, 2024
1 parent 10159f3 commit e391593
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 221 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 @@ -121,7 +122,7 @@ private fun SelectDroidBoot(c: CreateBackupDataHolder) {

@Composable
private fun Flash(c: CreateBackupDataHolder) {
Terminal(c.vm, logFile = "flash_${System.currentTimeMillis()}.txt") { terminal ->
Terminal(logFile = "flash_${System.currentTimeMillis()}.txt") { terminal ->
terminal.add(c.vm.activity.getString(R.string.term_starting))
try {
val p = c.meta!!.dumpKernelPartition(c.pi)
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.andbootmgr.app.util.AbmTheme
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 java.io.File
import java.io.FileInputStream
import java.io.InputStream
Expand Down Expand Up @@ -912,7 +913,7 @@ private fun Download(c: CreatePartDataHolder) {
@Composable
private fun Flash(c: CreatePartDataHolder) {
val vm = c.vm
Terminal(vm, logFile = "install_${System.currentTimeMillis()}.txt") { terminal ->
Terminal(logFile = "install_${System.currentTimeMillis()}.txt") { terminal ->
if (c.t == null) { // OS install
val parts = ArrayMap<Int, Int>()
val fn = c.t2.value
Expand Down Expand Up @@ -1019,7 +1020,7 @@ private fun Flash(c: CreatePartDataHolder) {
vm.logic.unmountBootset()
val r = vm.logic.create(c.p, offset, offset + k, code, "").to(terminal).exec()
try {
if (r.out.join("\n").contains("kpartx")) {
if (r.out.joinToString("\n").contains("kpartx")) {
terminal.add(vm.activity.getString(R.string.term_reboot_asap))
}
parts[it] = c.meta!!.nid
Expand Down Expand Up @@ -1054,7 +1055,7 @@ private fun Flash(c: CreatePartDataHolder) {
"0700",
c.t!!
).to(terminal).exec()
if (r.out.join("\n").contains("kpartx")) {
if (r.out.joinToString("\n").contains("kpartx")) {
terminal.add(vm.activity.getString(R.string.term_reboot_asap))
}
if (r.isSuccess) {
Expand Down
73 changes: 39 additions & 34 deletions app/src/main/java/org/andbootmgr/app/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import android.content.Context
import android.util.Log
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.andbootmgr.app.util.SDUtils
import org.json.JSONObject
import org.json.JSONTokener
Expand Down Expand Up @@ -38,7 +40,7 @@ interface DeviceInfo {
e.printStackTrace()
}
val result = Shell.cmd("grep ABM.bootloader=1 /proc/cmdline").exec()
return result.isSuccess && result.out.join("\n").contains("ABM.bootloader=1")
return result.isSuccess && result.out.joinToString("\n").contains("ABM.bootloader=1")
}
fun isCorrupt(logic: DeviceLogic): Boolean
fun getAbmSettings(logic: DeviceLogic): String?
Expand Down Expand Up @@ -77,42 +79,45 @@ class JsonDeviceInfo(
) : MetaOnSdDeviceInfo()

class JsonDeviceInfoFactory(private val ctx: Context) {
fun get(codename: String): DeviceInfo? {
suspend fun get(codename: String): DeviceInfo? {
return try {
var fromNet = true
val jsonText = try {
try {
ctx.assets.open("abm.json").readBytes().toString(Charsets.UTF_8)
} catch (e: FileNotFoundException) {
URL("https://raw.githubusercontent.com/Android-Boot-Manager/ABM-json/master/devices/$codename.json").readText()
withContext(Dispatchers.IO) {
var fromNet = true
val jsonText = try {
try {
ctx.assets.open("abm.json").readBytes().toString(Charsets.UTF_8)
} catch (e: FileNotFoundException) {
URL("https://raw.githubusercontent.com/Android-Boot-Manager/ABM-json/master/devices/$codename.json").readText()
}
} catch (e: Exception) {
fromNet = false
Log.e("ABM device info", Log.getStackTraceString(e))
val f = File(ctx.filesDir, "abm_dd_cache.json")
if (f.exists()) f.readText() else
ctx.assets.open("abm_fallback/$codename.json").readBytes()
.toString(Charsets.UTF_8)
}
} catch (e: Exception) {
fromNet = false
Log.e("ABM device info", Log.getStackTraceString(e))
val f = File(ctx.filesDir, "abm_dd_cache.json")
if (f.exists()) f.readText() else
ctx.assets.open("abm_fallback/$codename.json").readBytes().toString(Charsets.UTF_8)
}
val jsonRoot = JSONTokener(jsonText).nextValue() as JSONObject? ?: return null
val json = jsonRoot.getJSONObject("deviceInfo")
if (BuildConfig.VERSION_CODE < jsonRoot.getInt("minAppVersion"))
throw IllegalStateException("please upgrade app")
if (fromNet) {
val newRoot = JSONObject()
newRoot.put("deviceInfo", json)
newRoot.put("minAppVersion", jsonRoot.getInt("minAppVersion"))
File(ctx.filesDir, "abm_dd_cache.json").writeText(newRoot.toString())
val jsonRoot = JSONTokener(jsonText).nextValue() as JSONObject? ?: return@withContext null
val json = jsonRoot.getJSONObject("deviceInfo")
if (BuildConfig.VERSION_CODE < jsonRoot.getInt("minAppVersion"))
throw IllegalStateException("please upgrade app")
if (fromNet) {
val newRoot = JSONObject()
newRoot.put("deviceInfo", json)
newRoot.put("minAppVersion", jsonRoot.getInt("minAppVersion"))
File(ctx.filesDir, "abm_dd_cache.json").writeText(newRoot.toString())
}
if (!json.getBoolean("metaOnSd"))
throw IllegalArgumentException("sd less currently not implemented")
JsonDeviceInfo(
json.getString("codename"),
json.getString("blBlock"),
json.getString("sdBlock"),
json.getString("sdBlockP"),
json.getBoolean("postInstallScript"),
json.getBoolean("haveDtbo")
)
}
if (!json.getBoolean("metaOnSd"))
throw IllegalArgumentException("sd less currently not implemented")
JsonDeviceInfo(
json.getString("codename"),
json.getString("blBlock"),
json.getString("sdBlock"),
json.getString("sdBlockP"),
json.getBoolean("postInstallScript"),
json.getBoolean("haveDtbo")
)
} catch (e: Exception) {
Log.e("ABM device info", Log.getStackTraceString(e))
null
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/DeviceLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DeviceLogic(ctx: Context) {
.cmd("mount $ast ${abmBootset.absolutePath}")
.exec()
if (!result.isSuccess) {
val out = result.out.join("\n") + result.err.join("\n")
val out = result.out.joinToString("\n") + result.err.joinToString("\n")
if (out.contains("Device or resource busy")) {
mounted = false
}
Expand All @@ -45,7 +45,7 @@ class DeviceLogic(ctx: Context) {
if (!checkMounted()) return true
val result = Shell.cmd("umount ${abmBootset.absolutePath}").exec()
if (!result.isSuccess) {
val out = result.out.join("\n") + result.err.join("\n")
val out = result.out.joinToString("\n") + result.err.joinToString("\n")
if (out.contains("Device or resource busy")) {
mounted = true
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import com.topjohnwu.superuser.io.SuFile
import com.topjohnwu.superuser.io.SuFileInputStream
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 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 @@ -226,7 +228,7 @@ fun SelectInstallSh(vm: WizardActivityState, update: Boolean = false) {
@Composable
private fun Flash(vm: WizardActivityState) {
val flashType = "DroidBootFlashType"
Terminal(vm, logFile = "blflash_${System.currentTimeMillis()}.txt") { terminal ->
Terminal(logFile = "blflash_${System.currentTimeMillis()}.txt") { 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))
Expand Down Expand Up @@ -271,7 +273,7 @@ private fun Flash(vm: WizardActivityState) {
"8301",
"abm_settings"
).to(terminal).exec()
if (r.out.join("\n").contains("old")) {
if (r.out.joinToString("\n").contains("old")) {
terminal.add(vm.activity.getString(R.string.term_reboot_asap))
}
if (r.isSuccess) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.andbootmgr.app.util.AbmTheme
import org.andbootmgr.app.util.Terminal
import java.io.File
import java.io.IOException

Expand Down Expand Up @@ -57,7 +58,7 @@ private fun Start() {

@Composable
private fun Flash(vm: WizardActivityState) {
Terminal(vm, logFile = "blfix_${System.currentTimeMillis()}.txt") { terminal ->
Terminal(logFile = "blfix_${System.currentTimeMillis()}.txt") { terminal ->
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.flashStream("InstallShFlashType"), tmpFile)
Expand Down
14 changes: 9 additions & 5 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ class MainActivity : ComponentActivity() {
Shell.getShell { shell ->
CoroutineScope(Dispatchers.IO).launch {
vm.root = shell.isRoot
if (vm.root && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Shell.cmd("pm grant $packageName ${android.Manifest.permission.POST_NOTIFICATIONS}")
}
vm.deviceInfo = JsonDeviceInfoFactory(vm.activity!!).get(Build.DEVICE)
// == temp migration code start ==
if (Shell.cmd("mountpoint -q /data/abm/bootset").exec().isSuccess) {
Expand All @@ -242,6 +245,7 @@ class MainActivity : ComponentActivity() {
}
withContext(Dispatchers.Main) {
setContent {
// TODO support work resumption by instantly opening Terminal with null action if work is going on
val navController = rememberNavController()
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
Expand Down Expand Up @@ -753,7 +757,7 @@ private fun PartTool(vm: MainActivityState) {
processing = true
vm.logic!!.mount(p).submit {
processing = false
result = it.out.join("\n") + it.err.join("\n")
result = it.out.joinToString("\n") + it.err.joinToString("\n")
}
}, Modifier.padding(end = 5.dp)) {
Text(stringResource(R.string.mount))
Expand All @@ -762,7 +766,7 @@ private fun PartTool(vm: MainActivityState) {
processing = true
vm.logic!!.unmount(p).submit {
processing = false
result = it.out.join("\n") + it.err.join("\n")
result = it.out.joinToString("\n") + it.err.joinToString("\n")
}
}) {
Text(stringResource(R.string.umount))
Expand Down Expand Up @@ -817,7 +821,7 @@ private fun PartTool(vm: MainActivityState) {
processing = true
rename = false
vm.logic!!.rename(p, t).submit { r ->
result = r.out.join("\n") + r.err.join("\n")
result = r.out.joinToString("\n") + r.err.joinToString("\n")
parts = SDUtils.generateMeta(vm.deviceInfo!!)
editPartID = parts?.s!!.findLast { it.id == p.id }
processing = false
Expand Down Expand Up @@ -857,7 +861,7 @@ private fun PartTool(vm: MainActivityState) {
processing = false
editPartID = null
parts = SDUtils.generateMeta(vm.deviceInfo!!)
result = it.out.join("\n") + it.err.join("\n")
result = it.out.joinToString("\n") + it.err.joinToString("\n")
}
}
}) {
Expand Down Expand Up @@ -1086,7 +1090,7 @@ private fun PartTool(vm: MainActivityState) {
for (p in allp) { // Do not chain, but regenerate meta and unmount every time. Thanks void
val r = vm.logic!!.delete(p).exec()
parts = SDUtils.generateMeta(vm.deviceInfo!!)
tresult += r.out.join("\n") + r.err.join("\n") + "\n"
tresult += r.out.joinToString("\n") + r.err.joinToString("\n") + "\n"
}
vm.mountBootset()
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.topjohnwu.superuser.io.SuFileInputStream
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.andbootmgr.app.util.AbmTheme
import org.andbootmgr.app.util.Terminal
import java.io.File
import java.io.IOException

Expand Down Expand Up @@ -55,7 +56,7 @@ private fun Start() {

@Composable
private fun Flash(vm: WizardActivityState) {
Terminal(vm, logFile = "blup_${System.currentTimeMillis()}.txt") { terminal ->
Terminal(logFile = "blup_${System.currentTimeMillis()}.txt") { terminal ->
val tmpFile = if (vm.deviceInfo.postInstallScript) {
val tmpFile = createTempFileSu("abm", ".sh", vm.logic.rootTmpDir)
vm.copyPriv(vm.flashStream("InstallShFlashType"), tmpFile)
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/org/andbootmgr/app/UpdateFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFile
Expand All @@ -26,12 +23,12 @@ import okio.buffer
import okio.sink
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
import java.net.URL
import java.util.concurrent.TimeUnit
import kotlin.coroutines.CoroutineContext

class UpdateFlowWizardPageFactory(private val vm: WizardActivityState) {
fun get(): List<IWizardPage> {
Expand Down Expand Up @@ -250,9 +247,9 @@ private fun dlFile(u: UpdateFlowDataHolder, l: String): File? {

@Composable
private fun Flash(u: UpdateFlowDataHolder) {
Terminal(u.vm, logFile = "update_${System.currentTimeMillis()}.txt") { terminal ->
Terminal(logFile = "update_${System.currentTimeMillis()}.txt") { terminal ->
val sp = u.e!!["xpart"]!!.split(":")
val meta = SDUtils.generateMeta(u.vm.deviceInfo!!)!!
val meta = SDUtils.generateMeta(u.vm.deviceInfo)!!
Shell.cmd(SDUtils.umsd(meta)).exec()

if (u.hasUpdate) { // online
Expand Down
Loading

0 comments on commit e391593

Please sign in to comment.