How is QUERY_ALL_PACKAGES enforced?

Viewed 6064

Android 11 (target 30) introduces Package visibility restrictions.

They added the QUERY_ALL_PACKAGES that allows bypassing these restrictions, and only specific apps are allowed to use it.

The protection level for this permission is NORMAL, and so I wondered how limiting this permission is enforced.

In the documentation, they say:

In an upcoming policy update, look for Google Play to provide guidelines for apps that need the QUERY_ALL_PACKAGES permission.

Does anyone know where this policy update can be found?

1 Answers

QUERY_ALL_PACKAGES permission has a normal protection level so it doesn't throw an exception when added.

The way I enforce this permission is by calling queryUsageStats on a UsageStatsManager instance. If it returns an empty list, I notify the user to turn on Usage Data Access in phone Settings.

private fun requestPackagePermission() {
        val stats: List<UsageStats> = usageStatsManager
            .queryUsageStats(
                UsageStatsManager.INTERVAL_BEST,
                0,
                System.currentTimeMillis()
            )
        val isEmpty = stats.isEmpty()
        if (isEmpty) {
            // notify user before opening app settings
            startActivity(Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS))
        } else {
            // carry out your work
        }
    }

I call this method in onResume() of my activity or fragment so that when Usage Data Access is turned on and the user navigates back to the app, the method is called again in order to confirm the change.

Related