实现一个使用最简单的权限框架

2018-11-28  本文已影响0人  vpractical

[TOC]

这个已经在用了,记录下过程
GitHub地址

使用方式

1.在BaseActivity,BaseFragment中加入:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionCat.onRequestPermissionsResult(this,permissions, grantResults);
    }

注意:第一个参数是当前activity/fragment的对象

2.使用方式有注解or回调两种

    @PermCat(value = PERMISSION_CAMERA)
    public void btn11() {
        if(PermissionCat.has(this,PERMISSION_CAMERA)){
            //TODO
        }else{
            PermissionCat.request("拍照用",this,null,PERMISSION_CAMERA);
        }
    }
public void btn12() {
        if(PermissionCat.has(this,PERMISSION_CAMERA)){
            //TODO
        }else{
            PermissionCat.request("拍照用", this, new PermissionCallback() {
                @Override
                public void onGranted(String[] permissions, List<String> granted) {
                    if(!granted.isEmpty() && PERMISSION_CAMERA.equals(granted.get(0))){
                        btn12();
                    }
                }

                @Override
                public void onDenied(String[] permissions, List<String> denied) {
                }
            }, PERMISSION_CAMERA);
        }
    }
Android权限框架流程图.png

实现方式

包含的类

是否已授权权限

PermissionCat.has(this,PERMISSION_CAMERA)

1.PermissionCat中需要有has()方法,判断是否已授权该权限

    public static boolean has(Object object, String... perms) {
        if (!checkValid(object)) {
            throw new NullPointerException("PermissionCat -> object must be Activity or Fragment");
        }
        return instance.mHelper.has(object, perms);
    }

2.具体判断过程在代理类helper中实现,最终调用系统api判断

    /**
     * 判断是否允许了全部权限,没有全部允许就返回false
     * @param object
     * @param perms
     * @return
     */
    protected boolean has(Object object, String... perms){
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            return true;
        }
        for (String perm : perms) {
            if(ActivityCompat.checkSelfPermission(target(object),perm) != PackageManager.PERMISSION_GRANTED){
                return false;
            }
        }
        return true;
    }
    

3.用户传入的上下文已Object接收,可能有3种情况

    protected Activity target(Object object) {
        Activity activity;
        if (object instanceof Fragment) {
            activity = ((Fragment) object).getActivity();
        } else if (object instanceof android.app.Fragment) {
            activity = ((android.app.Fragment) object).getActivity();
        } else {
            activity = (Activity) object;
        }

        if (activity == null) {
            throw new IllegalStateException("the result of type conversion is null");
        }
        return activity;
    }

判断完成

申请权限

1.外部调用申请方法request(),在PermissionCat中
2.申请的时候判断是否已经授权了,如果已授权就直接通知授权成功下发申请结果,否则代理类执行申请方法

/**
     * 重载的请求授权方法,传入监听器时,不会回调注解方法
     * @param reason 要求打开权限的原因
     * @param object 上下文,fragment调用时需要传入fragment对象V
     * @param callback
     * @param perms
     */
    public static void request(String reason, Object object, PermissionCallback callback, String... perms) {
        if (!checkValid(object)) {
            throw new NullPointerException("PermissionCat -> object must be Activity or Fragment");
        }
        instance.objectName = object.getClass().getName();
        instance.mPermissionCallback = callback;
        instance.reason = reason;
        if (has(object, perms)) {
            instance.notifyResult(object, perms);
            return;
        }

        instance.mHelper.request(object, perms);
    }

3.最终也是调用系统api完成,has()时只接受boolean结果,所以都可以用activity对象调用,这里要接收授权结果,所以已传入的上下文对象调用api

    /**
     * 区分activity、fragment中操作的回调
     * @param object
     * @param perms
     */
    protected void request(Object object,String... perms){
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            return;
        }
        int requestCode = (int) (Math.random() * 100);
        if (object instanceof Fragment) {
            ((Fragment) object).requestPermissions(perms,requestCode);
        } else if (object instanceof android.app.Fragment) {
            ((android.app.Fragment) object).requestPermissions(perms,requestCode);
        } else {
            ((Activity) object).requestPermissions(perms,requestCode);
        }
    }

