admin管理员组

文章数量:1558075

最近写权限图片上传功能用到了相机权限申请,发现vivo,oppo很多机型不管授权与否都返回

PackageManager.PERMISSION_GRANTED(已授权),研究下了下源码发现有个mHasPermission 字段跟授权后的状态有关,已授权则返回true,否则返回false;既然已找到突破口,那处理起来就就简单了,直接上代码:

新建一个权限工具类PermissionUtils 跟rom工具类RomUtils :

package com.app.shanjiang.util;

import android.Manifest;
import android.content.Context;
import android.hardware.Camera;
import android.os.Build;

import java.lang.reflect.Field;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * @Title 权限工具类
 * @Description
 * @Author xuefeng.zhu
 * @Since 2018/9/3
 * @Version 2.7.0
 */
public class PermissionUtils {

    private static Boolean mCameraCanUse = true; //缓存上次的查询结果
    private static Camera mCamera = null;

    /**
     * 检测相机权限
     *
     * @param context
     * @return
     */
    public static boolean hasCameraPermissions(Context context) {
        try {
            if (RomUtils.isOppo() || RomUtils.isVivo()) {
                if (!isCameraCanUse()) {
                    return false;
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!isHasCameraPermission()) {
                        return false;
                    }
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return EasyPermissions.hasPermissions(context, Manifest.permission.CAMERA);
    }

    /**
     * 相机是否可用
     * ps:有些手机即使禁掉拍照权限获取到的camera也不为null(比如魅族,oppoR9s)
     *
     * @return
     */
    public static boolean isCameraCanUse() {
        boolean canUse = true;
        try {
            mCamera = getCamera();
            Camera.Parameters mParameters = mCamera.getParameters();
            mCamera.setParameters(mParameters);
        } catch (Exception e) {
            canUse = false;
        }
        mCameraCanUse = canUse;
        return canUse;
    }

    /**
     * 是否拿到相机权限
     * ps: vivo ,opo手机不管授权与否都会
     * 返回PackageManager.PERMISSION_GRANTED(已授权)故作特殊处理
     *
     * @return
     */

    public static boolean isHasCameraPermission() {
        Field fieldPassword;
        try {
            mCamera = getCamera();
            //通过反射去拿相机是否获得了权限
            fieldPassword = mCamera.getClass().getDeclaredField("mHasPermission");
            fieldPassword.setAccessible(true);
            Boolean result = (Boolean) fieldPassword.get(mCamera);
            if (mCamera != null) {
                mCamera.release();
            }
            mCamera = null;
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            mCamera = null;
            return true;
        }
    }

    /**
     * 相机是否可使用
     *
     * @return
     */
    public static Boolean getCameraCanUse() {
        return mCameraCanUse;
    }

    /**
     * 获取相机实例
     *
     * @return
     */
    public static Camera getCamera() {
        if (mCamera == null) {
            return Camera.open();
        }
        return mCamera;
    }

}
package com.app.shanjiang.util;

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Title Rom工具类
 * @Description
 * @Author xuefeng.zhu
 * @Since 2018/9/3
 * @Version 2.7.0
 */
public class RomUtils {

    private static final String TAG = "Rom";

    public static final String ROM_MIUI = "MIUI";
    public static final String ROM_EMUI = "EMUI";
    public static final String ROM_FLYME = "FLYME";
    public static final String ROM_OPPO = "OPPO";
    public static final String ROM_SMARTISAN = "SMARTISAN";
    public static final String ROM_VIVO = "VIVO";
    public static final String ROM_QIKU = "QIKU";

    private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name";
    private static final String KEY_VERSION_EMUI = "ro.build.version.emui";
    private static final String KEY_VERSION_OPPO = "ro.build.version.opporom";
    private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version";
    private static final String KEY_VERSION_VIVO = "ro.vivo.os.version";

    private static String sName;//当前系统名称
    private static String sVersion;//当前系统版本号

    /**
     * 是否为华为系统
     *
     * @return
     */
    public static boolean isEmui() {
        return check(ROM_EMUI);
    }

    /**
     * 是否为小米系统
     *
     * @return
     */
    public static boolean isMiui() {
        return check(ROM_MIUI);
    }

    /**
     * 是否为vivo系统
     *
     * @return
     */
    public static boolean isVivo() {
        return check(ROM_VIVO);
    }

    /**
     * 是否为oppo系统
     *
     * @return
     */
    public static boolean isOppo() {
        return check(ROM_OPPO);
    }

    /**
     * 是否为魅族系统
     *
     * @return
     */
    public static boolean isFlyme() {
        return check(ROM_FLYME);
    }

    /**
     * 是否为360系统
     *
     * @return
     */
    public static boolean is360() {
        return check(ROM_QIKU) || check("360");
    }

    /**
     * 是否为锤子系统
     *
     * @return
     */
    public static boolean isSmartisan() {
        return check(ROM_SMARTISAN);
    }

    public static String getName() {
        if (sName == null) {
            check("");
        }
        return sName;
    }

    public static String getVersion() {
        if (sVersion == null) {
            check("");
        }
        return sVersion;
    }

    /**
     * 获取手机的rom类型
     *
     * @param rom
     * @return
     */
    public static boolean check(String rom) {
        if (sName != null) {
            return sName.equals(rom);
        }

        if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) {
            sName = ROM_MIUI;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) {
            sName = ROM_EMUI;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) {
            sName = ROM_OPPO;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) {
            sName = ROM_VIVO;
        } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) {
            sName = ROM_SMARTISAN;
        } else {
            sVersion = Build.DISPLAY;
            if (sVersion.toUpperCase().contains(ROM_FLYME)) {
                sName = ROM_FLYME;
            } else {
                sVersion = Build.UNKNOWN;
                sName = Build.MANUFACTURER.toUpperCase();
            }
        }
        return sName.equals(rom);
    }

    public static String getProp(String name) {
        String line = null;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + name);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            Log.e(TAG, "Unable to read prop " + name, ex);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return line;
    }
}

在用到相机权限的地方作相关处理:


    @AfterPermissionGranted(RECOMMEND_CAMERA)
    private void requestRecommend() {
        if (PermissionUtils.hasCameraPermissions(this)) {
            RecommendScanActivity.start(this, REFERENCE_REQUEST_CODE);
        } else {
            if (!(RomUtils.isOppo() || RomUtils.isVivo()))
                EasyPermissions.requestPermissions(this, getString(R.string.scan_permission), RECOMMEND_CAMERA, RECOMMEND_PERMISSION);
        }
    }

这样相相当于对oppo,vivo系列的手机做了特殊处理;到此相机权限兼容完毕;

ps:代码中用到的EasyPermissions是一个github上面的权限申请库;

本文标签: 权限相机vivooppo