Search code examples
androidpermissionsurimulti-user

Does takePersistableUriPermission() work for multi users?


In my app, I pick a folder with Intent.ACTION_OPEN_DOCUMENT_TREE and get the Uri of picked folder. I call takePersistableUriPermission() to grant read/write permissions for selected Uri.

My question is: Does it work for multi users in my device? For example: I sign in with Google Account 1, install the app, select a folder and grant permissions. Then I remove Google Account 1 and sign in with Google Account 2. Are the Uri permissions still granted?

I tested on Note4 and they are still granted after changing users. But I'm not sure about other devices, and I didn't find any document about this issue.


Solution

  • Persisted URI based permissions are tied to package name (and user id - see below), not any kind of Google or otherwise account system. That means that as long as the package is still installed, it will retain the permission (unless, of course, the granting app revokes it).

    On devices that support multiple user profiles, such as many tablets, Android for Work, or phones running Marshmallow, each user is entirely sandboxed from one another, running separate instances of each app installed on their user profile. In those cases, URI based permissions would of course only apply to the user that the app is running under and not with other user profiles.