androidandroid-permissionsandroid-6.0-marshmallowandroid-appstandby

Android 6 UsageStatsManager isAppInactive() permission


I'm using isAppInactive() method from UsageStatsManager class on Android 6 (API 23) to detect the standby state of the app I'm developing.

The docs affirm that it is necessary to request PACKAGE_USAGE_STATS permission but I'm able to use that method without requesting the permission.

Is this a bug?


Solution

  • In UsageStatsService there is a hasPermission [1] method that checks if the permission PACKAGE_USAGE_STATS is granted. It is used in this methods:

    This is the code of isAppInactive [5], and you can see how the permission is not requested:

    @Override
    public boolean isAppInactive(String packageName, int userId) {
        try {
            userId = ActivityManagerNative.getDefault().handleIncomingUser(Binder.getCallingPid(),
                    Binder.getCallingUid(), userId, false, true, "isAppInactive", null);
        } catch (RemoteException re) {
            return false;
        }
        final long token = Binder.clearCallingIdentity();
        try {
            return UsageStatsService.this.isAppIdleFilteredOrParoled(packageName, userId, -1);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
    }
    

    As you can see in the message of commit that adds isAppInactive (originally is called isAppIdle then it is renamed) the API should be public:

    Add ability to get and set idle state of apps
    Add am shell command to set and get idle
    Add public API to check if an app is idle
    

    I don't think this is a bug, but only an unclear documentation.