Make sure there is always just one work profile

This commit is contained in:
MM20 2025-04-02 21:09:51 +02:00
parent bffd440408
commit 0ac5212da1
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 29 additions and 12 deletions

View File

@ -205,7 +205,7 @@ fun LazyListScope.AppResults(
if (showList) { if (showList) {
ListResults( ListResults(
key = "apps", key = "apps",
items = apps.filter { it.user == profiles[selectedProfileIndex].userHandle }, items = if (isProfileLocked) emptyList() else apps,
before = before?.let { { it() } }, before = before?.let { { it() } },
selectedIndex = selectedIndex, selectedIndex = selectedIndex,
itemContent = { app, showDetails, index -> itemContent = { app, showDetails, index ->
@ -223,7 +223,7 @@ fun LazyListScope.AppResults(
} else { } else {
GridResults( GridResults(
key = "apps", key = "apps",
items = apps.filter { it.user == profiles[selectedProfileIndex].userHandle }, items = if (isProfileLocked) emptyList() else apps,
before = before, before = before,
itemContent = { itemContent = {
GridItem( GridItem(

View File

@ -42,20 +42,28 @@ class ProfileManager(
private val scope = CoroutineScope(Dispatchers.Default + Job()) private val scope = CoroutineScope(Dispatchers.Default + Job())
private val profileStates: MutableStateFlow<List<ProfileWithState>> = /**
MutableStateFlow(emptyList()) * An array of exactly 3 profiles with their states.
* - Index 0: Personal profile
* - Index 1: Work profile
* - Index 2: Private profile
*
* Profiles that don't exist are null.
*/
private val profileStates: MutableStateFlow<Array<ProfileWithState?>> =
MutableStateFlow(arrayOf(null, null, null))
/** /**
* List of profiles that are active and unlocked. * List of profiles that are active and unlocked.
*/ */
val activeProfiles: Flow<List<Profile>> = profileStates.map { val activeProfiles: Flow<List<Profile>> = profileStates.map {
it.mapNotNull { it.mapNotNull {
if (it.state.locked) null else it.profile if (it?.state?.locked != false) null else it.profile
} }
}.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1) }.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1)
val profiles: Flow<List<Profile>> = profileStates.map { val profiles: Flow<List<Profile>> = profileStates.map {
it.map { it.profile } it.mapNotNull { it?.profile }
}.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1) }.shareIn(scope, SharingStarted.WhileSubscribed(), replay = 1)
init { init {
@ -94,15 +102,24 @@ class ProfileManager(
} }
} }
} }
private suspend fun refreshProfiles() { private suspend fun refreshProfiles() {
mutex.withLock { mutex.withLock {
val profiles = mutableListOf<ProfileWithState>() val profiles = arrayOf<ProfileWithState?>(null, null, null)
for (userHandle in launcherApps.profiles) { for (userHandle in launcherApps.profiles) {
val serial = userManager.getSerialNumberForUser(userHandle) val serial = userManager.getSerialNumberForUser(userHandle)
if (android.os.Build.MANUFACTURER == "samsung" && serial == 150L) continue // Hide Samsung Secure Folder if (android.os.Build.MANUFACTURER == "samsung" && serial == 150L) continue // Hide Samsung Secure Folder
profiles.add(
ProfileWithState( val type = getProfileType(userHandle)
val index = when (type) {
Profile.Type.Personal -> 0
Profile.Type.Work -> 1
Profile.Type.Private -> 2
}
if (profiles[index] == null) {
profiles[index] = ProfileWithState(
Profile( Profile(
type = getProfileType(userHandle), type = getProfileType(userHandle),
userHandle = userHandle, userHandle = userHandle,
@ -110,7 +127,7 @@ class ProfileManager(
), ),
getProfileState(userHandle), getProfileState(userHandle),
) )
) }
} }
profileStates.value = profiles profileStates.value = profiles
} }
@ -118,13 +135,13 @@ class ProfileManager(
fun getProfile(userHandle: UserHandle): Flow<Profile?> { fun getProfile(userHandle: UserHandle): Flow<Profile?> {
return profileStates.map { return profileStates.map {
it.find { it.profile.userHandle == userHandle }?.profile it.find { it?.profile?.userHandle == userHandle }?.profile
} }
} }
fun getProfileState(profile: Profile?): Flow<Profile.State?> { fun getProfileState(profile: Profile?): Flow<Profile.State?> {
return profileStates.map { profiles -> return profileStates.map { profiles ->
profiles.find { it.profile == profile }?.state profiles.find { it?.profile == profile }?.state
} }
} }