permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.22k stars 1.44k forks source link

Version 4.1.0 didn't drop the Xiaomi support? #593

Closed h11g closed 5 years ago

h11g commented 5 years ago

Overview

module build.gradle

dependencies {
    ...
    implementation "com.github.hotchemi:permissionsdispatcher:4.1.0"
    annotationProcessor "com.github.hotchemi:permissionsdispatcher-processor:4.1.0"
}

PermissionUtils.java

    /**
     * Returns true if the Activity or Fragment has access to all given permissions.
     *
     * @param context     context
     * @param permissions permission list
     * @return returns true if the Activity or Fragment has access to all given permissions.
     */
    public static boolean hasSelfPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (permissionExists(permission) && !hasSelfPermission(context, permission)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 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 PermissionChecker.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
        } catch (RuntimeException t) {
            return false;
        }
    }

    private static boolean hasSelfPermissionForXiaomi(Context context, String permission) {
        String permissionToOp = AppOpsManagerCompat.permissionToOp(permission);
        if (permissionToOp == null) {
            // in case of normal permissions(e.g. INTERNET)
            return true;
        }
        int noteOp = AppOpsManagerCompat.noteOp(context, permissionToOp, Process.myUid(), context.getPackageName());
        return noteOp == AppOpsManagerCompat.MODE_ALLOWED && PermissionChecker.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

Environment

hotchemi commented 5 years ago

@electhuang thank you, oh this is weird...anyway could you try 4.2? 🙇

h11g commented 5 years ago

@electhuang thank you, oh this is weird...anyway could you try 4.2? 🙇

hello @hotchemi ,when i try 4.2.0 or 4.3.0, project sync succeed, but build failed with error message:

Could not GET 'https://jitpack.io/com/github/hotchemi/permissionsdispatcher-processor/4.2.0/permissionsdispatcher-processor-4.2.0.pom'. Received status code 401 from server: Unauthorized

and i try to open the link in browser,it required access token

hotchemi commented 5 years ago

@electhuang From 4.2.0 we've changed package name so could you try?

ref: https://github.com/permissions-dispatcher/PermissionsDispatcher/blob/master/CHANGELOG.md#changelog

h11g commented 5 years ago

@hotchemi oh sorry, now 4.2.0 is ok, thank you!