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 androidx.preference.PreferenceManager
|
||||
import com.aurora.extensions.isNAndAbove
|
||||
import com.aurora.store.data.SingletonHolder
|
||||
import com.aurora.store.util.Preferences
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
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) {
|
||||
const val PREFERENCE_BLACKLIST = "PREFERENCE_BLACKLIST"
|
||||
private 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> {
|
||||
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 blacklist(packageName: String) {
|
||||
blacklist = blacklist.apply {
|
||||
add(packageName)
|
||||
}
|
||||
}
|
||||
|
||||
fun isBlacklisted(packageName: String): Boolean {
|
||||
return getBlackList().contains(packageName)
|
||||
}
|
||||
|
||||
fun blacklist(packageName: String) {
|
||||
blacklist(setOf(packageName))
|
||||
}
|
||||
|
||||
fun whitelist(packageName: String) {
|
||||
whitelist(setOf(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))
|
||||
blacklist = blacklist.apply {
|
||||
remove(packageName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.shareIn
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.withContext
|
||||
import javax.inject.Inject
|
||||
@@ -29,6 +28,7 @@ class AppUtil @Inject constructor(
|
||||
private val gson: Gson,
|
||||
private val authProvider: AuthProvider,
|
||||
private val updateDao: UpdateDao,
|
||||
private val blacklistProvider: BlacklistProvider,
|
||||
@ApplicationContext private val context: Context
|
||||
) {
|
||||
|
||||
@@ -73,12 +73,11 @@ class AppUtil @Inject constructor(
|
||||
packageInfoMap: MutableMap<String, PackageInfo>? = null
|
||||
): List<App> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val blackList = BlacklistProvider.with(context).getBlackList()
|
||||
val appDetailsHelper = AppDetailsHelper(authProvider.authData!!)
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
(packageInfoMap ?: PackageUtil.getPackageInfoMap(context)).keys.let { packages ->
|
||||
val filtersPackages = packages.filter { !blackList.contains(it) }
|
||||
val filtersPackages = packages.filter { !blacklistProvider.isBlacklisted(it) }
|
||||
|
||||
appDetailsHelper.getAppByPackageName(filtersPackages)
|
||||
.filter { it.displayName.isNotEmpty() }
|
||||
|
||||
@@ -29,7 +29,6 @@ import com.aurora.gplayapi.data.models.App
|
||||
import com.aurora.store.AuroraApp
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.providers.BlacklistProvider
|
||||
import com.aurora.store.databinding.FragmentGenericWithToolbarBinding
|
||||
import com.aurora.store.view.epoxy.views.BlackListViewModel_
|
||||
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
|
||||
@@ -41,13 +40,9 @@ import kotlinx.coroutines.launch
|
||||
class BlacklistFragment : BaseFragment<FragmentGenericWithToolbarBinding>() {
|
||||
private val viewModel: BlacklistViewModel by viewModels()
|
||||
|
||||
private lateinit var blacklistProvider: BlacklistProvider
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
blacklistProvider = BlacklistProvider.with(requireContext())
|
||||
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
viewModel.blackListedApps.collect {
|
||||
updateController(it)
|
||||
@@ -64,7 +59,7 @@ class BlacklistFragment : BaseFragment<FragmentGenericWithToolbarBinding>() {
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
blacklistProvider.save(viewModel.selected)
|
||||
viewModel.blacklistProvider.blacklist = viewModel.selected
|
||||
}
|
||||
|
||||
private fun updateController(blackList: List<App>?) {
|
||||
@@ -79,7 +74,9 @@ class BlacklistFragment : BaseFragment<FragmentGenericWithToolbarBinding>() {
|
||||
}
|
||||
} else {
|
||||
blackList
|
||||
.sortedByDescending { app -> blacklistProvider.isBlacklisted(app.packageName) }
|
||||
.sortedByDescending { app ->
|
||||
viewModel.blacklistProvider.isBlacklisted(app.packageName)
|
||||
}
|
||||
.forEach {
|
||||
add(
|
||||
BlackListViewModel_()
|
||||
|
||||
@@ -37,6 +37,7 @@ import com.aurora.store.databinding.SheetAppMenuBinding
|
||||
import com.aurora.store.util.PackageUtil
|
||||
import com.aurora.store.viewmodel.sheets.SheetsViewModel
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
||||
@@ -45,6 +46,9 @@ class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
||||
const val TAG = "APP_MENU_SHEET"
|
||||
}
|
||||
|
||||
@Inject
|
||||
lateinit var blacklistProvider: BlacklistProvider
|
||||
|
||||
private val viewModel: SheetsViewModel by viewModels()
|
||||
private val args: AppMenuSheetArgs by navArgs()
|
||||
|
||||
@@ -63,7 +67,6 @@ class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
val blacklistProvider = BlacklistProvider.with(requireContext())
|
||||
val isBlacklisted: Boolean = blacklistProvider.isBlacklisted(args.app.packageName)
|
||||
|
||||
with(binding.navigationView) {
|
||||
|
||||
@@ -42,10 +42,10 @@ import javax.inject.Inject
|
||||
@HiltViewModel
|
||||
@SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253
|
||||
class BlacklistViewModel @Inject constructor(
|
||||
val blacklistProvider: BlacklistProvider,
|
||||
@ApplicationContext private val context: Context,
|
||||
authProvider: AuthProvider
|
||||
) : ViewModel() {
|
||||
private val blacklistProvider: BlacklistProvider = BlacklistProvider.with(context)
|
||||
private val appDetailsHelper = AppDetailsHelper(authProvider.authData!!)
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
|
||||
@@ -55,7 +55,7 @@ class BlacklistViewModel @Inject constructor(
|
||||
var selected: MutableSet<String> = mutableSetOf()
|
||||
|
||||
init {
|
||||
selected = blacklistProvider.getBlackList()
|
||||
selected = blacklistProvider.blacklist
|
||||
fetchApps()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user