mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-13 13:28:24 -08:00
Cleanup unused code
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
package com.topjohnwu.magisk.core.tasks
|
||||
|
||||
import com.topjohnwu.magisk.core.model.module.OnlineModule
|
||||
import com.topjohnwu.magisk.data.database.RepoDao
|
||||
import com.topjohnwu.magisk.data.repository.NetworkService
|
||||
import com.topjohnwu.magisk.ktx.synchronized
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import timber.log.Timber
|
||||
import java.util.*
|
||||
|
||||
class RepoUpdater(
|
||||
private val svc: NetworkService,
|
||||
private val repoDB: RepoDao
|
||||
) {
|
||||
|
||||
suspend fun run(forced: Boolean) = withContext(Dispatchers.IO) {
|
||||
val cachedMap = HashMap<String, Date>().also { map ->
|
||||
repoDB.getModuleStubs().forEach { map[it.id] = Date(it.last_update) }
|
||||
}.synchronized()
|
||||
svc.fetchRepoInfo()?.let { info ->
|
||||
coroutineScope {
|
||||
info.modules.forEach {
|
||||
launch {
|
||||
val lastUpdated = cachedMap.remove(it.id)
|
||||
if (forced || lastUpdated?.before(Date(it.last_update)) != false) {
|
||||
try {
|
||||
val repo = OnlineModule(it).apply { load() }
|
||||
repoDB.addModule(repo)
|
||||
} catch (e: OnlineModule.IllegalRepoException) {
|
||||
Timber.e(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
repoDB.removeModules(cachedMap.keys)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.topjohnwu.magisk.data.database
|
||||
|
||||
import androidx.room.*
|
||||
import com.topjohnwu.magisk.core.Config
|
||||
import com.topjohnwu.magisk.core.model.module.OnlineModule
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Database(version = 8, entities = [OnlineModule::class], exportSchema = false)
|
||||
abstract class RepoDatabase : RoomDatabase() {
|
||||
abstract fun repoDao() : RepoDao
|
||||
}
|
||||
|
||||
@Dao
|
||||
abstract class RepoDao(private val db: RepoDatabase) {
|
||||
|
||||
suspend fun clear() = withContext(Dispatchers.IO) { db.clearAllTables() }
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
abstract fun addModule(repo: OnlineModule)
|
||||
|
||||
@Delete
|
||||
abstract fun removeModule(repo: OnlineModule)
|
||||
|
||||
@Query("DELETE FROM modules WHERE id = :id")
|
||||
abstract fun removeModule(id: String)
|
||||
|
||||
@Query("DELETE FROM modules WHERE id IN (:idList)")
|
||||
abstract fun removeModules(idList: Collection<String>)
|
||||
|
||||
@Query("SELECT * FROM modules WHERE id = :id")
|
||||
abstract fun getModule(id: String): OnlineModule?
|
||||
|
||||
@Query("SELECT id, last_update FROM modules")
|
||||
abstract fun getModuleStubs(): List<ModuleStub>
|
||||
|
||||
fun getModules(offset: Int, limit: Int = LIMIT) = when (Config.repoOrder) {
|
||||
Config.Value.ORDER_NAME -> getNameOrder(offset, limit)
|
||||
else -> getDateOrder(offset, limit)
|
||||
}
|
||||
|
||||
fun searchModules(query: String, offset: Int, limit: Int = LIMIT) = when (Config.repoOrder) {
|
||||
Config.Value.ORDER_NAME -> searchNameOrder(query, offset, limit)
|
||||
else -> searchDateOrder(query, offset, limit)
|
||||
}
|
||||
|
||||
@Query("SELECT * FROM modules WHERE id = :id AND versionCode > :versionCode LIMIT 1")
|
||||
abstract fun getUpdatableModule(id: String, versionCode: Int): OnlineModule?
|
||||
|
||||
@Query("SELECT * FROM modules ORDER BY last_update DESC LIMIT :limit OFFSET :offset")
|
||||
protected abstract fun getDateOrder(offset: Int, limit: Int): List<OnlineModule>
|
||||
|
||||
@Query("SELECT * FROM modules ORDER BY name COLLATE NOCASE LIMIT :limit OFFSET :offset")
|
||||
protected abstract fun getNameOrder(offset: Int, limit: Int): List<OnlineModule>
|
||||
|
||||
@Query(
|
||||
"""SELECT *
|
||||
FROM modules
|
||||
WHERE
|
||||
(author LIKE '%' || :query || '%') ||
|
||||
(name LIKE '%' || :query || '%') ||
|
||||
(description LIKE '%' || :query || '%')
|
||||
ORDER BY last_update DESC
|
||||
LIMIT :limit
|
||||
OFFSET :offset"""
|
||||
)
|
||||
protected abstract fun searchDateOrder(query: String, offset: Int, limit: Int): List<OnlineModule>
|
||||
|
||||
@Query(
|
||||
"""SELECT *
|
||||
FROM modules
|
||||
WHERE
|
||||
(author LIKE '%' || :query || '%') ||
|
||||
(name LIKE '%' || :query || '%') ||
|
||||
(description LIKE '%' || :query || '%')
|
||||
ORDER BY name COLLATE NOCASE
|
||||
LIMIT :limit
|
||||
OFFSET :offset"""
|
||||
)
|
||||
protected abstract fun searchNameOrder(query: String, offset: Int, limit: Int): List<OnlineModule>
|
||||
|
||||
companion object {
|
||||
const val LIMIT = 10
|
||||
}
|
||||
}
|
||||
|
||||
data class ModuleStub(
|
||||
@PrimaryKey val id: String,
|
||||
val last_update: Long
|
||||
)
|
||||
@@ -10,8 +10,6 @@ import com.topjohnwu.magisk.core.Const
|
||||
import com.topjohnwu.magisk.core.magiskdb.PolicyDao
|
||||
import com.topjohnwu.magisk.core.magiskdb.SettingsDao
|
||||
import com.topjohnwu.magisk.core.magiskdb.StringDao
|
||||
import com.topjohnwu.magisk.core.tasks.RepoUpdater
|
||||
import com.topjohnwu.magisk.data.database.RepoDatabase
|
||||
import com.topjohnwu.magisk.data.database.SuLogDatabase
|
||||
import com.topjohnwu.magisk.data.repository.LogRepository
|
||||
import com.topjohnwu.magisk.data.repository.NetworkService
|
||||
@@ -19,7 +17,6 @@ import com.topjohnwu.magisk.ktx.deviceProtectedContext
|
||||
import com.topjohnwu.magisk.ui.home.HomeViewModel
|
||||
import com.topjohnwu.magisk.ui.install.InstallViewModel
|
||||
import com.topjohnwu.magisk.ui.log.LogViewModel
|
||||
import com.topjohnwu.magisk.ui.settings.SettingsViewModel
|
||||
import com.topjohnwu.magisk.ui.superuser.SuperuserViewModel
|
||||
import com.topjohnwu.magisk.ui.surequest.SuRequestViewModel
|
||||
|
||||
@@ -36,9 +33,7 @@ object ServiceLocator {
|
||||
val policyDB = PolicyDao()
|
||||
val settingsDB = SettingsDao()
|
||||
val stringDB = StringDao()
|
||||
val repoDB by lazy { createRepoDatabase(context).repoDao() }
|
||||
val sulogDB by lazy { createSuLogDatabase(deContext).suLogDao() }
|
||||
val repoUpdater by lazy { RepoUpdater(networkService, repoDB) }
|
||||
val logRepo by lazy { LogRepository(sulogDB) }
|
||||
|
||||
// Networking
|
||||
@@ -60,7 +55,6 @@ object ServiceLocator {
|
||||
return when (clz) {
|
||||
HomeViewModel::class.java -> HomeViewModel(networkService)
|
||||
LogViewModel::class.java -> LogViewModel(logRepo)
|
||||
SettingsViewModel::class.java -> SettingsViewModel(repoDB)
|
||||
SuperuserViewModel::class.java -> SuperuserViewModel(policyDB)
|
||||
InstallViewModel::class.java -> InstallViewModel(networkService)
|
||||
SuRequestViewModel::class.java -> SuRequestViewModel(policyDB, timeoutPrefs)
|
||||
@@ -75,11 +69,6 @@ inline fun <reified VM : ViewModel> ViewModelStoreOwner.viewModel() =
|
||||
ViewModelProvider(this, ServiceLocator.VMFactory).get(VM::class.java)
|
||||
}
|
||||
|
||||
private fun createRepoDatabase(context: Context) =
|
||||
Room.databaseBuilder(context, RepoDatabase::class.java, "repo.db")
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
|
||||
private fun createSuLogDatabase(context: Context) =
|
||||
Room.databaseBuilder(context, SuLogDatabase::class.java, "sulogs.db")
|
||||
.fallbackToDestructiveMigration()
|
||||
|
||||
@@ -77,15 +77,6 @@ object AppSettings : BaseSettingsItem.Section() {
|
||||
override val title = R.string.home_app_title.asText()
|
||||
}
|
||||
|
||||
object ClearRepoCache : BaseSettingsItem.Blank() {
|
||||
override val title = R.string.settings_clear_cache_title.asText()
|
||||
override val description = R.string.settings_clear_cache_summary.asText()
|
||||
|
||||
override fun refresh() {
|
||||
isEnabled = Info.env.isActive
|
||||
}
|
||||
}
|
||||
|
||||
object Hide : BaseSettingsItem.Input() {
|
||||
override val title = R.string.settings_hide_app_title.asText()
|
||||
override val description = R.string.settings_hide_app_summary.asText()
|
||||
|
||||
@@ -13,7 +13,6 @@ import com.topjohnwu.magisk.core.Const
|
||||
import com.topjohnwu.magisk.core.Info
|
||||
import com.topjohnwu.magisk.core.isRunningAsStub
|
||||
import com.topjohnwu.magisk.core.tasks.HideAPK
|
||||
import com.topjohnwu.magisk.data.database.RepoDao
|
||||
import com.topjohnwu.magisk.databinding.adapterOf
|
||||
import com.topjohnwu.magisk.databinding.itemBindingOf
|
||||
import com.topjohnwu.magisk.di.AppContext
|
||||
@@ -25,9 +24,7 @@ import com.topjohnwu.magisk.utils.Utils
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class SettingsViewModel(
|
||||
private val repositoryDao: RepoDao
|
||||
) : BaseViewModel(), BaseSettingsItem.Callback {
|
||||
class SettingsViewModel : BaseViewModel(), BaseSettingsItem.Callback {
|
||||
|
||||
val adapter = adapterOf<BaseSettingsItem>()
|
||||
val itemBinding = itemBindingOf<BaseSettingsItem> { it.bindExtra(BR.callback, this) }
|
||||
@@ -57,7 +54,6 @@ class SettingsViewModel(
|
||||
UpdateChannel, UpdateChannelUrl, UpdateChecker, DownloadPath
|
||||
))
|
||||
if (Info.env.isActive) {
|
||||
list.add(ClearRepoCache)
|
||||
if (Const.USER_ID == 0) {
|
||||
if (hidden)
|
||||
list.add(Restore)
|
||||
@@ -106,7 +102,6 @@ class SettingsViewModel(
|
||||
is Biometrics -> authenticate(callback)
|
||||
is Theme -> SettingsFragmentDirections.actionSettingsFragmentToThemeFragment().navigate()
|
||||
is DenyListConfig -> SettingsFragmentDirections.actionSettingsFragmentToDenyFragment().navigate()
|
||||
is ClearRepoCache -> clearRepoCache()
|
||||
is SystemlessHosts -> createHosts()
|
||||
is Restore -> HideAPK.restore(view.activity)
|
||||
is AddShortcut -> AddHomeIconEvent().publish()
|
||||
@@ -136,13 +131,6 @@ class SettingsViewModel(
|
||||
}.publish()
|
||||
}
|
||||
|
||||
private fun clearRepoCache() {
|
||||
viewModelScope.launch {
|
||||
repositoryDao.clear()
|
||||
Utils.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createHosts() {
|
||||
Shell.su("add_hosts_module").submit {
|
||||
Utils.toast(R.string.settings_hosts_toast, Toast.LENGTH_SHORT)
|
||||
|
||||
Reference in New Issue
Block a user