content: package cn.com.superLei.aoparms.common.permission;
  
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.net.Uri;
  import android.os.Build;
  import android.provider.Settings;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.util.SimpleArrayMap;
  import android.util.SparseArray;
  
  import cn.com.superLei.aoparms.R;
  
  /**
   * Created by jerry on 2018/6/13.
   */
  
  public class AopPermissionUtils {
  
      private static final SimpleArrayMap<String, Integer> MIN_SDK_PERMISSIONS;
  
      static {
          MIN_SDK_PERMISSIONS = new SimpleArrayMap<>(8);
          MIN_SDK_PERMISSIONS.put("com.android.voicemail.permission.ADD_VOICEMAIL", 14);
          MIN_SDK_PERMISSIONS.put("android.permission.BODY_SENSORS", 20);
          MIN_SDK_PERMISSIONS.put("android.permission.READ_CALL_LOG", 16);
          MIN_SDK_PERMISSIONS.put("android.permission.READ_EXTERNAL_STORAGE", 16);
          MIN_SDK_PERMISSIONS.put("android.permission.USE_SIP", 9);
          MIN_SDK_PERMISSIONS.put("android.permission.WRITE_CALL_LOG", 16);
          MIN_SDK_PERMISSIONS.put("android.permission.SYSTEM_ALERT_WINDOW", 23);
          MIN_SDK_PERMISSIONS.put("android.permission.WRITE_SETTINGS", 23);
      }
      /**
       * 判断是否所有权限都同意了，都同意返回true 否则返回false
       *
       * @param context     context
       * @param permissions permission list
       * @return return true if all permissions granted else false
       */
      public static boolean hasSelfPermissions(Context context, String... permissions) {
          for (String permission : permissions) {
              if (permissionExists(permission) && !hasSelfPermission(context, permission)) {
                  return false;
              }
          }
          return true;
      }
  
      /**
       * 判断单个权限是否同意
       *
       * @param context    context
       * @param permission permission
       * @return return true if permission granted
       */
      private static boolean hasSelfPermission(Context context, String permission) {
          return ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
      }
  
      /**
       * 判断权限是否存在
       *
       * @param permission permission
       * @return return true if permission exists in SDK version
       */
      private static boolean permissionExists(String permission) {
          Integer minVersion = MIN_SDK_PERMISSIONS.get(permission);
          return minVersion == null || Build.VERSION.SDK_INT >= minVersion;
      }
  
      /**
       * 检查是否都赋予权限
       *
       * @param grantResults grantResults
       * @return 所有都同意返回true 否则返回false
       */
      public static boolean verifyPermissions(int... grantResults) {
          if (grantResults.length == 0) return false;
          for (int result : grantResults) {
              if (result != PackageManager.PERMISSION_GRANTED) {
                  return false;
              }
          }
          return true;
      }
  
      /**
       * 检查所给权限List是否需要给提示
       *
       * @param activity    Activity
       * @param permissions 权限list
       * @return 如果某个权限需要提示则返回true
       */
      public static boolean shouldShowRequestPermissionRationale(Activity activity, String... permissions) {
          for (String permission : permissions) {
              if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
                  return true;
              }
          }
          return false;
      }
  
      public static void showGoSetting(Activity activity, String rationale, String positiveButtonText, String NegativeButtonText){
          new AlertDialog.Builder(activity)
                  .setMessage(rationale)
                  .setPositiveButton(positiveButtonText, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.dismiss();
                          go2Setting(activity);
                      }
                  })
                  .setNegativeButton(NegativeButtonText, new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.dismiss();
                      }
                  })
                  .create().show();
      }
  
      public static void showGoSetting(Activity activity, String rationale){
          showGoSetting(activity, rationale, activity.getString(R.string.toSetting), activity.getString(R.string.btn_cancel));
      }
  
      /**
       * 跳转到设置页面
       * @param context
       */
      public static void go2Setting(Context context) {
          Intent intent = new Intent();
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
          intent.setData(Uri.fromParts("package", context.getPackageName(), null));
          context.startActivity(intent);
      }
  
  }
  