How to open Draw Overlay permission popup in MIUI?

Viewed 10720

I want to open this permission popup in MIUI. I have tried this code, but this will not open permission manager popup for a particular app.

public static Intent toPermissionManager(Context context, String packageName) {
    Intent intent = new Intent("miui.intent.action.APP_PERM_EDITOR");
    String version = getVersionName();
    if (MIUI_V5.equals(version)) {
        PackageInfo pInfo;
        try {
            pInfo = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException ignored) {
            return null;
        }
        intent.setClassName("com.android.settings", "com.miui.securitycenter.permission.AppPermissionsEditor");
        intent.putExtra("extra_package_uid", pInfo.applicationInfo.uid);
    } else { // MIUI_V6 and above
        final String PKG_SECURITY_CENTER = "com.miui.securitycenter";
        try {
            context.getPackageManager().getPackageInfo(PKG_SECURITY_CENTER, PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException ignored) {
            return null;
        }
        intent.setClassName(PKG_SECURITY_CENTER, "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
        intent.putExtra("extra_pkgname", packageName);
    }
    return intent;
}
3 Answers

To check if Display popup window permission is Granted or not, using onDisplayPopupPermission() this method is from my above answer.

I am using canDrawOverlayViews and reflection to check permission is granted or not this may be removed/changed in the latest releases.

This solution I have tried up to MIUI8 it's working, not sure about the latest updates. Please give an update on the comment so others can have updates.

1.Step

if (!canDrawOverlayViews() && isXiaomi()) {
        //permission not granted
        Log.e("canDrawOverlayViews", "-No-");
       onDisplayPopupPermission(); 
        //write above answered permission code for MIUI here
    }else {

    }

2.Step Check if permission granted or not

public boolean canDrawOverlayViews() {
    if (Build.VERSION.SDK_INT < 21) {
        return true;
    }
    Context con = this;
    try {
        return Settings.canDrawOverlays(con);
    } catch (NoSuchMethodError e) {
        return canDrawOverlaysUsingReflection(con);
    }
}

public static boolean isXiaomi() {
    return "xiaomi".equalsIgnoreCase(Build.MANUFACTURER);
}

canDrawOverlaysUsingReflection() I have found this solution here here translate this page

public static boolean canDrawOverlaysUsingReflection(Context context) {

try {

    AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
    Class clazz = AppOpsManager.class;
    Method dispatchMethod = clazz.getMethod("checkOp", new Class[] { int.class, int.class, String.class });
    //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
    int mode = (Integer) dispatchMethod.invoke(manager, new Object[] { 24, Binder.getCallingUid(), context.getApplicationContext().getPackageName() });

    return AppOpsManager.MODE_ALLOWED == mode;

} catch (Exception e) {  return false;  }

}

Related