Do not schedule unnecessary ACTION_ALARM_UPDATE alarm.

This commit is contained in:
azuo 2021-09-02 13:54:24 +08:00
parent 7b93548b0b
commit ea0be72478
4 changed files with 61 additions and 47 deletions

View File

@ -221,6 +221,8 @@ 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

@ -44,7 +44,6 @@ object AlarmHelper {
val intent = Intent(context, UpdatesReceiver::class.java).apply { val intent = Intent(context, UpdatesReceiver::class.java).apply {
action = Actions.ACTION_ALARM_UPDATE action = Actions.ACTION_ALARM_UPDATE
} }
cancel(PendingIntent.getBroadcast(context, ALARM_UPDATE_ID, intent, 0))
setExact( setExact(
AlarmManager.RTC, AlarmManager.RTC,
trigger, 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 private const val ALARM_UPDATE_ID = 24953
} }

View File

@ -135,8 +135,6 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) {
views.setOnClickPendingIntent(R.id.date_rect, calPIntent) views.setOnClickPendingIntent(R.id.date_rect, calPIntent)
views.setViewVisibility(R.id.first_line_rect, View.VISIBLE) views.setViewVisibility(R.id.first_line_rect, View.VISIBLE)
val nextAlarm = AlarmHelper.getNextAlarm(context)
// Spacing // Spacing
views.setViewVisibility( views.setViewVisibility(
R.id.sub_line_top_margin_small_sans, 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 -> { Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> {
if (Preferences.showNextAlarm && nextAlarm != "") { if (Preferences.showNextAlarm) {
val alarmIntent = PendingIntent.getActivity( val nextAlarm = AlarmHelper.getNextAlarm(context)
context, if (nextAlarm != "") {
widgetID, val alarmIntent = PendingIntent.getActivity(
IntentHelper.getClockIntent(context), context,
PendingIntent.FLAG_UPDATE_CURRENT widgetID,
) IntentHelper.getClockIntent(context),
views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent) PendingIntent.FLAG_UPDATE_CURRENT
showSomething = true )
break@loop views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent)
showSomething = true
break@loop
}
} }
} }
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { 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) 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) { if (Preferences.showEvents && context.checkGrantedPermission(Manifest.permission.READ_CALENDAR) && nextEvent != null && !Preferences.showEventsAsGlanceProvider) {
// Multiple counter // Multiple counter
bindingView.actionNext.isVisible = bindingView.actionNext.isVisible =
@ -602,16 +601,19 @@ class AlignedWidget(val context: Context, val rightAligned: Boolean = false) {
} }
} }
Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> {
if (Preferences.showNextAlarm && nextAlarm != "") { if (Preferences.showNextAlarm) {
bindingView.subLineIcon.setImageDrawable( val nextAlarm = AlarmHelper.getNextAlarm(context)
ContextCompat.getDrawable( if (nextAlarm != "") {
context, bindingView.subLineIcon.setImageDrawable(
R.drawable.round_alarm_24 ContextCompat.getDrawable(
context,
R.drawable.round_alarm_24
)
) )
) bindingView.subLineText.text = nextAlarm
bindingView.subLineText.text = AlarmHelper.getNextAlarm(context) showSomething = true
showSomething = true break@loop
break@loop }
} }
} }
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {

View File

@ -144,8 +144,6 @@ class StandardWidget(val context: Context) {
BitmapHelper.getBitmapFromView(bindingView.subLine, draw = false, width = bindingView.subLine.width, height = bindingView.subLine.height) BitmapHelper.getBitmapFromView(bindingView.subLine, draw = false, width = bindingView.subLine.width, height = bindingView.subLine.height)
) )
val nextAlarm = AlarmHelper.getNextAlarm(context)
// Spacing // Spacing
views.setViewVisibility( views.setViewVisibility(
R.id.sub_line_top_margin_small_sans, R.id.sub_line_top_margin_small_sans,
@ -287,16 +285,19 @@ class StandardWidget(val context: Context) {
} }
} }
Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> {
if (Preferences.showNextAlarm && nextAlarm != "") { if (Preferences.showNextAlarm) {
val alarmIntent = PendingIntent.getActivity( val nextAlarm = AlarmHelper.getNextAlarm(context)
context, if (nextAlarm != "") {
widgetID, val alarmIntent = PendingIntent.getActivity(
IntentHelper.getClockIntent(context), context,
PendingIntent.FLAG_UPDATE_CURRENT widgetID,
) IntentHelper.getClockIntent(context),
views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent) PendingIntent.FLAG_UPDATE_CURRENT
showSomething = true )
break@loop views.setOnClickPendingIntent(R.id.sub_line_rect, alarmIntent)
showSomething = true
break@loop
}
} }
} }
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {
@ -484,8 +485,6 @@ class StandardWidget(val context: Context) {
bindingView.date.text = DateHelper.getDateText(context, now) 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) { if (Preferences.showEvents && context.checkGrantedPermission(Manifest.permission.READ_CALENDAR) && nextEvent != null && !Preferences.showEventsAsGlanceProvider) {
// Multiple counter // Multiple counter
bindingView.actionNext.isVisible = bindingView.actionNext.isVisible =
@ -634,16 +633,19 @@ class StandardWidget(val context: Context) {
} }
} }
Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> { Constants.GlanceProviderId.NEXT_CLOCK_ALARM -> {
if (Preferences.showNextAlarm && nextAlarm != "") { if (Preferences.showNextAlarm) {
bindingView.subLineIcon.setImageDrawable( val nextAlarm = AlarmHelper.getNextAlarm(context)
ContextCompat.getDrawable( if (nextAlarm != "") {
context, bindingView.subLineIcon.setImageDrawable(
R.drawable.round_alarm_24 ContextCompat.getDrawable(
context,
R.drawable.round_alarm_24
)
) )
) bindingView.subLineText.text = nextAlarm
bindingView.subLineText.text = AlarmHelper.getNextAlarm(context) showSomething = true
showSomething = true break@loop
break@loop }
} }
} }
Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> { Constants.GlanceProviderId.BATTERY_LEVEL_LOW -> {