From 2eb001876a9f1421a111fdddb885302cb02f5be4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 11 Sep 2020 03:17:43 -0700 Subject: [PATCH] Code cleanup --- .../topjohnwu/magisk/core/model/su/SuPolicy.kt | 18 +++++++++--------- .../topjohnwu/magisk/di/ViewModelsModule.kt | 2 +- .../magisk/ui/superuser/SuperuserViewModel.kt | 4 +--- .../magisk/ui/surequest/SuRequestViewModel.kt | 4 ++-- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt b/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt index e2727263f..3d6a2eafb 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/model/su/SuPolicy.kt @@ -1,7 +1,7 @@ package com.topjohnwu.magisk.core.model.su -import android.content.pm.ApplicationInfo import android.content.pm.PackageManager +import android.graphics.drawable.Drawable import com.topjohnwu.magisk.core.model.su.SuPolicy.Companion.INTERACTIVE import com.topjohnwu.magisk.ktx.getLabel @@ -10,11 +10,11 @@ data class SuPolicy( var uid: Int, val packageName: String, val appName: String, + val icon: Drawable, var policy: Int = INTERACTIVE, var until: Long = -1L, val logging: Boolean = true, - val notification: Boolean = true, - val applicationInfo: ApplicationInfo + val notification: Boolean = true ) { companion object { @@ -46,12 +46,12 @@ fun Map.toPolicy(pm: PackageManager): SuPolicy { return SuPolicy( uid = uid, packageName = packageName, + appName = info.getLabel(pm), + icon = info.loadIcon(pm), policy = get("policy")?.toIntOrNull() ?: INTERACTIVE, until = get("until")?.toLongOrNull() ?: -1L, logging = get("logging")?.toIntOrNull() != 0, - notification = get("notification")?.toIntOrNull() != 0, - applicationInfo = info, - appName = info.getLabel(pm) + notification = get("notification")?.toIntOrNull() != 0 ) } @@ -63,8 +63,8 @@ fun Int.toPolicy(pm: PackageManager, policy: Int = INTERACTIVE): SuPolicy { return SuPolicy( uid = info.uid, packageName = pkg, - policy = policy, - applicationInfo = info, - appName = info.getLabel(pm) + appName = info.getLabel(pm), + icon = info.loadIcon(pm), + policy = policy ) } diff --git a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt index dd2c4b191..2c364d71d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/ViewModelsModule.kt @@ -23,7 +23,7 @@ val viewModelModules = module { viewModel { ModuleViewModel(get(), get(), get()) } viewModel { SafetynetViewModel() } viewModel { SettingsViewModel(get()) } - viewModel { SuperuserViewModel(get(), get(), get()) } + viewModel { SuperuserViewModel(get(), get()) } viewModel { ThemeViewModel() } viewModel { InstallViewModel(get()) } viewModel { MainViewModel() } 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 4ac7cafc2..898628f15 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 @@ -1,6 +1,5 @@ package com.topjohnwu.magisk.ui.superuser -import android.content.pm.PackageManager import android.content.res.Resources import androidx.databinding.ObservableArrayList import androidx.lifecycle.viewModelScope @@ -27,7 +26,6 @@ import me.tatarka.bindingcollectionadapter2.collections.MergeObservableList class SuperuserViewModel( private val db: PolicyDao, - private val packageManager: PackageManager, private val resources: Resources ) : BaseViewModel(), TappableHeadlineItem.Listener { @@ -51,7 +49,7 @@ class SuperuserViewModel( state = State.LOADING val (policies, diff) = withContext(Dispatchers.Default) { val policies = db.fetchAll { - PolicyRvItem(it, it.applicationInfo.loadIcon(packageManager), this@SuperuserViewModel) + PolicyRvItem(it, it.icon, this@SuperuserViewModel) }.sortedWith(compareBy( { it.item.appName.toLowerCase(currentLocale) }, { it.item.packageName } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt index e6280e76f..3dece0b8d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt @@ -29,7 +29,7 @@ import me.tatarka.bindingcollectionadapter2.ItemBinding import java.util.concurrent.TimeUnit.SECONDS class SuRequestViewModel( - private val pm: PackageManager, + pm: PackageManager, policyDB: PolicyDao, private val timeoutPrefs: SharedPreferences, private val res: Resources @@ -94,7 +94,7 @@ class SuRequestViewModel( } private fun showDialog(policy: SuPolicy) { - icon = policy.applicationInfo.loadIcon(pm) + icon = policy.icon title = policy.appName packageName = policy.packageName selectedItemPosition = timeoutPrefs.getInt(policy.packageName, 0)