From d546733287b40c1d084cacebaed99e8497c98b09 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Sun, 12 May 2019 17:25:26 +0200 Subject: [PATCH] Removed direct static usages of database from app --- app/src/main/java/com/topjohnwu/magisk/App.kt | 5 --- .../com/topjohnwu/magisk/di/DatabaseModule.kt | 3 +- .../model/entity/recycler/PolicyRvItem.kt | 9 +++--- .../topjohnwu/magisk/model/events/RxEvents.kt | 4 +-- .../magisk/ui/base/BasePreferenceFragment.kt | 4 +-- .../magisk/ui/settings/SettingsFragment.java | 4 ++- .../magisk/ui/superuser/SuperuserViewModel.kt | 31 +++++++++---------- 7 files changed, 27 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/App.kt b/app/src/main/java/com/topjohnwu/magisk/App.kt index c2251968d..c7edc7503 100644 --- a/app/src/main/java/com/topjohnwu/magisk/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/App.kt @@ -11,14 +11,12 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDex import com.chibatching.kotpref.Kotpref -import com.topjohnwu.magisk.data.database.MagiskDB import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.RootUtils import com.topjohnwu.magisk.utils.inject import com.topjohnwu.net.Networking import com.topjohnwu.superuser.Shell -import org.koin.android.ext.android.inject import org.koin.android.ext.koin.androidContext import org.koin.core.context.startKoin import timber.log.Timber @@ -28,9 +26,6 @@ open class App : Application(), Application.ActivityLifecycleCallbacks { lateinit var protectedContext: Context - @Deprecated("Use dependency injection", level = DeprecationLevel.ERROR) - val DB: MagiskDB by inject() - @Volatile private var foreground: Activity? = null diff --git a/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt b/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt index 4a0b37d9d..4248a82ad 100644 --- a/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt +++ b/app/src/main/java/com/topjohnwu/magisk/di/DatabaseModule.kt @@ -2,13 +2,12 @@ package com.topjohnwu.magisk.di import android.content.Context import androidx.room.Room -import com.topjohnwu.magisk.App import com.topjohnwu.magisk.data.database.* import org.koin.dsl.module val databaseModule = module { - single { MagiskDB(get().protectedContext) } + //single { MagiskDB(get().protectedContext) } single { createDatabase(get()) } single { LogDao() } single { PolicyDao(get()) } 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 c38c8881c..a713ba389 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 @@ -6,13 +6,14 @@ import com.skoumal.teanity.extensions.addOnPropertyChangedCallback import com.skoumal.teanity.rxbus.RxBus import com.skoumal.teanity.util.KObservableField import com.topjohnwu.magisk.R +import com.topjohnwu.magisk.model.entity.MagiskPolicy import com.topjohnwu.magisk.model.entity.Policy import com.topjohnwu.magisk.model.events.PolicyEnableEvent import com.topjohnwu.magisk.model.events.PolicyUpdateEvent import com.topjohnwu.magisk.utils.inject import com.topjohnwu.magisk.utils.toggle -class PolicyRvItem(val item: Policy, val icon: Drawable) : ComparableRvItem() { +class PolicyRvItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem() { override val layoutRes: Int = R.layout.item_policy @@ -32,13 +33,11 @@ class PolicyRvItem(val item: Policy, val icon: Drawable) : ComparableRvItem { + }, Throwable::printStackTrace); break; case Config.Key.DARK_THEME: requireActivity().recreate(); 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 c083861c2..06df126d0 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 @@ -10,7 +10,8 @@ import com.skoumal.teanity.util.DiffObservableList import com.skoumal.teanity.viewevents.SnackbarEvent import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.R -import com.topjohnwu.magisk.data.database.MagiskDB +import com.topjohnwu.magisk.data.repository.AppRepository +import com.topjohnwu.magisk.model.entity.MagiskPolicy import com.topjohnwu.magisk.model.entity.Policy import com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem import com.topjohnwu.magisk.model.events.PolicyEnableEvent @@ -24,7 +25,7 @@ import io.reactivex.Single import me.tatarka.bindingcollectionadapter2.ItemBinding class SuperuserViewModel( - private val database: MagiskDB, + private val appRepo: AppRepository, private val packageManager: PackageManager, private val resources: Resources, rxBus: RxBus @@ -50,9 +51,9 @@ class SuperuserViewModel( } fun updatePolicies() { - Single.fromCallable { database.policyList } + appRepo.fetchAll() .flattenAsFlowable { it } - .map { PolicyRvItem(it, it.info.loadIcon(packageManager)) } + .map { PolicyRvItem(it, it.applicationInfo.loadIcon(packageManager)) } .toList() .applySchedulers() .applyViewModel(this) @@ -84,12 +85,12 @@ class SuperuserViewModel( } private fun updatePolicy(it: PolicyUpdateEvent) = when (it) { - is PolicyUpdateEvent.Notification -> updatePolicy(it.item) { + is PolicyUpdateEvent.Notification -> updatePolicy(it.item.item.copy(notification = it.shouldNotify)) { val textId = if (it.logging) R.string.su_snack_notif_on else R.string.su_snack_notif_off val text = resources.getString(textId).format(it.appName) SnackbarEvent(text).publish() } - is PolicyUpdateEvent.Log -> updatePolicy(it.item) { + is PolicyUpdateEvent.Log -> updatePolicy(it.item.item.copy(logging = it.shouldLog)) { val textId = if (it.notification) R.string.su_snack_log_on else R.string.su_snack_log_off val text = resources.getString(textId).format(it.appName) @@ -97,16 +98,16 @@ class SuperuserViewModel( } } - private fun updatePolicy(item: PolicyRvItem, onSuccess: (Policy) -> Unit) = - updatePolicy(item.item) + private fun updatePolicy(item: MagiskPolicy, onSuccess: (MagiskPolicy) -> Unit) = + updatePolicy(item) .subscribeK { onSuccess(it) } .add() private fun togglePolicy(item: PolicyRvItem, enable: Boolean) { fun updateState() { - item.item.policy = if (enable) Policy.ALLOW else Policy.DENY + val app = item.item.copy(policy = if (enable) MagiskPolicy.ALLOW else MagiskPolicy.DENY) - updatePolicy(item.item) + updatePolicy(app) .map { it.policy == Policy.ALLOW } .subscribeK { val textId = if (it) R.string.su_snack_grant else R.string.su_snack_deny @@ -128,12 +129,10 @@ class SuperuserViewModel( } } - private fun updatePolicy(policy: Policy) = - Single.fromCallable { database.updatePolicy(policy); policy } - .applySchedulers() + private fun updatePolicy(policy: MagiskPolicy) = + appRepo.update(policy).andThen(Single.just(policy)) - private fun deletePolicy(policy: Policy) = - Single.fromCallable { database.deletePolicy(policy); policy } - .applySchedulers() + private fun deletePolicy(policy: MagiskPolicy) = + appRepo.delete(policy.uid).andThen(Single.just(policy)) } \ No newline at end of file