Skip to content

Commit

Permalink
json device definition mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Jul 31, 2024
1 parent 4226574 commit 36f0211
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 102 deletions.
117 changes: 53 additions & 64 deletions app/src/main/java/org/andbootmgr/app/DeviceInfo.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.andbootmgr.app

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.io.SuFile
import org.andbootmgr.app.util.SDUtils
import org.json.JSONObject
import org.json.JSONTokener
import java.io.File
import java.io.FileNotFoundException
import java.lang.reflect.Method
import java.net.URL

interface DeviceInfo {
val codename: String
Expand Down Expand Up @@ -41,7 +48,7 @@ abstract class MetaOnSdDeviceInfo : DeviceInfo {
override val metaonsd = true
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && SDUtils.generateMeta(this)?.let { meta ->
meta.p.isNotEmpty() && meta.dumpKernelPartition(1).type == SDUtils.PartitionType.RESERVED
meta.p.find { it.id == 1 && it.type == SDUtils.PartitionType.RESERVED } != null
} == true
}
override fun isCorrupt(logic: DeviceLogic): Boolean {
Expand All @@ -60,70 +67,52 @@ abstract class MetaOnSdDeviceInfo : DeviceInfo {
}
}

object HardcodedDeviceInfoFactory {
private fun getYggdrasil(): DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename = "yggdrasil"
override val blBlock = "/dev/block/by-name/lk"
override val bdev = "/dev/block/mmcblk1"
override val pbdev = bdev + "p"
override val postInstallScript = false
override val havedtbo = false
}
}

private fun getMimameid(): DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename = "mimameid"
override val blBlock = "/dev/block/by-name/lk"
override val bdev = "/dev/block/mmcblk1"
override val pbdev = bdev + "p"
override val postInstallScript = true
override val havedtbo = false
}
}

private fun getYggdrasilx(): DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename = "yggdrasilx"
override val blBlock = "/dev/block/by-name/lk"
override val bdev = "/dev/block/mmcblk1"
override val pbdev = bdev + "p"
override val postInstallScript = true
override val havedtbo = false
}
}

private fun getVidofnir(): DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename = "vidofnir"
override val blBlock = "/dev/block/by-name/lk"
override val bdev = "/dev/block/mmcblk0"
override val pbdev = bdev + "p"
override val postInstallScript = false
override val havedtbo = false
}
}

private fun getVayu(): DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename = "vayu"
override val blBlock = "/dev/block/by-name/boot"
override val bdev = "/dev/block/mmcblk0"
override val pbdev = bdev + "p"
override val postInstallScript = true
override val havedtbo = true
}
}
class JsonDeviceInfo(
override val codename: String,
override val blBlock: String,
override val bdev: String,
override val pbdev: String,
override val postInstallScript: Boolean,
override val havedtbo: Boolean
) : MetaOnSdDeviceInfo()

