From ea0be72478b80408c150492821ab550824db7787 Mon Sep 17 00:00:00 2001 From: azuo Date: Thu, 2 Sep 2021 13:54:24 +0800 Subject: [PATCH] Do not schedule unnecessary ACTION_ALARM_UPDATE alarm. --- .../components/GlanceSettingsDialog.kt | 2 + .../anotherwidget/helpers/AlarmHelper.kt | 10 +++- .../anotherwidget/ui/widgets/AlignedWidget.kt | 48 ++++++++++--------- .../ui/widgets/StandardWidget.kt | 48 ++++++++++--------- 4 files changed, 61 insertions(+), 47 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 dc94327..628c088 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/components/GlanceSettingsDialog.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/components/GlanceSettingsDialog.kt @@ -221,6 +221,8 @@ 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/AlarmHelper.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/AlarmHelper.kt index 9a1394f..595dc39 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/AlarmHelper.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/helpers/AlarmHelper.kt @@ -44,7 +44,6 @@ object AlarmHelper { val intent = Intent(context, UpdatesReceiver::class.java).apply { action = Actions.ACTION_ALARM_UPDATE } - cancel(PendingIntent.getBroadcast(context, ALARM_UPDATE_ID, intent, 0)) setExact( AlarmManager.RTC, trigger, @@ -58,5 +57,14 @@ object AlarmHelper { } } + fun clearTimeout(context: Context) { + with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) { + val intent = Intent(context, UpdatesReceiver::class.java).apply { + action = Actions.ACTION_ALARM_UPDATE + } + cancel(PendingIntent.getBroadcast(context, ALARM_UPDATE_ID, intent, 0)) + } + } + private const val ALARM_UPDATE_ID = 24953 } \ No newline at end of file diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt index 3b01f67..40f5327 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/AlignedWidget.kt @@ -135,8 +135,6 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { views.setOnClickPendingIntent(R.id.date_rect, calPIntent) views.setViewVisibility(R.id.first_line_rect, View.VISIBLE) - val nextAlarm = AlarmHelper.getNextAlarm(context) - // Spacing views.setViewVisibility( R.id.sub_line_top_margin_small_sans, @@ -259,16 +257,19 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { } } Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { - if (Preferences.showNextAlarm && nextAlarm != "") { - val alarmIntent = PendingIntent.getActivity( - context, - widgetID, - IntentHelper.getClockIntent(context), - PendingIntent.FLAG_UPDATE_CURRENT - ) - views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent) - showSomething = true - break@loop + if (Preferences.showNextAlarm) { + val nextAlarm = AlarmHelper.getNextAlarm(context) + if (nextAlarm != "") { + val alarmIntent = PendingIntent.getActivity( + context, + widgetID, + IntentHelper.getClockIntent(context), + PendingIntent.FLAG_UPDATE_CURRENT + ) + views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent) + showSomething = true + break@loop + } } } Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { @@ -457,8 +458,6 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { bindingView.date.text = DateHelper.getDateText(context, now) - val nextAlarm = AlarmHelper.getNextAlarm(context) - if (Preferences.showEvents && context.checkGrantedPermission(Manifest.permission.READ_CALENDAR) && nextEvent != null && !Preferences.showEventsAsGlanceProvider) { // Multiple counter bindingView.actionNext.isVisible = @@ -602,16 +601,19 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) { } } Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { - if (Preferences.showNextAlarm && nextAlarm != "") { - bindingView.subLineIcon.setImageDrawable( - ContextCompat.getDrawable( - context, - R.drawable.round_alarm_24 + if (Preferences.showNextAlarm) { + val nextAlarm = AlarmHelper.getNextAlarm(context) + if (nextAlarm != "") { + bindingView.subLineIcon.setImageDrawable( + ContextCompat.getDrawable( + context, + R.drawable.round_alarm_24 + ) ) - ) - bindingView.subLineText.text = AlarmHelper.getNextAlarm(context) - showSomething = true - break@loop + bindingView.subLineText.text = nextAlarm + showSomething = true + break@loop + } } } Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { diff --git a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt index 556841f..9671bc6 100644 --- a/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt +++ b/app/src/main/java/com/tommasoberlose/anotherwidget/ui/widgets/StandardWidget.kt @@ -144,8 +144,6 @@ class StandardWidget(val context: Context) { BitmapHelper.getBitmapFromView(bindingView.subLine, draw = false, width = bindingView.subLine.width, height = bindingView.subLine.height) ) - val nextAlarm = AlarmHelper.getNextAlarm(context) - // Spacing views.setViewVisibility( R.id.sub_line_top_margin_small_sans, @@ -287,16 +285,19 @@ class StandardWidget(val context: Context) { } } Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { - if (Preferences.showNextAlarm && nextAlarm != "") { - val alarmIntent = PendingIntent.getActivity( - context, - widgetID, - IntentHelper.getClockIntent(context), - PendingIntent.FLAG_UPDATE_CURRENT - ) - views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent) - showSomething = true - break@loop + if (Preferences.showNextAlarm) { + val nextAlarm = AlarmHelper.getNextAlarm(context) + if (nextAlarm != "") { + val alarmIntent = PendingIntent.getActivity( + context, + widgetID, + IntentHelper.getClockIntent(context), + PendingIntent.FLAG_UPDATE_CURRENT + ) + views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent) + showSomething = true + break@loop + } } } Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { @@ -484,8 +485,6 @@ class StandardWidget(val context: Context) { bindingView.date.text = DateHelper.getDateText(context, now) - val nextAlarm = AlarmHelper.getNextAlarm(context) - if (Preferences.showEvents && context.checkGrantedPermission(Manifest.permission.READ_CALENDAR) && nextEvent != null && !Preferences.showEventsAsGlanceProvider) { // Multiple counter bindingView.actionNext.isVisible = @@ -634,16 +633,19 @@ class StandardWidget(val context: Context) { } } Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { - if (Preferences.showNextAlarm && nextAlarm != "") { - bindingView.subLineIcon.setImageDrawable( - ContextCompat.getDrawable( - context, - R.drawable.round_alarm_24 + if (Preferences.showNextAlarm) { + val nextAlarm = AlarmHelper.getNextAlarm(context) + if (nextAlarm != "") { + bindingView.subLineIcon.setImageDrawable( + ContextCompat.getDrawable( + context, + R.drawable.round_alarm_24 + ) ) - ) - bindingView.subLineText.text = AlarmHelper.getNextAlarm(context) - showSomething = true - break@loop + bindingView.subLineText.text = nextAlarm + showSomething = true + break@loop + } } } Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {