-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
5 changed files
with
113 additions
and
33 deletions.
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
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
62 changes: 34 additions & 28 deletions
62
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 |
---|---|---|
@@ -1,55 +1,61 @@ | ||
package de.felixnuesse.timedsilence.receiver | ||
|
||
|
||
import android.os.Build | ||
import android.service.notification.NotificationListenerService | ||
import android.service.notification.StatusBarNotification | ||
import android.util.Log | ||
import de.felixnuesse.timedsilence.Constants | ||
import androidx.annotation.RequiresApi | ||
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 | ||
companion object { | ||
private var service: NotificationListener? = null | ||
fun getService(): NotificationListener? { | ||
return service | ||
} | ||
} | ||
|
||
override fun onNotificationPosted(notification: StatusBarNotification) { | ||
handleNotification(false, notification) | ||
var allNotifications = arrayListOf<StatusBarNotification>() | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
service = this | ||
} | ||
|
||
override fun onNotificationRemoved(notification: StatusBarNotification) { | ||
handleNotification(true, notification) | ||
fun getAllActiveNotifications(): ArrayList<StatusBarNotification> { | ||
return allNotifications | ||
} | ||
|
||
fun handleNotification(isRemove: Boolean, notification: StatusBarNotification) { | ||
val proceed = PreferencesManager(this).shouldSearchInNotifications() | ||
if(!proceed) { | ||
override fun onNotificationPosted(notification: StatusBarNotification) { | ||
if(notification.packageName == this.baseContext.packageName) { | ||
return | ||
} | ||
allNotifications.clear() | ||
allNotifications.addAll(activeNotifications) | ||
handleNotification() | ||
} | ||
|
||
if(isRemove) { | ||
Log.e(TAG(), "NotificationListener: Removed notification, check!") | ||
VolumeHandler(this, "NotificationListener").setVolumeStateAndApply(StateGenerator(this).stateAt(System.currentTimeMillis())) | ||
override fun onNotificationRemoved(notification: StatusBarNotification) { | ||
if(notification.packageName == this.baseContext.packageName) { | ||
return | ||
} | ||
allNotifications.clear() | ||
allNotifications.addAll(activeNotifications) | ||
handleNotification() | ||
} | ||
|
||
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 | ||
} | ||
private fun handleNotification() { | ||
val proceed = PreferencesManager(this).shouldSearchInNotifications() | ||
if(!proceed) { | ||
return | ||
} | ||
} | ||
|
||
Log.e(TAG(), "NotificationListener: Posted or removed notification, check!") | ||
VolumeHandler(this, "NotificationListener").setVolumeStateAndApply(StateGenerator(this).stateAt(System.currentTimeMillis())) | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
app/src/main/java/de/felixnuesse/timedsilence/volumestate/Notifications.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,66 @@ | ||
package de.felixnuesse.timedsilence.volumestate | ||
|
||
|
||
import android.app.NotificationManager | ||
import android.content.Context | ||
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.VolumeState | ||
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_UNSET | ||
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.isFirstLouder | ||
import de.felixnuesse.timedsilence.model.database.DatabaseHandler | ||
import de.felixnuesse.timedsilence.receiver.NotificationListener | ||
|
||
|
||
open class Notifications(private var mContext: Context): DeterministicCalculationInterface() { | ||
|
||
private val mPrefs = PreferencesManager(mContext) | ||
private val mDB = DatabaseHandler(mContext) | ||
|
||
override fun stateAt(timeInMs: Long): ArrayList<VolumeState> { | ||
return ArrayList() | ||
} | ||
|
||
override fun states(): ArrayList<VolumeState> { | ||
val list = ArrayList<VolumeState>() | ||
|
||
if(mPrefs.shouldSearchInNotifications()) { | ||
var currentVolumeState = VolumeState(TIME_SETTING_UNSET) | ||
NotificationListener.getService()?.getAllActiveNotifications()?.forEach { | ||
val checkedState = handleNotification(it)?: VolumeState(TIME_SETTING_UNSET) | ||
if (isFirstLouder(checkedState, currentVolumeState)) { | ||
currentVolumeState = checkedState | ||
} | ||
} | ||
if(currentVolumeState.state != TIME_SETTING_UNSET){ | ||
list.add(currentVolumeState) | ||
} | ||
} | ||
|
||
Log.e(TAG(), "Size: ${list.size}") | ||
return list | ||
} | ||
|
||
private fun handleNotification(notification: StatusBarNotification): VolumeState? { | ||
Log.e(TAG(), "Size: ${notification.notification}") | ||
val toSearch = notification.notification.extras.toString()+notification.notification.toString()+notification.toString() | ||
|
||
mDB.getKeywords().forEach { | ||
if (toSearch.lowercase().contains(it.keyword.lowercase())) { | ||
val state = VolumeState(it.volume) | ||
state.startTime = System.currentTimeMillis() | ||
state.endTime = state.startTime + 60*1000 // add a minute | ||
state.setReason(Constants.REASON_NOTIFICATION_VISIBLE, "Keyword: ${it.keyword}") | ||
return state | ||
} | ||
} | ||
return null | ||
} | ||
|
||
override fun isEnabled(): Boolean { | ||
return mPrefs.shouldSearchInNotifications() | ||
} | ||
} |
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