class JsonDeviceInfoFactory(private val ctx: Context) {
fun get(codename: String): DeviceInfo? {
return when (codename) {
"yggdrasil" -> getYggdrasil()
"yggdrasilx" -> getYggdrasilx()
"mimameid" -> getMimameid()
"vidofnir" -> getVidofnir()
"vayu" -> getVayu()
else -> null
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()
}
} 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 (fromNet) {
val newRoot = JSONObject()
newRoot.put("deviceInfo", json)
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")
)
} catch (e: Exception) {
Log.e("ABM device info", Log.getStackTraceString(e))
null
}
}
}
}
72 changes: 38 additions & 34 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,42 +144,46 @@ class MainActivity : ComponentActivity() {
}
if (!fail) {
Shell.getShell { shell ->
vm.root = shell.isRoot
vm.deviceInfo = HardcodedDeviceInfoFactory.get(Build.DEVICE)
// == temp migration code start ==
if (Shell.cmd("mountpoint -q /data/abm/bootset").exec().isSuccess) {
Shell.cmd("umount /data/abm/bootset").exec()
}
SuFile.open("/data/abm").let {
if (it.exists())
Shell.cmd("rm -rf /data/abm").exec()
}
// == temp migration code end ==
if (vm.deviceInfo != null && vm.deviceInfo!!.isInstalled(vm.logic!!)) {
vm.logic!!.mountBootset(vm.deviceInfo!!)
} else {
Log.i("ABM", "not installed, not trying to mount")
}
if (vm.deviceInfo != null) {
vm.isOk = ((vm.deviceInfo!!.isInstalled(vm.logic!!)) &&
vm.deviceInfo!!.isBooted(vm.logic!!) &&
!(!vm.logic!!.mounted || vm.deviceInfo!!.isCorrupt(vm.logic!!)))
}
runOnUiThread {
setContent {
val navController = rememberNavController()
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
vm.navController = navController
vm.drawerState = drawerState
vm.scope = scope
vm.noobMode = LocalContext.current.getSharedPreferences("abm", 0).getBoolean("noob_mode", BuildConfig.DEFAULT_NOOB_MODE)
AppContent(vm) {
NavGraph(vm, it)
Thread {
vm.root = shell.isRoot
vm.deviceInfo = JsonDeviceInfoFactory(vm.activity!!).get(Build.DEVICE)
// == temp migration code start ==
if (Shell.cmd("mountpoint -q /data/abm/bootset").exec().isSuccess) {
Shell.cmd("umount /data/abm/bootset").exec()
}
SuFile.open("/data/abm").let {
if (it.exists())
Shell.cmd("rm -rf /data/abm").exec()
}
// == temp migration code end ==
if (vm.deviceInfo != null && vm.deviceInfo!!.isInstalled(vm.logic!!)) {
vm.logic!!.mountBootset(vm.deviceInfo!!)
} else {
Log.i("ABM", "not installed, not trying to mount")
}
if (vm.deviceInfo != null) {
vm.isOk = ((vm.deviceInfo!!.isInstalled(vm.logic!!)) &&
vm.deviceInfo!!.isBooted(vm.logic!!) &&
!(!vm.logic!!.mounted || vm.deviceInfo!!.isCorrupt(vm.logic!!)))
}
runOnUiThread {
setContent {
val navController = rememberNavController()
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
vm.navController = navController
vm.drawerState = drawerState
vm.scope = scope
vm.noobMode =
LocalContext.current.getSharedPreferences("abm", 0)
.getBoolean("noob_mode", BuildConfig.DEFAULT_NOOB_MODE)
AppContent(vm) {
NavGraph(vm, it)
}
}
vm.isReady = true
}
vm.isReady = true
}
}.start()
}
} else {
setContent {
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/andbootmgr/app/WizardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.andbootmgr.app
import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.widget.Toast
import androidx.activity.ComponentActivity
Expand Down Expand Up @@ -67,6 +66,7 @@ class WizardActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
vm = WizardActivityState(intent.getStringExtra("codename")!!)
vm.activity = this
vm.deviceInfo = JsonDeviceInfoFactory(this).get(vm.codename)!!
vm.logic = DeviceLogic(this)
chooseFile = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
if (uri == null) {
Expand Down Expand Up @@ -182,8 +182,8 @@ class WizardActivityState(val codename: String) {
lateinit var navController: NavHostController
lateinit var activity: WizardActivity
lateinit var logic: DeviceLogic
val deviceInfo = HardcodedDeviceInfoFactory.get(codename)
var current = mutableStateOf("start")
lateinit var deviceInfo: DeviceInfo
private var current = mutableStateOf("start")
var prevText = mutableStateOf("")
var nextText = mutableStateOf("")
var onPrev: MutableState<(WizardActivity) -> Unit> = mutableStateOf({})
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/org/andbootmgr/app/util/Toolkit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Toolkit(private val ctx: Context) {
if (!File(ctx.filesDir.parentFile, "files").exists()) fail = fail or !File(ctx.filesDir.parentFile, "files").mkdir()
if (!File(ctx.filesDir.parentFile, "cache").exists()) fail = fail or !File(ctx.filesDir.parentFile, "cache").mkdir()
copyAssets("Toolkit", "Toolkit")
copyAssets("Scripts", "Scripts")
copyAssets("cp", "")
}
shell.newJob().add("chmod -R +x " + targetPath.absolutePath).exec()
Expand Down

0 comments on commit 36f0211

Please sign in to comment.