2019-10-18 17:04:41 +02:00
|
|
|
package com.topjohnwu.magisk.redesign.hide
|
|
|
|
|
2019-10-30 21:58:42 +01:00
|
|
|
import android.content.pm.ApplicationInfo
|
2019-10-31 20:34:07 +01:00
|
|
|
import android.view.MenuItem
|
2019-10-30 21:58:42 +01:00
|
|
|
import com.topjohnwu.magisk.BR
|
2019-10-31 20:34:07 +01:00
|
|
|
import com.topjohnwu.magisk.R
|
2019-10-30 21:58:42 +01:00
|
|
|
import com.topjohnwu.magisk.data.repository.MagiskRepository
|
2019-10-31 20:34:07 +01:00
|
|
|
import com.topjohnwu.magisk.databinding.ComparableRvItem
|
2019-10-30 21:58:42 +01:00
|
|
|
import com.topjohnwu.magisk.extensions.subscribeK
|
|
|
|
import com.topjohnwu.magisk.model.entity.HideAppInfo
|
|
|
|
import com.topjohnwu.magisk.model.entity.HideTarget
|
|
|
|
import com.topjohnwu.magisk.model.entity.ProcessHideApp
|
|
|
|
import com.topjohnwu.magisk.model.entity.StatefulProcess
|
|
|
|
import com.topjohnwu.magisk.model.entity.recycler.HideItem
|
|
|
|
import com.topjohnwu.magisk.model.entity.recycler.HideProcessItem
|
2019-10-18 17:04:41 +02:00
|
|
|
import com.topjohnwu.magisk.redesign.compat.CompatViewModel
|
2019-10-30 21:58:42 +01:00
|
|
|
import com.topjohnwu.magisk.redesign.home.itemBindingOf
|
2019-10-31 20:34:07 +01:00
|
|
|
import com.topjohnwu.magisk.utils.DiffObservableList
|
|
|
|
import com.topjohnwu.magisk.utils.FilterableDiffObservableList
|
2019-10-30 21:58:42 +01:00
|
|
|
import com.topjohnwu.magisk.utils.KObservableField
|
|
|
|
import com.topjohnwu.magisk.utils.currentLocale
|
2019-10-18 17:04:41 +02:00
|
|
|
|
2019-10-30 21:58:42 +01:00
|
|
|
class HideViewModel(
|
2019-10-31 20:34:07 +01:00
|
|
|
private val magiskRepo: MagiskRepository
|
2019-10-30 21:58:42 +01:00
|
|
|
) : CompatViewModel() {
|
|
|
|
|
2019-10-31 20:34:07 +01:00
|
|
|
var isShowSystem = false
|
2019-10-30 21:58:42 +01:00
|
|
|
set(value) {
|
|
|
|
field = value
|
2019-10-31 20:34:07 +01:00
|
|
|
query()
|
2019-10-30 21:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
val query = KObservableField("")
|
2019-10-31 20:34:07 +01:00
|
|
|
val items = filterableListOf<HideItem>()
|
2019-10-30 21:58:42 +01:00
|
|
|
val itemBinding = itemBindingOf<HideItem> {
|
|
|
|
it.bindExtra(BR.viewModel, this)
|
|
|
|
}
|
|
|
|
val itemInternalBinding = itemBindingOf<HideProcessItem> {
|
|
|
|
it.bindExtra(BR.viewModel, this)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun refresh() = magiskRepo.fetchApps()
|
|
|
|
.map { it to magiskRepo.fetchHideTargets().blockingGet() }
|
|
|
|
.map { pair -> pair.first.map { mergeAppTargets(it, pair.second) } }
|
|
|
|
.flattenAsFlowable { it }
|
|
|
|
.map { HideItem(it) }
|
|
|
|
.toList()
|
|
|
|
.map { it.sort() }
|
2019-10-31 20:34:07 +01:00
|
|
|
.map { it to items.calculateDiff(it) }
|
2019-10-31 20:53:37 +01:00
|
|
|
.applyViewModel(this)
|
2019-10-30 21:58:42 +01:00
|
|
|
.subscribeK {
|
2019-10-31 20:34:07 +01:00
|
|
|
items.update(it.first, it.second)
|
|
|
|
query()
|
2019-10-30 21:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
private fun mergeAppTargets(a: HideAppInfo, ts: List<HideTarget>): ProcessHideApp {
|
|
|
|
val relevantTargets = ts.filter { it.packageName == a.info.packageName }
|
2019-10-31 20:34:07 +01:00
|
|
|
val packageName = a.info.packageName
|
2019-10-30 21:58:42 +01:00
|
|
|
val processes = a.processes
|
2019-10-31 20:34:07 +01:00
|
|
|
.map { StatefulProcess(it, packageName, relevantTargets.any { i -> it == i.process }) }
|
2019-10-30 21:58:42 +01:00
|
|
|
return ProcessHideApp(a, processes)
|
|
|
|
}
|
|
|
|
|
2019-10-31 20:34:07 +01:00
|
|
|
private fun List<HideItem>.sort() = compareByDescending<HideItem> { it.itemsChecked.value }
|
|
|
|
.thenBy { it.item.info.name.toLowerCase(currentLocale) }
|
|
|
|
.thenBy { it.item.info.info.packageName }
|
|
|
|
.let { sortedWith(it) }
|
2019-10-30 21:58:42 +01:00
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
private fun query(
|
|
|
|
query: String = this.query.value,
|
2019-10-31 20:34:07 +01:00
|
|
|
showSystem: Boolean = isShowSystem
|
|
|
|
) = items.filter {
|
|
|
|
fun filterSystem(): Boolean {
|
|
|
|
return showSystem || it.item.info.info.flags and ApplicationInfo.FLAG_SYSTEM == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fun filterQuery(): Boolean {
|
2019-10-30 21:58:42 +01:00
|
|
|
val inName = it.item.info.name.contains(query, true)
|
|
|
|
val inPackage = it.item.info.info.packageName.contains(query, true)
|
|
|
|
val inProcesses = it.item.processes.any { it.name.contains(query, true) }
|
2019-10-31 20:34:07 +01:00
|
|
|
return inName || inPackage || inProcesses
|
2019-10-30 21:58:42 +01:00
|
|
|
}
|
2019-10-31 20:34:07 +01:00
|
|
|
|
|
|
|
filterSystem() && filterQuery()
|
|
|
|
}
|
2019-10-30 21:58:42 +01:00
|
|
|
|
|
|
|
// ---
|
|
|
|
|
2019-10-31 20:34:07 +01:00
|
|
|
fun menuItemPressed(menuItem: MenuItem) = when (menuItem.itemId) {
|
|
|
|
R.id.action_show_system -> isShowSystem = (!menuItem.isChecked)
|
|
|
|
.also { menuItem.isChecked = it }
|
|
|
|
else -> null
|
|
|
|
}?.let { true } ?: false
|
|
|
|
|
|
|
|
fun toggleItem(item: HideProcessItem) = magiskRepo
|
|
|
|
.toggleHide(item.isHidden.value, item.item.packageName, item.item.name)
|
|
|
|
// might wanna reorder the list to display the item at the top
|
2019-10-30 21:58:42 +01:00
|
|
|
.subscribeK()
|
|
|
|
.add()
|
|
|
|
|
2019-10-31 20:34:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline fun <T : ComparableRvItem<T>> filterableListOf(
|
|
|
|
vararg newItems: T
|
|
|
|
) = FilterableDiffObservableList(object : DiffObservableList.Callback<T> {
|
|
|
|
override fun areItemsTheSame(oldItem: T, newItem: T) = oldItem.genericItemSameAs(newItem)
|
|
|
|
override fun areContentsTheSame(oldItem: T, newItem: T) = oldItem.genericContentSameAs(newItem)
|
|
|
|
}).also { it.update(newItems.toList()) }
|