From c7e30ac63e4037648432a7ff309982ab580e3fb7 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 10 Aug 2020 02:33:44 -0700 Subject: [PATCH] Update superuser list --- .../model/entity/recycler/PolicyRvItem.kt | 47 +-- .../magisk/ui/superuser/SuperuserViewModel.kt | 20 +- .../magisk/utils/DataBindingAdapters.kt | 5 + .../res/layout/fragment_superuser_md2.xml | 12 +- app/src/main/res/layout/item_policy_md2.xml | 304 ++++++++---------- 5 files changed, 188 insertions(+), 200 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt index a2eae7721..5f748f400 100644 --- a/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt +++ b/app/src/main/java/com/topjohnwu/magisk/model/entity/recycler/PolicyRvItem.kt @@ -9,53 +9,60 @@ import com.topjohnwu.magisk.databinding.ObservableItem import com.topjohnwu.magisk.ui.superuser.SuperuserViewModel import com.topjohnwu.magisk.utils.set -class PolicyItem(val item: MagiskPolicy, val icon: Drawable) : ObservableItem() { +class PolicyItem( + val item: MagiskPolicy, + val icon: Drawable, + val viewModel: SuperuserViewModel +) : ObservableItem() { override val layoutRes = R.layout.item_policy_md2 @get:Bindable var isExpanded = false set(value) = set(value, field, { field = it }, BR.expanded) - @get:Bindable - var isEnabled = item.policy == MagiskPolicy.ALLOW + // This property hosts the policy state + var policyState = item.policy == MagiskPolicy.ALLOW set(value) = set(value, field, { field = it }, BR.enabled) + // This property binds with the UI state + @get:Bindable + var isEnabled + get() = policyState + set(value) = set(value, policyState, { viewModel.togglePolicy(this, it) }, BR.enabled) + @get:Bindable var shouldNotify = item.notification - set(value) = set(value, field, { field = it }, BR.shouldNotify) + set(value) = set(value, field, { field = it }, BR.shouldNotify) { + viewModel.updatePolicy(updatedPolicy, isLogging = false) + } @get:Bindable var shouldLog = item.logging - set(value) = set(value, field, { field = it }, BR.shouldLog) + set(value) = set(value, field, { field = it }, BR.shouldLog) { + viewModel.updatePolicy(updatedPolicy, isLogging = true) + } private val updatedPolicy get() = item.copy( - policy = if (isEnabled) MagiskPolicy.ALLOW else MagiskPolicy.DENY, + policy = if (policyState) MagiskPolicy.ALLOW else MagiskPolicy.DENY, notification = shouldNotify, logging = shouldLog ) - fun toggle(viewModel: SuperuserViewModel) { - if (isExpanded) { - toggle() - return - } - isEnabled = !isEnabled - viewModel.togglePolicy(this, isEnabled) - } - - fun toggle() { + fun toggleExpand() { isExpanded = !isExpanded } - fun toggleNotify(viewModel: SuperuserViewModel) { + fun toggleNotify() { shouldNotify = !shouldNotify - viewModel.updatePolicy(updatedPolicy, isLogging = false) } - fun toggleLog(viewModel: SuperuserViewModel) { + fun toggleLog() { shouldLog = !shouldLog - viewModel.updatePolicy(updatedPolicy, isLogging = true) + } + + fun revoke() { + viewModel.deletePressed(this) } override fun contentSameAs(other: PolicyItem) = itemSameAs(other) diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt index 034ccd636..30510e793 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/superuser/SuperuserViewModel.kt @@ -35,9 +35,7 @@ class SuperuserViewModel( private val itemNoData = TextItem(R.string.superuser_policy_none) private val itemsPolicies = diffListOf() - private val itemsHelpers = ObservableArrayList().also { - it.add(itemNoData) - } + private val itemsHelpers = ObservableArrayList() val adapter = adapterOf>() val items = MergeObservableList>() @@ -45,7 +43,6 @@ class SuperuserViewModel( .insertList(itemsHelpers) .insertList(itemsPolicies) val itemBinding = itemBindingOf> { - it.bindExtra(BR.viewModel, this) it.bindExtra(BR.listener, this) } @@ -55,7 +52,7 @@ class SuperuserViewModel( state = State.LOADING val (policies, diff) = withContext(Dispatchers.Default) { val policies = db.fetchAll { - PolicyItem(it, it.applicationInfo.loadIcon(packageManager)) + PolicyItem(it, it.applicationInfo.loadIcon(packageManager), this@SuperuserViewModel) }.sortedWith(compareBy( { it.item.appName.toLowerCase(currentLocale) }, { it.item.packageName } @@ -63,15 +60,15 @@ class SuperuserViewModel( policies to itemsPolicies.calculateDiff(policies) } itemsPolicies.update(policies, diff) - if (itemsPolicies.isNotEmpty()) { - itemsHelpers.remove(itemNoData) - } + if (itemsPolicies.isNotEmpty()) + itemsHelpers.clear() + else if (itemsHelpers.isEmpty()) + itemsHelpers.add(itemNoData) state = State.LOADED } // --- - @Suppress("REDUNDANT_ELSE_IN_WHEN") override fun onItemPressed(item: TappableHeadlineItem) = when (item) { TappableHeadlineItem.Hide -> hidePressed() else -> Unit @@ -120,6 +117,8 @@ class SuperuserViewModel( fun togglePolicy(item: PolicyItem, enable: Boolean) { fun updateState() { + item.policyState = enable + val policy = if (enable) MagiskPolicy.ALLOW else MagiskPolicy.DENY val app = item.item.copy(policy = policy) @@ -127,14 +126,13 @@ class SuperuserViewModel( db.update(app) val res = if (app.policy == MagiskPolicy.ALLOW) R.string.su_snack_grant else R.string.su_snack_deny - SnackbarEvent(resources.getString(res).format(item.item.appName)) + SnackbarEvent(resources.getString(res).format(item.item.appName)).publish() } } if (BiometricHelper.isEnabled) { BiometricDialog { onSuccess { updateState() } - onFailure { item.isEnabled = !item.isEnabled } }.publish() } else { updateState() diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt b/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt index fcc6d72e2..970240da1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/DataBindingAdapters.kt @@ -37,6 +37,11 @@ fun setImageResource(view: ImageView, @DrawableRes resId: Int) { view.setImageResource(resId) } +@BindingAdapter("srcCompat") +fun setImageResource(view: ImageView, drawable: Drawable) { + view.setImageDrawable(drawable) +} + @BindingAdapter("movieBehavior", "movieBehaviorText") fun setMovieBehavior(view: TextView, isMovieBehavior: Boolean, text: String) { (view.tag as? Job)?.cancel() diff --git a/app/src/main/res/layout/fragment_superuser_md2.xml b/app/src/main/res/layout/fragment_superuser_md2.xml index 8928b83ee..018c4f0de 100644 --- a/app/src/main/res/layout/fragment_superuser_md2.xml +++ b/app/src/main/res/layout/fragment_superuser_md2.xml @@ -21,7 +21,7 @@ android:id="@+id/superuser_list" adapter="@{viewModel.adapter}" dividerHorizontal="@{@drawable/divider_l1}" - dividerVertical="@{@drawable/divider_l1}" + dividerVertical="@{@drawable/divider_l_50}" goneUnless="@{viewModel.loaded || !viewModel.items.empty}" itemBinding="@{viewModel.itemBinding}" items="@{viewModel.items}" @@ -33,10 +33,7 @@ android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size + (int) @dimen/l1}" android:paddingBottom="@{viewModel.insets.bottom + (int) @dimen/l2}" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" - app:spanCount="2" - tools:layout_marginTop="24dp" - tools:listitem="@layout/item_policy_md2" - tools:paddingTop="@dimen/l1" /> + tools:listitem="@layout/item_policy_md2" /> + android:orientation="vertical" + tools:visibility="gone"> - - - - + android:layout_gravity="center"> - + android:alpha="@{item.enabled ? 1f : .5f}" + android:onClick="@{() -> item.toggleExpand()}"> - + android:layout_height="wrap_content" + android:orientation="vertical"> - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + +