2019-04-24 20:28:41 +02:00
|
|
|
package com.topjohnwu.magisk.ui.flash
|
|
|
|
|
2020-03-24 18:07:20 +01:00
|
|
|
import android.net.Uri
|
|
|
|
import android.view.MenuItem
|
2020-07-12 12:17:50 +02:00
|
|
|
import androidx.databinding.Bindable
|
2021-01-16 06:28:59 +01:00
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
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
|
2020-03-24 18:07:20 +01:00
|
|
|
import com.topjohnwu.magisk.R
|
2020-08-18 15:31:15 +02:00
|
|
|
import com.topjohnwu.magisk.arch.BaseViewModel
|
|
|
|
import com.topjohnwu.magisk.arch.diffListOf
|
|
|
|
import com.topjohnwu.magisk.arch.itemBindingOf
|
2020-03-24 18:07:20 +01:00
|
|
|
import com.topjohnwu.magisk.core.Const
|
2021-01-18 22:32:10 +01:00
|
|
|
import com.topjohnwu.magisk.core.Info
|
2020-07-07 07:30:21 +02:00
|
|
|
import com.topjohnwu.magisk.core.tasks.FlashZip
|
2020-03-24 18:07:20 +01:00
|
|
|
import com.topjohnwu.magisk.core.tasks.MagiskInstaller
|
2020-12-20 07:25:44 +01:00
|
|
|
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
|
|
|
|
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
|
2020-08-18 15:31:15 +02:00
|
|
|
import com.topjohnwu.magisk.databinding.RvBindingAdapter
|
2020-08-19 11:05:23 +02:00
|
|
|
import com.topjohnwu.magisk.events.SnackbarEvent
|
2020-07-11 14:36:31 +02:00
|
|
|
import com.topjohnwu.magisk.ktx.*
|
2020-07-15 10:21:57 +02:00
|
|
|
import com.topjohnwu.magisk.utils.set
|
2020-08-18 15:31:15 +02:00
|
|
|
import com.topjohnwu.magisk.view.Notifications
|
2020-07-17 09:13:03 +02:00
|
|
|
import com.topjohnwu.superuser.CallbackList
|
2020-03-24 18:07:20 +01:00
|
|
|
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
|
2019-04-24 20:28:41 +02:00
|
|
|
|
2021-01-22 11:28:53 +01:00
|
|
|
class FlashViewModel(args: FlashFragmentArgs) : BaseViewModel() {
|
2020-03-24 18:07:20 +01:00
|
|
|
|
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)
|
|
|
|
|
2021-01-16 06:28:59 +01:00
|
|
|
private val _subtitle = MutableLiveData(R.string.flashing)
|
|
|
|
val subtitle get() = _subtitle as LiveData<Int>
|
2020-03-24 18:07:20 +01:00
|
|
|
|
2020-08-18 15:31:15 +02:00
|
|
|
val adapter = RvBindingAdapter<ConsoleItem>()
|
2020-03-24 18:07:20 +01:00
|
|
|
val items = diffListOf<ConsoleItem>()
|
|
|
|
val itemBinding = itemBindingOf<ConsoleItem>()
|
|
|
|
|
2020-07-17 09:13:03 +02:00
|
|
|
private val logItems = mutableListOf<String>().synchronized()
|
|
|
|
private val outItems = object : CallbackList<String>() {
|
|
|
|
override fun onAddElement(e: String?) {
|
|
|
|
e ?: return
|
|
|
|
items.add(ConsoleItem(e))
|
|
|
|
logItems.add(e)
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 18:07:20 +01:00
|
|
|
|
|
|
|
init {
|
2021-01-22 11:28:53 +01:00
|
|
|
val (action, uri, id) = args
|
|
|
|
if (id != -1)
|
|
|
|
Notifications.mgr.cancel(id)
|
|
|
|
startFlashing(action, uri)
|
2020-03-24 18:07:20 +01:00
|
|
|
}
|
|
|
|
|
2021-01-22 11:28:53 +01:00
|
|
|
private fun startFlashing(action: String, uri: Uri?) {
|
2020-07-07 07:30:21 +02:00
|
|
|
viewModelScope.launch {
|
|
|
|
val result = when (action) {
|
|
|
|
Const.Value.FLASH_ZIP -> {
|
2021-01-22 11:28:53 +01:00
|
|
|
FlashZip(uri!!, outItems, logItems).exec()
|
2020-07-07 07:30:21 +02:00
|
|
|
}
|
|
|
|
Const.Value.UNINSTALL -> {
|
2020-07-12 12:17:50 +02:00
|
|
|
showReboot = false
|
2021-01-22 11:28:53 +01:00
|
|
|
MagiskInstaller.Uninstall(outItems, logItems).exec()
|
2020-07-07 07:30:21 +02:00
|
|
|
}
|
|
|
|
Const.Value.FLASH_MAGISK -> {
|
2021-01-18 22:32:10 +01:00
|
|
|
if (Info.isEmulator)
|
2021-01-22 11:28:53 +01:00
|
|
|
MagiskInstaller.Emulator(outItems, logItems).exec()
|
2021-01-18 22:32:10 +01:00
|
|
|
else
|
2021-01-22 11:28:53 +01:00
|
|
|
MagiskInstaller.Direct(outItems, logItems).exec()
|
2020-07-07 07:30:21 +02:00
|
|
|
}
|
|
|
|
Const.Value.FLASH_INACTIVE_SLOT -> {
|
2021-01-22 11:28:53 +01:00
|
|
|
MagiskInstaller.SecondSlot(outItems, logItems).exec()
|
2020-07-07 07:30:21 +02:00
|
|
|
}
|
|
|
|
Const.Value.PATCH_FILE -> {
|
|
|
|
uri ?: return@launch
|
2020-07-12 12:17:50 +02:00
|
|
|
showReboot = false
|
2021-01-22 11:28:53 +01:00
|
|
|
MagiskInstaller.Patch(uri, outItems, logItems).exec()
|
2020-07-07 07:30:21 +02:00
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
back()
|
|
|
|
return@launch
|
|
|
|
}
|
2020-07-07 06:05:43 +02:00
|
|
|
}
|
2020-07-07 07:30:21 +02:00
|
|
|
onResult(result)
|
2020-03-24 18:07:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 07:30:21 +02:00
|
|
|
private fun onResult(success: Boolean) {
|
2020-03-24 18:07:20 +01:00
|
|
|
state = if (success) State.LOADED else State.LOADING_FAILED
|
2021-01-16 06:28:59 +01:00
|
|
|
when {
|
|
|
|
success -> _subtitle.postValue(R.string.done)
|
|
|
|
else -> _subtitle.postValue(R.string.failure)
|
2020-03-24 18:07:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-12-20 07:25:44 +01:00
|
|
|
viewModelScope.launch(Dispatchers.IO) {
|
|
|
|
val name = "magisk_install_log_%s.log".format(now.toTime(timeFormatStandard))
|
2020-12-09 14:03:49 +01:00
|
|
|
val file = MediaStoreUtils.getFile(name, true)
|
2020-12-20 07:25:44 +01:00
|
|
|
file.uri.outputStream().bufferedWriter().use { writer ->
|
|
|
|
logItems.forEach {
|
|
|
|
writer.write(it)
|
|
|
|
writer.newLine()
|
2020-03-24 18:07:20 +01:00
|
|
|
}
|
|
|
|
}
|
2020-12-20 07:25:44 +01:00
|
|
|
SnackbarEvent(file.toString()).publish()
|
2020-03-24 18:07:20 +01:00
|
|
|
}
|
2020-07-09 13:49:14 +02:00
|
|
|
}
|
2020-03-24 18:07:20 +01:00
|
|
|
|
|
|
|
fun restartPressed() = reboot()
|
2020-07-07 06:05:43 +02:00
|
|
|
}
|