FLASHCODER
FLASHCODER

Reputation: 294

How check if ACCESS_RESTRICTED_SETTINGS is enabled?

Is there some Java code to check if the ACCESS_RESTRICTED_SETTINGS permission is enabled?

enter image description here

Upvotes: 2

Views: 1996

Answers (3)

Rajveer Singh
Rajveer Singh

Reputation: 11

   public boolean isAllowRestrictedSettingEnable(Context context) {
    StringBuilder output = new StringBuilder();
    try {
        String packageName = context.getPackageName();
        String cmd = "cmd appops get " + packageName + " ACCESS_RESTRICTED_SETTINGS";
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
        outputStream.writeBytes(cmd + "\n");
        outputStream.flush();
        outputStream.writeBytes("exit\n");
        outputStream.flush();
        outputStream.close();
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            output.append(line).append("\n");
        }
        int exitVal = process.waitFor();
        if (exitVal == 0) {
            String initialOutput = output.toString();
            String finalOutput = initialOutput.split(":")[1].trim().split(";")[0];
            if (finalOutput != null && finalOutput.contains("allow")) {
                return true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}"

Upvotes: 0

snachmsm
snachmsm

Reputation: 19263

Kotlin version with possible SecurityException awareness

fun hasAccessRestrictedPerm(context: Context): Boolean {
    // e.g. can enable Accessibility Service with Settings.ACTION_ACCESSIBILITY_SETTINGS
    return if (Build.VERSION.SDK_INT < 33) {
        true
    } else {
        try {
            val packageManager = context.packageManager as PackageManager
            val applicationInfo = packageManager.getApplicationInfo(context.packageName, 0)
            val appOpsManager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
            val mode = appOpsManager.unsafeCheckOpNoThrow(
                "android:access_restricted_settings",
                applicationInfo.uid,
                applicationInfo.packageName,
            )
            mode == AppOpsManager.MODE_ALLOWED
        } catch (e: PackageManager.NameNotFoundException) {
            false
        } catch (e: SecurityException) {
            false
        }
    }
}

Upvotes: 1

FLASHCODER
FLASHCODER

Reputation: 294

public static boolean hasAccessRestrictedPerm(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
        AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        int mode = appOpsManager.checkOpNoThrow("android:access_restricted_settings", applicationInfo.uid, applicationInfo.packageName);
        return (mode == AppOpsManager.MODE_ALLOWED);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

Upvotes: 0

Related Questions