Skip to content

Commit

Permalink
细节优化完成
Browse files Browse the repository at this point in the history
  • Loading branch information
CraftsmanHyj committed Jul 13, 2022
1 parent b45c7f9 commit 313501c
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 298 deletions.
5 changes: 1 addition & 4 deletions Demo/src/main/java/com/hyj/permission/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ class MainActivity : AppCompatActivity() {
btnNotification.setOnClickListener {
//Permission.NOTIFICATION_SERVICE
singlePermissionLauncher.launchP(
this@MainActivity,
"android.permission.NOTIFICATION_SERVICE"
this@MainActivity, "android.permission.NOTIFICATION_SERVICE"
)
}
btnNotificationListener.setOnClickListener {
Expand All @@ -91,11 +90,9 @@ class MainActivity : AppCompatActivity() {
btnAlarm.setOnClickListener {
singlePermissionLauncher.launchP(this@MainActivity, SCHEDULE_EXACT_ALARM)
}
//TODO 不一样,需要处理
btnNotDisturb.setOnClickListener {
singlePermissionLauncher.launchP(this@MainActivity, ACCESS_NOTIFICATION_POLICY)
}
//TODO 不一样,需要处理
btnIgnoreBattery.setOnClickListener {
singlePermissionLauncher.launchP(
this@MainActivity, REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
Expand Down
2 changes: 1 addition & 1 deletion Demo/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<style name="btnType">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">18sp</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
4 changes: 2 additions & 2 deletions Permission/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ plugins {
}

ext {
VERSION_CODE = 1
VERSION_NAME = "1.0.0"
VERSION_CODE = 4
VERSION_NAME = "1.0.4"
}

android {
Expand Down
5 changes: 1 addition & 4 deletions Permission/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hyj.lib.permission">

</manifest>
<manifest package="com.hyj.lib.permission" />
88 changes: 42 additions & 46 deletions Permission/src/main/java/com/hyj/lib/permission/PermissionApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,16 @@ internal object PermissionApi {
/**
* 是否有存储权限
*/
fun isGrantedStoragePermission(context: Context): Boolean {
private fun isGrantedStoragePermission(context: Context): Boolean {
return if (AndroidVersion.isAndroid11) {
Environment.isExternalStorageManager()
} else isGrantedPermissions(
context, Permission.Group.STORAGE
)
} else isGrantedPermissions(context, Permission.Group.STORAGE)
}

/**
* 是否有安装权限
*/
fun isGrantedInstallPermission(context: Context): Boolean {
private fun isGrantedInstallPermission(context: Context): Boolean {
return if (AndroidVersion.isAndroid8) {
context.packageManager.canRequestPackageInstalls()
} else true
Expand All @@ -41,7 +39,7 @@ internal object PermissionApi {
/**
* 是否有悬浮窗权限
*/
fun isGrantedWindowPermission(context: Context): Boolean {
private fun isGrantedWindowPermission(context: Context): Boolean {
return if (AndroidVersion.isAndroid6) {
Settings.canDrawOverlays(context)
} else true
Expand All @@ -50,7 +48,7 @@ internal object PermissionApi {
/**
* 是否有系统设置权限
*/
fun isGrantedSettingPermission(context: Context): Boolean {
private fun isGrantedSettingPermission(context: Context): Boolean {
return if (AndroidVersion.isAndroid6) {
Settings.System.canWrite(context)
} else true
Expand All @@ -59,14 +57,14 @@ internal object PermissionApi {
/**
* 是否有通知栏权限
*/
fun isGrantedNotifyPermission(context: Context): Boolean {
private fun isGrantedNotifyPermission(context: Context): Boolean {
return NotificationManagerCompat.from(context).areNotificationsEnabled()
}

/**
* 是否通知栏监听的权限
*/
fun isGrantedNotificationListenerPermission(context: Context): Boolean {
private fun isGrantedNotificationListenerPermission(context: Context): Boolean {
if (AndroidVersion.isAndroid4_3) {
val packageNames: Set<String> =
NotificationManagerCompat.getEnabledListenerPackages(context)
Expand All @@ -78,7 +76,7 @@ internal object PermissionApi {
/**
* 是否有使用统计权限
*/
fun isGrantedPackagePermission(context: Context): Boolean {
private fun isGrantedPackagePermission(context: Context): Boolean {
if (AndroidVersion.isAndroid5) {
val appOps: AppOpsManager =
context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
Expand All @@ -101,27 +99,25 @@ internal object PermissionApi {
/**
* 是否有闹钟权限
*/
fun isGrantedAlarmPermission(context: Context): Boolean {
private fun isGrantedAlarmPermission(context: Context): Boolean {
return if (AndroidVersion.isAndroid12) {
context.getSystemService(AlarmManager::class.java)
.canScheduleExactAlarms()
context.getSystemService(AlarmManager::class.java).canScheduleExactAlarms()
} else true
}

/**
* 是否有勿扰模式权限
*/
fun isGrantedNotDisturbPermission(context: Context): Boolean {
private fun isGrantedNotDisturbPermission(context: Context): Boolean {
return if (AndroidVersion.isAndroid6) {
context.getSystemService(NotificationManager::class.java)
.isNotificationPolicyAccessGranted
context.getSystemService(NotificationManager::class.java).isNotificationPolicyAccessGranted
} else true
}

/**
* 是否忽略电池优化选项
*/
fun isGrantedIgnoreBatteryPermission(context: Context): Boolean {
private fun isGrantedIgnoreBatteryPermission(context: Context): Boolean {
return if (AndroidVersion.isAndroid6) {
context.getSystemService(PowerManager::class.java)
.isIgnoringBatteryOptimizations(context.packageName)
Expand Down Expand Up @@ -176,7 +172,7 @@ internal object PermissionApi {
* 获取已经授予的权限
*/
fun getGrantedPermissions(context: Context, permissions: List<String>): List<String> {
val grantedPermission: MutableList<String> = ArrayList(permissions.size)
val grantedPermission = mutableListOf<String>()
for (permission in permissions) {
if (isGrantedPermission(context, permission)) {
grantedPermission.add(permission)
Expand Down Expand Up @@ -260,9 +256,9 @@ internal object PermissionApi {
// 检测 Android 12 的三个新权限
if (!AndroidVersion.isAndroid12) {
if (Permission.BLUETOOTH_SCAN == permission) {
return context.checkSelfPermission(Permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED
return context.checkSelfPermission(Permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
}

if (Permission.BLUETOOTH_CONNECT == permission || Permission.BLUETOOTH_ADVERTISE == permission) {
return true
}
Expand All @@ -271,13 +267,12 @@ internal object PermissionApi {
// 检测 Android 10 的三个新权限
if (!AndroidVersion.isAndroid10) {
if (Permission.ACCESS_BACKGROUND_LOCATION == permission) {
return context.checkSelfPermission(Permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED
return context.checkSelfPermission(Permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
}
if (Permission.ACTIVITY_RECOGNITION == permission) {
return context.checkSelfPermission(Permission.BODY_SENSORS) ==
PackageManager.PERMISSION_GRANTED
return context.checkSelfPermission(Permission.BODY_SENSORS) == PackageManager.PERMISSION_GRANTED
}

if (Permission.ACCESS_MEDIA_LOCATION == permission) {
return true
}
Expand All @@ -296,10 +291,10 @@ internal object PermissionApi {
return true
}
if (Permission.READ_PHONE_NUMBERS == permission) {
return context.checkSelfPermission(Permission.READ_PHONE_STATE) ==
PackageManager.PERMISSION_GRANTED
return context.checkSelfPermission(Permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
}
}

return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
}

Expand All @@ -324,7 +319,7 @@ internal object PermissionApi {
* @param activity Activity对象
* @param permission 请求的权限
*/
fun isPermissionPermanentDenied(activity: Activity, permission: String): Boolean {
private fun isPermissionPermanentDenied(activity: Activity, permission: String): Boolean {
// 特殊权限不算,本身申请方式和危险权限申请方式不同,因为没有永久拒绝的选项,所以这里返回 false
if (isSpecialPermission(permission)) {
return false
Expand All @@ -343,8 +338,8 @@ internal object PermissionApi {
return false
}
}
if (AndroidVersion.isAndroid10) {

if (AndroidVersion.isAndroid10) {
// 重新检测后台定位权限是否永久拒绝
if (Permission.ACCESS_BACKGROUND_LOCATION == permission &&
!isGrantedPermission(activity, Permission.ACCESS_BACKGROUND_LOCATION) &&
Expand Down Expand Up @@ -386,35 +381,19 @@ internal object PermissionApi {
!activity.shouldShowRequestPermissionRationale(Permission.READ_PHONE_STATE)
}
}

return !isGrantedPermission(activity, permission) &&
!activity.shouldShowRequestPermissionRationale(permission)
}

/**
* 获取没有授予的权限
*
* @param permissions 需要请求的权限组
* @param grantResults 允许结果组
*/
fun getDeniedPermissions(permissions: List<String>, grantResults: IntArray): List<String> {
val deniedPermissions: MutableList<String> = ArrayList()
for (i in grantResults.indices) {
// 把没有授予过的权限加入到集合中
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
deniedPermissions.add(permissions[i])
}
}
return deniedPermissions
}

/**
* 获取已授予的权限
*
* @param permissions 需要请求的权限组
* @param grantResults 允许结果组
*/
fun getGrantedPermissions(permissions: List<String>, grantResults: IntArray): List<String> {
val grantedPermissions: MutableList<String> = ArrayList()
val grantedPermissions = mutableListOf<String>()
for (i in grantResults.indices) {
// 把授予过的权限加入到集合中
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
Expand All @@ -423,4 +402,21 @@ internal object PermissionApi {
}
return grantedPermissions
}

/**
* 获取没有授予的权限
*
* @param permissions 需要请求的权限组
* @param grantResults 允许结果组
*/
fun getDeniedPermissions(permissions: List<String>, grantResults: IntArray): List<String> {
val deniedPermissions = mutableListOf<String>()
for (i in grantResults.indices) {
// 把没有授予过的权限加入到集合中
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
deniedPermissions.add(permissions[i])
}
}
return deniedPermissions
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
package com.hyj.lib.permission

import android.content.pm.PackageManager
import androidx.activity.result.ActivityResultCaller
import androidx.core.content.ContextCompat

/**
* 权限回调接口
*/
class PermissionCallback(private val launcherCaller: ActivityResultCaller) {
private var onGranted: (() -> Unit)? = null
private var onDenied: (() -> Unit)? = null
private var onPermanentlyDenied: (() -> Unit)? = null
private var onPermanentlyDenied: ((Array<String>) -> Unit)? = null

//“不再询问”后的弹窗提示
private var permanentlyDeniedTip: String = "应用需要此权限才可以运行,请在设置中授权"
private var permanentlyDeniedTip: String = "应用需要此权限才可以运行,请在应用信息的权限管理中授权"

//跳转到应用详情设置权限
private val appSetLauncher =
launcherCaller.registerForActivityResult(LaunchAppSettingContract()) { permissions ->
// val denied = permissions?.find {
// PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(
// launcherCaller.context(), it
// )
// }

// if (!denied.isNullOrBlank()) {
// denied()
// } else {
// granted()
// }

if (PermissionApi.isGrantedPermissions(launcherCaller.context(), permissions)) {
granted()
} else {
Expand All @@ -54,7 +40,7 @@ class PermissionCallback(private val launcherCaller: ActivityResultCaller) {
/**
* 权限拒绝,且勾选了不再询问时的回调
*/
fun onPermanentlyDenied(method: () -> Unit) {
fun onPermanentlyDenied(method: (Array<String>) -> Unit) {
onPermanentlyDenied = method
}

Expand All @@ -74,7 +60,7 @@ class PermissionCallback(private val launcherCaller: ActivityResultCaller) {
}

internal fun permanentlyDenied(deniedPermission: Array<String>) {
onPermanentlyDenied?.invoke() ?: showSetDialog(deniedPermission)
onPermanentlyDenied?.invoke(deniedPermission) ?: showSetDialog(deniedPermission)
}

private fun showSetDialog(deniedPermission: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.io.IOException
* User: hyj
* Date: 2022/6/19 14:59
*/
object PermissionChecker {
internal object PermissionChecker {
/**
* 检查权限是否符合要求
* 必须要传入正常的权限或者权限组才能申请权限
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ fun ActivityResultCaller.registerForPermissionResult(
permCallback.callBack()

return registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { resultMap ->
// if (!resultMap.containsValue(false)) {
// permCallback.granted()
// return@registerForActivityResult
// }

// 获得未授权权限列表、第一次拒绝列表
// var aDenied = resultMap.filter { !it.value }.keys.toTypedArray()
// aDenied = resultMap.keys.toTypedArray()
//
// //查找权限勾选了不再询问,那么所有拒绝的权限都跳转到设置界面去授权
// val permanentlyDenied =
// aDenied.find { !ActivityCompat.shouldShowRequestPermissionRationale(context(), it) }

//resultMap中返回的授权结果不准确,需要自己重新判断,
//比如:android.permission.ACCESS_NOTIFICATION_POLICY,未授权也会返回true
val lDenied = mutableListOf<String>()//拒绝权限
Expand Down Expand Up @@ -85,14 +72,6 @@ fun ActivityResultLauncher<Array<String>>.launchP(
// 优化所申请的权限列表
aPermission = PermissionChecker.optimizeDeprecatedPermission(aPermission)

// if (PermissionApi.isGrantedPermissions(context, aPermission)) {
// 证明这些权限已经全部授予过,直接回调成功
// if (callback != null) {
// mInterceptor.grantedPermissions(activity, permissions, permissions, true, callback)
// }
// return
// }

launch(aPermission)
} catch (e: Exception) {
e.printStackTrace()
Expand Down
Loading

0 comments on commit 313501c

Please sign in to comment.