BlacklistProvider: Migrate to hilt
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -23,81 +23,69 @@ import android.content.Context
|
|||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import com.aurora.extensions.isNAndAbove
|
import com.aurora.extensions.isNAndAbove
|
||||||
import com.aurora.store.data.SingletonHolder
|
|
||||||
import com.aurora.store.util.Preferences
|
import com.aurora.store.util.Preferences
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.GsonBuilder
|
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.lang.reflect.Modifier
|
import javax.inject.Inject
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
class BlacklistProvider private constructor(var context: Context) {
|
@Singleton
|
||||||
|
class BlacklistProvider @Inject constructor(
|
||||||
|
private val gson: Gson,
|
||||||
|
@ApplicationContext val context: Context,
|
||||||
|
) {
|
||||||
|
|
||||||
companion object : SingletonHolder<BlacklistProvider, Context>(::BlacklistProvider) {
|
private val PREFERENCE_BLACKLIST = "PREFERENCE_BLACKLIST"
|
||||||
const val PREFERENCE_BLACKLIST = "PREFERENCE_BLACKLIST"
|
|
||||||
|
var blacklist: MutableSet<String>
|
||||||
|
set(value) = Preferences.putString(context, PREFERENCE_BLACKLIST, gson.toJson(value))
|
||||||
|
get() {
|
||||||
|
return try {
|
||||||
|
val rawBlacklist = if (isNAndAbove()) {
|
||||||
|
val refMethod = Context::class.java.getDeclaredMethod(
|
||||||
|
"getSharedPreferences",
|
||||||
|
File::class.java,
|
||||||
|
Int::class.java
|
||||||
|
)
|
||||||
|
val refSharedPreferences = refMethod.invoke(
|
||||||
|
context,
|
||||||
|
File("/product/etc/com.aurora.store/blacklist.xml"),
|
||||||
|
Context.MODE_PRIVATE
|
||||||
|
) as SharedPreferences
|
||||||
|
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
|
.getString(
|
||||||
|
PREFERENCE_BLACKLIST,
|
||||||
|
refSharedPreferences.getString(PREFERENCE_BLACKLIST, "")
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
Preferences.getString(context, PREFERENCE_BLACKLIST)
|
||||||
|
}
|
||||||
|
if (rawBlacklist!!.isEmpty())
|
||||||
|
mutableSetOf()
|
||||||
|
else
|
||||||
|
gson.fromJson(rawBlacklist, object : TypeToken<Set<String?>?>() {}.type)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
mutableSetOf()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isBlacklisted(packageName: String): Boolean {
|
||||||
|
return blacklist.contains(packageName)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var gson: Gson = GsonBuilder()
|
|
||||||
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
|
|
||||||
.create()
|
|
||||||
|
|
||||||
fun getBlackList(): MutableSet<String> {
|
fun blacklist(packageName: String) {
|
||||||
return try {
|
blacklist = blacklist.apply {
|
||||||
val rawBlacklist = if (isNAndAbove()) {
|
add(packageName)
|
||||||
val refMethod = Context::class.java.getDeclaredMethod(
|
|
||||||
"getSharedPreferences",
|
|
||||||
File::class.java,
|
|
||||||
Int::class.java
|
|
||||||
)
|
|
||||||
val refSharedPreferences = refMethod.invoke(
|
|
||||||
context,
|
|
||||||
File("/product/etc/com.aurora.store/blacklist.xml"),
|
|
||||||
Context.MODE_PRIVATE
|
|
||||||
) as SharedPreferences
|
|
||||||
|
|
||||||
PreferenceManager.getDefaultSharedPreferences(context)
|
|
||||||
.getString(
|
|
||||||
PREFERENCE_BLACKLIST,
|
|
||||||
refSharedPreferences.getString(PREFERENCE_BLACKLIST, "")
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
Preferences.getString(context, PREFERENCE_BLACKLIST)
|
|
||||||
}
|
|
||||||
if (rawBlacklist!!.isEmpty())
|
|
||||||
mutableSetOf()
|
|
||||||
else
|
|
||||||
gson.fromJson(rawBlacklist, object : TypeToken<Set<String?>?>() {}.type)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
mutableSetOf()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isBlacklisted(packageName: String): Boolean {
|
|
||||||
return getBlackList().contains(packageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun blacklist(packageName: String) {
|
|
||||||
blacklist(setOf(packageName))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun whitelist(packageName: String) {
|
fun whitelist(packageName: String) {
|
||||||
whitelist(setOf(packageName))
|
blacklist = blacklist.apply {
|
||||||
}
|
remove(packageName)
|
||||||
|
}
|
||||||
fun blacklist(packageNames: Set<String>) {
|
|
||||||
val oldBlackList: MutableSet<String> = getBlackList()
|
|
||||||
oldBlackList.addAll(packageNames)
|
|
||||||
save(oldBlackList)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun whitelist(packageNames: Set<String>) {
|
|
||||||
val oldBlackList: MutableSet<String> = getBlackList()
|
|
||||||
oldBlackList.removeAll(packageNames)
|
|
||||||
save(oldBlackList)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Synchronized
|
|
||||||
fun save(blacklist: Set<String>) {
|
|
||||||
Preferences.putString(context, PREFERENCE_BLACKLIST, gson.toJson(blacklist))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import dagger.hilt.android.qualifiers.ApplicationContext
|
|||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.SharingStarted
|
import kotlinx.coroutines.flow.SharingStarted
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.flow.shareIn
|
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -29,6 +28,7 @@ class AppUtil @Inject constructor(
|
|||||||
private val gson: Gson,
|
private val gson: Gson,
|
||||||
private val authProvider: AuthProvider,
|
private val authProvider: AuthProvider,
|
||||||
private val updateDao: UpdateDao,
|
private val updateDao: UpdateDao,
|
||||||
|
private val blacklistProvider: BlacklistProvider,
|
||||||
@ApplicationContext private val context: Context
|
@ApplicationContext private val context: Context
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@@ -73,12 +73,11 @@ class AppUtil @Inject constructor(
|
|||||||
packageInfoMap: MutableMap<String, PackageInfo>? = null
|
packageInfoMap: MutableMap<String, PackageInfo>? = null
|
||||||
): List<App> {
|
): List<App> {
|
||||||
return withContext(Dispatchers.IO) {
|
return withContext(Dispatchers.IO) {
|
||||||
val blackList = BlacklistProvider.with(context).getBlackList()
|
|
||||||
val appDetailsHelper = AppDetailsHelper(authProvider.authData!!)
|
val appDetailsHelper = AppDetailsHelper(authProvider.authData!!)
|
||||||
.using(HttpClient.getPreferredClient(context))
|
.using(HttpClient.getPreferredClient(context))
|
||||||
|
|
||||||
(packageInfoMap ?: PackageUtil.getPackageInfoMap(context)).keys.let { packages ->
|
(packageInfoMap ?: PackageUtil.getPackageInfoMap(context)).keys.let { packages ->
|
||||||
val filtersPackages = packages.filter { !blackList.contains(it) }
|
val filtersPackages = packages.filter { !blacklistProvider.isBlacklisted(it) }
|
||||||
|
|
||||||
appDetailsHelper.getAppByPackageName(filtersPackages)
|
appDetailsHelper.getAppByPackageName(filtersPackages)
|
||||||
.filter { it.displayName.isNotEmpty() }
|
.filter { it.displayName.isNotEmpty() }
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ import com.aurora.gplayapi.data.models.App
|
|||||||
import com.aurora.store.AuroraApp
|
import com.aurora.store.AuroraApp
|
||||||
import com.aurora.store.R
|
import com.aurora.store.R
|
||||||
import com.aurora.store.data.event.BusEvent
|
import com.aurora.store.data.event.BusEvent
|
||||||
import com.aurora.store.data.providers.BlacklistProvider
|
|
||||||
import com.aurora.store.databinding.FragmentGenericWithToolbarBinding
|
import com.aurora.store.databinding.FragmentGenericWithToolbarBinding
|
||||||
import com.aurora.store.view.epoxy.views.BlackListViewModel_
|
import com.aurora.store.view.epoxy.views.BlackListViewModel_
|
||||||
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
|
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
|
||||||
@@ -41,13 +40,9 @@ import kotlinx.coroutines.launch
|
|||||||
class BlacklistFragment : BaseFragment<FragmentGenericWithToolbarBinding>() {
|
class BlacklistFragment : BaseFragment<FragmentGenericWithToolbarBinding>() {
|
||||||
private val viewModel: BlacklistViewModel by viewModels()
|
private val viewModel: BlacklistViewModel by viewModels()
|
||||||
|
|
||||||
private lateinit var blacklistProvider: BlacklistProvider
|
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
blacklistProvider = BlacklistProvider.with(requireContext())
|
|
||||||
|
|
||||||
viewLifecycleOwner.lifecycleScope.launch {
|
viewLifecycleOwner.lifecycleScope.launch {
|
||||||
viewModel.blackListedApps.collect {
|
viewModel.blackListedApps.collect {
|
||||||
updateController(it)
|
updateController(it)
|
||||||
@@ -64,7 +59,7 @@ class BlacklistFragment : BaseFragment<FragmentGenericWithToolbarBinding>() {
|
|||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
blacklistProvider.save(viewModel.selected)
|
viewModel.blacklistProvider.blacklist = viewModel.selected
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateController(blackList: List<App>?) {
|
private fun updateController(blackList: List<App>?) {
|
||||||
@@ -79,7 +74,9 @@ class BlacklistFragment : BaseFragment<FragmentGenericWithToolbarBinding>() {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
blackList
|
blackList
|
||||||
.sortedByDescending { app -> blacklistProvider.isBlacklisted(app.packageName) }
|
.sortedByDescending { app ->
|
||||||
|
viewModel.blacklistProvider.isBlacklisted(app.packageName)
|
||||||
|
}
|
||||||
.forEach {
|
.forEach {
|
||||||
add(
|
add(
|
||||||
BlackListViewModel_()
|
BlackListViewModel_()
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import com.aurora.store.databinding.SheetAppMenuBinding
|
|||||||
import com.aurora.store.util.PackageUtil
|
import com.aurora.store.util.PackageUtil
|
||||||
import com.aurora.store.viewmodel.sheets.SheetsViewModel
|
import com.aurora.store.viewmodel.sheets.SheetsViewModel
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
||||||
@@ -45,6 +46,9 @@ class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
|||||||
const val TAG = "APP_MENU_SHEET"
|
const val TAG = "APP_MENU_SHEET"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
lateinit var blacklistProvider: BlacklistProvider
|
||||||
|
|
||||||
private val viewModel: SheetsViewModel by viewModels()
|
private val viewModel: SheetsViewModel by viewModels()
|
||||||
private val args: AppMenuSheetArgs by navArgs()
|
private val args: AppMenuSheetArgs by navArgs()
|
||||||
|
|
||||||
@@ -63,7 +67,6 @@ class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
|
||||||
val blacklistProvider = BlacklistProvider.with(requireContext())
|
|
||||||
val isBlacklisted: Boolean = blacklistProvider.isBlacklisted(args.app.packageName)
|
val isBlacklisted: Boolean = blacklistProvider.isBlacklisted(args.app.packageName)
|
||||||
|
|
||||||
with(binding.navigationView) {
|
with(binding.navigationView) {
|
||||||
|
|||||||
@@ -42,10 +42,10 @@ import javax.inject.Inject
|
|||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
@SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253
|
@SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253
|
||||||
class BlacklistViewModel @Inject constructor(
|
class BlacklistViewModel @Inject constructor(
|
||||||
|
val blacklistProvider: BlacklistProvider,
|
||||||
@ApplicationContext private val context: Context,
|
@ApplicationContext private val context: Context,
|
||||||
authProvider: AuthProvider
|
authProvider: AuthProvider
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
private val blacklistProvider: BlacklistProvider = BlacklistProvider.with(context)
|
|
||||||
private val appDetailsHelper = AppDetailsHelper(authProvider.authData!!)
|
private val appDetailsHelper = AppDetailsHelper(authProvider.authData!!)
|
||||||
.using(HttpClient.getPreferredClient(context))
|
.using(HttpClient.getPreferredClient(context))
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ class BlacklistViewModel @Inject constructor(
|
|||||||
var selected: MutableSet<String> = mutableSetOf()
|
var selected: MutableSet<String> = mutableSetOf()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
selected = blacklistProvider.getBlackList()
|
selected = blacklistProvider.blacklist
|
||||||
fetchApps()
|
fetchApps()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user