From 10516ef9a99084fb6ec9612d132deffe7313e169 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sat, 4 Jul 2026 20:35:37 +0200 Subject: [PATCH] user launcher profilemanager --- .../mm20/launcher2/profiles/ProfileManager.kt | 15 ++++++++++ data/search-actions/build.gradle.kts | 1 + .../actions/PrivateSpaceLockAction.kt | 14 ++++++---- .../builders/PrivateSpaceLockActionBuilder.kt | 28 +++++++++---------- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt b/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt index 25eb36c18..4bf9c2a2e 100644 --- a/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt +++ b/core/profiles/src/main/java/de/mm20/launcher2/profiles/ProfileManager.kt @@ -145,12 +145,27 @@ class ProfileManager( } } + /** + * Returns the profile of the given type, or null if it doesn't exist. + */ + fun getProfile(profileType: Profile.Type): Profile? { + return when (profileType) { + Profile.Type.Personal -> profileStates.value[0]?.profile + Profile.Type.Work -> profileStates.value[1]?.profile + Profile.Type.Private -> profileStates.value[2]?.profile + } + } + fun getProfileState(profile: Profile?): Flow { return profileStates.map { profiles -> profiles.find { it?.profile == profile }?.state } } + fun getProfileStateOnce(profile: Profile?): Profile.State? { + return profileStates.value.find { it?.profile == profile }?.state + } + private fun getProfileType(userHandle: UserHandle): Profile.Type { if (isAtLeastApiLevel(35)) { val launcherUserInfo = launcherApps.getLauncherUserInfo(userHandle) diff --git a/data/search-actions/build.gradle.kts b/data/search-actions/build.gradle.kts index 11bddc0c5..6a3957cd3 100644 --- a/data/search-actions/build.gradle.kts +++ b/data/search-actions/build.gradle.kts @@ -50,6 +50,7 @@ dependencies { implementation(libs.coil.core) implementation(project(":core:base")) + implementation(project(":core:profiles")) implementation(project(":data:database")) implementation(project(":core:ktx")) implementation(project(":core:preferences")) diff --git a/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/actions/PrivateSpaceLockAction.kt b/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/actions/PrivateSpaceLockAction.kt index 92d30d172..fd01e2b70 100644 --- a/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/actions/PrivateSpaceLockAction.kt +++ b/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/actions/PrivateSpaceLockAction.kt @@ -1,14 +1,15 @@ package de.mm20.launcher2.searchactions.actions import android.content.Context -import android.os.UserHandle -import android.os.UserManager import de.mm20.launcher2.ktx.isAtLeastApiLevel +import de.mm20.launcher2.profiles.Profile +import de.mm20.launcher2.profiles.ProfileManager data class PrivateSpaceLockAction( override val label: String, val isLocked: Boolean, - val userHandle: UserHandle, + val profile: Profile, + val profileManager: ProfileManager, ) : SearchAction { override val icon = SearchActionIcon.PrivateSpace override val iconColor = 0 @@ -16,8 +17,11 @@ data class PrivateSpaceLockAction( override fun start(context: Context) { if (isAtLeastApiLevel(28)) { - context.getSystemService(UserManager::class.java) - ?.requestQuietModeEnabled(!isLocked, userHandle) + if (isLocked) { + profileManager.unlockProfile(profile) + } else { + profileManager.lockProfile(profile) + } } } } diff --git a/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/builders/PrivateSpaceLockActionBuilder.kt b/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/builders/PrivateSpaceLockActionBuilder.kt index db05cf15c..2fb28e8f7 100644 --- a/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/builders/PrivateSpaceLockActionBuilder.kt +++ b/data/search-actions/src/main/java/de/mm20/launcher2/searchactions/builders/PrivateSpaceLockActionBuilder.kt @@ -1,19 +1,23 @@ package de.mm20.launcher2.searchactions.builders import android.content.Context -import android.content.pm.LauncherApps -import android.os.UserManager import de.mm20.launcher2.ktx.isAtLeastApiLevel +import de.mm20.launcher2.profiles.Profile +import de.mm20.launcher2.profiles.ProfileManager import de.mm20.launcher2.search.ResultScore import de.mm20.launcher2.searchactions.R import de.mm20.launcher2.searchactions.TextClassificationResult import de.mm20.launcher2.searchactions.actions.PrivateSpaceLockAction import de.mm20.launcher2.searchactions.actions.SearchAction import de.mm20.launcher2.searchactions.actions.SearchActionIcon +import org.koin.core.component.KoinComponent +import org.koin.core.component.inject class PrivateSpaceLockActionBuilder( override val label: String, -) : SearchActionBuilder { +) : SearchActionBuilder, KoinComponent { + + private val profileManager: ProfileManager by inject() constructor(context: Context) : this(context.getString(R.string.search_query_private_space)) @@ -23,6 +27,9 @@ class PrivateSpaceLockActionBuilder( override fun build(context: Context, classifiedQuery: TextClassificationResult): SearchAction? { if (!isAtLeastApiLevel(35)) return null + val privateProfile = profileManager.getProfile(Profile.Type.Private) ?: return null + val profileState = profileManager.getProfileStateOnce(privateProfile) ?: return null + val keyword = context.getString(R.string.search_query_private_space).lowercase() val score = ResultScore.from( query = classifiedQuery.text.lowercase(), @@ -30,21 +37,14 @@ class PrivateSpaceLockActionBuilder( ) if (score.score < 0.8f) return null - val launcherApps = context.getSystemService(LauncherApps::class.java) ?: return null - val userManager = context.getSystemService(UserManager::class.java) ?: return null - - val privateHandle = launcherApps.profiles.firstOrNull { - launcherApps.getLauncherUserInfo(it)?.userType == UserManager.USER_TYPE_PROFILE_PRIVATE - } ?: return null - - val isLocked = !userManager.isUserUnlocked(privateHandle) return PrivateSpaceLockAction( label = context.getString( - if (isLocked) R.string.search_action_private_space_unlock + if (profileState.locked) R.string.search_action_private_space_unlock else R.string.search_action_private_space_lock ), - isLocked = isLocked, - userHandle = privateHandle, + isLocked = profileState.locked, + profile = privateProfile, + profileManager = profileManager, ) } }