Android Q Roles

Łukasz Chromy
ProAndroidDev

--

Alongside the Android Q Beta 1 release, we got a new interesting feature: Roles. Though this feature is still in early stage of development, let’s play with it a little.

The purpose of Roles will be to associate an app to a specific, well-defined system function. After your app became a role holder it will gain certain privileges, specific for that role. Each role represents a common use case, such as playing music, viewing photos in a gallery or sending SMS messages.
There could be more than one installed application that qualifies for the given role, but only one can hold it at the time.

Roles defined in Android Q

Following, pre-defined roles come with Android Q beta:

╔════════════════╦═════════════════════════════════════════════════╗
║ Name ║ Purpose ║
╠════════════════╬═════════════════════════════════════════════════╣
║ ROLE_BROWSER ║ Web browser ║
║ ROLE_DIALER ║ Phone dialer ║
║ ROLE_SMS ║ SMS messaging app ║
║ ROLE_HOME ║ Home screen app ║
║ ROLE_MUSIC ║ Music player ║
║ ROLE_GALLERY ║ Photo gallery viewer ║
╚════════════════╩═════════════════════════════════════════════════╝

How to use Roles

Assume that your app is a gallery viewer and we want to prompt users to give it the right role - which in this case is ROLE_GALLERY.

We need to start with a few modifications in AndroidManifest.xml:

As you can see we have added two new things that came with Android Q: android.permission.READ_MEDIA_IMAGES and android.intent.category.APP_GALLERY. Now our app is ready to hold a ROLE_GALLERY. For this purpose, we will leverage the new class called RoleManager.

RoleManager comes with three public methods:

createRequestRoleIntent(String roleName)
Returns an Intent which prompts the user to grant a role to this application.
isRoleAvailable(String roleName)
Check whether a role is available in the system.
isRoleHeld(String roleName)
Check whether the calling application is holding a particular role.

First, we should check if our app is qualified to be a role holder and whether it already hold this particular role:

Then let’s prompt users to grant role:

Use will see a similar dialog:

Role grant dialog

Voila! Our app is now a default gallery app.

Ok, but what will happen if some other app will try to gain the same role? Assume our app RoleTest hold ROLE_GALLERY, and our second app (RoleTest2) request the same role:

Request for an already taken role

As we can see apps still can request for a given role even if it’s already held by another app. Unfortunately, the message we see needs a little tweaking ;) Dialog is generated by OS and we have no control over its content, so we have to wait till Google fix this issue.

It’s too early to say what privileges apps will gain with those new Roles and how common will be the usage of this feature, so stay put and wait for updates!

More reading about roles in Android Q

All samples from the article are available as a sample project in my repository.

github.com/sayler666/RoleTest

https://developer.android.com/preview/features/roles
https://developer.android.com/reference/android/app/role/RoleManager.html

--

--