Permissiondispatcher's pit & solution on MIUI 11

After being tortured by the authority pop-up window of Xiaomi 9 for more than a day, I finally found a solution, recorded it and asked for help

The problem is, due to the increasing attention to personal privacy information, many application markets require apps to pop up a description box to explain to users when dynamically requesting high-risk permissions.

At first glance, this problem is not difficult. Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang Bang.

Good. After I finished these, I tested it on my Xiaomi 10 Youth Version (MIUI12) to perfectly solve the permission compliance problem in the application market.

However, when testing on Xiaomi 9 (MIUI11), I found that a system permission pop-up window for generation countdown will pop up before I dynamically request permission.

This makes me puzzled. I found that the first system pop-up window did not pop up when the application applied for high-level permissions, but was triggered when calling PermissionUtils#hasSelfPermissions provided by PermissionDispatcher. Get into the source code and watch a wave. I found that hasSelfPermissions will make special changes to Xiaomi devices. I can't see it all the way...

/**
 * Determine context has access to the given permission.
 * <p>
 * This is a workaround for RuntimeException of Parcel#readException.
 * For more detail, check this issue https://github.com/hotchemi/PermissionsDispatcher/issues/107
 *
 * @param context    context
 * @param permission permission
 * @return returns true if context has access to the given permission, false otherwise.
 * @see #hasSelfPermissions(Context, String...)
 */
private static boolean hasSelfPermission(Context context, String permission) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && "Xiaomi".equalsIgnoreCase(Build.MANUFACTURER)) {
        return hasSelfPermissionForXiaomi(context, permission);
    }
    try {
        return checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    } catch (RuntimeException t) {
        return false;
    }
}

Now that the root cause has been found, shall we not use this interface? Using context#checkSelfPermission, although this method can only verify one permission at a time, in order to solve the problem, I will write an additional for loop.

Three lines of code, compile. function... Request. Rollover.

This time, the system will pop up first and then jump to the countdown system permission window. If this pop-up window is not processed, the system will jump to the system permission window without countdown.

Looking at the code, we find that PermissionUtils#hasSelfPermissions will be called when xxxWithPermissionCheck is performed in the generated class of Dispatcher.

static void getCameraWithPermissionCheck(@NonNull MainActivity target) {
    if (PermissionUtils.hasSelfPermissions(target, PERMISSION_GETCAMERAPERMISSION)) {
      target.getCamera();
    } else {
      ActivityCompat.requestPermissions(target, PERMISSION_GETCAMERAPERMISSION, REQUEST_GETCAMERAPERMISSION);
    }
}

It's over. This wave can't be avoided. I can't give him a commit. Change this call. Wait, will the open source library be upgraded?! I use 3.1.0.

I went to GitHub and found that the latest release version was 4.8.0. Great joy. Upgrade a wave. However, various search data are compiled (although the code can be copied on gitee, but many iteration records are not available. Students allowed by the network still prefer to see GitHub). The original dispatcher changed the dependent package name after 4.x. After modification, the compilation passed and the regression test passed.

// Permission application library 3.1.0
api "com.github.hotchemi:permissionsdispatcher:3.1.0"
annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:3.1.0"
    
// Permission application library 4.7.0
api "org.permissionsdispatcher:permissionsdispatcher:4.7.0"
annotationProcessor "org.permissionsdispatcher:permissionsdispatcher-processor:4.7.0"

Check the dispatcher's submission record. The special treatment of Xiaomi was removed in 4.1.0.

4.1.0

@hotchemi hotchemi released this on 7 Dec 2018 ยท 141 commits to master since this release

Fix: compile time validation for detecting OnNeverAskAgain with special permissions #549

Update: Drop Xiaomi support #548

Fix: Fix CallNeedsPermissionDetector to scan only annotated classes #536

Tags: Android dispatcher

Posted on Sun, 05 Sep 2021 22:38:41 -0400 by dcav