From 33d193d5ad413269c35bd85f5c861d273b30b590 Mon Sep 17 00:00:00 2001 From: Hunter Kehoe Date: Fri, 3 Mar 2023 07:57:36 -0700 Subject: [PATCH] add toggle to disable UnifiedPush --- .../main/java/io/heckel/ntfy/db/Repository.kt | 11 ++++++++++ .../io/heckel/ntfy/ui/SettingsActivity.kt | 20 +++++++++++++++++++ .../io/heckel/ntfy/up/BroadcastReceiver.kt | 5 +++++ app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/values/values.xml | 1 + app/src/main/res/xml/main_preferences.xml | 4 ++++ 6 files changed, 44 insertions(+) diff --git a/app/src/main/java/io/heckel/ntfy/db/Repository.kt b/app/src/main/java/io/heckel/ntfy/db/Repository.kt index 423c716..5d9695c 100644 --- a/app/src/main/java/io/heckel/ntfy/db/Repository.kt +++ b/app/src/main/java/io/heckel/ntfy/db/Repository.kt @@ -292,6 +292,16 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas .apply() } + fun getEnableUP(): Boolean { + return sharedPrefs.getBoolean(SHARED_PREFS_ENABLE_UP, true) // Enabled by default + } + + fun setEnableUP(enabled: Boolean) { + sharedPrefs.edit() + .putBoolean(SHARED_PREFS_ENABLE_UP, enabled) + .apply() + } + fun getInsistentMaxPriorityEnabled(): Boolean { return sharedPrefs.getBoolean(SHARED_PREFS_INSISTENT_MAX_PRIORITY_ENABLED, false) // Disabled by default } @@ -477,6 +487,7 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas const val SHARED_PREFS_CONNECTION_PROTOCOL = "ConnectionProtocol" const val SHARED_PREFS_DARK_MODE = "DarkMode" const val SHARED_PREFS_BROADCAST_ENABLED = "BroadcastEnabled" + const val SHARED_PREFS_ENABLE_UP = "EnableUP" const val SHARED_PREFS_INSISTENT_MAX_PRIORITY_ENABLED = "InsistentMaxPriority" const val SHARED_PREFS_RECORD_LOGS_ENABLED = "RecordLogs" const val SHARED_PREFS_BATTERY_OPTIMIZATIONS_REMIND_TIME = "BatteryOptimizationsRemindTime" 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 a03942a..7323829 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt @@ -364,6 +364,26 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere } } + // Enable UnifiedPush + val enableUPPrefId = context?.getString(R.string.settings_advanced_enable_up_key) ?: return + val enableUP: SwitchPreference? = findPreference(enableUPPrefId) + enableUP?.isChecked = repository.getEnableUP() + enableUP?.preferenceDataStore = object : PreferenceDataStore() { + override fun putBoolean(key: String?, value: Boolean) { + repository.setEnableUP(value) + } + override fun getBoolean(key: String?, defValue: Boolean): Boolean { + return repository.getEnableUP() + } + } + enableUP?.summaryProvider = Preference.SummaryProvider { pref -> + if (pref.isChecked) { + getString(R.string.settings_advanced_enable_up_summary_enabled) + } else { + getString(R.string.settings_advanced_enable_up_summary_disabled) + } + } + // Export logs val exportLogsPrefId = context?.getString(R.string.settings_advanced_export_logs_key) ?: return val exportLogs: ListPreference? = findPreference(exportLogsPrefId) diff --git a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt index 20cf963..a1ab52f 100644 --- a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt +++ b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt @@ -38,6 +38,11 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { val repository = app.repository val distributor = Distributor(app) Log.d(TAG, "REGISTER received for app $appId (connectorToken=$connectorToken)") + if (!repository.getEnableUP()) { + Log.w(TAG, "Refusing registration because 'EnableUP' is disabled") + distributor.sendRegistrationFailed(appId, connectorToken, "EnableUP setting is disabled") + return + } if (appId.isBlank()) { Log.w(TAG, "Refusing registration: Empty application") distributor.sendRegistrationFailed(appId, connectorToken, "Empty application string") diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b7f57f3..0a2971f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -320,6 +320,9 @@ Broadcast messages Apps can receive incoming notifications as broadcasts Apps cannot receive notifications as broadcasts + Enable UnifiedPush + ntfy will act as a UnifiedPush distributor + ntfy will NOT act as a UnifiedPush distributor Record logs Logging (up to 1,000 entries) to device … Turn on logging, so you can share logs later to diagnose issues. diff --git a/app/src/main/res/values/values.xml b/app/src/main/res/values/values.xml index ebab08b..b851724 100644 --- a/app/src/main/res/values/values.xml +++ b/app/src/main/res/values/values.xml @@ -25,6 +25,7 @@ Backup Restore BroadcastEnabled + EnableUP RecordLogs ExportLogs ClearLogs diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index d7178a6..630c284 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -76,6 +76,10 @@ app:key="@string/settings_advanced_broadcast_key" app:title="@string/settings_advanced_broadcast_title" app:enabled="true"/> +