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 e391593 commit 3dd207a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 36 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ tasks.register("setAssetTs", Task::class) {
}

dependencies {
implementation("androidx.appcompat:appcompat:1.7.0")
implementation("androidx.core:core-splashscreen:1.0.1")
implementation("androidx.appcompat:appcompat:1.7.0")
implementation("androidx.legacy:legacy-support-v4:1.0.0")
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.cardview:cardview:1.0.0")
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 @@ -334,18 +334,22 @@ private fun Flash(vm: WizardActivityState) {
entry.exportToFile(File(vm.logic.abmEntries, "real.conf"))
if (!vm.deviceInfo.isBooted(vm.logic)) {
terminal.add(vm.activity.getString(R.string.term_flashing_droidboot))
val backupLk = File(vm.logic.fileDir, "backup_lk1.img")
val f = SuFile.open(vm.deviceInfo.blBlock)
if (!f.canWrite())
terminal.add(vm.activity.getString(R.string.term_cant_write_bl))
vm.copyPriv(
SuFileInputStream.open(vm.deviceInfo.blBlock),
File(vm.logic.fileDir, "backup_lk1.img")
)
vm.copyPriv(SuFileInputStream.open(vm.deviceInfo.blBlock), backupLk)
try {
vm.copyPriv(vm.flashStream(flashType), File(vm.deviceInfo.blBlock))
} catch (e: IOException) {
terminal.add(vm.activity.getString(R.string.term_bl_failed))
terminal.add(if (e.message != null) e.message!! else "(null)")
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
} catch (e: HashMismatchException) {
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.restoring_backup))
vm.copyPriv(SuFileInputStream.open(backupLk), File(vm.deviceInfo.blBlock))
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
}
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/org/andbootmgr/app/FixDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,22 @@ private fun Flash(vm: WizardActivityState) {
tmpFile
} else null
terminal.add(vm.activity.getString(R.string.term_flashing_droidboot))
val backupLk = File(vm.logic.fileDir, "backup_lk.img")
val f = SuFile.open(vm.deviceInfo.blBlock)
if (!f.canWrite())
terminal.add(vm.activity.getString(R.string.term_cant_write_bl))
vm.copyPriv(SuFileInputStream.open(vm.deviceInfo.blBlock), File(vm.logic.fileDir, "backup_lk.img"))
vm.copyPriv(SuFileInputStream.open(vm.deviceInfo.blBlock), backupLk)
try {
vm.copyPriv(vm.flashStream("DroidBootFlashType"), File(vm.deviceInfo.blBlock))
} catch (e: IOException) {
terminal.add(vm.activity.getString(R.string.term_bl_failed))
terminal.add(if (e.message != null) e.message!! else "(null)")
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
} catch (e: HashMismatchException) {
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.restoring_backup))
vm.copyPriv(SuFileInputStream.open(backupLk), File(vm.deviceInfo.blBlock))
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
}
Expand Down
21 changes: 2 additions & 19 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand Down Expand Up @@ -92,7 +93,6 @@ class MainActivityState {
val theme = ThemeViewModel(this)
var defaultCfg = mutableStateMapOf<String, String>()
var isReady = false
var name by mutableStateOf("") /* default value moved to onCreate() */
var navController: NavHostController? = null
var drawerState: DrawerState? = null
var scope: CoroutineScope? = null
Expand Down Expand Up @@ -154,27 +154,10 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val vm = MainActivityState()
vm.name = getString(R.string.android)
installSplashScreen().setKeepOnScreenCondition { !vm.isReady }
vm.activity = this
vm.logic = DeviceLogic(this)

val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
// Check if the initial data is ready.
return if (vm.isReady) {
// The content is ready; start drawing.
content.viewTreeObserver.removeOnPreDrawListener(this)
true
} else {
// The content is not ready; suspend.
false
}
}
}
)

