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 -> {
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) {

View File

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

View File

@ -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 ""
}
}
}

View File

@ -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
)
)
}
}
}
}
}

View File

@ -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<MediaSession.Token>(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() {
}
}
}
}
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_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 -> {