Added env check to homepage

This commit is contained in:
Viktor De Pasquale 2019-10-17 15:37:10 +02:00
parent 23978ef4d2
commit 38019f7f42
2 changed files with 105 additions and 4 deletions

View File

@ -0,0 +1,77 @@
package com.topjohnwu.magisk.model.events.dialog
import android.content.Context
import android.content.DialogInterface
import android.widget.Toast
import androidx.core.net.toUri
import com.topjohnwu.magisk.Info
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.extensions.cachedFile
import com.topjohnwu.magisk.extensions.reboot
import com.topjohnwu.magisk.net.Networking
import com.topjohnwu.magisk.tasks.MagiskInstaller
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.view.MagiskDialog
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import com.topjohnwu.superuser.internal.UiThreadHandler
import com.topjohnwu.superuser.io.SuFile
import org.koin.core.KoinComponent
import org.koin.core.get
import java.io.File
class EnvFixDialog : DialogEvent() {
override fun build(dialog: MagiskDialog) = dialog
.applyTitle(R.string.env_fix_title)
.applyMessage(R.string.env_fix_msg)
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
titleRes = R.string.yes
preventDismiss = true
onClick {
dialog.applyTitle(R.string.setup_title)
.applyMessage(R.string.setup_msg)
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
title = ""
}
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
title = ""
}
.cancellable(false)
fixEnv(it)
}
}
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = R.string.no_thanks
}
.let { Unit }
private fun fixEnv(dialog: DialogInterface) {
object : MagiskInstaller(), KoinComponent {
override fun operations(): Boolean {
val context = get<Context>()
val zip: File = context.cachedFile("magisk.zip")
installDir = SuFile("/data/adb/magisk")
Shell.su("rm -rf /data/adb/magisk/*").exec()
if (!ShellUtils.checkSum("MD5", zip, Info.remote.magisk.md5))
Networking.get(Info.remote.magisk.link).execForFile(zip)
zipUri = zip.toUri()
return extractZip() && Shell.su("fix_env").exec().isSuccess
}
override fun onResult(success: Boolean) {
dialog.dismiss()
Utils.toast(
if (success) R.string.reboot_delay_toast else R.string.setup_fail,
Toast.LENGTH_LONG
)
if (success)
UiThreadHandler.handler.postDelayed({ reboot() }, 5000)
}
}.exec()
}
}

View File

@ -5,21 +5,20 @@ import com.topjohnwu.magisk.Info
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.repository.MagiskRepository
import com.topjohnwu.magisk.databinding.ComparableRvItem
import com.topjohnwu.magisk.extensions.packageName
import com.topjohnwu.magisk.extensions.replaceRandomWithSpecial
import com.topjohnwu.magisk.extensions.res
import com.topjohnwu.magisk.extensions.subscribeK
import com.topjohnwu.magisk.extensions.*
import com.topjohnwu.magisk.model.entity.MagiskJson
import com.topjohnwu.magisk.model.entity.ManagerJson
import com.topjohnwu.magisk.model.entity.UpdateInfo
import com.topjohnwu.magisk.model.entity.recycler.HomeItem
import com.topjohnwu.magisk.model.events.OpenInappLinkEvent
import com.topjohnwu.magisk.model.events.dialog.EnvFixDialog
import com.topjohnwu.magisk.model.events.dialog.MagiskInstallDialog
import com.topjohnwu.magisk.model.events.dialog.ManagerInstallDialog
import com.topjohnwu.magisk.model.observer.Observer
import com.topjohnwu.magisk.redesign.compat.CompatViewModel
import com.topjohnwu.magisk.ui.home.MagiskState
import com.topjohnwu.magisk.utils.KObservableField
import com.topjohnwu.superuser.Shell
import me.tatarka.bindingcollectionadapter2.BR
import me.tatarka.bindingcollectionadapter2.ItemBinding
import me.tatarka.bindingcollectionadapter2.OnItemBind
@ -69,6 +68,8 @@ class HomeViewModel(
it.bindExtra(BR.viewModel, this)
}
private var shownDialog = false
override fun refresh() = repoMagisk.fetchUpdate()
.onErrorReturn { Info.remote }
.subscribeK { updateBy(it) }
@ -101,6 +102,8 @@ class HomeViewModel(
)
else -> ""
}
ensureEnv()
}
fun onLinkPressed(link: String) = OpenInappLinkEvent(link).publish()
@ -111,6 +114,27 @@ class HomeViewModel(
fun onMagiskPressed() = MagiskInstallDialog().publish()
private fun ensureEnv() {
val invalidStates = listOf(
MagiskState.NOT_INSTALLED,
MagiskState.LOADING
)
// Don't bother checking env when magisk is not installed, loading or already has been shown
if (invalidStates.any { it == stateMagisk.value } || shownDialog) {
return
}
Shell.su("env_check")
.toSingle()
.map { it.exec() }
.filter { !it.isSuccess }
.subscribeK {
shownDialog = true
EnvFixDialog().publish()
}
}
}
@Suppress("unused")