From fe86329af5da82048d94ef173cfc8fff76ed666a Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Wed, 11 Sep 2024 13:47:27 +0200 Subject: [PATCH] Add Preferences to enable Ntfy in the Settings app --- app/build.gradle | 6 ++++ app/src/main/AndroidManifest.xml | 30 ++++++++++++++++++- .../heckel/ntfy/service/SubscriberService.kt | 6 ++-- .../ntfy/service/SubscriberServiceManager.kt | 13 +++++--- .../io/heckel/ntfy/ui/MainSettingsActivity.kt | 24 +++++++++++++++ .../io/heckel/ntfy/ui/PreferencesFragment.kt | 29 ++++++++++++++++++ app/src/main/res/drawable/ic_notification.xml | 14 ++++----- .../res/layout/main_settings_activity.xml | 11 +++++++ app/src/main/res/values/strings.xml | 5 ++++ app/src/main/res/xml/settings_preferences.xml | 10 +++++++ build.gradle | 1 + 11 files changed, 135 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/io/heckel/ntfy/ui/MainSettingsActivity.kt create mode 100644 app/src/main/java/io/heckel/ntfy/ui/PreferencesFragment.kt create mode 100644 app/src/main/res/layout/main_settings_activity.xml create mode 100644 app/src/main/res/xml/settings_preferences.xml diff --git a/app/build.gradle b/app/build.gradle index 90dc1ed..a9d448f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,6 +27,10 @@ android { } } + buildFeatures { + viewBinding = true + } + buildTypes { release { minifyEnabled true @@ -128,4 +132,6 @@ dependencies { // Image viewer implementation 'com.github.stfalcon-studio:StfalconImageViewer:v1.0.1' + + implementation 'foundation.e:elib:0.0.1-alpha11' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5cccf5c..348682f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + xmlns:tools="http://schemas.android.com/tools" + package="io.heckel.ntfy"> @@ -176,5 +177,32 @@ android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"/> + + + + + + + + + + diff --git a/app/src/main/java/io/heckel/ntfy/service/SubscriberService.kt b/app/src/main/java/io/heckel/ntfy/service/SubscriberService.kt index 7fd6f93..4268e85 100644 --- a/app/src/main/java/io/heckel/ntfy/service/SubscriberService.kt +++ b/app/src/main/java/io/heckel/ntfy/service/SubscriberService.kt @@ -10,6 +10,7 @@ import android.os.PowerManager import android.os.SystemClock import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat +import androidx.preference.PreferenceManager import io.heckel.ntfy.BuildConfig import io.heckel.ntfy.R import io.heckel.ntfy.app.Application @@ -93,7 +94,9 @@ class SubscriberService : Service() { override fun onDestroy() { Log.d(TAG, "Subscriber service has been destroyed") stopService() - sendBroadcast(Intent(this, AutoRestartReceiver::class.java)) // Restart it if necessary! + if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("isEnabled", false)) { + sendBroadcast(Intent(this, AutoRestartReceiver::class.java)) + } super.onDestroy() } @@ -127,7 +130,6 @@ class SubscriberService : Service() { } } wakeLock = null - stopForeground(true) stopSelf() } catch (e: Exception) { Log.d(TAG, "Service stopped without being started: ${e.message}") diff --git a/app/src/main/java/io/heckel/ntfy/service/SubscriberServiceManager.kt b/app/src/main/java/io/heckel/ntfy/service/SubscriberServiceManager.kt index f7573f2..8301363 100644 --- a/app/src/main/java/io/heckel/ntfy/service/SubscriberServiceManager.kt +++ b/app/src/main/java/io/heckel/ntfy/service/SubscriberServiceManager.kt @@ -2,7 +2,7 @@ package io.heckel.ntfy.service import android.content.Context import android.content.Intent -import androidx.core.content.ContextCompat +import androidx.preference.PreferenceManager import androidx.work.* import io.heckel.ntfy.app.Application import io.heckel.ntfy.util.Log @@ -43,11 +43,16 @@ class SubscriberServiceManager(private val context: Context) { Log.d(TAG, "ServiceStartWorker: Failed, no application found (work ID: ${id})") return Result.failure() } + withContext(Dispatchers.IO) { val app = context.applicationContext as Application - val subscriptionIdsWithInstantStatus = app.repository.getSubscriptionIdsWithInstantStatus() - val instantSubscriptions = subscriptionIdsWithInstantStatus.toList().filter { (_, instant) -> instant }.size - val action = if (instantSubscriptions > 0) SubscriberService.Action.START else SubscriberService.Action.STOP + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(app) + val action = if (sharedPreferences.getBoolean("isEnabled", false)) { + SubscriberService.Action.START + } else { + SubscriberService.Action.STOP + } + val serviceState = SubscriberService.readServiceState(context) if (serviceState == SubscriberService.ServiceState.STOPPED && action == SubscriberService.Action.STOP) { return@withContext Result.success() diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainSettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainSettingsActivity.kt new file mode 100644 index 0000000..6675363 --- /dev/null +++ b/app/src/main/java/io/heckel/ntfy/ui/MainSettingsActivity.kt @@ -0,0 +1,24 @@ +package io.heckel.ntfy.ui + +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import io.heckel.ntfy.R +import io.heckel.ntfy.databinding.MainSettingsActivityBinding + + +class MainSettingsActivity: AppCompatActivity() { + + private lateinit var mBinding: MainSettingsActivityBinding + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + mBinding = MainSettingsActivityBinding.inflate(layoutInflater) + setContentView(mBinding.root) + + supportFragmentManager + .beginTransaction() + .replace(R.id.fragment_container, PreferencesFragment()) + .commit() + } +} \ No newline at end of file diff --git a/app/src/main/java/io/heckel/ntfy/ui/PreferencesFragment.kt b/app/src/main/java/io/heckel/ntfy/ui/PreferencesFragment.kt new file mode 100644 index 0000000..fac1ee1 --- /dev/null +++ b/app/src/main/java/io/heckel/ntfy/ui/PreferencesFragment.kt @@ -0,0 +1,29 @@ +package io.heckel.ntfy.ui + +import android.content.Intent +import android.os.Bundle +import androidx.preference.PreferenceFragmentCompat +import androidx.preference.SwitchPreferenceCompat +import io.heckel.ntfy.R +import io.heckel.ntfy.service.SubscriberService + +class PreferencesFragment : PreferenceFragmentCompat() { + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + setPreferencesFromResource(R.xml.settings_preferences, rootKey) + val preference: SwitchPreferenceCompat? = findPreference("isEnabled") + + preference?.setOnPreferenceChangeListener { _, newValue -> + val isChecked = newValue as Boolean + val intent = Intent(context, SubscriberService::class.java) + intent.action = if (isChecked) { + SubscriberService.Action.START.name + } else { + SubscriberService.Action.STOP.name + } + + requireContext().startService(intent) + true + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_notification.xml b/app/src/main/res/drawable/ic_notification.xml index d02140a..900100b 100644 --- a/app/src/main/res/drawable/ic_notification.xml +++ b/app/src/main/res/drawable/ic_notification.xml @@ -1,31 +1,31 @@ diff --git a/app/src/main/res/layout/main_settings_activity.xml b/app/src/main/res/layout/main_settings_activity.xml new file mode 100644 index 0000000..4d24822 --- /dev/null +++ b/app/src/main/res/layout/main_settings_activity.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c775990..81f918a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -391,4 +391,9 @@ Cancel Delete user Save + + + UnifiedPush + Enable UnifiedPush distributor + This will allow applications to receive UnifiedPush notifications diff --git a/app/src/main/res/xml/settings_preferences.xml b/app/src/main/res/xml/settings_preferences.xml new file mode 100644 index 0000000..55da1ee --- /dev/null +++ b/app/src/main/res/xml/settings_preferences.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index fd50800..8659258 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ allprojects { repositories { google() mavenCentral() + maven { url 'https://gitlab.e.foundation/api/v4/groups/9/-/packages/maven'} maven { url "https://jitpack.io" } // For StfalconImageViewer } }