diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7cee65c..c71d9e5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -99,6 +99,20 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/felixnuesse/timedsilence/fragments/settings/GeneralFragment.kt b/app/src/main/java/de/felixnuesse/timedsilence/fragments/settings/GeneralFragment.kt index cf54b1e..b8aea2f 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/fragments/settings/GeneralFragment.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/fragments/settings/GeneralFragment.kt @@ -1,12 +1,36 @@ package de.felixnuesse.timedsilence.fragments.settings +import android.content.Intent +import android.content.SharedPreferences import android.os.Bundle +import android.provider.Settings +import androidx.core.content.ContextCompat import androidx.preference.PreferenceFragmentCompat import de.felixnuesse.timedsilence.R -class GeneralFragment : PreferenceFragmentCompat() { +class GeneralFragment : PreferenceFragmentCompat(), + SharedPreferences.OnSharedPreferenceChangeListener { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.general_preferences, rootKey) } + + override fun onResume() { + super.onResume() + preferenceManager.sharedPreferences!!.registerOnSharedPreferenceChangeListener(this) + } + + override fun onPause() { + preferenceManager.sharedPreferences!!.unregisterOnSharedPreferenceChangeListener(this) + super.onPause() + } + + override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) { + val pref_key = getString(R.string.pref_general_search_notifications) + if (key.equals(pref_key)) { + if (sharedPreferences?.getBoolean(pref_key, false) == true) { + ContextCompat.startActivity(this.requireContext(), Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), null) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesHolder.kt b/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesHolder.kt index fe4e809..bffce8e 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesHolder.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesHolder.kt @@ -27,4 +27,5 @@ class PreferencesHolder(@Transient val mDefaultVolumeValue: Int = 80) { @EncodeDefault var runWhenIdle = true @EncodeDefault var changeRingerVolume = false + @EncodeDefault var shouldSearchNotifications = false } diff --git a/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesManager.kt b/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesManager.kt index dcd4de4..b522a21 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesManager.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/handler/PreferencesManager.kt @@ -77,6 +77,11 @@ class PreferencesManager(private var mContext: Context) { mPreferencesEditor.apply() } + fun shouldSearchInNotifications(): Boolean{ + mPreferencesHolder.shouldSearchNotifications = mPreferences.getBoolean(getKey(R.string.pref_general_search_notifications), mPreferencesHolder.shouldSearchNotifications) + return mPreferencesHolder.shouldSearchNotifications + } + /////////////////////// // --- Calendar --- /// diff --git a/app/src/main/java/de/felixnuesse/timedsilence/receiver/NotificationListener.kt b/app/src/main/java/de/felixnuesse/timedsilence/receiver/NotificationListener.kt new file mode 100644 index 0000000..6bea1a2 --- /dev/null +++ b/app/src/main/java/de/felixnuesse/timedsilence/receiver/NotificationListener.kt @@ -0,0 +1,55 @@ +package de.felixnuesse.timedsilence.receiver + + +import android.service.notification.NotificationListenerService +import android.service.notification.StatusBarNotification +import android.util.Log +import de.felixnuesse.timedsilence.Constants +import de.felixnuesse.timedsilence.extensions.TAG +import de.felixnuesse.timedsilence.handler.PreferencesManager +import de.felixnuesse.timedsilence.handler.volume.VolumeHandler +import de.felixnuesse.timedsilence.handler.volume.VolumeState +import de.felixnuesse.timedsilence.model.database.DatabaseHandler +import de.felixnuesse.timedsilence.volumestate.StateGenerator + + +class NotificationListener : NotificationListenerService() { + + private val TAG: String = this.javaClass.simpleName + + override fun onNotificationPosted(notification: StatusBarNotification) { + handleNotification(false, notification) + } + + override fun onNotificationRemoved(notification: StatusBarNotification) { + handleNotification(true, notification) + } + + fun handleNotification(isRemove: Boolean, notification: StatusBarNotification) { + val proceed = PreferencesManager(this).shouldSearchInNotifications() + if(!proceed) { + return + } + + if(isRemove) { + Log.e(TAG(), "NotificationListener: Removed notification, check!") + VolumeHandler(this, "NotificationListener").setVolumeStateAndApply(StateGenerator(this).stateAt(System.currentTimeMillis())) + return + } + + Log.e(TAG(), "NotificationListener: Posted notification, check!") + + val toSearch = notification.notification.extras.toString()+notification.notification.toString()+notification.toString() + + val db = DatabaseHandler(this) + db.getKeywords().forEach { + if (toSearch.lowercase().contains(it.keyword.lowercase())) { + val state = VolumeState(it.volume) + state.setReason(Constants.REASON_MANUALLY_SET, "Keyword ${it.keyword} was found in notification") + VolumeHandler(this, "NotificationListener").setVolumeStateAndApply(state) + return + } + } + } + +} diff --git a/app/src/main/res/values/stringpreferences.xml b/app/src/main/res/values/stringpreferences.xml index 2e39d6c..bb747b5 100644 --- a/app/src/main/res/values/stringpreferences.xml +++ b/app/src/main/res/values/stringpreferences.xml @@ -24,6 +24,7 @@ PREF_BOOT_RESTART PREF_BOOT_FORCE_RESTART + PREF_GENERAL_SEARCH_NOTIFICATIONS diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f601b37..0185f84 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -190,4 +190,6 @@ Fix! Bluetooth Access not granted. When the device restarted, also restart checks, regardless of their pre-reboot-state. + TimedSilence Notification Listener + Allow Notification content to be searched for keywords diff --git a/app/src/main/res/xml/general_preferences.xml b/app/src/main/res/xml/general_preferences.xml index 1b234c2..11403c0 100644 --- a/app/src/main/res/xml/general_preferences.xml +++ b/app/src/main/res/xml/general_preferences.xml @@ -26,6 +26,13 @@ android:defaultValue="false" android:title="@string/force_restart_checks_after_reboot" android:key="@string/pref_general_boot_force_restart" /> + + \ No newline at end of file