package top.lyoun.myusb;
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.provider.Settings; import android.support.v4.app.ActivityCompat;
/** * 我自己的动态权限组,用于android6.0以上,并适配android8.0以上动态权限申请 */ public class MyPermissionsUtil { //以下都是需要动态申请的权限组
//STORAGE 存储卡文件读写权限组 public final static int requestCode_storage = 100; public static String permissions_storage[] = new String[]{ "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE" };
//MICROPHONE 麦克风权限组 public final static int requestCode_audio = 101; public static String permissions_audio[] = new String[]{ "android.permission.RECORD_AUDIO" };
//CAMERA 相机权限组 public final static int requestCode_camera = 102; public static String permissions_camera[] = new String[]{ "android.permission.CAMERA" };
//LOCATION 位置权限组 public final static int requestCode_location = 103; public static String permissions_location[] = new String[]{ "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COASE_LOCATION" };
//PHONE 手机权限组 public final static int requestCode_phone = 104; public static String permissions_phone[] = new String[]{ "android.permission.READ_PHONE_STATE", "android.permission.CALL_PHONE", "android.permission.READ_CALL_LOG", "android.permission.WRITE_CALL_LOG" };
//CALENDAR 日历权限组 public final static int requestCode_calendar = 105; public static String permissions_calendar[] = new String[]{ "android.permission.READ_CALENDAR", "android.permission.WRITE_CALENDAR" };
//CONTACTS 联系人权限组 public final static int requestCode_contacts = 106; public static String permissions_contacts[] = new String[]{ "android.permission.READ_CONTACTS", "android.permission.WRITE_CONTACTS", "android.permission.GET_ACCOUNTS" };
//SMS 短信权限组 public final static int requestCode_sms = 107; public static String permissions_sms[] = new String[]{ "android.permission.SEND_SMS", "android.permission.RECEIVE_SMS", "android.permission.READ_SMS", "android.permission.RECEIVE_WAP_PUSH" };
//BODY_SENSORS 传感器权限组 public final static int requestCode_sensors = 108; public static String permissions_sensors[] = new String[]{ "android.permission.BODY_SENSORS" };
public final static int requestCode_all = 109; public static int granted = PackageManager.PERMISSION_GRANTED;
private Activity mActivity; private int mRequestCode;
//mustGrantedPermiss,必须要授予的权限,如果没有授予这些权限,则提示用户必须要授权后,app才能正常用 public static String[] mustGrantedPermiss = Splicing(permissions_storage, permissions_audio , permissions_camera, permissions_location, permissions_phone, permissions_calendar, permissions_contacts, permissions_sms, permissions_sensors );
public void checkPermissions(Activity activity, String[] needChecks, int requestCode) { this.mActivity = activity; this.mRequestCode = requestCode;
if (needChecks != null && needChecks.length > 0) { //权限检查,只有android6.0及其以上才需要动态权限检查 if (android.os.Build.VERSION.SDK_INT > 22) { for (int i = 0; i < needChecks.length; i++) { if (mActivity.checkSelfPermission(needChecks[i]) != granted) { System.out.println("您缺少" + needChecks[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 ActivityCompat.requestPermissions(mActivity, needChecks, requestCode); break; } } } } }
boolean storagesGranted = false; //表明该权限是否已被授权 boolean audiosGranted = false; boolean camerasGranted = false; boolean locationsGranted = false; boolean phonesGranted = false; boolean calendarsGranted = false; boolean contactsGranted = false; boolean smsGranted = false; boolean sensorsGranted = false;
String[] checkStorages = null; //表明客户端有没有请求授予该权限 String[] checkAudios = null; String[] checkCameras = null; String[] checkLocations = null; String[] checkPhones = null; String[] checkCalendars = null; String[] checkContacts = null; String[] checkSms = null; String[] checkSensors = null;
int storagesDenyed = 0; //表明当前权限有没有被用户拒绝 int audiosDenyed = 0; int camerasDenyed = 0; int locationsDenyed = 0; int phonesDenyed = 0; int calendarsDenyed = 0; int contactsDenyed = 0; int smsDenyed = 0; int sensorsDenyed = 0;
public static int compareCount = 0; public static int needCompareCount = 0;
/** * @param activity * @param checkStorages //存储卡文件读写权限组 * @param checkAudios //麦克风录音权限 * @param checkCameras //相机权限组 * @param checkLocations //位置权限组 * @param checkPhones //手机权限组 * @param checkCalendars //日历权限组 * @param checkContacts //联系人权限组 * @param checkSms //短信权限组 * @param checkSensors //传感器权限组 */ public void checkMorePermissions(Activity activity, String[] checkStorages, String[] checkAudios, String[] checkCameras, String[] checkLocations, String[] checkPhones, String[] checkCalendars, String[] checkContacts, String[] checkSms, String[] checkSensors) { this.mActivity = activity; this.checkStorages = checkStorages; this.checkAudios = checkAudios; this.checkCameras = checkCameras; this.checkLocations = checkLocations; this.checkPhones = checkPhones; this.checkCalendars = checkCalendars; this.checkContacts = checkContacts; this.checkSms = checkSms; this.checkSensors = checkSensors;
//把数据初始化 storagesDenyed = 0; audiosDenyed = 0; camerasDenyed = 0; locationsDenyed = 0; phonesDenyed = 0; calendarsDenyed = 0; contactsDenyed = 0; smsDenyed = 0; sensorsDenyed = 0;
compareCount = 0; needCompareCount = 0;
if(checkStorages != null && checkStorages.length > 0){ needCompareCount++; } if(checkAudios != null && checkAudios.length > 0){ needCompareCount++; } if(checkCameras != null && checkCameras.length > 0){ needCompareCount++; } if(checkLocations != null && checkLocations.length > 0){ needCompareCount++; } if(checkPhones != null && checkPhones.length > 0){ needCompareCount++; } if(checkCalendars != null && checkCalendars.length > 0){ needCompareCount++; } if(checkContacts != null && checkContacts.length > 0){ needCompareCount++; } if(checkSms != null && checkSms.length > 0){ needCompareCount++; } if(checkSensors != null && checkSensors.length > 0){ needCompareCount++; }
//权限检查,只有android6.0及其以上才需要动态权限检查 if (android.os.Build.VERSION.SDK_INT > 22) {
try { if (checkStorages != null && checkStorages.length > 0) { int i = 0; for (; i < checkStorages.length; i++) { if (mActivity.checkSelfPermission(checkStorages[i]) != granted) { System.out.println("您缺少" + checkStorages[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 storagesGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkStorages, requestCode_storage); break; } } if (i == checkStorages.length) { storagesGranted = true; if(needCompareCount > 0){ needCompareCount--; } } }
if (checkAudios != null && checkAudios.length > 0 && checkStorages != null && (storagesGranted || !(checkStorages != null && checkStorages.length > 0))) {// int i = 0; for (; i < checkAudios.length; i++) { if (mActivity.checkSelfPermission(checkAudios[i]) != granted) { System.out.println("您缺少" + checkAudios[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 audiosGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkAudios, requestCode_audio);
break; } } if (i == checkAudios.length) { audiosGranted = true; if(needCompareCount > 0){ needCompareCount--; } } } if (checkCameras != null && checkCameras.length > 0 && (storagesGranted || !(checkStorages != null && checkStorages.length > 0)) && (audiosGranted || !(checkAudios != null && checkAudios.length > 0))) { int i = 0; for (; i < checkCameras.length; i++) { if (mActivity.checkSelfPermission(checkCameras[i]) != granted) { System.out.println("您缺少" + checkCameras[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 camerasGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkCameras, requestCode_camera); break; } } if (i == checkCameras.length) { camerasGranted = true; if(needCompareCount > 0){ needCompareCount--; } } } if (checkLocations != null && checkLocations.length > 0 && !locationsGranted && (storagesGranted || !(checkStorages != null && checkStorages.length > 0)) && (audiosGranted || !(checkAudios != null && checkAudios.length > 0)) && (camerasGranted || !(checkCameras != null && checkCameras.length > 0))) { int i = 0; for (; i < checkLocations.length; i++) { System.out.println("location: " + i + " : " + checkLocations[i] + " ," + mActivity.checkSelfPermission(checkLocations[i])); if (mActivity.checkSelfPermission(checkLocations[i]) == granted) { //requestCode 请求码,表明当前是对哪个权限的请求 locationsGranted = true; if(needCompareCount > 0){ needCompareCount--; } break; } }
if (i == checkLocations.length && !locationsGranted) { System.out.println("why location: " + i + " ," + checkLocations.length + " ," + locationsGranted); locationsGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkLocations, requestCode_location); } } if (checkPhones != null && checkPhones.length > 0 && !phonesGranted && (storagesGranted || !(checkStorages != null && checkStorages.length > 0)) && (audiosGranted || !(checkAudios != null && checkAudios.length > 0)) && (camerasGranted || !(checkCameras != null && checkCameras.length > 0)) && (locationsGranted || !(checkLocations != null && checkLocations.length > 0))) {// int i = 0; for (; i < checkPhones.length; i++) { if (mActivity.checkSelfPermission(checkPhones[i]) != granted) { System.out.println("您缺少" + checkPhones[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 phonesGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkPhones, requestCode_phone); break; } } if (i == checkPhones.length) { phonesGranted = true; if(needCompareCount > 0){ needCompareCount--; } } } if (checkCalendars != null && checkCalendars.length > 0 && !calendarsGranted && (storagesGranted || !(checkStorages != null && checkStorages.length > 0)) && (audiosGranted || !(checkAudios != null && checkAudios.length > 0)) && (camerasGranted || !(checkCameras != null && checkCameras.length > 0)) && (locationsGranted || !(checkLocations != null && checkLocations.length > 0)) && (phonesGranted || !(checkPhones != null && checkPhones.length > 0))) {//&& (phonesGranted || !(checkPhones != null && checkPhones.length > 0)) int i = 0; for (; i < checkCalendars.length; i++) { if (mActivity.checkSelfPermission(checkCalendars[i]) != granted) { System.out.println("您缺少" + checkCalendars[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 calendarsGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkCalendars, requestCode_calendar); break; } } if (i == checkCalendars.length) { calendarsGranted = true; if(needCompareCount > 0){ needCompareCount--; } } } if (checkContacts != null && checkContacts.length > 0 && !contactsGranted && (storagesGranted || !(checkStorages != null && checkStorages.length > 0)) && (audiosGranted || !(checkAudios != null && checkAudios.length > 0)) && (camerasGranted || !(checkCameras != null && checkCameras.length > 0)) && (locationsGranted || !(checkLocations != null && checkLocations.length > 0)) && (phonesGranted || !(checkPhones != null && checkPhones.length > 0)) && (calendarsGranted || !(checkCalendars != null && checkCalendars.length > 0))) {// int i = 0; for (; i < checkContacts.length; i++) { if (mActivity.checkSelfPermission(checkContacts[i]) != granted) { System.out.println("您缺少" + checkContacts[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 contactsGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkContacts, requestCode_contacts); break; } } if (i == checkContacts.length) { contactsGranted = true; if(needCompareCount > 0){ needCompareCount--; } } } if (checkSms != null && checkSms.length > 0 && !smsGranted && (storagesGranted || !(checkStorages != null && checkStorages.length > 0)) && (audiosGranted || !(checkAudios != null && checkAudios.length > 0)) && (camerasGranted || !(checkCameras != null && checkCameras.length > 0)) && (locationsGranted || !(checkLocations != null && checkLocations.length > 0)) && (phonesGranted || !(checkPhones != null && checkPhones.length > 0)) && (calendarsGranted || !(checkCalendars != null && checkCalendars.length > 0)) && (contactsGranted || !(checkContacts != null && checkContacts.length > 0))) {// int i = 0; for (; i < checkSms.length; i++) { if (mActivity.checkSelfPermission(checkSms[i]) != granted) { System.out.println("您缺少" + checkSms[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 smsGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkSms, requestCode_sms); break; } } if (i == checkSms.length) { smsGranted = true; if(needCompareCount > 0){ needCompareCount--; } } } if (checkSensors != null && checkSensors.length > 0 && !sensorsGranted && (storagesGranted || !(checkStorages != null && checkStorages.length > 0)) && (audiosGranted || !(checkAudios != null && checkAudios.length > 0)) && (camerasGranted || !(checkCameras != null && checkCameras.length > 0)) && (locationsGranted || !(checkLocations != null && checkLocations.length > 0)) && (phonesGranted || !(checkPhones != null && checkPhones.length > 0)) && (calendarsGranted || !(checkCalendars != null && checkCalendars.length > 0)) && (contactsGranted || !(checkContacts != null && checkContacts.length > 0)) && (smsGranted || !(checkSms != null && checkSms.length > 0))) {// int i = 0; for (; i < checkSensors.length; i++) { if (mActivity.checkSelfPermission(checkSensors[i]) != granted) { System.out.println("您缺少" + checkSensors[i] + "权限,需要动态添加权限"); //requestCode 请求码,表明当前是对哪个权限的请求 sensorsGranted = false; //needCompareCount++; ActivityCompat.requestPermissions(mActivity, checkSensors, requestCode_sensors); break; } } if (i == checkSensors.length) { sensorsGranted = true; if(needCompareCount > 0){ needCompareCount--; } } } } catch (Exception e) { e.printStackTrace(); } } }
/** * 不再提示权限时的展示对话框 */ AlertDialog mPermissionDialog;
public void showSystemPermissionsSettingDialog(String tips) { final String mPackName = mActivity.getPackageName(); if (mPermissionDialog == null) { mPermissionDialog = new AlertDialog.Builder(mActivity) .setMessage(tips).setCancelable(false) .setPositiveButton("设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { cancelPermissionDialog(); Uri packageURI = Uri.parse("package:" + mPackName); Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI); mActivity.startActivity(intent); mActivity.finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //关闭页面或者做其他操作 cancelPermissionDialog(); } }) .create(); } mPermissionDialog.show(); }
//关闭对话框 private void cancelPermissionDialog() { if (mPermissionDialog != null) { mPermissionDialog.cancel(); mPermissionDialog = null; if(mActivity != null){ mActivity.finish(); } } }
//把两个数组拼接到一起 public static String[] Splicing(String[] checks1, String[] checks2) { String[] checks = new String[0]; if (checks1 == null && checks2 == null) { return null; } else if (checks1 == null) { checks = checks2; } else if (checks2 == null) { checks = checks1; } else { int len1 = checks1.length; int len2 = checks2.length; int len = len1 + len2; checks = new String[len]; for (int i = 0; i < len; i++) { if (i < len1) { checks[i] = checks1[i]; } else { checks[i] = checks2[i - len1]; } } } return checks; }
//把任意多个数组拼接到一起 public static String[] Splicing(String[] checks1, String[]... args) { String[] checks = new String[0]; if (checks1 == null && args == null) { return null; } else if (args == null) { checks = checks1; } else { if (checks1 != null) { int len1 = checks1.length; checks = new String[len1]; int j = 0; for (; j < checks.length; j++) { checks[j] = checks1[j]; } } for (int i = 0; i < args.length; i++) { String[] arg = args[i]; if (arg != null && arg.length > 0) { String[] temp = checks; checks = new String[checks.length + arg.length]; for (int k = 0; k < checks.length; k++) { if (k < temp.length) { checks[k] = temp[k]; } else { checks[k] = arg[k - temp.length]; } } } } } return checks; }
public static boolean getGranted(String[] permissions, int[] grantResults) { boolean granted = false; if (permissions != null && grantResults != null && permissions.length == grantResults.length) { int i = 0; for (; i < grantResults.length; i++) { if (grantResults[i] != PackageManager.PERMISSION_GRANTED) { granted = false; break; } } if (i == grantResults.length) { granted = true; } } return granted; }
} |