Removed direct static usages of database from app

This commit is contained in:
Viktor De Pasquale 2019-05-12 17:25:26 +02:00
parent c275326d59
commit d546733287
7 changed files with 27 additions and 33 deletions

View File

@ -11,14 +11,12 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.multidex.MultiDex import androidx.multidex.MultiDex
import com.chibatching.kotpref.Kotpref import com.chibatching.kotpref.Kotpref
import com.topjohnwu.magisk.data.database.MagiskDB
import com.topjohnwu.magisk.di.koinModules import com.topjohnwu.magisk.di.koinModules
import com.topjohnwu.magisk.utils.LocaleManager import com.topjohnwu.magisk.utils.LocaleManager
import com.topjohnwu.magisk.utils.RootUtils import com.topjohnwu.magisk.utils.RootUtils
import com.topjohnwu.magisk.utils.inject import com.topjohnwu.magisk.utils.inject
import com.topjohnwu.net.Networking import com.topjohnwu.net.Networking
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin import org.koin.core.context.startKoin
import timber.log.Timber import timber.log.Timber
@ -28,9 +26,6 @@ open class App : Application(), Application.ActivityLifecycleCallbacks {
lateinit var protectedContext: Context lateinit var protectedContext: Context
@Deprecated("Use dependency injection", level = DeprecationLevel.ERROR)
val DB: MagiskDB by inject()
@Volatile @Volatile
private var foreground: Activity? = null private var foreground: Activity? = null

View File

@ -2,13 +2,12 @@ package com.topjohnwu.magisk.di
import android.content.Context import android.content.Context
import androidx.room.Room import androidx.room.Room
import com.topjohnwu.magisk.App
import com.topjohnwu.magisk.data.database.* import com.topjohnwu.magisk.data.database.*
import org.koin.dsl.module import org.koin.dsl.module
val databaseModule = module { val databaseModule = module {
single { MagiskDB(get<App>().protectedContext) } //single { MagiskDB(get<App>().protectedContext) }
single { createDatabase(get()) } single { createDatabase(get()) }
single { LogDao() } single { LogDao() }
single { PolicyDao(get()) } single { PolicyDao(get()) }

View File

@ -6,13 +6,14 @@ import com.skoumal.teanity.extensions.addOnPropertyChangedCallback
import com.skoumal.teanity.rxbus.RxBus import com.skoumal.teanity.rxbus.RxBus
import com.skoumal.teanity.util.KObservableField import com.skoumal.teanity.util.KObservableField
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.model.entity.MagiskPolicy
import com.topjohnwu.magisk.model.entity.Policy import com.topjohnwu.magisk.model.entity.Policy
import com.topjohnwu.magisk.model.events.PolicyEnableEvent import com.topjohnwu.magisk.model.events.PolicyEnableEvent
import com.topjohnwu.magisk.model.events.PolicyUpdateEvent import com.topjohnwu.magisk.model.events.PolicyUpdateEvent
import com.topjohnwu.magisk.utils.inject import com.topjohnwu.magisk.utils.inject
import com.topjohnwu.magisk.utils.toggle import com.topjohnwu.magisk.utils.toggle
class PolicyRvItem(val item: Policy, val icon: Drawable) : ComparableRvItem<PolicyRvItem>() { class PolicyRvItem(val item: MagiskPolicy, val icon: Drawable) : ComparableRvItem<PolicyRvItem>() {
override val layoutRes: Int = R.layout.item_policy override val layoutRes: Int = R.layout.item_policy
@ -32,13 +33,11 @@ class PolicyRvItem(val item: Policy, val icon: Drawable) : ComparableRvItem<Poli
} }
shouldNotify.addOnPropertyChangedCallback { shouldNotify.addOnPropertyChangedCallback {
it ?: return@addOnPropertyChangedCallback it ?: return@addOnPropertyChangedCallback
item.notification = it rxBus.post(PolicyUpdateEvent.Notification(this@PolicyRvItem, shouldNotify.value))
rxBus.post(PolicyUpdateEvent.Notification(this@PolicyRvItem))
} }
shouldLog.addOnPropertyChangedCallback { shouldLog.addOnPropertyChangedCallback {
it ?: return@addOnPropertyChangedCallback it ?: return@addOnPropertyChangedCallback
item.logging = it rxBus.post(PolicyUpdateEvent.Log(this@PolicyRvItem, shouldLog.value))
rxBus.post(PolicyUpdateEvent.Log(this@PolicyRvItem))
} }
} }

View File

@ -9,8 +9,8 @@ class HideProcessEvent(val item: HideProcessRvItem) : RxBus.Event
class PolicyEnableEvent(val item: PolicyRvItem, val enable: Boolean) : RxBus.Event class PolicyEnableEvent(val item: PolicyRvItem, val enable: Boolean) : RxBus.Event
sealed class PolicyUpdateEvent(val item: PolicyRvItem) : RxBus.Event { sealed class PolicyUpdateEvent(val item: PolicyRvItem) : RxBus.Event {
class Notification(item: PolicyRvItem) : PolicyUpdateEvent(item) class Notification(item: PolicyRvItem, val shouldNotify: Boolean) : PolicyUpdateEvent(item)
class Log(item: PolicyRvItem) : PolicyUpdateEvent(item) class Log(item: PolicyRvItem, val shouldLog: Boolean) : PolicyUpdateEvent(item)
} }
class ModuleUpdatedEvent(val item: ModuleRvItem) : RxBus.Event class ModuleUpdatedEvent(val item: ModuleRvItem) : RxBus.Event

View File

@ -13,7 +13,7 @@ import androidx.preference.*
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.topjohnwu.magisk.App import com.topjohnwu.magisk.App
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.database.MagiskDB import com.topjohnwu.magisk.data.repository.SettingRepository
import org.koin.android.ext.android.inject import org.koin.android.ext.android.inject
abstract class BasePreferenceFragment : PreferenceFragmentCompat(), abstract class BasePreferenceFragment : PreferenceFragmentCompat(),
@ -21,7 +21,7 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat(),
protected val prefs: SharedPreferences by inject() protected val prefs: SharedPreferences by inject()
protected val app: App by inject() protected val app: App by inject()
protected val database: MagiskDB by inject() protected val settingRepo: SettingRepository by inject()
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,

View File

@ -212,7 +212,9 @@ public final class SettingsFragment extends BasePreferenceFragment {
case Config.Key.ROOT_ACCESS: case Config.Key.ROOT_ACCESS:
case Config.Key.SU_MULTIUSER_MODE: case Config.Key.SU_MULTIUSER_MODE:
case Config.Key.SU_MNT_NS: case Config.Key.SU_MNT_NS:
getDatabase().setSettings(key, Utils.getPrefsInt(prefs, key)); getSettingRepo().put(key, Utils.getPrefsInt(prefs, key))
.subscribe(() -> {
}, Throwable::printStackTrace);
break; break;
case Config.Key.DARK_THEME: case Config.Key.DARK_THEME:
requireActivity().recreate(); requireActivity().recreate();

View File

@ -10,7 +10,8 @@ import com.skoumal.teanity.util.DiffObservableList
import com.skoumal.teanity.viewevents.SnackbarEvent import com.skoumal.teanity.viewevents.SnackbarEvent
import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.R 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.Policy
import com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem import com.topjohnwu.magisk.model.entity.recycler.PolicyRvItem
import com.topjohnwu.magisk.model.events.PolicyEnableEvent import com.topjohnwu.magisk.model.events.PolicyEnableEvent
@ -24,7 +25,7 @@ import io.reactivex.Single
import me.tatarka.bindingcollectionadapter2.ItemBinding import me.tatarka.bindingcollectionadapter2.ItemBinding
class SuperuserViewModel( class SuperuserViewModel(
private val database: MagiskDB, private val appRepo: AppRepository,
private val packageManager: PackageManager, private val packageManager: PackageManager,
private val resources: Resources, private val resources: Resources,
rxBus: RxBus rxBus: RxBus
@ -50,9 +51,9 @@ class SuperuserViewModel(
} }
fun updatePolicies() { fun updatePolicies() {
Single.fromCallable { database.policyList } appRepo.fetchAll()
.flattenAsFlowable { it } .flattenAsFlowable { it }
.map { PolicyRvItem(it, it.info.loadIcon(packageManager)) } .map { PolicyRvItem(it, it.applicationInfo.loadIcon(packageManager)) }
.toList() .toList()
.applySchedulers() .applySchedulers()
.applyViewModel(this) .applyViewModel(this)
@ -84,12 +85,12 @@ class SuperuserViewModel(
} }
private fun updatePolicy(it: PolicyUpdateEvent) = when (it) { 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 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) val text = resources.getString(textId).format(it.appName)
SnackbarEvent(text).publish() SnackbarEvent(text).publish()
} }
is PolicyUpdateEvent.Log -> updatePolicy(it.item) { is PolicyUpdateEvent.Log -> updatePolicy(it.item.item.copy(logging = it.shouldLog)) {
val textId = val textId =
if (it.notification) R.string.su_snack_log_on else R.string.su_snack_log_off if (it.notification) R.string.su_snack_log_on else R.string.su_snack_log_off
val text = resources.getString(textId).format(it.appName) val text = resources.getString(textId).format(it.appName)
@ -97,16 +98,16 @@ class SuperuserViewModel(
} }
} }
private fun updatePolicy(item: PolicyRvItem, onSuccess: (Policy) -> Unit) = private fun updatePolicy(item: MagiskPolicy, onSuccess: (MagiskPolicy) -> Unit) =
updatePolicy(item.item) updatePolicy(item)
.subscribeK { onSuccess(it) } .subscribeK { onSuccess(it) }
.add() .add()
private fun togglePolicy(item: PolicyRvItem, enable: Boolean) { private fun togglePolicy(item: PolicyRvItem, enable: Boolean) {
fun updateState() { 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 } .map { it.policy == Policy.ALLOW }
.subscribeK { .subscribeK {
val textId = if (it) R.string.su_snack_grant else R.string.su_snack_deny 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) = private fun updatePolicy(policy: MagiskPolicy) =
Single.fromCallable { database.updatePolicy(policy); policy } appRepo.update(policy).andThen(Single.just(policy))
.applySchedulers()
private fun deletePolicy(policy: Policy) = private fun deletePolicy(policy: MagiskPolicy) =
Single.fromCallable { database.deletePolicy(policy); policy } appRepo.delete(policy.uid).andThen(Single.just(policy))
.applySchedulers()
} }