Optimize notification handling, fix activity detection and greetings.

This commit is contained in:
azuo 2021-10-01 17:05:43 +08:00
parent 5dcf0afe02
commit 5763a18421
6 changed files with 66 additions and 30 deletions

View File

@ -177,6 +177,8 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance
Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> {
Preferences.showNextAlarm = isChecked Preferences.showNextAlarm = isChecked
checkNextAlarm() checkNextAlarm()
if (!isChecked)
AlarmHelper.clearTimeout(context)
} }
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {
Preferences.showBatteryCharging = isChecked Preferences.showBatteryCharging = isChecked
@ -184,6 +186,8 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance
Constants.GlanceProviderId.NOTIFICATIONS -> { Constants.GlanceProviderId.NOTIFICATIONS -> {
Preferences.showNotifications = isChecked Preferences.showNotifications = isChecked
checkLastNotificationsPermission() checkLastNotificationsPermission()
if (!isChecked)
ActiveNotificationsHelper.clearLastNotification(context)
} }
Constants.GlanceProviderId.GREETINGS -> { Constants.GlanceProviderId.GREETINGS -> {
Preferences.showGreetings = isChecked Preferences.showGreetings = isChecked
@ -238,8 +242,6 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance
} }
private fun checkNextAlarm() { private fun checkNextAlarm() {
if (!Preferences.showNextAlarm)
AlarmHelper.clearTimeout(context)
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) { with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
val alarm = nextAlarmClock val alarm = nextAlarmClock
if (alarm != null && alarm.showIntent != null) { if (alarm != null && alarm.showIntent != null) {

View File

@ -25,6 +25,7 @@ object ActiveNotificationsHelper {
remove(Preferences::lastNotificationIcon) remove(Preferences::lastNotificationIcon)
} }
MainWidget.updateWidget(context) MainWidget.updateWidget(context)
NotificationListener.clearTimeout(context)
} }
fun checkNotificationAccess(context: Context): Boolean { fun checkNotificationAccess(context: Context): Boolean {

View File

@ -102,9 +102,9 @@ object GreetingsHelper {
val array = when { val array = when {
hour in 5..8 -> context.resources.getStringArray(R.array.morning_greetings) hour in 5..8 -> context.resources.getStringArray(R.array.morning_greetings)
hour in 19..21 -> context.resources.getStringArray(R.array.evening_greetings) hour in 19..21 -> context.resources.getStringArray(R.array.evening_greetings)
hour >= 22 && hour < 5 -> context.resources.getStringArray(R.array.night_greetings) hour >= 22 || hour < 5 -> context.resources.getStringArray(R.array.night_greetings)
else -> emptyArray() else -> emptyArray()
} }
return if (array.isNotEmpty()) array[Random().nextInt(array.size)] else "" return if (array.isNotEmpty()) array[Random().nextInt(array.size)] else ""
} }
} }

View File

@ -32,11 +32,11 @@ class ActivityDetectionReceiver : BroadcastReceiver() {
val result = ActivityTransitionResult.extractResult(intent)!! val result = ActivityTransitionResult.extractResult(intent)!!
val lastEvent = result.transitionEvents.last() val lastEvent = result.transitionEvents.last()
if (lastEvent.activityType == DetectedActivity.WALKING || lastEvent.activityType == DetectedActivity.RUNNING && lastEvent.transitionType == ActivityTransition.ACTIVITY_TRANSITION_EXIT) { if ((lastEvent.activityType == DetectedActivity.WALKING || lastEvent.activityType == DetectedActivity.RUNNING) && lastEvent.transitionType == ActivityTransition.ACTIVITY_TRANSITION_EXIT) {
requestDailySteps(context) requestDailySteps(context)
} }
} else { } else {
if (intent.action == Intent.ACTION_BOOT_COMPLETED || intent.action == Intent.ACTION_MY_PACKAGE_REPLACED && Preferences.showDailySteps && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || context.checkGrantedPermission(Manifest.permission.ACTIVITY_RECOGNITION)) { if ((intent.action == Intent.ACTION_BOOT_COMPLETED || intent.action == Intent.ACTION_MY_PACKAGE_REPLACED) && Preferences.showDailySteps && (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || context.checkGrantedPermission(Manifest.permission.ACTIVITY_RECOGNITION))) {
resetDailySteps(context) resetDailySteps(context)
registerFence(context) registerFence(context)
} else { } else {
@ -45,13 +45,6 @@ class ActivityDetectionReceiver : BroadcastReceiver() {
} }
} }
private fun resetDailySteps(context: Context) {
Kotpref.init(context)
Preferences.blockingBulk {
remove(Preferences::googleFitSteps)
}
}
companion object { companion object {
val FITNESS_OPTIONS: FitnessOptions = FitnessOptions.builder() val FITNESS_OPTIONS: FitnessOptions = FitnessOptions.builder()
.addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ) .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
@ -119,6 +112,9 @@ class ActivityDetectionReceiver : BroadcastReceiver() {
).cancel() ).cancel()
} }
} }
resetDailySteps(context)
clearTimeout(context)
} }
fun requestDailySteps(context: Context) { fun requestDailySteps(context: Context) {
@ -162,9 +158,15 @@ class ActivityDetectionReceiver : BroadcastReceiver() {
} }
} }
private fun resetDailySteps(context: Context) {
Kotpref.init(context)
Preferences.blockingBulk {
remove(Preferences::googleFitSteps)
}
}
private fun setTimeout(context: Context) { private fun setTimeout(context: Context) {
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) { with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
cancel(PendingIntent.getBroadcast(context, 5, Intent(context, ActivityDetectionReceiver::class.java), 0))
setExact( setExact(
AlarmManager.RTC, AlarmManager.RTC,
Calendar.getInstance().timeInMillis + 5 * 60 * 1000, Calendar.getInstance().timeInMillis + 5 * 60 * 1000,
@ -177,5 +179,18 @@ class ActivityDetectionReceiver : BroadcastReceiver() {
) )
} }
} }
private fun clearTimeout(context: Context) {
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
cancel(
PendingIntent.getBroadcast(
context,
5,
Intent(context, ActivityDetectionReceiver::class.java),
0
)
)
}
}
} }
} }

View File

@ -23,19 +23,26 @@ import java.util.*
class NotificationListener : NotificationListenerService() { class NotificationListener : NotificationListenerService() {
override fun onListenerConnected() { override fun onListenerConnected() {
MediaPlayerHelper.updatePlayingMediaInfo(this) MediaPlayerHelper.updatePlayingMediaInfo(this)
MainWidget.updateWidget(this) ActiveNotificationsHelper.clearLastNotification(this)
super.onListenerConnected() super.onListenerConnected()
} }
override fun onListenerDisconnected() {
MediaPlayerHelper.updatePlayingMediaInfo(this)
ActiveNotificationsHelper.clearLastNotification(this)
super.onListenerDisconnected()
}
override fun onNotificationPosted(sbn: StatusBarNotification?) { override fun onNotificationPosted(sbn: StatusBarNotification?) {
sbn?.notification?.extras?.let { bundle -> sbn?.notification?.extras?.let { bundle ->
bundle.getParcelable<MediaSession.Token>(Notification.EXTRA_MEDIA_SESSION)?.let { bundle.getParcelable<MediaSession.Token>(Notification.EXTRA_MEDIA_SESSION)?.let {
MediaPlayerHelper.updatePlayingMediaInfo(this) if (Preferences.showMusic)
MediaPlayerHelper.updatePlayingMediaInfo(this)
} ?: run { } ?: run {
val isGroupHeader = sbn.notification.flags and Notification.FLAG_GROUP_SUMMARY != 0 val isGroupHeader = sbn.notification.flags and Notification.FLAG_GROUP_SUMMARY != 0
val isOngoing = sbn.notification.flags and Notification.FLAG_ONGOING_EVENT != 0 val isOngoing = sbn.notification.flags and Notification.FLAG_ONGOING_EVENT != 0
if (bundle.containsKey(Notification.EXTRA_TITLE) && !isGroupHeader && !isOngoing && ActiveNotificationsHelper.isAppAccepted(sbn.packageName) && !sbn.packageName.contains("com.android.systemui")) { if (Preferences.showNotifications && bundle.containsKey(Notification.EXTRA_TITLE) && !isGroupHeader && !isOngoing && ActiveNotificationsHelper.isAppAccepted(sbn.packageName) && !sbn.packageName.contains("com.android.systemui")) {
Preferences.lastNotificationId = sbn.id Preferences.lastNotificationId = sbn.id
Preferences.lastNotificationTitle = bundle.getString(Notification.EXTRA_TITLE) ?: "" Preferences.lastNotificationTitle = bundle.getString(Notification.EXTRA_TITLE) ?: ""
try { try {
@ -59,15 +66,13 @@ class NotificationListener : NotificationListenerService() {
} }
override fun onNotificationRemoved(sbn: StatusBarNotification?) { override fun onNotificationRemoved(sbn: StatusBarNotification?) {
MediaPlayerHelper.updatePlayingMediaInfo(this) if (Preferences.showMusic)
MediaPlayerHelper.updatePlayingMediaInfo(this)
sbn?.let { sbn?.let {
if (sbn.id == Preferences.lastNotificationId && sbn.packageName == Preferences.lastNotificationPackage) { if (Preferences.showNotifications && sbn.id == Preferences.lastNotificationId && sbn.packageName == Preferences.lastNotificationPackage) {
ActiveNotificationsHelper.clearLastNotification(this) ActiveNotificationsHelper.clearLastNotification(this)
} }
} }
MainWidget.updateWidget(this)
super.onNotificationRemoved(sbn) super.onNotificationRemoved(sbn)
} }
@ -76,7 +81,6 @@ class NotificationListener : NotificationListenerService() {
val intent = Intent(context, UpdatesReceiver::class.java).apply { val intent = Intent(context, UpdatesReceiver::class.java).apply {
action = Actions.ACTION_CLEAR_NOTIFICATION action = Actions.ACTION_CLEAR_NOTIFICATION
} }
cancel(PendingIntent.getBroadcast(context, 28943, intent, 0))
val timeoutPref = Constants.GlanceNotificationTimer.fromInt(Preferences.hideNotificationAfter) val timeoutPref = Constants.GlanceNotificationTimer.fromInt(Preferences.hideNotificationAfter)
if (timeoutPref != Constants.GlanceNotificationTimer.WHEN_DISMISSED) { if (timeoutPref != Constants.GlanceNotificationTimer.WHEN_DISMISSED) {
setExact( setExact(
@ -87,7 +91,7 @@ class NotificationListener : NotificationListenerService() {
Constants.GlanceNotificationTimer.FIVE_MINUTES -> 5 * 60 * 1000 Constants.GlanceNotificationTimer.FIVE_MINUTES -> 5 * 60 * 1000
Constants.GlanceNotificationTimer.TEN_MINUTES -> 10 * 60 * 1000 Constants.GlanceNotificationTimer.TEN_MINUTES -> 10 * 60 * 1000
Constants.GlanceNotificationTimer.FIFTEEN_MINUTES -> 15 * 60 * 1000 Constants.GlanceNotificationTimer.FIFTEEN_MINUTES -> 15 * 60 * 1000
else -> 0 else -> 60 * 1000
}, },
PendingIntent.getBroadcast( PendingIntent.getBroadcast(
context, context,
@ -99,4 +103,15 @@ class NotificationListener : NotificationListenerService() {
} }
} }
} }
}
companion object {
fun clearTimeout(context: Context) {
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
val intent = Intent(context, UpdatesReceiver::class.java).apply {
action = Actions.ACTION_CLEAR_NOTIFICATION
}
cancel(PendingIntent.getBroadcast(context, 5, intent, 0))
}
}
}
}

View File

@ -27,11 +27,15 @@ class UpdatesReceiver : BroadcastReceiver() {
Intent.ACTION_MY_PACKAGE_REPLACED, Intent.ACTION_MY_PACKAGE_REPLACED,
Intent.ACTION_TIME_CHANGED, Intent.ACTION_TIME_CHANGED,
Intent.ACTION_TIMEZONE_CHANGED, Intent.ACTION_TIMEZONE_CHANGED,
Intent.ACTION_LOCALE_CHANGED, Intent.ACTION_LOCALE_CHANGED -> {
CalendarHelper.updateEventList(context)
MediaPlayerHelper.updatePlayingMediaInfo(context)
ActiveNotificationsHelper.clearLastNotification(context)
GreetingsHelper.toggleGreetings(context)
}
Intent.ACTION_DATE_CHANGED, Intent.ACTION_DATE_CHANGED,
Actions.ACTION_CALENDAR_UPDATE -> { Actions.ACTION_CALENDAR_UPDATE -> {
ActiveNotificationsHelper.clearLastNotification(context)
MediaPlayerHelper.updatePlayingMediaInfo(context)
CalendarHelper.updateEventList(context) CalendarHelper.updateEventList(context)
} }
@ -48,7 +52,6 @@ class UpdatesReceiver : BroadcastReceiver() {
Actions.ACTION_CLEAR_NOTIFICATION -> { Actions.ACTION_CLEAR_NOTIFICATION -> {
ActiveNotificationsHelper.clearLastNotification(context) ActiveNotificationsHelper.clearLastNotification(context)
MainWidget.updateWidget(context)
} }
Actions.ACTION_REFRESH -> { Actions.ACTION_REFRESH -> {