Skip to content

Commit

Permalink
allow app to search notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Aug 29, 2024
1 parent b5c6cdd commit 08d1766
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 1 deletion.
14 changes: 14 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<service android:name=".receiver.NotificationListener"
android:label="@string/notification_listener_service_name"
android:exported="false"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
<meta-data
android:name="android.service.notification.default_filter_types"
android:value="conversations|alerting|ongoing">
</meta-data>
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ class PreferencesHolder(@Transient val mDefaultVolumeValue: Int = 80) {
@EncodeDefault var runWhenIdle = true

@EncodeDefault var changeRingerVolume = false
@EncodeDefault var shouldSearchNotifications = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 --- ///
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values/stringpreferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<string name="pref_general_boot_restart" translatable="false">PREF_BOOT_RESTART</string>
<string name="pref_general_boot_force_restart" translatable="false">PREF_BOOT_FORCE_RESTART</string>
<string name="pref_general_search_notifications" translatable="false">PREF_GENERAL_SEARCH_NOTIFICATIONS</string>



Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@
<string name="dnd_missing_permission_action_title">Fix!</string>
<string name="bluetooth_missing_permissions">Bluetooth Access not granted.</string>
<string name="force_restart_checks_after_reboot">When the device restarted, also restart checks, regardless of their pre-reboot-state.</string>
<string name="notification_listener_service_name">TimedSilence Notification Listener</string>
<string name="use_keywords_for_notifications">Allow Notification content to be searched for keywords</string>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/general_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
android:defaultValue="false"
android:title="@string/force_restart_checks_after_reboot"
android:key="@string/pref_general_boot_force_restart" />

<SwitchPreference
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:defaultValue="false"
android:title="@string/use_keywords_for_notifications"
android:key="@string/pref_general_search_notifications" />
</PreferenceCategory>

</PreferenceScreen>

0 comments on commit 08d1766

Please sign in to comment.