add toggle to disable UnifiedPush

This commit is contained in:
Hunter Kehoe 2023-03-03 07:57:36 -07:00
parent 249aae77f0
commit 33d193d5ad
6 changed files with 44 additions and 0 deletions

View file

@ -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"

View file

@ -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<SwitchPreference> { 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)

View file

@ -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")

View file

@ -320,6 +320,9 @@
<string name="settings_advanced_broadcast_title">Broadcast messages</string>
<string name="settings_advanced_broadcast_summary_enabled">Apps can receive incoming notifications as broadcasts</string>
<string name="settings_advanced_broadcast_summary_disabled">Apps cannot receive notifications as broadcasts</string>
<string name="settings_advanced_enable_up_title">Enable UnifiedPush</string>
<string name="settings_advanced_enable_up_summary_enabled">ntfy will act as a UnifiedPush distributor</string>
<string name="settings_advanced_enable_up_summary_disabled">ntfy will NOT act as a UnifiedPush distributor</string>
<string name="settings_advanced_record_logs_title">Record logs</string>
<string name="settings_advanced_record_logs_summary_enabled">Logging (up to 1,000 entries) to device …</string>
<string name="settings_advanced_record_logs_summary_disabled">Turn on logging, so you can share logs later to diagnose issues.</string>

View file

@ -25,6 +25,7 @@
<string name="settings_backup_restore_backup_key" translatable="false">Backup</string>
<string name="settings_backup_restore_restore_key" translatable="false">Restore</string>
<string name="settings_advanced_broadcast_key" translatable="false">BroadcastEnabled</string>
<string name="settings_advanced_enable_up_key" translatable="false">EnableUP</string>
<string name="settings_advanced_record_logs_key" translatable="false">RecordLogs</string>
<string name="settings_advanced_export_logs_key" translatable="false">ExportLogs</string>
<string name="settings_advanced_clear_logs_key" translatable="false">ClearLogs</string>

View file

@ -76,6 +76,10 @@
app:key="@string/settings_advanced_broadcast_key"
app:title="@string/settings_advanced_broadcast_title"
app:enabled="true"/>
<SwitchPreference
app:key="@string/settings_advanced_enable_up_key"
app:title="@string/settings_advanced_enable_up_title"
app:enabled="true"/>
<SwitchPreference
app:key="@string/settings_advanced_record_logs_key"
app:title="@string/settings_advanced_record_logs_title"