4.在使用者的封装的BaseActivity和BaseFragment中调用方法,将系统api的授权结果传递到PermissionCat,

    PermissionCat.onRequestPermissionsResult(this,permissions, grantResults);

    /**
     * 授予/禁止权限后的回调
     *
     * @param object
     * @param permissions
     * @param grantResults
     */
    public static void onRequestPermissionsResult(Object object, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (!instance.objectName.equals(object.getClass().getName())) return;
        List<String> granted = new ArrayList<>();
        List<String> denied = new ArrayList<>();

        for (int i = 0; i < permissions.length; i++) {
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                granted.add(permissions[i]);
            } else {
                denied.add(permissions[i]);
            }
        }

        if (!denied.isEmpty() && instance.mHelper.noAsk(object, denied)) {
            //有权限被拒绝并不再询问
            instance.showAskSetting(object, permissions);
        } else {
            instance.setPermissionResult(object, permissions, granted, denied);
        }
    }

5.授权结果中包含所有申请的权限和已经授权的权限,判断拒绝的权限,是否被不再询问,需要一个拒绝权限列表,所以我们将权限分为授权granted,拒绝denied两个集合。

6.1.如果顺利进行,那么开始下发这次申请的结果:

private void setPermissionResult(Object object, String[] permissions, List<String> granted, List<String> denied) {
        if (!granted.isEmpty() && mPermissionCallback != null) {
            //授予回调
            mPermissionCallback.onGranted(permissions, granted);
        }

        if (!denied.isEmpty() && mPermissionCallback != null) {
            //拒绝回调
            mPermissionCallback.onDenied(permissions, denied);
        }

        if (denied.isEmpty() && mPermissionCallback == null) {
            //全部授予,调用注解方法
            reflectMethod(object, permissions);
        }
    }

6.2.使用注解时反射调用注解方法:

    /**
     * 反射调用注解的使用权限的方法
     *
     * @param permissions
     */
    private void reflectMethod(Object object, String[] permissions) {
        Class clz = object.getClass();
        Method[] methods = clz.getDeclaredMethods();

        for (Method m : methods) {
            if (m.isAnnotationPresent(PermCat.class)) {
                Annotation[] annotations = m.getDeclaredAnnotations();
                PermCat permCat = null;
                for (Annotation a : annotations) {
                    if (a instanceof PermCat) {
                        permCat = (PermCat) a;
                        break;
                    }
                }
                if (permCat == null) continue;
                //检测注解,防止注解重名
                if (Arrays.asList(permissions).contains(permCat.value())) {
                    if (m.isAccessible()) {
                        //如果方法是私有,设置为可以访问,否则无法反射访问
                        m.setAccessible(true);
                    }

                    try {
                        m.invoke(object);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

7.1.被拒绝的权限组denied,在代理类helper中判断是否有被选择不再询问的,也是调用系统api完成

    /**
     * 是否对某个权限选择拒绝并不再询问
     * @param object
     * @param perms
     * @return
     */
    protected boolean noAsk(Object object,List<String> perms){
        for (String perm:perms) {
            if(!ActivityCompat.shouldShowRequestPermissionRationale(target(object),perm)){
                return true;
            }
        }
        return false;
    }

7.2.如果有被不再询问的权限,那么弹出dialog,询问是否去设置页

    /**
     * 权限被禁用,弹出询问是否去设置权限的对话框
     *
     * @param object
     * @param perms
     */
    private void showAskSetting(Object object, String... perms) {
        askPerms = perms;
        AppSettingDialog.Builder builder = new AppSettingDialog.Builder(object);
        builder
                .title("权限申请")
                .content(reason == null ? "应用需要该权限" : reason)
                .cancelStr("取消")
                .confirmStr("去设置")
                .build()
                .show();
    }

看着是show了一个dialog,实际上是打开了一个无界面activity,再弹窗,并接收设置页结果,最终回调到PermissionCat中

/**
     * 设置页面退后回调
     *
     * @param object
     */
    protected void onActivityResult(Object object) {
        List<String> granted = new ArrayList<>();
        List<String> denied = new ArrayList<>();

        String[] perms = askPerms.clone();
        askPerms = null;

        for (String perm : perms) {
            if (has(object, perm)) {
                granted.add(perm);
            } else {
                denied.add(perm);
            }
        }
        setPermissionResult(object, perms, granted, denied);
    }

7.3分发结果

完成,已测试

上一篇 下一篇

猜你喜欢

热点阅读