|
1 | 1 | package co.adityarajput.notifilter.utils |
2 | 2 |
|
| 3 | +import android.Manifest |
| 4 | +import android.annotation.SuppressLint |
| 5 | +import android.app.Activity |
3 | 6 | import android.app.NotificationManager |
4 | 7 | import android.content.Context |
5 | 8 | import android.content.Context.NOTIFICATION_SERVICE |
6 | 9 | import android.content.Context.POWER_SERVICE |
| 10 | +import android.content.Intent |
| 11 | +import android.os.Build |
| 12 | +import android.os.Build.VERSION_CODES.TIRAMISU |
7 | 13 | import android.os.PowerManager |
8 | 14 | import android.provider.Settings |
| 15 | +import androidx.core.app.ActivityCompat.requestPermissions |
| 16 | +import androidx.core.net.toUri |
| 17 | +import co.adityarajput.notifilter.data.models.Action |
| 18 | +import co.adityarajput.notifilter.data.models.Filter |
9 | 19 |
|
10 | | -fun Context.hasNotificationListenerPermission() = |
11 | | - Settings.Secure.getString(contentResolver, "enabled_notification_listeners") |
12 | | - ?.contains(packageName) ?: false |
| 20 | +enum class Permission { |
| 21 | + NOTIFICATION_LISTENER, |
| 22 | + ACCESSIBILITY_SERVICE, |
| 23 | + UNRESTRICTED_BACKGROUND_USAGE, |
| 24 | + POST_NOTIFICATIONS, |
| 25 | + NOTIFICATION_POLICY, |
| 26 | +} |
13 | 27 |
|
14 | | -fun Context.hasAccessibilityServicePermission() = |
15 | | - Settings.Secure.getString(contentResolver, "enabled_accessibility_services") |
16 | | - ?.contains(packageName) ?: false |
| 28 | +fun Context.isGranted(permission: Permission) = when (permission) { |
| 29 | + Permission.NOTIFICATION_LISTENER -> |
| 30 | + Settings.Secure.getString(contentResolver, "enabled_notification_listeners") |
| 31 | + ?.contains(packageName) ?: false |
17 | 32 |
|
18 | | -fun Context.hasUnrestrictedBackgroundUsagePermission() = |
19 | | - (getSystemService(POWER_SERVICE) as PowerManager) |
20 | | - .isIgnoringBatteryOptimizations(packageName) |
| 33 | + Permission.ACCESSIBILITY_SERVICE -> |
| 34 | + Settings.Secure.getString(contentResolver, "enabled_accessibility_services") |
| 35 | + ?.contains(packageName) ?: false |
21 | 36 |
|
22 | | -fun Context.hasPostNotificationsPermission() = |
23 | | - (getSystemService(NOTIFICATION_SERVICE) as NotificationManager) |
24 | | - .areNotificationsEnabled() |
| 37 | + Permission.UNRESTRICTED_BACKGROUND_USAGE -> |
| 38 | + (getSystemService(POWER_SERVICE) as PowerManager) |
| 39 | + .isIgnoringBatteryOptimizations(packageName) |
25 | 40 |
|
26 | | -fun Context.hasNotificationPolicyPermission() = |
27 | | - (getSystemService(NOTIFICATION_SERVICE) as NotificationManager) |
28 | | - .isNotificationPolicyAccessGranted() |
| 41 | + Permission.POST_NOTIFICATIONS -> |
| 42 | + (getSystemService(NOTIFICATION_SERVICE) as NotificationManager) |
| 43 | + .areNotificationsEnabled() |
| 44 | + |
| 45 | + Permission.NOTIFICATION_POLICY -> |
| 46 | + (getSystemService(NOTIFICATION_SERVICE) as NotificationManager) |
| 47 | + .isNotificationPolicyAccessGranted() |
| 48 | +} |
| 49 | + |
| 50 | +fun Context.isGranted(permissions: Iterable<Permission>) = |
| 51 | + permissions.associateWith(::isGranted).withDefault { false } |
| 52 | + |
| 53 | +@SuppressLint("BatteryLife") |
| 54 | +fun Context.request(permission: Permission, remove: Boolean = false) = try { |
| 55 | + when (permission) { |
| 56 | + Permission.NOTIFICATION_LISTENER -> |
| 57 | + startActivity(Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)) |
| 58 | + |
| 59 | + Permission.ACCESSIBILITY_SERVICE -> |
| 60 | + startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) |
| 61 | + |
| 62 | + Permission.UNRESTRICTED_BACKGROUND_USAGE -> |
| 63 | + startActivity( |
| 64 | + if (remove) |
| 65 | + Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) |
| 66 | + else |
| 67 | + Intent( |
| 68 | + Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, |
| 69 | + "package:${packageName}".toUri(), |
| 70 | + ), |
| 71 | + ) |
| 72 | + |
| 73 | + Permission.POST_NOTIFICATIONS -> |
| 74 | + if (Build.VERSION.SDK_INT >= TIRAMISU) { |
| 75 | + requestPermissions( |
| 76 | + this as Activity, |
| 77 | + arrayOf(Manifest.permission.POST_NOTIFICATIONS), |
| 78 | + 0, |
| 79 | + ) |
| 80 | + } else { |
| 81 | + startActivity( |
| 82 | + Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) |
| 83 | + .apply { putExtra(Settings.EXTRA_APP_PACKAGE, packageName) }, |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + Permission.NOTIFICATION_POLICY -> |
| 88 | + startActivity(Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)) |
| 89 | + } |
| 90 | +} catch (e: Exception) { |
| 91 | + Logger.e("Permissions", "Error while requesting $permission", e) |
| 92 | +} |
| 93 | + |
| 94 | +fun permissionsRequired(filters: List<Filter>) = buildList { |
| 95 | + if (filters.any { it.action is Action.TAP_NOTIFICATION }) |
| 96 | + add(Permission.ACCESSIBILITY_SERVICE) |
| 97 | + |
| 98 | + if (filters.any { it.action is Action.ALERT }) |
| 99 | + add(Permission.POST_NOTIFICATIONS) |
| 100 | + |
| 101 | + if (filters.any { it.action is Action.DISTURB }) |
| 102 | + add(Permission.NOTIFICATION_POLICY) |
| 103 | +} |
0 commit comments