val toast =
Toast.makeText(this, getString(R.string.toolkit_extracting), Toast.LENGTH_LONG)
CoroutineScope(Dispatchers.IO).launch {
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/org/andbootmgr/app/UpdateDroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,22 @@ private fun Flash(vm: WizardActivityState) {
tmpFile
} else null
terminal.add(vm.activity.getString(R.string.term_flashing_droidboot))
val backupLk = File(vm.logic.fileDir, "backup2_lk.img")
val f = SuFile.open(vm.deviceInfo.blBlock)
if (!f.canWrite())
terminal.add(vm.activity.getString(R.string.term_cant_write_bl))
vm.copyPriv(SuFileInputStream.open(vm.deviceInfo.blBlock), File(vm.logic.fileDir, "backup2_lk.img"))
vm.copyPriv(SuFileInputStream.open(vm.deviceInfo.blBlock), backupLk)
try {
vm.copyPriv(vm.flashStream("DroidBootFlashType"), File(vm.deviceInfo.blBlock))
} catch (e: IOException) {
terminal.add(vm.activity.getString(R.string.term_bl_failed))
terminal.add(if (e.message != null) e.message!! else "(null)")
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
} catch (e: HashMismatchException) {
terminal.add(e.message ?: "(null)")
terminal.add(vm.activity.getString(R.string.restoring_backup))
vm.copyPriv(SuFileInputStream.open(backupLk), File(vm.deviceInfo.blBlock))
terminal.add(vm.activity.getString(R.string.term_consult_doc))
return@Terminal
}
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/org/andbootmgr/app/WizardActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ class WizardActivityState(val codename: String) {
navController.navigate(current.value)
}

// TODO have callers handle HashMismatchException when appropriate
fun copy(inputStream: InputStream, outputStream: OutputStream): Long {
var nread = 0L
val buf = ByteArray(8192)
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/java/org/andbootmgr/app/util/StayAliveService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.content.Intent
import android.content.ServiceConnection
import android.os.Binder
import android.os.IBinder
import android.os.PowerManager
import android.os.PowerManager.WakeLock
import android.util.Log
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
Expand All @@ -23,6 +25,7 @@ interface IStayAlive {
}

class StayAliveService : LifecycleService(), IStayAlive {
private lateinit var wakeLock: WakeLock
private var work: (suspend (Context) -> Unit)? = null
var isWorkDone = false
get() {
Expand All @@ -44,6 +47,7 @@ class StayAliveService : LifecycleService(), IStayAlive {
}
private var destroyed = false
private var onDone: (() -> Unit)? = null
@SuppressLint("WakelockTimeout")
override fun startWork(work: suspend (Context) -> Unit, extra: Any) {
if (destroyed) {
throw IllegalStateException("This StayAliveService was leaked. It is already destroyed.")
Expand All @@ -56,9 +60,14 @@ class StayAliveService : LifecycleService(), IStayAlive {
this.work = work
this.extra = extra
lifecycleScope.launch {
this@StayAliveService.work!!.invoke(this@StayAliveService)
isWorkDone = true
onDone!!.invoke()
wakeLock.acquire()
try {
this@StayAliveService.work!!.invoke(this@StayAliveService)
isWorkDone = true
onDone!!.invoke()
} finally {
wakeLock.release()
}
}
}
fun finish() {
Expand Down Expand Up @@ -89,6 +98,8 @@ class StayAliveService : LifecycleService(), IStayAlive {
.setOnlyAlertOnce(true)
.setLocalOnly(true)
.build())
wakeLock = getSystemService(PowerManager::class.java)
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ABM::StayAlive(user_task)")
lifecycleScope.launch {
delay(10000)
// If there was nothing started after 10 seconds, there's a bug.
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/org/andbootmgr/app/util/Terminal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand All @@ -24,7 +25,8 @@ import org.andbootmgr.app.R
import java.io.File
import java.io.FileOutputStream

private class BudgetCallbackList(private val log: FileOutputStream?) : MutableList<String> {
private class BudgetCallbackList(private val scope: CoroutineScope,
private val log: FileOutputStream?) : MutableList<String> {
val internalList = ArrayList<String>()
var cb: ((String) -> Unit)? = null
override val size: Int
Expand Down Expand Up @@ -119,7 +121,9 @@ private class BudgetCallbackList(private val log: FileOutputStream?) : MutableLi
}

fun onAdd(element: String) {
log?.write((element + "\n").encodeToByteArray())
scope.launch {
log?.write((element + "\n").encodeToByteArray())
}
cb?.invoke(element)
}
}
Expand All @@ -139,7 +143,7 @@ fun Terminal(logFile: String? = null, action: (suspend (MutableList<String>) ->
StayAliveConnection(ctx) { service ->
if (action != null) {
val log = logFile?.let { FileOutputStream(File(ctx.externalCacheDir, it)) }
val s = BudgetCallbackList(log)
val s = BudgetCallbackList(CoroutineScope(Dispatchers.IO), log)
s.cb = { element ->
scope.launch {
text.value += element + "\n"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,5 @@
<string name="service_notifications">Service notifications</string>
<string name="abm_processing_title">ABM is processing</string>
<string name="abm_processing_text">Please make sure you do not leave the app to avoid issues</string>
<string name="restoring_backup">Restoring backup…</string>
</resources>

0 comments on commit 3dd207a

Please sign in to comment.