Skip to content

Instantly share code, notes, and snippets.

@scottyab
Last active October 24, 2024 11:24
Show Gist options
  • Save scottyab/865a98618a781bc40fc4eabb06ceedab to your computer and use it in GitHub Desktop.
Save scottyab/865a98618a781bc40fc4eabb06ceedab to your computer and use it in GitHub Desktop.
Simple way of determining is the app is running in the context of main user, managed profile, other (i.e private space) or unknown. Potentially useful for alerting users if your app should not be used in PrivateSpace.
import android.content.Context
import android.os.Build
import android.os.UserManager
// Enum to represent the user profile types
enum class UserProfile {
MAIN_USER,
MANAGED_PROFILE,
OTHER_PROFILE,
UNKNOWN
}
fun getUserProfile(context: Context): UserProfile {
val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager
// Check if the current user is the system (main) user
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (userManager.isSystemUser) {
return UserProfile.MAIN_USER
}
}
// If not the main user, check if it's a managed profile
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
return if (userManager.isManagedProfile) {
UserProfile.MANAGED_PROFILE
} else {
UserProfile.OTHER_PROFILE // Other profile i.e Private Space user
}
}
// If unable to determine, return UNKNOWN
return UserProfile.UNKNOWN
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment