-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 25 additions & 1 deletion
26
app/src/main/java/de/felixnuesse/timedsilence/fragments/settings/GeneralFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/main/java/de/felixnuesse/timedsilence/receiver/NotificationListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters