Compare commits

..

1 commit

Author SHA1 Message Date
Jonathan Klee
51294c23a5 Use system setting to maintain activation status of ntfy
Indeed, since the preference fragment is part of the Settings
app, we cannot use shared preference to share Ntfy activation status.
2024-09-25 22:13:56 +02:00
10 changed files with 40 additions and 21 deletions

View file

@ -10,7 +10,7 @@ android {
compileSdkVersion 33 compileSdkVersion 33
defaultConfig { defaultConfig {
applicationId "foundation.e.ntfy" applicationId "io.heckel.ntfy"
minSdkVersion 23 minSdkVersion 23
targetSdkVersion 33 targetSdkVersion 33
@ -68,7 +68,6 @@ android {
'-Xjvm-default=all-compatibility' // https://stackoverflow.com/a/71234042/1440785 '-Xjvm-default=all-compatibility' // https://stackoverflow.com/a/71234042/1440785
] ]
} }
namespace "io.heckel.ntfy"
} }
// Disables GoogleServices tasks for F-Droid variant // Disables GoogleServices tasks for F-Droid variant

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools"
package="io.heckel.ntfy">
<!-- Permissions --> <!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.INTERNET"/>
@ -11,6 +12,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/> <!-- Only required on SDK <= 28 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/> <!-- Only required on SDK <= 28 -->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/> <!-- To reschedule the websocket retry --> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/> <!-- To reschedule the websocket retry -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <!-- As of Android 13, we need to ask for permission to post notifications --> <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <!-- As of Android 13, we need to ask for permission to post notifications -->
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<!-- <!--
Permission REQUEST_INSTALL_PACKAGES (F-Droid only!): Permission REQUEST_INSTALL_PACKAGES (F-Droid only!):
@ -179,12 +182,14 @@
<activity <activity
android:name=".ui.MainSettingsActivity" android:name=".ui.MainSettingsActivity"
android:theme="@style/PreferenceTheme"/> android:theme="@style/PreferenceTheme"
android:process=":ui"/>
<activity-alias <activity-alias
android:name=".ui.SettingsActivityLink" android:name=".ui.SettingsActivityLink"
android:exported="true" android:exported="true"
android:label="@string/eos_settings_title" android:label="@string/eos_settings_title"
android:process=":ui"
android:targetActivity=".ui.MainSettingsActivity"> android:targetActivity=".ui.MainSettingsActivity">
<intent-filter> <intent-filter>
<action android:name="com.android.settings.action.EXTRA_SETTINGS" /> <action android:name="com.android.settings.action.EXTRA_SETTINGS" />

View file

