From 5763a184213d8dbfbdf6a7ab16edfab57ebfc4d8 Mon Sep 17 00:00:00 2001 From: azuo Date: Fri, 1 Oct 2021 17:05:43 +0800 Subject: [PATCH] Optimize notification handling, fix activity detection and greetings. --- .../components/GlanceSettingsDialog.kt | 6 ++- .../helpers/ActiveNotificationsHelper.kt | 1 + .../anotherwidget/helpers/GreetingsHelper.kt | 4 +- .../receivers/ActivityDetectionReceiver.kt | 37 +++++++++++++------ .../receivers/NotificationListener.kt | 37 +++++++++++++------ .../receivers/UpdatesReceiver.kt | 11 ++++-- 6 files changed, 66 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/components/GlanceSettingsDialog.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/components/GlanceSettingsDialog.kt index c8d35a4..ab13fed 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/components/GlanceSettingsDialog.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/components/GlanceSettingsDialog.kt @@ -177,6 +177,8 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { Preferences.showNextAlarm = isChecked checkNextAlarm() + if (!isChecked) + AlarmHelper.clearTimeout(context) } Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { Preferences.showBatteryCharging = isChecked @@ -184,6 +186,8 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance Constants.GlanceProviderId.NOTIFICATIONS -> { Preferences.showNotifications = isChecked checkLastNotificationsPermission() + if (!isChecked) + ActiveNotificationsHelper.clearLastNotification(context) } Constants.GlanceProviderId.GREETINGS -> { Preferences.showGreetings = isChecked @@ -238,8 +242,6 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance } private fun checkNextAlarm() { - if (!Preferences.showNextAlarm) - AlarmHelper.clearTimeout(context) with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) { val alarm = nextAlarmClock if (alarm != null && alarm.showIntent != null) { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/ActiveNotificationsHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/ActiveNotificationsHelper.kt index 658ec79..bfac0c7 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/ActiveNotificationsHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/ActiveNotificationsHelper.kt @@ -25,6 +25,7 @@ object ActiveNotificationsHelper { remove(Preferences::lastNotificationIcon) } MainWidget.updateWidget(context) + NotificationListener.clearTimeout(context) } fun checkNotificationAccess(context: Context): Boolean { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/GreetingsHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/GreetingsHelper.kt index 3ceec5b..04432d3 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/GreetingsHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/GreetingsHelper.kt @@ -102,9 +102,9 @@ object GreetingsHelper { val array = when { hour in 5..8 -> context.resources.getStringArray(R.array.morning_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() } return if (array.isNotEmpty()) array[Random().nextInt(array.size)] else "" } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/ActivityDetectionReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/ActivityDetectionReceiver.kt index 200deb4..bf4fd94 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/ActivityDetectionReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/ActivityDetectionReceiver.kt @@ -32,11 +32,11 @@ class ActivityDetectionReceiver : BroadcastReceiver() { val result = ActivityTransitionResult.extractResult(intent)!! 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) } } 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) registerFence(context) } else { @@ -45,13 +45,6 @@ class ActivityDetectionReceiver : BroadcastReceiver() { } } - private fun resetDailySteps(context: Context) { - Kotpref.init(context) - Preferences.blockingBulk { - remove(Preferences::googleFitSteps) - } - } - companion object { val FITNESS_OPTIONS: FitnessOptions = FitnessOptions.builder() .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ) @@ -119,6 +112,9 @@ class ActivityDetectionReceiver : BroadcastReceiver() { ).cancel() } } + + resetDailySteps(context) + clearTimeout(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) { with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) { - cancel(PendingIntent.getBroadcast(context, 5, Intent(context, ActivityDetectionReceiver::class.java), 0)) setExact( AlarmManager.RTC, 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 + ) + ) + } + } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/NotificationListener.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/NotificationListener.kt index 6a6bcf1..56bb92e 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/NotificationListener.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/NotificationListener.kt @@ -23,19 +23,26 @@ import java.util.* class NotificationListener : NotificationListenerService() { override fun onListenerConnected() { MediaPlayerHelper.updatePlayingMediaInfo(this) - MainWidget.updateWidget(this) + ActiveNotificationsHelper.clearLastNotification(this) super.onListenerConnected() } + override fun onListenerDisconnected() { + MediaPlayerHelper.updatePlayingMediaInfo(this) + ActiveNotificationsHelper.clearLastNotification(this) + super.onListenerDisconnected() + } + override fun onNotificationPosted(sbn: StatusBarNotification?) { sbn?.notification?.extras?.let { bundle -> bundle.getParcelable(Notification.EXTRA_MEDIA_SESSION)?.let { - MediaPlayerHelper.updatePlayingMediaInfo(this) + if (Preferences.showMusic) + MediaPlayerHelper.updatePlayingMediaInfo(this) } ?: run { val isGroupHeader = sbn.notification.flags and Notification.FLAG_GROUP_SUMMARY != 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.lastNotificationTitle = bundle.getString(Notification.EXTRA_TITLE) ?: "" try { @@ -59,15 +66,13 @@ class NotificationListener : NotificationListenerService() { } override fun onNotificationRemoved(sbn: StatusBarNotification?) { - MediaPlayerHelper.updatePlayingMediaInfo(this) - + if (Preferences.showMusic) + MediaPlayerHelper.updatePlayingMediaInfo(this) 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) } } - - MainWidget.updateWidget(this) super.onNotificationRemoved(sbn) } @@ -76,7 +81,6 @@ class NotificationListener : NotificationListenerService() { val intent = Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_CLEAR_NOTIFICATION } - cancel(PendingIntent.getBroadcast(context, 28943, intent, 0)) val timeoutPref = Constants.GlanceNotificationTimer.fromInt(Preferences.hideNotificationAfter) if (timeoutPref != Constants.GlanceNotificationTimer.WHEN_DISMISSED) { setExact( @@ -87,7 +91,7 @@ class NotificationListener : NotificationListenerService() { Constants.GlanceNotificationTimer.FIVE_MINUTES -> 5 * 60 * 1000 Constants.GlanceNotificationTimer.TEN_MINUTES -> 10 * 60 * 1000 Constants.GlanceNotificationTimer.FIFTEEN_MINUTES -> 15 * 60 * 1000 - else -> 0 + else -> 60 * 1000 }, PendingIntent.getBroadcast( context, @@ -99,4 +103,15 @@ class NotificationListener : NotificationListenerService() { } } } -} \ No newline at end of file + + 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)) + } + } + } +} diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/UpdatesReceiver.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/UpdatesReceiver.kt index afc612c..11ca792 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/UpdatesReceiver.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/receivers/UpdatesReceiver.kt @@ -27,11 +27,15 @@ class UpdatesReceiver : BroadcastReceiver() { Intent.ACTION_MY_PACKAGE_REPLACED, Intent.ACTION_TIME_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, Actions.ACTION_CALENDAR_UPDATE -> { - ActiveNotificationsHelper.clearLastNotification(context) - MediaPlayerHelper.updatePlayingMediaInfo(context) CalendarHelper.updateEventList(context) } @@ -48,7 +52,6 @@ class UpdatesReceiver : BroadcastReceiver() { Actions.ACTION_CLEAR_NOTIFICATION -> { ActiveNotificationsHelper.clearLastNotification(context) - MainWidget.updateWidget(context) } Actions.ACTION_REFRESH -> {