From debba63a5df88b5a2833c92bf30d92cfbd5d375c Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Thu, 14 Apr 2022 10:54:17 -0400 Subject: [PATCH] WIP DND override --- app/src/main/AndroidManifest.xml | 1 + .../main/java/io/heckel/ntfy/db/Repository.kt | 6 ++++ .../io/heckel/ntfy/msg/NotificationService.kt | 15 +++++++--- .../java/io/heckel/ntfy/ui/MainActivity.kt | 5 ++-- .../io/heckel/ntfy/ui/SettingsActivity.kt | 30 +++++++++++++++++++ app/src/main/res/values/values.xml | 1 + 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9fdb969..7c5c6ee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,6 +10,7 @@ + = Build.VERSION_CODES.O) { // Note: To change a notification channel, you must delete the old one and create a new one! + val dndOverridePriority = repository.getDnsOverridePriority() val pause = 300L val channel = when (priority) { 1 -> NotificationChannel(CHANNEL_ID_MIN, context.getString(R.string.channel_notifications_min_name), NotificationManager.IMPORTANCE_MIN) @@ -225,6 +230,7 @@ class NotificationService(val context: Context) { 4 -> { val channel = NotificationChannel(CHANNEL_ID_HIGH, context.getString(R.string.channel_notifications_high_name), NotificationManager.IMPORTANCE_HIGH) channel.enableVibration(true) + channel.setBypassDnd(dndOverridePriority >= 4) channel.vibrationPattern = longArrayOf( pause, 100, pause, 100, pause, 100, pause, 2000 @@ -235,6 +241,7 @@ class NotificationService(val context: Context) { val channel = NotificationChannel(CHANNEL_ID_MAX, context.getString(R.string.channel_notifications_max_name), NotificationManager.IMPORTANCE_HIGH) // IMPORTANCE_MAX does not exist channel.enableLights(true) channel.enableVibration(true) + channel.setBypassDnd(dndOverridePriority >= 4) channel.vibrationPattern = longArrayOf( pause, 100, pause, 100, pause, 100, pause, 2000, diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt index cf6e554..319958a 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt @@ -30,7 +30,6 @@ import io.heckel.ntfy.app.Application import io.heckel.ntfy.db.Repository import io.heckel.ntfy.db.Subscription import io.heckel.ntfy.firebase.FirebaseMessenger -import io.heckel.ntfy.util.Log import io.heckel.ntfy.msg.ApiService import io.heckel.ntfy.msg.NotificationDispatcher import io.heckel.ntfy.service.SubscriberService @@ -376,7 +375,9 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc true } R.id.main_menu_docs -> { - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_docs_url)))) + //startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.main_menu_docs_url)))) + val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS) + startActivity(intent) true } else -> super.onOptionsItemSelected(item) diff --git a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt index ac6e241..ee5f564 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt @@ -2,6 +2,7 @@ package io.heckel.ntfy.ui import android.Manifest import android.app.AlertDialog +import android.app.NotificationManager import android.content.ClipData import android.content.ClipboardManager import android.content.Context @@ -120,6 +121,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere class SettingsFragment : PreferenceFragmentCompat() { private lateinit var repository: Repository private lateinit var serviceManager: SubscriberServiceManager + private lateinit var notificationManager: NotificationManager private var autoDownloadSelection = AUTO_DOWNLOAD_SELECTION_NOT_SET override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { @@ -129,6 +131,7 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere repository = Repository.getInstance(requireActivity()) serviceManager = SubscriberServiceManager(requireActivity()) autoDownloadSelection = repository.getAutoDownloadMaxSize() // Only used for <= Android P, due to permissions request + notificationManager = requireContext().getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager // Important note: We do not use the default shared prefs to store settings. Every // preferenceDataStore is overridden to use the repository. This is convenient, because @@ -200,6 +203,33 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere } } + // DND override priority + val dndOverrideEnabled = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && notificationManager.isNotificationPolicyAccessGranted + val dndOverridePriorityPrefId = context?.getString(R.string.settings_notifications_dnd_override_priority_key) ?: return + val dndOverridePriority: ListPreference? = findPreference(minPriorityPrefId) + dndOverridePriority?.isVisible = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M + dndOverridePriority?.value = repository.getDnsOverridePriority().toString() + dndOverridePriority?.preferenceDataStore = object : PreferenceDataStore() { + override fun putString(key: String?, value: String?) { + val dndOverridePriorityValue = value?.toIntOrNull() ?:return + //repository.setMinPriority(minPriorityValue) + } + override fun getString(key: String?, defValue: String?): String { + return repository.getDnsOverridePriority().toString() + } + } + dndOverridePriority?.summaryProvider = Preference.SummaryProvider { pref -> + val priorityValue = pref.value.toIntOrNull() ?: 1 // 1/low means all priorities + when (priorityValue) { + 1 -> getString(R.string.settings_notifications_min_priority_summary_any) + 5 -> getString(R.string.settings_notifications_min_priority_summary_max) + else -> { + val minPriorityString = toPriorityString(requireContext(), priorityValue) + getString(R.string.settings_notifications_min_priority_summary_x_or_higher, priorityValue, minPriorityString) + } + } + } + // Auto download val autoDownloadPrefId = context?.getString(R.string.settings_notifications_auto_download_key) ?: return val autoDownload: ListPreference? = findPreference(autoDownloadPrefId) diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index 2dd7d2f..ea36c8b 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -15,6 +15,7 @@ MutedUntil MinPriority + DndOverridePriority AutoDownload AutoDelete DefaultBaseURL