2

how to check is there any app using accessibility permission(BIND_ACCESSIBILITY_SERVICE), or the name of applications which requested for the same along with its granted or not to them ?

To detect that below apps are using accessibility permission:

  1. https://play.google.com/store/apps/details?id=you.in.spark.access.dots&hl=en_IN&gl=US
  2. https://play.google.com/store/apps/details?id=com.oddlyspaced.burnermedia.burnerguard&hl=en_IN&gl=US
  3. https://play.google.com/store/apps/details?id=com.lastpass.lpandroid&hl=en_IN&gl=US

Already Tried Below code which is not working for above apps, not showing any entry for Access Dot app and BurnerGuard app, while showing entry for Last pass but not affecting on change of permission:

List<PackageInfo> allpackages = getPackageManager().getInstalledPackages(PackageManager.GET_PERMISSIONS);
    for(int i =0;i<allpackages.size();i++){
        PackageInfo pi = allpackages.get(i);
        if (pi.requestedPermissions == null) {
            // No permissions are requested in the AndroidManifest
            continue;
        }
        String[] requestedPermissions = pi.requestedPermissions;
        int[] requestPermissionFlags;
        for(int j=0;j<requestedPermissions.length;j++){
            String reqParm = requestedPermissions[j];
            int status = pi.requestedPermissionsFlags[j] & PackageInfo.REQUESTED_PERMISSION_GRANTED;
            
            try {
                PermissionInfo permissionInfo = getPackageManager().getPermissionInfo(reqParm,0);
      
                if(permissionInfo.name.equals("android.permission.BIND_ACCESSIBILITY_SERVICE")) {
                    if(status!=0) {
                        Log.i("accessibility", "Package Name :: " + pi.packageName + "    permission name :: " + permissionInfo.name + " Permission Granted " );
                    } else {
                        Log.i("accessibility", "Package Name :: " + pi.packageName + "    permission name :: " + permissionInfo.name + " Permission Requested " );
                    }
                }
            } catch (PackageManager.NameNotFoundException e) {
                //Log.e("accessibility", "Unknown permission: ");
                continue;
            }
    }

Thanks

Sandeep
  • 21
  • 4

2 Answers2

1

For permissions in general (those declared with <uses-permission> in the manifest), this should give you the information you want:

// Iterate over all installed packages and include information about their permissions
packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS).forEach { pkg ->
    // Find out if the current package has declared BIND_ACCESSIBILITY_SERVICE in its manifest..
    val index = pkg.requestedPermissions?.indexOf(Manifest.permission.BIND_ACCESSIBILITY_SERVICE) ?: -1
    if (index != -1) {
        // ..it has, so log whether the permission has been granted.
        val flags = pkg.requestedPermissionFlags[index]
        val grantStatus = if ((flags and PackageManager.PERMISSION_GRANTED) == PackageManager.PERMISSION_GRANTED) "granted" else "not granted"
        Log.d("Foo", "Package ${pkg.packageName} wants BIND_ACCESSIBILITY_SERVICE, and it is currently $grantStatus.")
    }
}

However, for accessibility services you may need to query the AccessibilityManager instead:

val accessibilityManager = getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
val installedServices = accessibilityManager.installedAccessibilityServiceList
val enabledServices = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK)
installedServices.forEach { installed ->
    val svcInfo = installed.resolveInfo.serviceInfo
    val appLabel = packageManager.getApplicationLabel(packageManager.getApplicationInfo(svcInfo.packageName, 0))
    val state = if (enabledServices.any { it.resolveInfo.serviceInfo.packageName == svcInfo.packageName && it.resolveInfo.serviceInfo.name == svcInfo.name && svcInfo.permission == Manifest.permission.BIND_ACCESSIBILITY_SERVICE)) {
        "enabled"
    } else {
        "installed but currently disabled"
    }
    Log.d("Foo", "Service ${svcInfo.name} belonging to $appLabel is $state.")
}
Michael
  • 57,169
  • 9
  • 80
  • 125
  • Thanks for your solution, but its not working for particular app, [Access Dots](https://play.google.com/store/apps/details?id=you.in.spark.access.dots&hl=en_IN&gl=US) , Access Dots is showing in `installedServices` but its not showing into `enabledServices` after granting the permission to it. Can You explain, why its not working for Particular this App – Sandeep Feb 12 '21 at 04:15
  • I've tried out both code chunks and can confirm that the second method using `AccessibilityManager` works well, except that on Android 11 onwards you need to declare the `` element in your app manifest for it to get past the package visibility filtering changes. However, I did not manage to get the first method that uses `packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS)` to work; anyone know the reason why it's not showing the correct result or actually managed to get it to work? – huthut28 Jul 13 '22 at 11:16
0

to check accessibility is on/off

(requireActivity().getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager).apply {
    installedAccessibilityServiceList.forEach { installedService ->
        installedService.resolveInfo.serviceInfo.apply {
            if (getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK).any { it.resolveInfo.serviceInfo.packageName == packageName && it.resolveInfo.serviceInfo.name == name && permission == Manifest.permission.BIND_ACCESSIBILITY_SERVICE && it.resolveInfo.serviceInfo.packageName == requireActivity().packageName })
                isAccessibilityEnabled = true
        }
    } }
Rizwan Rasheed
  • 326
  • 4
  • 14