android 应用权限判断

sancaiodm Android源码摘录 2021-08-25 1449 0
   private static Hashtable<String, Integer> sPermissions = new Hashtable<String, Integer>();

    public static boolean hasPermission(final String permission) {
            if (/*!sPermissions.containsKey(permission)
                    || sPermissions.get(permission) == PackageManager.PERMISSION_DENIED*/true) {//bug498552
                final Context context = Factory.get().getApplicationContext();
                final int permissionState = context.checkSelfPermission(permission);
                sPermissions.put(permission, permissionState);
            }
            return sPermissions.get(permission) == PackageManager.PERMISSION_GRANTED;
        
    }

    public static boolean hasPermission(final String permission ,final Context context) {
        if (OsUtil.isAtLeastM()) {
            // It is safe to cache the PERMISSION_GRANTED result as the process gets killed if the
            // user revokes the permission setting. However, PERMISSION_DENIED should not be
            // cached as the process does not get killed if the user enables the permission setting.
            if (/*!sPermissions.containsKey(permission)
                    || sPermissions.get(permission) == PackageManager.PERMISSION_DENIED*/true) {//bug498552
                final int permissionState = context.checkSelfPermission(permission);
                sPermissions.put(permission, permissionState);
            }
            return sPermissions.get(permission) == PackageManager.PERMISSION_GRANTED;
        } else {
            return true;
        }
    }

    /** Does the app have all the specified permissions */
    public static boolean hasPermissions(final String[] permissions , final Context context) {
        for (final String permission : permissions) {
            if (!hasPermission(permission,context)) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasRequiredPermissions(final Context context) {
        return hasPermissions(sRequiredPermissions,context);
    }

    /** Does the app have all the specified permissions */
    public static boolean hasPermissions(final String[] permissions) {
        for (final String permission : permissions) {
            if (!hasPermission(permission)) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasPhonePermission() {
        return hasPermission(Manifest.permission.READ_PHONE_STATE);
    }

    public static boolean hasSmsPermission() {
        return hasPermission(Manifest.permission.READ_SMS);
    }

    public static boolean hasLocationPermission() {
        return OsUtil.hasPermission(Manifest.permission.ACCESS_FINE_LOCATION);
    }


    public static boolean hasStoragePermission() {
        // Note that READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are granted or denied
        // together.
        return OsUtil.hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    public static boolean hasRecordAudioPermission() {
        return OsUtil.hasPermission(Manifest.permission.RECORD_AUDIO);
    }

    public static String[] getMissingPermissions(final String[] permissions) {
        final ArrayList<String> missingList = new ArrayList<String>();
        for (final String permission : permissions) {
            if (!hasPermission(permission)) {
                missingList.add(permission);
            }
        }

        final String[] missingArray = new String[missingList.size()];
        missingList.toArray(missingArray);
        return missingArray;
    }

    private static String[] sRequiredPermissions = new String[] {
        // Required to read existing SMS threads
        Manifest.permission.READ_SMS,
        // Required for knowing the phone number, number of SIMs, etc.
        Manifest.permission.READ_PHONE_STATE,
        // This is not strictly required, but simplifies the contact picker scenarios
        Manifest.permission.READ_CONTACTS,
        Manifest.permission.RECEIVE_WAP_PUSH,
        // spread: add permission for share picture
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_SMS,
        Manifest.permission.RECEIVE_SMS,
        Manifest.permission.RECEIVE_MMS,
        Manifest.permission.SEND_SMS,
        Manifest.permission.RECEIVE_WAP_PUSH,
        Manifest.permission.CALL_PHONE,
        Manifest.permission.WRITE_CONTACTS,
        Manifest.permission.MANAGE_DOCUMENTS,
    };

    /** Does the app have the minimum set of permissions required to operate. */
    public static boolean hasRequiredPermissions() {
        return hasPermissions(sRequiredPermissions);
    }

    public static String[] getMissingRequiredPermissions() {
        return getMissingPermissions(sRequiredPermissions);
    }

    public static void getStoragePreminssion(Activity activity) {
         activity.requestPermissions(
             new String[] { Manifest.permission.READ_EXTERNAL_STORAGE ,Manifest.permission.WRITE_EXTERNAL_STORAGE},0);
    }
    
    public static void requestMissingPermission(Activity act){
        act.requestPermissions(getMissingRequiredPermissions(),0);
    }


评论