From f080d0e95393d0bc3c05577b7553ae0d352d990f Mon Sep 17 00:00:00 2001 From: nift4 Date: Mon, 14 Aug 2023 21:57:34 +0200 Subject: [PATCH] Support droidboot downloads --- .../org/andbootmgr/app/BackupRestoreFlow.kt | 4 +- .../java/org/andbootmgr/app/CreatePartFlow.kt | 4 +- .../java/org/andbootmgr/app/DroidBootFlow.kt | 34 +++++++++++++-- .../org/andbootmgr/app/FixDroidBootFlow.kt | 41 +------------------ .../org/andbootmgr/app/UpdateDroidBootFlow.kt | 41 +------------------ .../java/org/andbootmgr/app/WizardActivity.kt | 17 +++++++- app/src/main/res/values/strings.xml | 2 +- 7 files changed, 54 insertions(+), 89 deletions(-) diff --git a/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt b/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt index 60711656..2224b419 100644 --- a/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt @@ -32,7 +32,7 @@ class BackupRestoreWizardPageFactory(private val vm: WizardActivityState) { NavButton(vm.activity.getString(R.string.prev)) { it.navigate("start") }, NavButton("") {} ) { - Select(c) + SelectDroidBoot(c) }, WizardPage("go", NavButton("") {}, NavButton("") {} @@ -72,7 +72,7 @@ private fun ChooseAction(c: CreateBackupDataHolder) { } @Composable -private fun Select(c: CreateBackupDataHolder) { +private fun SelectDroidBoot(c: CreateBackupDataHolder) { val nextButtonAvailable = remember { mutableStateOf(false) } Column( diff --git a/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt b/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt index 0d52d7da..4e60f7cd 100644 --- a/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt @@ -350,8 +350,8 @@ private fun Start(c: CreatePartDataHolder) { @Composable private fun Shop(c: CreatePartDataHolder) { - var json: JSONObject? by remember { mutableStateOf(null) } - var error by remember { mutableStateOf(false) } + var json: JSONObject? by remember { mutableStateOf(null) } + var error by remember { mutableStateOf(false) } LaunchedEffect(Unit) { c.run { Thread { diff --git a/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt b/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt index 34023088..26b2b341 100644 --- a/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt @@ -1,11 +1,15 @@ package org.andbootmgr.app import android.content.Intent +import android.net.Uri +import android.util.Log +import android.widget.Toast import androidx.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign @@ -18,8 +22,11 @@ import com.topjohnwu.superuser.io.SuFileOutputStream import org.andbootmgr.app.util.AbmTheme import org.andbootmgr.app.util.ConfigFile import org.andbootmgr.app.util.SDUtils +import org.json.JSONObject +import org.json.JSONTokener import java.io.File import java.io.IOException +import java.net.URL class DroidBootWizardPageFactory(private val vm: WizardActivityState) { fun get(): List { @@ -37,7 +44,7 @@ class DroidBootWizardPageFactory(private val vm: WizardActivityState) { NavButton(vm.activity.getString(R.string.prev)) { it.navigate("input") }, NavButton("") {} ) { - Select(vm) + SelectDroidBoot(vm) }, WizardPage("flash", NavButton("") {}, NavButton("") {} @@ -99,9 +106,9 @@ private fun Input(vm: WizardActivityState) { } } - +// shared across DroidBootFlow, UpdateDroidBootFlow, FixDroidBootFlow @Composable -private fun Select(vm: WizardActivityState) { +fun SelectDroidBoot(vm: WizardActivityState) { val nextButtonAvailable = remember { mutableStateOf(false) } val flashType = "DroidBootFlashType" @@ -119,7 +126,7 @@ private fun Select(vm: WizardActivityState) { vm.nextText.value = stringResource(id = R.string.next) vm.onNext.value = { it.navigate("flash") } } else { - Text(stringResource(R.string.choose_droidboot)) + Text(stringResource(R.string.choose_droidboot_online)) Button(onClick = { vm.activity.chooseFile("*/*") { vm.flashes[flashType] = it @@ -128,6 +135,25 @@ private fun Select(vm: WizardActivityState) { }) { Text(stringResource(id = R.string.choose_file)) } + val ctx = LocalContext.current + Button(onClick = { + Thread { + try { + val jsonText = + URL("https://raw.githubusercontent.com/Android-Boot-Manager/ABM-json/master/devices/" + vm.codename + ".json").readText() + val json = JSONTokener(jsonText).nextValue() as JSONObject + val bl = json.getJSONObject("bootloader") + val url = bl.getString("url") + vm.flashes[flashType] = Uri.parse(url) + nextButtonAvailable.value = true + } catch (e: Exception) { + Toast.makeText(ctx, R.string.dl_error, Toast.LENGTH_LONG).show() + Log.e("ABM droidboot json", Log.getStackTraceString(e)) + } + }.start() + }) { + Text(stringResource(id = R.string.download)) + } } } } diff --git a/app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt b/app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt index 7a34c1bd..9b8a0337 100644 --- a/app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt @@ -2,18 +2,13 @@ package org.andbootmgr.app import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.defaultMinSize import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.* import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp import com.topjohnwu.superuser.io.SuFile import com.topjohnwu.superuser.io.SuFileInputStream import org.andbootmgr.app.util.AbmTheme @@ -31,7 +26,7 @@ class FixDroidBootWizardPageFactory(private val vm: WizardActivityState) { NavButton(vm.activity.getString(R.string.prev)) { it.navigate("start") }, NavButton("") {} ) { - Select(vm) + SelectDroidBoot(vm) }, WizardPage("flash", NavButton("") {}, NavButton("") {} @@ -51,38 +46,6 @@ private fun Start(vm: WizardActivityState) { } } -@Composable -private fun Select(vm: WizardActivityState) { - val nextButtonAvailable = remember { mutableStateOf(false) } - val flashType = "DroidBootFlashType" - - Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, - modifier = Modifier.fillMaxSize() - ) { - Icon( - painterResource(R.drawable.ic_droidbooticon), - stringResource(id = R.string.droidboot_icon_content_desc), - Modifier.defaultMinSize(32.dp, 32.dp) - ) - - if (nextButtonAvailable.value) { - Text(stringResource(id = R.string.successfully_selected)) - vm.nextText.value = stringResource(id = R.string.next) - vm.onNext.value = { it.navigate("flash") } - } else { - Text(stringResource(id = R.string.choose_droidboot)) - Button(onClick = { - vm.activity.chooseFile("*/*") { - vm.flashes[flashType] = it - nextButtonAvailable.value = true - } - }) { - Text(stringResource(id = R.string.choose_file)) - } - } - } -} - @Composable private fun Flash(vm: WizardActivityState) { val flashType = "DroidBootFlashType" @@ -119,7 +82,7 @@ private fun Preview() { modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { - Select(vm) + SelectDroidBoot(vm) } } } \ No newline at end of file diff --git a/app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt b/app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt index a61e5fbc..4354662f 100644 --- a/app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt @@ -2,18 +2,13 @@ package org.andbootmgr.app import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.defaultMinSize import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.* import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier -import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp import com.topjohnwu.superuser.io.SuFile import com.topjohnwu.superuser.io.SuFileInputStream import org.andbootmgr.app.util.AbmTheme @@ -31,7 +26,7 @@ class UpdateDroidBootWizardPageFactory(private val vm: WizardActivityState) { NavButton(vm.activity.getString(R.string.prev)) { it.navigate("start") }, NavButton("") {} ) { - Select(vm) + SelectDroidBoot(vm) }, WizardPage("flash", NavButton("") {}, NavButton("") {} @@ -51,38 +46,6 @@ private fun Start(vm: WizardActivityState) { } } -@Composable -private fun Select(vm: WizardActivityState) { - val nextButtonAvailable = remember { mutableStateOf(false) } - val flashType = "DroidBootFlashType" - - Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center, - modifier = Modifier.fillMaxSize() - ) { - Icon( - painterResource(R.drawable.ic_droidbooticon), - stringResource(id = R.string.droidboot_icon_content_desc), - Modifier.defaultMinSize(32.dp, 32.dp) - ) - - if (nextButtonAvailable.value) { - Text(stringResource(id = R.string.successfully_selected)) - vm.nextText.value = stringResource(id = R.string.next) - vm.onNext.value = { it.navigate("flash") } - } else { - Text(stringResource(id = R.string.choose_droidboot)) - Button(onClick = { - vm.activity.chooseFile("*/*") { - vm.flashes[flashType] = it - nextButtonAvailable.value = true - } - }) { - Text(stringResource(id = R.string.choose_file)) - } - } - } -} - @Composable private fun Flash(vm: WizardActivityState) { val flashType = "DroidBootFlashType" @@ -119,7 +82,7 @@ private fun Preview() { modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { - Select(vm) + SelectDroidBoot(vm) } } } \ No newline at end of file diff --git a/app/src/main/java/org/andbootmgr/app/WizardActivity.kt b/app/src/main/java/org/andbootmgr/app/WizardActivity.kt index 55928b92..99a9c39f 100644 --- a/app/src/main/java/org/andbootmgr/app/WizardActivity.kt +++ b/app/src/main/java/org/andbootmgr/app/WizardActivity.kt @@ -23,6 +23,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import androidx.core.net.toFile import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable @@ -32,10 +33,12 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.andbootmgr.app.util.AbmTheme import java.io.File +import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException import java.io.InputStream import java.io.OutputStream +import java.net.URL import java.nio.file.Files import java.nio.file.StandardCopyOption @@ -207,8 +210,18 @@ class WizardActivityState(val codename: String) { } fun flashStream(flashType: String): InputStream { - return activity.contentResolver.openInputStream(flashes[flashType]!!) - ?: throw IOException("in == null") + return flashes[flashType]?.let { + when (it.scheme) { + "content" -> + activity.contentResolver.openInputStream(it) + ?: throw IOException("in == null") + "file" -> + FileInputStream(it.toFile()) + "http", "https" -> + URL(it.toString()).openStream() + else -> null + } + } ?: throw IllegalArgumentException() } fun copyUnpriv(inputStream: InputStream, output: File) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 97224e38..c7659266 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -70,7 +70,7 @@ OS name Invalid input DroidBoot Icon - Please choose a DroidBoot image! + Please choose a DroidBoot image, or use the download button to automatically download the latest version from the internet (recommened for most users). This will reinstall DroidBoot File not available, please try again later! Internal error - no file handler added!! Please cancel install