155 lines
4.7 KiB
Kotlin
155 lines
4.7 KiB
Kotlin
package com.sw.platecabinet.utils
|
|
|
|
import android.app.Activity
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.pm.PackageManager
|
|
import android.net.Uri
|
|
import android.provider.Settings
|
|
import androidx.appcompat.app.AlertDialog
|
|
import androidx.core.app.ActivityCompat
|
|
import androidx.core.content.ContextCompat
|
|
import androidx.fragment.app.Fragment
|
|
|
|
class PermissionHelper private constructor(
|
|
private val context: Context,
|
|
private val permissions: Array<String>,
|
|
internal val requestCode: Int,
|
|
private val rationale: String? = null,
|
|
private val onGranted: (() -> Unit)?
|
|
) {
|
|
|
|
companion object {
|
|
/**
|
|
* 创建权限请求构建器
|
|
*/
|
|
fun with(context: Context, permissions: Array<String>, requestCode: Int): Builder {
|
|
return Builder(context, permissions, requestCode)
|
|
}
|
|
|
|
/**
|
|
* 检查是否已授予所有权限
|
|
*/
|
|
fun checkPermissions(context: Context, permissions: Array<String>): Boolean {
|
|
return permissions.all { permission ->
|
|
ContextCompat.checkSelfPermission(
|
|
context,
|
|
permission
|
|
) == PackageManager.PERMISSION_GRANTED
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理权限请求结果(供BaseActivity/BaseFragment调用)
|
|
* @return 是否所有权限都已授予
|
|
*/
|
|
fun handlePermissionResult(
|
|
context: Context,
|
|
requestCode: Int,
|
|
permissions: Array<out String>,
|
|
grantResults: IntArray,
|
|
permissionHelper: PermissionHelper? = null
|
|
): Boolean {
|
|
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
|
|
permissionHelper?.onGranted?.invoke()
|
|
return true
|
|
}
|
|
|
|
val permanentlyDeniedPermissions = mutableListOf<String>()
|
|
permissions.forEachIndexed { index, permission ->
|
|
if (grantResults[index] != PackageManager.PERMISSION_GRANTED) {
|
|
if (!ActivityCompat.shouldShowRequestPermissionRationale(
|
|
context as Activity,
|
|
permission
|
|
)
|
|
) {
|
|
permanentlyDeniedPermissions.add(permission)
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
/**
|
|
* 跳转到应用设置页面
|
|
*/
|
|
fun openAppSettings(context: Context) {
|
|
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
|
|
data = Uri.fromParts("package", context.packageName, null)
|
|
}
|
|
if (context is Activity) {
|
|
context.startActivity(intent)
|
|
} else if (context is Fragment) {
|
|
context.startActivity(intent)
|
|
}
|
|
}
|
|
}
|
|
|
|
class Builder(
|
|
private val context: Context,
|
|
private val permissions: Array<String>,
|
|
private val requestCode: Int
|
|
) {
|
|
private var rationale: String? = null
|
|
private var onGranted: (() -> Unit)? = null
|
|
|
|
/**
|
|
* 设置权限说明
|
|
*/
|
|
fun setRationale(rationale: String): Builder {
|
|
this.rationale = rationale
|
|
return this
|
|
}
|
|
|
|
/**
|
|
* 设置权限授予回调
|
|
*/
|
|
fun onGranted(callback: () -> Unit): Builder {
|
|
this.onGranted = callback
|
|
return this
|
|
}
|
|
|
|
/**
|
|
* 构建PermissionHelper实例
|
|
*/
|
|
fun build(): PermissionHelper {
|
|
return PermissionHelper(context, permissions, requestCode, rationale, onGranted)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 检查并请求权限
|
|
*/
|
|
fun checkAndRequest() {
|
|
if (checkPermissions(context, permissions)) {
|
|
onGranted?.invoke()
|
|
} else {
|
|
requestPermissions()
|
|
}
|
|
}
|
|
|
|
private fun requestPermissions() {
|
|
val activity = context as? Activity ?: return
|
|
|
|
val shouldShowRationale = permissions.any { permission ->
|
|
ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)
|
|
}
|
|
|
|
if (shouldShowRationale && rationale != null) {
|
|
AlertDialog.Builder(activity)
|
|
.setTitle("权限说明")
|
|
.setMessage(rationale)
|
|
.setPositiveButton("确定") { _, _ ->
|
|
doRequestPermissions(activity)
|
|
}
|
|
.setNegativeButton("取消", null)
|
|
.show()
|
|
} else {
|
|
doRequestPermissions(activity)
|
|
}
|
|
}
|
|
|
|
private fun doRequestPermissions(activity: Activity) {
|
|
ActivityCompat.requestPermissions(activity, permissions, requestCode)
|
|
}
|
|
} |