Magisk/app/src/main/java/com/topjohnwu/magisk/ui/flash/FlashViewModel.kt

127 lines
4.3 KiB
Kotlin
Raw Normal View History

2019-04-24 20:28:41 +02:00
package com.topjohnwu.magisk.ui.flash
import android.content.res.Resources
import android.net.Uri
import android.view.MenuItem
2020-07-12 12:17:50 +02:00
import androidx.databinding.Bindable
import androidx.databinding.ObservableArrayList
2020-07-07 07:30:21 +02:00
import androidx.lifecycle.viewModelScope
2020-07-12 12:17:50 +02:00
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Const
2020-07-07 07:30:21 +02:00
import com.topjohnwu.magisk.core.tasks.FlashZip
import com.topjohnwu.magisk.core.tasks.MagiskInstaller
import com.topjohnwu.magisk.core.view.Notifications
2020-07-11 14:36:31 +02:00
import com.topjohnwu.magisk.ktx.*
import com.topjohnwu.magisk.model.binding.BindingAdapter
import com.topjohnwu.magisk.model.entity.recycler.ConsoleItem
import com.topjohnwu.magisk.model.events.SnackbarEvent
2020-01-13 15:01:46 +01:00
import com.topjohnwu.magisk.ui.base.BaseViewModel
import com.topjohnwu.magisk.ui.base.diffListOf
import com.topjohnwu.magisk.ui.base.itemBindingOf
2020-07-15 10:21:57 +02:00
import com.topjohnwu.magisk.utils.set
import com.topjohnwu.superuser.Shell
2020-07-09 13:49:14 +02:00
import kotlinx.coroutines.Dispatchers
2020-07-07 07:30:21 +02:00
import kotlinx.coroutines.launch
2020-07-09 13:49:14 +02:00
import kotlinx.coroutines.withContext
import java.io.File
import java.util.*
2019-04-24 20:28:41 +02:00
class FlashViewModel(
2020-07-07 06:05:43 +02:00
args: FlashFragmentArgs,
private val resources: Resources
2020-07-07 07:30:21 +02:00
) : BaseViewModel() {
2020-07-12 12:17:50 +02:00
@get:Bindable
2020-07-15 10:21:57 +02:00
var showReboot = Shell.rootAccess()
set(value) = set(value, field, { field = it }, BR.showReboot)
2020-07-12 12:17:50 +02:00
@get:Bindable
2020-07-15 10:21:57 +02:00
var behaviorText = resources.getString(R.string.flashing)
set(value) = set(value, field, { field = it }, BR.behaviorText)
val adapter = BindingAdapter<ConsoleItem>()
val items = diffListOf<ConsoleItem>()
val itemBinding = itemBindingOf<ConsoleItem>()
private val outItems = ObservableArrayList<String>()
2020-07-07 06:05:43 +02:00
private val logItems = Collections.synchronizedList(mutableListOf<String>())
init {
2020-07-10 13:19:18 +02:00
outItems.sendUpdatesTo(items, viewModelScope) { it.map { ConsoleItem(it) } }
outItems.copyNewInputInto(logItems)
args.dismissId.takeIf { it != -1 }?.also {
Notifications.mgr.cancel(it)
}
val (installer, action, uri) = args
startFlashing(installer, uri, action)
}
private fun startFlashing(installer: Uri, uri: Uri?, action: String) {
2020-07-07 07:30:21 +02:00
viewModelScope.launch {
val result = when (action) {
Const.Value.FLASH_ZIP -> {
FlashZip(installer, outItems, logItems).exec()
}
Const.Value.UNINSTALL -> {
2020-07-12 12:17:50 +02:00
showReboot = false
2020-07-07 07:30:21 +02:00
FlashZip.Uninstall(installer, outItems, logItems).exec()
}
Const.Value.FLASH_MAGISK -> {
MagiskInstaller.Direct(installer, outItems, logItems).exec()
}
Const.Value.FLASH_INACTIVE_SLOT -> {
MagiskInstaller.SecondSlot(installer, outItems, logItems).exec()
}
Const.Value.PATCH_FILE -> {
uri ?: return@launch
2020-07-12 12:17:50 +02:00
showReboot = false
2020-07-07 07:30:21 +02:00
MagiskInstaller.Patch(installer, uri, outItems, logItems).exec()
}
else -> {
back()
return@launch
}
2020-07-07 06:05:43 +02:00
}
2020-07-07 07:30:21 +02:00
onResult(result)
}
}
2020-07-07 07:30:21 +02:00
private fun onResult(success: Boolean) {
state = if (success) State.LOADED else State.LOADING_FAILED
2020-07-12 12:17:50 +02:00
behaviorText = when {
success -> resources.getString(R.string.done)
else -> resources.getString(R.string.failure)
}
}
fun onMenuItemClicked(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_save -> savePressed()
}
return true
}
2020-07-09 13:49:14 +02:00
private fun savePressed() = withExternalRW {
if (!it)
return@withExternalRW
viewModelScope.launch {
val name = Const.MAGISK_INSTALL_LOG_FILENAME.format(now.toTime(timeFormatStandard))
val file = File(Config.downloadDirectory, name)
withContext(Dispatchers.IO) {
file.bufferedWriter().use { writer ->
logItems.forEach {
writer.write(it)
writer.newLine()
}
}
}
2020-07-09 13:49:14 +02:00
SnackbarEvent(file.path).publish()
}
2020-07-09 13:49:14 +02:00
}
fun restartPressed() = reboot()
2020-07-07 06:05:43 +02:00
}