Commit 2ed3614b authored by wanglei's avatar wanglei

...权限

parent fd94dcc4
package com.base.locationsharewhite.utils
package com.base.appzxhy.utils
import android.Manifest
import android.app.Activity
......@@ -11,12 +11,12 @@ import android.os.Build
import android.os.Environment
import android.provider.Settings
import androidx.core.app.ActivityCompat
import com.base.appzxhy.utils.ActivityLauncher
import com.base.appzxhy.utils.IntentUtils.intentAppDetailsSettings
import com.base.appzxhy.utils.LogEx
object PermissionUtils {
//region 通知
fun Context.areNotificationsEnabled(): Boolean {
val notificationManager =
this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
......@@ -24,7 +24,21 @@ object PermissionUtils {
return flag
}
fun Context.checkNotificationPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
} else {
return true
}
}
fun Context.checkAllLocationPermission(): Boolean {
return checkLocationPermission() && checkBackgroundLocationPermission()
}
//endregion
//region 储存
fun Context.checkStorePermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Environment.isExternalStorageManager()
......@@ -35,18 +49,32 @@ object PermissionUtils {
}
}
fun Context.checkNotificationPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
fun Context.requestStoragePermission(
launcher: ActivityLauncher,
jumpAction: (() -> Unit)? = null,
result: (flag: Boolean) -> Unit
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val intent =
Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.addCategory("android.intent.category.DEFAULT")
intent.data = Uri.parse("package:${packageName}")
jumpAction?.invoke()
launcher.launch(intent) {
result.invoke(checkStorePermission())
}
} else {
return true
launcher.launch(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { map ->
result(map.values.all { it })
}
}
}
fun Context.checkAllLocationPermission(): Boolean {
return checkLocationPermission() && checkBackgroundLocationPermission()
}
//endregion
//region 定位
fun Context.checkLocationPermission(): Boolean {
val fineLocationPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
......@@ -55,7 +83,6 @@ object PermissionUtils {
return fineLocationPermission == PackageManager.PERMISSION_GRANTED && accessCoarsePermission == PackageManager.PERMISSION_GRANTED;
}
fun Activity.checkLocationPermissionDeniedByUser(): Boolean {
val deniedByUser1 =
......@@ -69,7 +96,6 @@ object PermissionUtils {
return deniedByUser1 && deniedByUser2
}
fun Context.checkBackgroundLocationPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActivityCompat.checkSelfPermission(
......@@ -86,30 +112,6 @@ object PermissionUtils {
return deniedByUser1
}
fun Context.requestStoragePermission(
launcher: ActivityLauncher,
jumpAction: (() -> Unit)? = null,
result: (flag: Boolean) -> Unit
) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val intent =
Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
intent.addCategory("android.intent.category.DEFAULT")
intent.data = Uri.parse("package:${packageName}")
jumpAction?.invoke()
launcher.launch(intent) {
result.invoke(checkStorePermission())
}
} else {
launcher.launch(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { map ->
result(map.values.all { it })
}
}
}
fun Activity.requestLocationAllowAllTime(
launcher: ActivityLauncher,
jumpOutAction: (() -> Unit)? = null,//跳转到设置
......@@ -158,4 +160,7 @@ object PermissionUtils {
}
}
}
//endregion
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment