Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 51294c23 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

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.
parent db299383
Loading
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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.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.WRITE_SECURE_SETTINGS"
        tools:ignore="ProtectedPermissions" />

    <!--
        Permission REQUEST_INSTALL_PACKAGES (F-Droid only!):
+9 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import android.os.SystemClock
import android.provider.Settings
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager
@@ -94,8 +95,14 @@ class SubscriberService : Service() {
    override fun onDestroy() {
        Log.d(TAG, "Subscriber service has been destroyed")
        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))
        }

+10 −4
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package io.heckel.ntfy.service

import android.content.Context
import android.content.Intent
import android.provider.Settings
import androidx.preference.PreferenceManager
import androidx.work.*
import io.heckel.ntfy.app.Application
@@ -22,6 +23,7 @@ import kotlinx.coroutines.withContext
class SubscriberServiceManager(private val context: Context) {
    fun refresh() {
        Log.d(TAG, "Enqueuing work to refresh subscriber service")

        val workManager = WorkManager.getInstance(context)
        val startServiceRequest = OneTimeWorkRequest.Builder(ServiceStartWorker::class.java).build()
        workManager.enqueueUniqueWork(WORK_NAME_ONCE, ExistingWorkPolicy.KEEP, startServiceRequest) // Unique avoids races!
@@ -46,10 +48,14 @@ class SubscriberServiceManager(private val context: Context) {
            }

            withContext(Dispatchers.IO) {
                val app = context.applicationContext as Application
                val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(app)
                val preferenceKey = context.getString(R.string.eos_preference_key_is_enabled)
                val action = if (sharedPreferences.getBoolean(preferenceKey, false)) {

                val isEnabled = Settings.Global.getInt(
                    context.contentResolver,
                    context.getString(R.string.eos_preference_key_is_enabled),
                    0
                )

                val action = if (isEnabled == 1) {
                    SubscriberService.Action.START
                } else {
                    SubscriberService.Action.STOP
+8 −6
Original line number Diff line number Diff line
package io.heckel.ntfy.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toolbar
import android.provider.Settings
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreferenceCompat
import io.heckel.ntfy.R
import io.heckel.ntfy.service.SubscriberService
import io.heckel.ntfy.util.Log

class PreferencesFragment : PreferenceFragmentCompat() {

@@ -23,6 +18,13 @@ class PreferencesFragment : PreferenceFragmentCompat() {

        preference?.setOnPreferenceChangeListener { _, newValue ->
            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)
            intent.action = if (isChecked) {
                SubscriberService.Action.START.name