|
| 1 | +package com.yyxx.wechatfp.util; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.pm.PackageManager; |
| 5 | +import android.os.Build; |
| 6 | +import android.os.Process; |
| 7 | +import android.support.v4.app.AppOpsManagerCompat; |
| 8 | + |
| 9 | +import static android.support.v4.content.PermissionChecker.checkSelfPermission; |
| 10 | + |
| 11 | +/** |
| 12 | + * Created by Jason on 2017/11/12. |
| 13 | + */ |
| 14 | + |
| 15 | +public class PermissionUtils { |
| 16 | + |
| 17 | + public static boolean hasFingerprintPermission(Context context) { |
| 18 | + return hasSelfPermission(context, "android.permission.USE_FINGERPRINT") |
| 19 | + || hasSelfPermission(context, "com.fingerprints.service.ACCESS_FINGERPRINT_MANAGER") |
| 20 | + || hasSelfPermission(context, "com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY") |
| 21 | + ; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns true if the Activity or Fragment has access to all given permissions. |
| 26 | + * |
| 27 | + * @param context context |
| 28 | + * @param permissions permission list |
| 29 | + * @return returns true if the Activity or Fragment has access to all given permissions. |
| 30 | + */ |
| 31 | + public static boolean hasSelfPermissions(Context context, String... permissions) { |
| 32 | + for (String permission : permissions) { |
| 33 | + if (!hasSelfPermission(context, permission)) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Determine context has access to the given permission. |
| 42 | + * <p> |
| 43 | + * This is a workaround for RuntimeException of Parcel#readException. |
| 44 | + * For more detail, check this issue https://github.com/hotchemi/PermissionsDispatcher/issues/107 |
| 45 | + * |
| 46 | + * @param context context |
| 47 | + * @param permission permission |
| 48 | + * @return returns true if context has access to the given permission, false otherwise. |
| 49 | + * @see #hasSelfPermissions(Context, String...) |
| 50 | + */ |
| 51 | + private static boolean hasSelfPermission(Context context, String permission) { |
| 52 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && "Xiaomi".equalsIgnoreCase(Build.MANUFACTURER)) { |
| 53 | + return hasSelfPermissionForXiaomi(context, permission); |
| 54 | + } |
| 55 | + try { |
| 56 | + return checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; |
| 57 | + } catch (RuntimeException t) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static boolean hasSelfPermissionForXiaomi(Context context, String permission) { |
| 63 | + String permissionToOp = AppOpsManagerCompat.permissionToOp(permission); |
| 64 | + if (permissionToOp == null) { |
| 65 | + // in case of normal permissions(e.g. INTERNET) |
| 66 | + return true; |
| 67 | + } |
| 68 | + int noteOp = AppOpsManagerCompat.noteOp(context, permissionToOp, Process.myUid(), context.getPackageName()); |
| 69 | + return noteOp == AppOpsManagerCompat.MODE_ALLOWED && checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; |
| 70 | + } |
| 71 | +} |
0 commit comments