Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android13 Adapt media permissions #2429

Closed
mingxu13 opened this issue Jul 22, 2022 · 1 comment
Closed

Android13 Adapt media permissions #2429

mingxu13 opened this issue Jul 22, 2022 · 1 comment

Comments

@mingxu13
Copy link

Current use version?

当前使用的版本是多少?


Will this problem occur in demo?

Demo能否复现这问题?


Describe the problem or provide an error log?

描述问题或提供错误log?


@Petterpx
Copy link

Petterpx commented Oct 28, 2022

如题,不用fork的话这样适配即可,自己拦截权限处理:

/** @author petterp 结合XXPermissions 权限适配simple */
/** 拦截相册库的权限申请流程,simple */
class OnPermissionsInterceptListenerImpl : OnPermissionsInterceptListener {
    override fun requestPermission(
        fragment: Fragment?,
        permissionArray: Array<out String>?,
        call: OnRequestPermissionListener?
    ) {
        if (permissionArray == null || fragment == null || fragment.context == null) return
        if (XXPermissions.isGranted(fragment.context, permissionArray.toCompatPermissions)) {
            call?.onCall(permissionArray, true)
            return
        }
        // 这里去申请自己的权限,可以在此处增加弹窗询问用户是否需要给权限
        XXPermissions.with(fragment)
            .permission(permissionArray.toCompatPermissions)
            .request(OnPermissionCallbackImpl(fragment, permissionArray, call))
    }

    override fun hasPermissions(fragment: Fragment?, permissionArray: Array<out String>?): Boolean {
        if (permissionArray == null || fragment == null) return false
        return XXPermissions.isGranted(
            fragment.context,
            permissionArray.toCompatPermissions
        )
    }
}

/** 拦截相册库的权限拒绝流程 */
class OnPermissionDeniedListenerImpl : OnPermissionDeniedListener {
    override fun onDenied(
        fragment: Fragment?,
        permissionArray: Array<out String>?,
        requestCode: Int,
        call: OnCallbackListener<Boolean>?
    ) {
        if (fragment == null || permissionArray == null || fragment.context == null) return
        // 这里即未获取到权限时,合格的流程这里应该询问用户是否需要前往设置打开,示例如下
        XXPermissions.startPermissionActivity(
            fragment,
            permissionArray.toCompatPermissions,
            object : OnPermissionPageCallback {
                override fun onGranted() {
                    call?.onCall(true)
                }

                override fun onDenied() {
                    ToastUtil.show("权限打开失败")
                    call?.onCall(false)
                }
            }
        )
    }
}

/** 具体的权限申请impl,在这里,我们对权限进行二次调整 */
class OnPermissionCallbackImpl(
    private val fragment: Fragment,
    private val permissionArray: Array<out String>,
    private val call: OnRequestPermissionListener?
) : OnPermissionCallback {

    override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
        if (permissions == null) {
            call?.onCall(permissionArray, false)
            return
        }
        if (all) {
            call?.onCall(permissionArray, true)
        } else {
            // 此时直接return,因为我们知道这是用户自己操作的
            return
        }
    }

    override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
        if (fragment.activity == null || permissions == null) return
        // 永久拒绝时让流程延续下去
        if (never) {
            call?.onCall(permissionArray, false)
        }
    }
}

private val Array<out String>.isFilePermission: Boolean
    get() = this.contains(Permission.WRITE_EXTERNAL_STORAGE) ||
        this.contains(Permission.READ_EXTERNAL_STORAGE)

private val Array<out String>.isCamera: Boolean
    get() = this.contains(Permission.CAMERA)

private val Array<out String>.toCompatPermissions: Array<out String>
    get() {
        if (isFilePermission) return arrayOf(Permission.READ_MEDIA_IMAGES)
        return this
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants