|
|
@@ -1,7 +1,9 @@
|
|
|
package fr.barteam.watchosm.ui
|
|
|
|
|
|
+import android.content.ActivityNotFoundException
|
|
|
import android.content.Context
|
|
|
import android.content.Intent
|
|
|
+import android.net.Uri
|
|
|
import android.provider.Settings
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
|
|
|
|
@@ -9,10 +11,32 @@ import androidx.core.app.NotificationManagerCompat
|
|
|
fun isNotificationAccessEnabled(context: Context): Boolean =
|
|
|
NotificationManagerCompat.getEnabledListenerPackages(context).contains(context.packageName)
|
|
|
|
|
|
-/** Ouvre l'écran système de gestion de l'accès aux notifications. */
|
|
|
-fun openNotificationAccessSettings(context: Context) {
|
|
|
- context.startActivity(
|
|
|
- Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS)
|
|
|
- .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
|
|
|
+/**
|
|
|
+ * Ouvre l'écran de gestion de l'accès aux notifications.
|
|
|
+ *
|
|
|
+ * Wear OS n'expose pas toujours `ACTION_NOTIFICATION_LISTENER_SETTINGS` : selon
|
|
|
+ * la montre/la ROM, cet écran peut être absent. On tente donc, dans l'ordre,
|
|
|
+ * l'écran dédié aux écouteurs de notifications, puis la fiche de l'application
|
|
|
+ * (où l'utilisateur trouve l'accès aux notifications), puis les réglages.
|
|
|
+ *
|
|
|
+ * @return true si un écran a pu être ouvert.
|
|
|
+ */
|
|
|
+fun openNotificationAccessSettings(context: Context): Boolean {
|
|
|
+ val candidates = listOf(
|
|
|
+ Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS),
|
|
|
+ Intent(
|
|
|
+ Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
|
|
|
+ Uri.fromParts("package", context.packageName, null),
|
|
|
+ ),
|
|
|
+ Intent(Settings.ACTION_SETTINGS),
|
|
|
)
|
|
|
+ for (intent in candidates) {
|
|
|
+ try {
|
|
|
+ context.startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
|
|
|
+ return true
|
|
|
+ } catch (_: ActivityNotFoundException) {
|
|
|
+ // Écran absent sur cette ROM : on essaie le suivant.
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false
|
|
|
}
|