Android Profiles and DIY Launchers
2024/01/14 21:43My current job does not provide a company phone but allows to set the company google account on the personal devices, the account does not install a separate profile, I initially just added the google work account to my phone but felt wrong. Randomly on hackernews or reddit I found out that is possible to install TestDPC that allows to separate accounts for applications, exactly as if a work profile is enforced. This is nice and good but my DIY launcher did not support work profiles, so I rolled up my sleeves and after few too many searches I was able to implement it. The following is a summary of what is needed to support work profiles in a launcher, with the hope it can helps anyone looking for similar information.
LauncherApps
A newer approach to retrieve activities with profile support, include it in your app using launcherApps = getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps
. Check the documentation for more details.
List profiles and apps for all profiles
launcherApps.profiles.flatMap { profile -> launcherApps.getActivityList(null, profile) }
Get a LauncherActivityInfo from a package name and user (handle)
val intent = packageManager.getLaunchIntentForPackage(packageName)
val launcherActivityInfo = launcherApps.resolveActivity(intent, userHandle)
Get the icon
Activities retrieved using LauncherApps
luckily provide a way to get the badged icon, the application icon with a small suitcase on the side.
launcherActivityInfo.getBadgedIcon(0)
Launch the activity with a specific profile
val componentName = launcherActivityInfo.componentName
launcherApps.startMainActivity(componentName, userHandle, null, null)
Retrieve a UserHandle based on user id
On my phone the main user is 0
, and the work profile user is 10
. I did not found a way to go from the int value to UserHandle and resort to retrieving all the profiles from LauncherApps
and keeping them around.
Disclaimer: these are possible not the best or more efficient way to retrieve and use these information, but they seems to work and be reliable.
Additional links