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
4 changed files with 29 additions and 12 deletions

View file

@ -12,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!):

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