mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-06 15:30:46 -05:00
Added logic to superuser screen
This commit is contained in:
@@ -9,6 +9,8 @@ import androidx.fragment.app.Fragment
|
||||
import androidx.transition.TransitionManager
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.base.BaseActivity
|
||||
import com.topjohnwu.magisk.extensions.snackbar
|
||||
import com.topjohnwu.magisk.model.events.SnackbarEvent
|
||||
import com.topjohnwu.magisk.model.events.ViewEvent
|
||||
import com.topjohnwu.magisk.model.navigation.MagiskNavigationEvent
|
||||
import com.topjohnwu.magisk.model.navigation.Navigator
|
||||
@@ -54,6 +56,9 @@ abstract class CompatActivity<ViewModel : CompatViewModel, Binding : ViewDataBin
|
||||
|
||||
override fun onEventDispatched(event: ViewEvent) {
|
||||
delegate.onEventExecute(event, this)
|
||||
when (event) {
|
||||
is SnackbarEvent -> snackbar(snackbarView, event.message(this), event.length, event.f)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
|
||||
@@ -1,20 +1,34 @@
|
||||
package com.topjohnwu.magisk.redesign.superuser
|
||||
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.res.Resources
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.data.database.PolicyDao
|
||||
import com.topjohnwu.magisk.databinding.ComparableRvItem
|
||||
import com.topjohnwu.magisk.extensions.applySchedulers
|
||||
import com.topjohnwu.magisk.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.extensions.toggle
|
||||
import com.topjohnwu.magisk.model.entity.MagiskPolicy
|
||||
import com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem
|
||||
import com.topjohnwu.magisk.model.events.PolicyEnableEvent
|
||||
import com.topjohnwu.magisk.model.events.PolicyUpdateEvent
|
||||
import com.topjohnwu.magisk.model.events.SnackbarEvent
|
||||
import com.topjohnwu.magisk.model.events.dialog.FingerprintDialog
|
||||
import com.topjohnwu.magisk.model.events.dialog.SuperuserRevokeDialog
|
||||
import com.topjohnwu.magisk.model.navigation.Navigation
|
||||
import com.topjohnwu.magisk.redesign.compat.CompatViewModel
|
||||
import com.topjohnwu.magisk.redesign.home.itemBindingOf
|
||||
import com.topjohnwu.magisk.utils.DiffObservableList
|
||||
import com.topjohnwu.magisk.utils.FingerprintHelper
|
||||
import com.topjohnwu.magisk.utils.RxBus
|
||||
import io.reactivex.Single
|
||||
|
||||
class SuperuserViewModel(
|
||||
private val rxBus: RxBus,
|
||||
private val db: PolicyDao,
|
||||
private val packageManager: PackageManager
|
||||
private val packageManager: PackageManager,
|
||||
private val resources: Resources
|
||||
) : CompatViewModel() {
|
||||
|
||||
val items = diffListOf<PolicyRvItem>()
|
||||
@@ -22,6 +36,17 @@ class SuperuserViewModel(
|
||||
it.bindExtra(BR.viewModel, this)
|
||||
}
|
||||
|
||||
init {
|
||||
rxBus.register<PolicyEnableEvent>()
|
||||
.subscribeK { togglePolicy(it.item, it.enable) }
|
||||
.add()
|
||||
rxBus.register<PolicyUpdateEvent>()
|
||||
.subscribeK { updatePolicy(it) }
|
||||
.add()
|
||||
}
|
||||
|
||||
// ---
|
||||
|
||||
override fun refresh() = db.fetchAll()
|
||||
.flattenAsFlowable { it }
|
||||
.parallel()
|
||||
@@ -39,12 +64,77 @@ class SuperuserViewModel(
|
||||
.applyViewModel(this)
|
||||
.subscribeK { items.update(it.first, it.second) }
|
||||
|
||||
// ---
|
||||
|
||||
fun hidePressed() = Navigation.hide().publish()
|
||||
|
||||
fun deletePressed(item: PolicyRvItem) {
|
||||
TODO()
|
||||
fun updateState() = deletePolicy(item.item)
|
||||
.subscribeK { items.removeAll { it.itemSameAs(item) } }
|
||||
.add()
|
||||
|
||||
if (FingerprintHelper.useFingerprint()) {
|
||||
FingerprintDialog {
|
||||
onSuccess { updateState() }
|
||||
}.publish()
|
||||
} else {
|
||||
SuperuserRevokeDialog {
|
||||
appName = item.item.appName
|
||||
onSuccess { updateState() }
|
||||
}.publish()
|
||||
}
|
||||
}
|
||||
|
||||
//---
|
||||
|
||||
private fun updatePolicy(it: PolicyUpdateEvent) = when (it) {
|
||||
is PolicyUpdateEvent.Notification -> updatePolicy(it.item).map {
|
||||
when {
|
||||
it.notification -> R.string.su_snack_notif_on
|
||||
else -> R.string.su_snack_notif_off
|
||||
} to it.appName
|
||||
}
|
||||
is PolicyUpdateEvent.Log -> updatePolicy(it.item).map {
|
||||
when {
|
||||
it.logging -> R.string.su_snack_log_on
|
||||
else -> R.string.su_snack_log_off
|
||||
} to it.appName
|
||||
}
|
||||
}.map { resources.getString(it.first, it.second) }
|
||||
.subscribeK { SnackbarEvent(it).publish() }
|
||||
.add()
|
||||
|
||||
private fun togglePolicy(item: PolicyRvItem, enable: Boolean) {
|
||||
fun updateState() {
|
||||
val policy = if (enable) MagiskPolicy.ALLOW else MagiskPolicy.DENY
|
||||
val app = item.item.copy(policy = policy)
|
||||
|
||||
updatePolicy(app)
|
||||
.map { it.policy == MagiskPolicy.ALLOW }
|
||||
.map { if (it) R.string.su_snack_grant else R.string.su_snack_deny }
|
||||
.map { resources.getString(it).format(item.item.appName) }
|
||||
.subscribeK { SnackbarEvent(it).publish() }
|
||||
.add()
|
||||
}
|
||||
|
||||
if (FingerprintHelper.useFingerprint()) {
|
||||
FingerprintDialog {
|
||||
onSuccess { updateState() }
|
||||
onFailure { item.isEnabled.toggle() }
|
||||
}.publish()
|
||||
} else {
|
||||
updateState()
|
||||
}
|
||||
}
|
||||
|
||||
//---
|
||||
|
||||
private fun updatePolicy(policy: MagiskPolicy) =
|
||||
db.update(policy).andThen(Single.just(policy))
|
||||
|
||||
private fun deletePolicy(policy: MagiskPolicy) =
|
||||
db.delete(policy.uid).andThen(Single.just(policy))
|
||||
|
||||
}
|
||||
|
||||
inline fun <T : ComparableRvItem<T>> diffListOf(
|
||||
|
||||
Reference in New Issue
Block a user