添加了界面及逻辑
This commit is contained in:
@@ -9,21 +9,19 @@ 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>,
|
||||
private val requestCode: Int,
|
||||
private val rationale: String? = null
|
||||
internal val requestCode: Int,
|
||||
private val rationale: String? = null,
|
||||
private val onGranted: (() -> Unit)?
|
||||
) {
|
||||
private var onGranted: (() -> Unit)? = null
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* 创建权限请求构建器
|
||||
* @param context 上下文(Activity或Fragment)
|
||||
* @param permissions 需要请求的权限数组
|
||||
* @param requestCode 请求码
|
||||
*/
|
||||
fun with(context: Context, permissions: Array<String>, requestCode: Int): Builder {
|
||||
return Builder(context, permissions, requestCode)
|
||||
@@ -43,15 +41,17 @@ class PermissionHelper private constructor(
|
||||
|
||||
/**
|
||||
* 处理权限请求结果(供BaseActivity/BaseFragment调用)
|
||||
* @return 是否所有权限都已授予
|
||||
*/
|
||||
fun handlePermissionResult(
|
||||
context: Context,
|
||||
requestCode: Int,
|
||||
permissions: Array<out String>,
|
||||
grantResults: IntArray,
|
||||
onPermissionPermanentlyDenied: (List<String>) -> Unit
|
||||
permissionHelper: PermissionHelper? = null
|
||||
): Boolean {
|
||||
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
|
||||
permissionHelper?.onGranted?.invoke()
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -67,10 +67,6 @@ class PermissionHelper private constructor(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (permanentlyDeniedPermissions.isNotEmpty()) {
|
||||
onPermissionPermanentlyDenied(permanentlyDeniedPermissions)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -83,6 +79,8 @@ class PermissionHelper private constructor(
|
||||
}
|
||||
if (context is Activity) {
|
||||
context.startActivity(intent)
|
||||
} else if (context is Fragment) {
|
||||
context.startActivity(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,9 +91,10 @@ class PermissionHelper private constructor(
|
||||
private val requestCode: Int
|
||||
) {
|
||||
private var rationale: String? = null
|
||||
private var onGranted: (() -> Unit)? = null
|
||||
|
||||
/**
|
||||
* 设置权限说明(当需要向用户解释权限用途时显示)
|
||||
* 设置权限说明
|
||||
*/
|
||||
fun setRationale(rationale: String): Builder {
|
||||
this.rationale = rationale
|
||||
@@ -105,10 +104,16 @@ class PermissionHelper private constructor(
|
||||
/**
|
||||
* 设置权限授予回调
|
||||
*/
|
||||
fun onGranted(callback: () -> Unit): PermissionHelper {
|
||||
val helper = PermissionHelper(context, permissions, requestCode, rationale)
|
||||
helper.onGranted = callback
|
||||
return helper
|
||||
fun onGranted(callback: () -> Unit): Builder {
|
||||
this.onGranted = callback
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建PermissionHelper实例
|
||||
*/
|
||||
fun build(): PermissionHelper {
|
||||
return PermissionHelper(context, permissions, requestCode, rationale, onGranted)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,13 +131,11 @@ class PermissionHelper private constructor(
|
||||
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)
|
||||
@@ -142,7 +145,6 @@ class PermissionHelper private constructor(
|
||||
.setNegativeButton("取消", null)
|
||||
.show()
|
||||
} else {
|
||||
// 直接请求权限
|
||||
doRequestPermissions(activity)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user