package example.emas.com.myweexpeoject;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
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.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author deadline
* @time 2016-10-28
* @usage android >=M 的权限申请统一处理
* <p>
* notice:
* 很多手机对原生系统做了修改,比如小米4的6.0的shouldShowRequestPermissionRationale
* 就一直返回false,而且在申请权限时,如果用户选择了拒绝,则不会再弹出对话框了, 因此有了
* void doAfterDenied(String... permission);
*/
public class PermissionHelper1 {
private static final int REQUEST_PERMISSION_CODE = 1000;
private Activity mContext;
private PermissionListener mListener;
private static List<String> hintMessageList;
private static List<String> shouldrequestPermission;
private static List<String> shouldrequestPermission1;
String message1 = "";
String message2 = "";
public PermissionHelper1(@NonNull Activity object) {
checkCallingObjectSuitability(object);
this.mContext = object;
}
/**
* 权限授权申请
*
* @param hintMessages 要申请的权限的提示
* @param permissions 要申请的权限
* @param listener 申请成功之后的callback
*/
public void requestPermissions(@NonNull String[] hintMessages,
@Nullable PermissionListener listener,
@NonNull String... permissions) {
if (listener != null) {
mListener = listener;
}
shouldrequestPermission = new ArrayList<>();//没有权限
shouldrequestPermission1 = new ArrayList<>();//需要动态申请的权限
hintMessageList = new ArrayList<>();//需要申请的权限提示语
//没全部权限
if (!hasPermissions(mContext, permissions, hintMessages)) {
shouldrequestPermission1 = getPermissionType(shouldrequestPermission);
if (shouldrequestPermission.size() > shouldrequestPermission1.size()) {
//手动去设置中授予权限
showMessageOKCancel2(message2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//跳转到设置
// Intent intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
// mContext.startActivity(intent);
Uri packageURI = Uri.parse("package:" + "example.emas.com.myweexpeoject");
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
mContext.startActivity(intent);
dialog.dismiss();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
} else {
final String[] objects = shouldrequestPermission1.toArray(new String[shouldrequestPermission1.size()]);
showMessageOKCancel1(message1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
executePermissionsRequest(mContext, objects,
REQUEST_PERMISSION_CODE);
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
} else {
if (mListener != null) { //有全部权限
mListener.doAfterGrand();
}
}
}
/**
* 将没有开通的权限进行分类(决绝的,可申请的)
*
* @param shouldrequestPermission
*/
private List<String> getPermissionType(List<String> shouldrequestPermission) {
int j = 0;
for (int i = 0; i < shouldrequestPermission.size(); i++) {
boolean shouldShowRationale = shouldShowRequestPermissionRationale(mContext, shouldrequestPermission.get(i));
if (shouldShowRationale) {
//没有内决绝(拒绝+不在提示),直接申请权限即可
shouldrequestPermission1.add(shouldrequestPermission.get(i));
message1 = message1 + " " + hintMessageList.get(i);
} else {
message2 = message2 + " " + hintMessageList.get(i);
}
}
return shouldrequestPermission1;
}
/**
* 处理onRequestPermissionsResult
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public void handleRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_PERMISSION_CODE:
boolean allGranted = true;
for (int grant : grantResults) {
if (grant != PackageManager.PERMISSION_GRANTED) {
allGranted = false;
break;
}
}
if (allGranted && mListener != null) {
mListener.doAfterGrand();
} else if (!allGranted && mListener != null) {
mListener.doAfterDenied();
}
break;
}
}
/**
* 判断是否具有某权限
*
* @param object
* @param perms
* @return
*/
public static boolean hasPermissions(@NonNull Object object, @NonNull String[] perms, @NonNull String[] hintMessage) {
boolean isAllGranted = true;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
int j = 0;
for (int i = 0; i < perms.length; i++) {
boolean hasPerm = (ContextCompat.checkSelfPermission(getActivity(object), perms[i]) ==
PackageManager.PERMISSION_GRANTED);
if (!hasPerm) {
shouldrequestPermission.add(perms[i]);
hintMessageList.add(hintMessage[i]);
isAllGranted = false;
}
}
return isAllGranted;
}
public void showMessageOKCancel1(String message, DialogInterface.OnClickListener okListener, DialogInterface.OnClickListener cancleListener) {
new AlertDialog.Builder(getActivity(mContext))
.setMessage("请授权" + message + "权限,否则无法使用该功能")
.setPositiveButton("确定", okListener)
.setNegativeButton("取消", cancleListener)
.setCancelable(false)
.create()
.show();
}
public void showMessageOKCancel2(String message, DialogInterface.OnClickListener okListener, DialogInterface.OnClickListener cancleListener) {
new AlertDialog.Builder(getActivity(mContext))
.setMessage("请手动在设置中授权" + message + "权限,否则无法使用此功能")
.setPositiveButton("确定", okListener)
.setNegativeButton("取消", cancleListener)
.setCancelable(false)
.create()
.show();
}
/**
* 兼容fragment
*
* @param object
* @param perm
* @return
*/
@TargetApi(23)
private static boolean shouldShowRequestPermissionRationale(@NonNull Object object, @NonNull String perm) {
if (object instanceof Activity) {
return ActivityCompat.shouldShowRequestPermissionRationale((Activity) object, perm);
} else if (object instanceof Fragment) {
return ((Fragment) object).shouldShowRequestPermissionRationale(perm);
} else if (object instanceof Fragment) {
return ((Fragment) object).shouldShowRequestPermissionRationale(perm);
} else {
return false;
}
}
/**
* 执行申请,兼容fragment
*
* @param object
* @param perms
* @param requestCode
*/
@TargetApi(23)
private void executePermissionsRequest(@NonNull Object object, @NonNull String[] perms, int requestCode) {
if (object instanceof Activity) {
ActivityCompat.requestPermissions((Activity) object, perms, requestCode);
} else if (object instanceof android.support.v4.app.Fragment) {
((android.support.v4.app.Fragment) object).requestPermissions(perms, requestCode);
} else if (object instanceof Fragment) {
((Fragment) object).requestPermissions(perms, requestCode);
}
}
/**
* 检查传递Context是否合法
*
* @param object
*/
private void checkCallingObjectSuitability(@Nullable Object object) {
if (object == null) {
throw new NullPointerException("Activity or Fragment should not be null");
}
boolean isActivity = object instanceof Activity;
boolean isSupportFragment = object instanceof android.support.v4.app.Fragment;
boolean isAppFragment = object instanceof Fragment;
if (!(isSupportFragment || isActivity || (isAppFragment && isNeedRequest()))) {
if (isAppFragment) {
throw new IllegalArgumentException(
"Target SDK needs to be greater than 23 if caller is android.app.Fragment");
} else {
throw new IllegalArgumentException("Caller must be an Activity or a Fragment.");
}
}
}
@TargetApi(11)
private static Activity getActivity(@NonNull Object object) {
if (object instanceof Activity) {
return ((Activity) object);
} else if (object instanceof android.support.v4.app.Fragment) {
return ((android.support.v4.app.Fragment) object).getActivity();
} else if (object instanceof Fragment) {
return ((Fragment) object).getActivity();
} else {
return null;
}
}
public static boolean isNeedRequest() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}
public interface PermissionListener {
void doAfterGrand();
void doAfterDenied();
}
}
权限请求工具类
最新推荐文章于 2024-06-12 14:58:43 发布