Skip to content

Commit

Permalink
Support droidboot downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Aug 14, 2023
1 parent cc03e92 commit f080d0e
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 89 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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("") {}
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 30 additions & 4 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<IWizardPage> {
Expand All @@ -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("") {}
Expand Down Expand Up @@ -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"

Expand All @@ -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
Expand All @@ -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))
}
}
}
}
Expand Down
41 changes: 2 additions & 39 deletions app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("") {}
Expand All @@ -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"
Expand Down Expand Up @@ -119,7 +82,7 @@ private fun Preview() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Select(vm)
SelectDroidBoot(vm)
}
}
}
41 changes: 2 additions & 39 deletions app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("") {}
Expand All @@ -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"
Expand Down Expand Up @@ -119,7 +82,7 @@ private fun Preview() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Select(vm)
SelectDroidBoot(vm)
}
}
}
17 changes: 15 additions & 2 deletions app/src/main/java/org/andbootmgr/app/WizardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<string name="os_name">OS name</string>
<string name="invalid_in">Invalid input</string>
<string name="droidboot_icon_content_desc">DroidBoot Icon</string>
<string name="choose_droidboot">Please choose a DroidBoot image!</string>
<string name="choose_droidboot_online">Please choose a DroidBoot image, or use the download button to automatically download the latest version from the internet (recommened for most users).</string>
<string name="reinstall_dboot">This will reinstall DroidBoot</string>
<string name="file_unavailable">File not available, please try again later!</string>
<string name="internal_file_error1">Internal error - no file handler added!! Please cancel install</string>
Expand Down

0 comments on commit f080d0e

Please sign in to comment.