some refactoring

This commit is contained in:
Josia Pietsch 2025-01-27 00:50:31 +01:00
parent 23f8cfb70e
commit ecd58b91bb
Signed by: jrpie
GPG key ID: E70B571D66986A2D
2 changed files with 13 additions and 9 deletions

View file

@ -76,6 +76,17 @@ fun getUserFromId(userId: Int?, context: Context): UserHandle {
return profiles.firstOrNull { it.hashCode() == userId } ?: profiles[0]
}
fun getPrivateSpaceUser(context: Context): UserHandle? {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
return null
}
val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
return userManager.userProfiles.firstOrNull { u ->
launcherApps.getLauncherUserInfo(u)?.userType == UserManager.USER_TYPE_PROFILE_PRIVATE
}
}
fun openInBrowser(url: String, context: Context) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
intent.putExtras(Bundle().apply { putBoolean("new_window", true) })

View file

@ -16,6 +16,7 @@ import android.widget.Toast
import de.jrpie.android.launcher.Application
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.apps.AppFilter
import de.jrpie.android.launcher.getPrivateSpaceUser
import de.jrpie.android.launcher.isDefaultHomeScreen
import de.jrpie.android.launcher.preferences.LauncherPreferences
import de.jrpie.android.launcher.ui.list.ListActivity
@ -230,10 +231,7 @@ private fun togglePrivateSpaceLock(context: Context) {
return
}
val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
val launcherApps = context.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
val privateSpaceUser = userManager.userProfiles.firstOrNull { u ->
launcherApps.getLauncherUserInfo(u)?.userType == UserManager.USER_TYPE_PROFILE_PRIVATE
}
val privateSpaceUser = getPrivateSpaceUser(context)
if (privateSpaceUser == null) {
Toast.makeText(context, context.getString(R.string.toast_private_space_not_available), Toast.LENGTH_LONG).show()
@ -249,11 +247,6 @@ private fun togglePrivateSpaceLock(context: Context) {
}
if (userManager.isQuietModeEnabled(privateSpaceUser)) {
userManager.requestQuietModeEnabled(false, privateSpaceUser)
Toast.makeText(
context,
context.getString(R.string.toast_private_space_unlocked),
Toast.LENGTH_LONG
).show()
return
}
userManager.requestQuietModeEnabled(true, privateSpaceUser)