@ -8,6 +8,7 @@ import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import android.os.SystemClock import android.os.SystemClock
import android.provider.Settings
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
@ -94,8 +95,14 @@ class SubscriberService : Service() {
override fun onDestroy() { override fun onDestroy() {
Log.d(TAG, "Subscriber service has been destroyed") Log.d(TAG, "Subscriber service has been destroyed")
stopService() stopService()
val preferenceKey = getString(R.string.eos_preference_key_is_enabled)
if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(preferenceKey, false)) { val isEnabled = Settings.Global.getInt(
applicationContext.contentResolver,
applicationContext.getString(R.string.eos_preference_key_is_enabled),
0
)
if (isEnabled == 1) {
sendBroadcast(Intent(this, AutoRestartReceiver::class.java)) sendBroadcast(Intent(this, AutoRestartReceiver::class.java))
} }

View file

@ -2,6 +2,7 @@ package io.heckel.ntfy.service
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.provider.Settings
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.work.* import androidx.work.*
import io.heckel.ntfy.app.Application import io.heckel.ntfy.app.Application
@ -22,6 +23,7 @@ import kotlinx.coroutines.withContext
class SubscriberServiceManager(private val context: Context) { class SubscriberServiceManager(private val context: Context) {
fun refresh() { fun refresh() {
Log.d(TAG, "Enqueuing work to refresh subscriber service") Log.d(TAG, "Enqueuing work to refresh subscriber service")
val workManager = WorkManager.getInstance(context) val workManager = WorkManager.getInstance(context)
val startServiceRequest = OneTimeWorkRequest.Builder(ServiceStartWorker::class.java).build() val startServiceRequest = OneTimeWorkRequest.Builder(ServiceStartWorker::class.java).build()
workManager.enqueueUniqueWork(WORK_NAME_ONCE, ExistingWorkPolicy.KEEP, startServiceRequest) // Unique avoids races! workManager.enqueueUniqueWork(WORK_NAME_ONCE, ExistingWorkPolicy.KEEP, startServiceRequest) // Unique avoids races!
@ -46,10 +48,14 @@ class SubscriberServiceManager(private val context: Context) {
} }
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val app = context.applicationContext as Application
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(app) val isEnabled = Settings.Global.getInt(
val preferenceKey = context.getString(R.string.eos_preference_key_is_enabled) context.contentResolver,
val action = if (sharedPreferences.getBoolean(preferenceKey, false)) { context.getString(R.string.eos_preference_key_is_enabled),
0
)
val action = if (isEnabled == 1) {
SubscriberService.Action.START SubscriberService.Action.START
} else { } else {
SubscriberService.Action.STOP SubscriberService.Action.STOP

View file

@ -1,17 +1,12 @@
package io.heckel.ntfy.ui package io.heckel.ntfy.ui
import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.provider.Settings
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toolbar
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat import androidx.preference.SwitchPreferenceCompat
import io.heckel.ntfy.R import io.heckel.ntfy.R
import io.heckel.ntfy.service.SubscriberService import io.heckel.ntfy.service.SubscriberService
import io.heckel.ntfy.util.Log
class PreferencesFragment : PreferenceFragmentCompat() { class PreferencesFragment : PreferenceFragmentCompat() {
@ -23,6 +18,13 @@ class PreferencesFragment : PreferenceFragmentCompat() {
preference?.setOnPreferenceChangeListener { _, newValue -> preference?.setOnPreferenceChangeListener { _, newValue ->
val isChecked = newValue as Boolean val isChecked = newValue as Boolean
Settings.Global.putInt(
requireContext().contentResolver,
requireContext().getString(R.string.eos_preference_key_is_enabled),
if (isChecked) 1 else 0
)
val intent = Intent(context, SubscriberService::class.java) val intent = Intent(context, SubscriberService::class.java)
intent.action = if (isChecked) { intent.action = if (isChecked) {
SubscriberService.Action.START.name SubscriberService.Action.START.name

View file

@ -343,6 +343,6 @@
<string name="settings_advanced_unifiedpush_summary_disabled">ntfy arbeitet nicht als UnifiedPush-Distributor</string> <string name="settings_advanced_unifiedpush_summary_disabled">ntfy arbeitet nicht als UnifiedPush-Distributor</string>
<string name="settings_advanced_unifiedpush_title">UnifiedPush aktivieren</string> <string name="settings_advanced_unifiedpush_title">UnifiedPush aktivieren</string>
<string name="settings_advanced_unifiedpush_summary_enabled">ntfy arbeitet als UnifiedPush-Distributor</string> <string name="settings_advanced_unifiedpush_summary_enabled">ntfy arbeitet als UnifiedPush-Distributor</string>
<string name="eos_settings_enable_title">Den Verteiler aktivieren</string> <string name="eos_settings_enable_title">UnifiedPush-Distributor aktivieren</string>
<string name="eos_settings_enable_description">Es ermöglicht Drittanbieteranwendungen, UnifiedPush-Benachrichtigungen zu empfangen</string> <string name="eos_settings_enable_description">Es ermöglicht Drittanbieteranwendungen, UnifiedPush-Benachrichtigungen zu empfangen</string>
</resources> </resources>

View file

@ -343,6 +343,6 @@
<string name="settings_advanced_unifiedpush_title">Activar UnifiedPush</string> <string name="settings_advanced_unifiedpush_title">Activar UnifiedPush</string>
<string name="settings_advanced_unifiedpush_summary_enabled">ntfy actuará como distribuidor UnifiedPush</string> <string name="settings_advanced_unifiedpush_summary_enabled">ntfy actuará como distribuidor UnifiedPush</string>
<string name="settings_advanced_unifiedpush_summary_disabled">ntfy no actuará como distribuidor UnifiedPush</string> <string name="settings_advanced_unifiedpush_summary_disabled">ntfy no actuará como distribuidor UnifiedPush</string>
<string name="eos_settings_enable_title">Habilitar el distribuidor</string> <string name="eos_settings_enable_title">Habilitar el distribuidor de UnifiedPush</string>
<string name="eos_settings_enable_description">Permite a las aplicaciones de terceros recibir notificaciones de UnifiedPush</string> <string name="eos_settings_enable_description">Permite a las aplicaciones de terceros recibir notificaciones de UnifiedPush</string>
</resources> </resources>

View file

@ -343,6 +343,6 @@
<string name="settings_advanced_unifiedpush_summary_enabled">ntfy agira comme un distributeur UnifiedPush</string> <string name="settings_advanced_unifiedpush_summary_enabled">ntfy agira comme un distributeur UnifiedPush</string>
<string name="settings_advanced_unifiedpush_summary_disabled">ntfy n\'agira pas comme un distributeur UnifiedPush</string> <string name="settings_advanced_unifiedpush_summary_disabled">ntfy n\'agira pas comme un distributeur UnifiedPush</string>
<string name="settings_advanced_unifiedpush_title">Activer le \"UnifiedPush\"</string> <string name="settings_advanced_unifiedpush_title">Activer le \"UnifiedPush\"</string>
<string name="eos_settings_enable_title">Activer le distributeur</string> <string name="eos_settings_enable_title">Activer le distributeur UnifiedPush</string>
<string name="eos_settings_enable_description">Cela permet aux applications tierces de recevoir des notifications UnifiedPush</string> <string name="eos_settings_enable_description">Cela permet aux applications tierces de recevoir des notifications UnifiedPush</string>
</resources> </resources>

View file

@ -330,6 +330,6 @@
<string name="channel_notifications_group_default_name">Default</string> <string name="channel_notifications_group_default_name">Default</string>
<string name="main_menu_donate_title">Dona 💸</string> <string name="main_menu_donate_title">Dona 💸</string>
<string name="detail_item_cannot_open_apk">Le app non possono più essere installate: devono essere scaricate via browser. Vedi l\'issue #531 per dettagli.</string> <string name="detail_item_cannot_open_apk">Le app non possono più essere installate: devono essere scaricate via browser. Vedi l\'issue #531 per dettagli.</string>
<string name="eos_settings_enable_title">Abilitare il distributore</string> <string name="eos_settings_enable_title">Abilita il distributore UnifiedPush</string>
<string name="eos_settings_enable_description">Consente alle applicazioni di terze parti di ricevere notifiche UnifiedPush</string> <string name="eos_settings_enable_description">Consente alle applicazioni di terze parti di ricevere notifiche UnifiedPush</string>
</resources> </resources>

View file

@ -395,6 +395,6 @@
<!-- /e/OS integration preferences --> <!-- /e/OS integration preferences -->
<string name="eos_preference_key_is_enabled" translatable="false">isEnabled</string> <string name="eos_preference_key_is_enabled" translatable="false">isEnabled</string>
<string name="eos_settings_title" translatable="false">UnifiedPush</string> <string name="eos_settings_title" translatable="false">UnifiedPush</string>
<string name="eos_settings_enable_title" translatable="true">Enable the distributor</string> <string name="eos_settings_enable_title" translatable="true">Enable UnifiedPush distributor</string>
<string name="eos_settings_enable_description" translatable="true">It allows 3rd party applications to receive UnifiedPush notifications</string> <string name="eos_settings_enable_description" translatable="true">It allows 3rd party applications to receive UnifiedPush notifications</string>
</resources> </resources>