Update superuser list

This commit is contained in:
topjohnwu 2020-08-10 02:33:44 -07:00
parent f5e547944a
commit c7e30ac63e
5 changed files with 188 additions and 200 deletions

View File

@ -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<PolicyItem>() {
class PolicyItem(
val item: MagiskPolicy,
val icon: Drawable,
val viewModel: SuperuserViewModel
) : ObservableItem<PolicyItem>() {
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)

View File

@ -35,9 +35,7 @@ class SuperuserViewModel(
private val itemNoData = TextItem(R.string.superuser_policy_none)
private val itemsPolicies = diffListOf<PolicyItem>()
private val itemsHelpers = ObservableArrayList<TextItem>().also {
it.add(itemNoData)
}
private val itemsHelpers = ObservableArrayList<TextItem>()
val adapter = adapterOf<ComparableRvItem<*>>()
val items = MergeObservableList<ComparableRvItem<*>>()
@ -45,7 +43,6 @@ class SuperuserViewModel(
.insertList(itemsHelpers)
.insertList(itemsPolicies)
val itemBinding = itemBindingOf<ComparableRvItem<*>> {
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()

View File

@ -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()

View File

@ -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" />
<LinearLayout
goneUnless="@{viewModel.loading &amp;&amp; viewModel.items.empty}"
@ -44,9 +41,10 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
android:orientation="vertical"
tools:visibility="gone">
<androidx.appcompat.widget.AppCompatTextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/safetynet_attest_loading"

View File

@ -9,190 +9,170 @@
name="item"
type="com.topjohnwu.magisk.model.entity.recycler.PolicyItem" />
<variable
name="viewModel"
type="com.topjohnwu.magisk.ui.superuser.SuperuserViewModel" />
</data>
<com.google.android.material.card.MaterialCardView
style="@style/WidgetFoundation.Card.Variant"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:alpha="@{item.isEnabled() ? 1f : .5f}"
android:onClick="@{() -> item.toggle(viewModel)}"
android:onLongClick="@{() -> item.toggle()}"
tools:layout_marginBottom="@dimen/l1"
tools:layout_marginEnd="@dimen/l1">
android:layout_gravity="center">
<LinearLayout
<com.google.android.material.card.MaterialCardView
style="@style/WidgetFoundation.Card.Variant"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:alpha="@{item.enabled ? 1f : .5f}"
android:onClick="@{() -> item.toggleExpand()}">
<androidx.constraintlayout.widget.ConstraintLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/policy_app_icon"
style="@style/WidgetFoundation.Image"
android:layout_marginStart="@dimen/l1"
android:layout_marginTop="@dimen/l1"
android:layout_marginBottom="@dimen/l1"
android:src="@{item.icon}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0"
tools:srcCompat="@drawable/ic_logo" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/policy_app_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_generic"
android:layout_marginRight="@dimen/margin_generic"
android:ellipsize="middle"
android:gravity="start"
android:maxLines="2"
android:text="@{item.item.appName}"
android:textAppearance="@style/AppearanceFoundation.Body"
android:textIsSelectable="false"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/policy_indicator"
app:layout_constraintStart_toEndOf="@+id/policy_app_icon"
app:layout_constraintTop_toTopOf="@+id/policy_app_icon"
tools:text="@string/app_name" />
<ImageView
android:id="@+id/policy_app_icon"
style="@style/WidgetFoundation.Image"
android:layout_marginStart="@dimen/l1"
android:layout_marginTop="@dimen/l1"
android:layout_marginBottom="@dimen/l1"
srcCompat="@{item.icon}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0"
tools:srcCompat="@drawable/ic_logo" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/policy_package_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/l1"
android:ellipsize="middle"
android:gravity="start"
android:maxLines="2"
android:text="@{item.item.packageName}"
android:textAppearance="@style/AppearanceFoundation.Caption.Variant"
android:textColor="@android:color/tertiary_text_dark"
android:textIsSelectable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/policy_app_name"
app:layout_constraintStart_toStartOf="@id/policy_app_name"
app:layout_constraintTop_toBottomOf="@id/policy_app_name"
app:layout_constraintVertical_bias="0"
tools:text="com.topjohnwu.magisk" />
<TextView
android:id="@+id/policy_app_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_generic"
android:layout_marginRight="@dimen/margin_generic"
android:ellipsize="middle"
android:gravity="start"
android:maxLines="2"
android:text="@{item.item.appName}"
android:textAppearance="@style/AppearanceFoundation.Body"
android:textIsSelectable="false"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/policy_indicator"
app:layout_constraintStart_toEndOf="@+id/policy_app_icon"
app:layout_constraintTop_toTopOf="@+id/policy_app_icon"
tools:text="@string/app_name" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/policy_indicator"
style="@style/WidgetFoundation.Switch"
isSelected="@{item.enabled}"
android:layout_marginEnd="@dimen/l1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/policy_package_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/l1"
android:ellipsize="middle"
android:gravity="start"
android:maxLines="2"
android:text="@{item.item.packageName}"
android:textAppearance="@style/AppearanceFoundation.Caption.Variant"
android:textColor="@android:color/tertiary_text_dark"
android:textIsSelectable="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/policy_app_name"
app:layout_constraintStart_toStartOf="@id/policy_app_name"
app:layout_constraintTop_toBottomOf="@id/policy_app_name"
app:layout_constraintVertical_bias="0"
tools:text="com.topjohnwu.magisk" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/policy_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@={item.enabled}"
android:layout_marginEnd="@dimen/l1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/policy_expand_container"
gone="@{!item.isExpanded}"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?colorSurfaceVariant"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/policy_package_name"
tools:visibility="visible">
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/policy_notify"
style="@style/WidgetFoundation.Button.Text"
isSelected="@{item.shouldNotify}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:onClick="@{() -> item.toggleNotify(viewModel)}"
android:text="@string/superuser_toggle_notification"
android:textAllCaps="false"
android:textAppearance="@style/AppearanceFoundation.Tiny"
android:textColor="@color/color_state_primary_transient"
app:icon="@drawable/ic_notifications_md2"
app:iconTint="@color/color_state_primary_transient"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/policy_log"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/color_state_primary_transient" />
<LinearLayout
android:id="@+id/policy_expand_container"
android:orientation="horizontal"
gone="@{!item.isExpanded}"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?colorSurfaceVariant"
tools:visibility="visible">
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/l_50"
android:layout_marginBottom="@dimen/l_50"
android:background="?colorSurface"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/policy_notify"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/policy_notify"
style="@style/WidgetFoundation.Button.Text"
isSelected="@{item.shouldNotify}"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:onClick="@{() -> item.toggleNotify()}"
android:text="@string/superuser_toggle_notification"
android:textAllCaps="false"
android:textAppearance="@style/AppearanceFoundation.Tiny"
android:textColor="@color/color_state_primary_transient"
app:icon="@drawable/ic_notifications_md2"
app:iconTint="@color/color_state_primary_transient"
app:tint="@color/color_state_primary_transient" />
<com.google.android.material.button.MaterialButton
android:id="@+id/policy_log"
style="@style/WidgetFoundation.Button.Text"
isSelected="@{item.shouldLog}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:onClick="@{() -> item.toggleLog(viewModel)}"
android:text="@string/logs"
android:textAllCaps="false"
android:textAppearance="@style/AppearanceFoundation.Tiny"
android:textColor="@color/color_state_primary_transient"
app:icon="@drawable/ic_bug_md2"
app:iconTint="@color/color_state_primary_transient"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/policy_delete"
app:layout_constraintStart_toEndOf="@+id/policy_notify"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/color_state_primary_transient" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/l_50"
android:layout_marginBottom="@dimen/l_50"
android:background="?colorSurfaceSurfaceVariant" />
<View
android:layout_width="1dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/l_50"
android:layout_marginBottom="@dimen/l_50"
android:background="?colorSurface"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/policy_log"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/policy_log"
style="@style/WidgetFoundation.Button.Text"
isSelected="@{item.shouldLog}"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:onClick="@{() -> item.toggleLog()}"
android:text="@string/logs"
android:textAllCaps="false"
android:textAppearance="@style/AppearanceFoundation.Tiny"
android:textColor="@color/color_state_primary_transient"
app:icon="@drawable/ic_bug_md2"
app:iconTint="@color/color_state_primary_transient"
app:tint="@color/color_state_primary_transient" />
<com.google.android.material.button.MaterialButton
android:id="@+id/policy_delete"
style="@style/WidgetFoundation.Button.Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:onClick="@{() -> viewModel.deletePressed(item)}"
android:text="@string/superuser_toggle_revoke"
android:textAllCaps="false"
android:textColor="?colorError"
android:textSize="12sp"
app:icon="@drawable/ic_delete_md2"
app:iconTint="?colorError"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/policy_log"
app:layout_constraintTop_toTopOf="parent"
app:rippleColor="?colorError" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/l_50"
android:layout_marginBottom="@dimen/l_50"
android:background="?colorSurfaceSurfaceVariant" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/policy_delete"
style="@style/WidgetFoundation.Button.Text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:onClick="@{() -> item.revoke()}"
android:text="@string/superuser_toggle_revoke"
android:textAllCaps="false"
android:textColor="?colorError"
android:textSize="12sp"
app:icon="@drawable/ic_delete_md2"
app:iconTint="?colorError"
app:rippleColor="?colorError" />
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
</layout>