Skip to content

Commit

Permalink
feat: Save loadSystemApps state
Browse files Browse the repository at this point in the history
Change-Id: Ia16062717db6801c65f9ef847e0db1325c493491
  • Loading branch information
XayahSuSuSu committed Jan 9, 2025
1 parent 5f6a6ea commit 4a3b9e8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xayah.core.data.repository

import android.content.Context
import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.LauncherApps
import android.content.pm.PackageManager
import android.graphics.drawable.AdaptiveIconDrawable
Expand All @@ -17,6 +18,7 @@ import com.xayah.core.database.dao.PackageDao
import com.xayah.core.datastore.di.DbDispatchers.Default
import com.xayah.core.datastore.di.Dispatcher
import com.xayah.core.datastore.readCustomSUFile
import com.xayah.core.datastore.readLoadSystemApps
import com.xayah.core.datastore.readLoadedIconMD5
import com.xayah.core.datastore.saveLoadedIconMD5
import com.xayah.core.hiddenapi.castTo
Expand Down Expand Up @@ -125,6 +127,8 @@ class AppsRepo @Inject constructor(
fun countApps(opType: OpType) = appsDao.countPackagesFlow(opType = opType, blocked = false)
fun countSelectedApps(opType: OpType) = appsDao.countActivatedPackagesFlow(opType = opType, blocked = false)

suspend fun getLoadSystemApps() = context.readLoadSystemApps().first()

suspend fun selectApp(id: Long, selected: Boolean) {
appsDao.activateById(id, selected)
}
Expand Down Expand Up @@ -192,6 +196,7 @@ class AppsRepo @Inject constructor(
* Faster than [fastInitialize] if there are too many newly installed apps.
*/
suspend fun fullInitialize(onInit: suspend (cur: Int, max: Int, content: String) -> Unit) {
val loadSystemApps = context.readLoadSystemApps().first()
val settings = settingsDataRepo.settingsData.first()
val pm = context.packageManager
val userInfoList = rootService.getUsers()
Expand All @@ -208,7 +213,10 @@ class AppsRepo @Inject constructor(
installedPackages.forEachIndexed { index, info ->
onInit(index, installedPackages.size, info.packageName)
if (storedSet.contains(info.packageName).not()) {
apps.add(initializeApp(settings, pm, userId, info))
val isSystemApp = ((info.applicationInfo?.flags ?: 0) and ApplicationInfo.FLAG_SYSTEM) != 0
if (loadSystemApps || isSystemApp.not()) {
apps.add(initializeApp(settings, pm, userId, info))
}
}
}
appsDao.upsert(apps)
Expand All @@ -219,6 +227,7 @@ class AppsRepo @Inject constructor(
* Initialize only newly installed apps or remove uninstalled apps.
*/
suspend fun fastInitialize(onInit: suspend (cur: Int, max: Int, content: String) -> Unit) {
val loadSystemApps = context.readLoadSystemApps().first()
val settings = settingsDataRepo.settingsData.first()
val pm = context.packageManager
val userInfoList = rootService.getUsers()
Expand All @@ -238,7 +247,10 @@ class AppsRepo @Inject constructor(
onInit(index, missingPackages.size, pkg)
val info = rootService.getPackageInfoAsUser(pkg, 0, userId)
if (info != null) {
apps.add(initializeApp(settings, pm, userId, info))
val isSystemApp = ((info.applicationInfo?.flags ?: 0) and ApplicationInfo.FLAG_SYSTEM) != 0
if (loadSystemApps || isSystemApp.not()) {
apps.add(initializeApp(settings, pm, userId, info))
}
}
}
appsDao.upsert(apps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -72,7 +73,7 @@ class ListDataRepo @Inject constructor(
Filters(
cloud = cloudName,
backupDir = backupDir,
showSystemApps = false,
showSystemApps = runBlocking { appsRepo.getLoadSystemApps() },
hasBackups = true,
hasNoBackups = true,
installedApps = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.xayah.core.data.repository.Filters
import com.xayah.core.datastore.saveLoadSystemApps
import com.xayah.core.hiddenapi.castTo
import com.xayah.core.model.OpType
import com.xayah.core.model.SortType
Expand All @@ -49,6 +50,7 @@ import com.xayah.core.ui.component.TitleSort
import com.xayah.core.ui.component.paddingHorizontal
import com.xayah.core.ui.token.SizeTokens
import com.xayah.core.util.localBackupSaveDir
import com.xayah.core.work.WorkManagerInitializer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -249,6 +251,8 @@ internal fun AppsFilterSheet(
onSortByIndex: (Int) -> Unit,
onDismissRequest: () -> Unit,
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
if (isShow) {
ModalBottomSheet(onDismissRequest = onDismissRequest, sheetState = sheetState) {
Title(text = stringResource(id = R.string.filters))
Expand All @@ -257,7 +261,15 @@ internal fun AppsFilterSheet(
setFilters(filters.copy(cloud = cloud, backupDir = backupDir))
}
}
CheckBox(checked = filters.showSystemApps, text = stringResource(id = R.string.load_system_apps), onValueChange = { setFilters(filters.copy(showSystemApps = filters.showSystemApps.not())) })
CheckBox(checked = filters.showSystemApps, text = stringResource(id = R.string.load_system_apps), onValueChange = {
scope.launch {
if (filters.showSystemApps.not()) {
WorkManagerInitializer.fastInitializeAndUpdateApps(context)
}
context.saveLoadSystemApps(filters.showSystemApps.not())
setFilters(filters.copy(showSystemApps = filters.showSystemApps.not()))
}
})
when (opType) {
OpType.BACKUP -> {
CheckBox(checked = filters.hasBackups, text = stringResource(R.string.apps_which_have_backups), onValueChange = { setFilters(filters.copy(hasBackups = filters.hasBackups.not())) })
Expand Down

0 comments on commit 4a3b9e8

Please sign in to comment.