setExact is more reliable than setRepeating on some ROMs (e.g., MIUI).
This commit is contained in:
parent
818b4ec0ba
commit
9f47d626a9
@ -65,6 +65,8 @@ class UpdatesReceiver : BroadcastReceiver() {
|
|||||||
const val EVENT_ID = "EVENT_ID"
|
const val EVENT_ID = "EVENT_ID"
|
||||||
|
|
||||||
fun setUpdates(context: Context, eventId: Long? = null) {
|
fun setUpdates(context: Context, eventId: Long? = null) {
|
||||||
|
if (!Preferences.showEvents)
|
||||||
|
return
|
||||||
val eventRepository = EventRepository(context)
|
val eventRepository = EventRepository(context)
|
||||||
if (eventId == null) {
|
if (eventId == null) {
|
||||||
// schedule ACTION_CALENDAR_UPDATE at midnight (ACTION_DATE_CHANGED no longer works)
|
// schedule ACTION_CALENDAR_UPDATE at midnight (ACTION_DATE_CHANGED no longer works)
|
||||||
@ -84,7 +86,8 @@ class UpdatesReceiver : BroadcastReceiver() {
|
|||||||
Intent(context, UpdatesReceiver::class.java).apply {
|
Intent(context, UpdatesReceiver::class.java).apply {
|
||||||
action = Actions.ACTION_CALENDAR_UPDATE
|
action = Actions.ACTION_CALENDAR_UPDATE
|
||||||
},
|
},
|
||||||
0)
|
0
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,14 +152,14 @@ class UpdatesReceiver : BroadcastReceiver() {
|
|||||||
else -> 0
|
else -> 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// avoid redundant updates at midnight
|
// no need to schedule updates after the next ACTION_CALENDAR_UPDATE
|
||||||
if (Calendar.getInstance().run {
|
if (Calendar.getInstance().apply {
|
||||||
timeInMillis = fireTime
|
set(Calendar.MILLISECOND, 0)
|
||||||
get(Calendar.MILLISECOND) == 0 &&
|
set(Calendar.SECOND, 0)
|
||||||
get(Calendar.SECOND) == 0 &&
|
set(Calendar.MINUTE, 0)
|
||||||
get(Calendar.MINUTE) == 0 &&
|
set(Calendar.HOUR_OF_DAY, 0)
|
||||||
get(Calendar.HOUR_OF_DAY) == 0
|
add(Calendar.DATE, 1)
|
||||||
}) return
|
}.timeInMillis <= fireTime) return
|
||||||
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||||
setExact(
|
setExact(
|
||||||
AlarmManager.RTC,
|
AlarmManager.RTC,
|
||||||
|
@ -22,13 +22,8 @@ class WeatherReceiver : BroadcastReceiver() {
|
|||||||
Intent.ACTION_MY_PACKAGE_REPLACED,
|
Intent.ACTION_MY_PACKAGE_REPLACED,
|
||||||
Intent.ACTION_TIMEZONE_CHANGED,
|
Intent.ACTION_TIMEZONE_CHANGED,
|
||||||
Intent.ACTION_LOCALE_CHANGED,
|
Intent.ACTION_LOCALE_CHANGED,
|
||||||
Intent.ACTION_TIME_CHANGED -> setUpdates(context)
|
Intent.ACTION_TIME_CHANGED,
|
||||||
|
Actions.ACTION_WEATHER_UPDATE -> setUpdates(context)
|
||||||
Actions.ACTION_WEATHER_UPDATE -> {
|
|
||||||
GlobalScope.launch(Dispatchers.IO) {
|
|
||||||
WeatherHelper.updateWeather(context)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,13 +41,15 @@ class WeatherReceiver : BroadcastReceiver() {
|
|||||||
else -> 60
|
else -> 60
|
||||||
}
|
}
|
||||||
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
|
||||||
setRepeating(
|
setExact(
|
||||||
AlarmManager.RTC,
|
AlarmManager.RTC,
|
||||||
Calendar.getInstance().timeInMillis,
|
System.currentTimeMillis() + interval,
|
||||||
interval,
|
|
||||||
PendingIntent.getBroadcast(context, 0, Intent(context, WeatherReceiver::class.java).apply { action = Actions.ACTION_WEATHER_UPDATE }, 0)
|
PendingIntent.getBroadcast(context, 0, Intent(context, WeatherReceiver::class.java).apply { action = Actions.ACTION_WEATHER_UPDATE }, 0)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
GlobalScope.launch(Dispatchers.IO) {
|
||||||
|
WeatherHelper.updateWeather(context)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,6 +88,7 @@ class UpdateCalendarService : Service() {
|
|||||||
) {
|
) {
|
||||||
eventRepository.resetNextEventData()
|
eventRepository.resetNextEventData()
|
||||||
eventRepository.clearEvents()
|
eventRepository.clearEvents()
|
||||||
|
Preferences.showEvents = false
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
val provider = CalendarProvider(this@UpdateCalendarService)
|
val provider = CalendarProvider(this@UpdateCalendarService)
|
||||||
|
@ -140,6 +140,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh
|
|||||||
|
|
||||||
if (Preferences.showEvents && !checkGrantedPermission(Manifest.permission.READ_CALENDAR)) {
|
if (Preferences.showEvents && !checkGrantedPermission(Manifest.permission.READ_CALENDAR)) {
|
||||||
Preferences.showEvents = false
|
Preferences.showEvents = false
|
||||||
|
com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver.removeUpdates(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +182,7 @@ class CalendarFragment : Fragment() {
|
|||||||
|
|
||||||
binding.showAllDayToggle.setOnCheckedChangeListener { _, isChecked ->
|
binding.showAllDayToggle.setOnCheckedChangeListener { _, isChecked ->
|
||||||
Preferences.calendarAllDay = isChecked
|
Preferences.calendarAllDay = isChecked
|
||||||
MainWidget.updateWidget(requireContext())
|
updateCalendar()
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.actionChangeAttendeeFilter.setOnClickListener {
|
binding.actionChangeAttendeeFilter.setOnClickListener {
|
||||||
@ -227,7 +227,7 @@ class CalendarFragment : Fragment() {
|
|||||||
|
|
||||||
binding.showOnlyBusyEventsToggle.setOnCheckedChangeListener { _, isChecked ->
|
binding.showOnlyBusyEventsToggle.setOnCheckedChangeListener { _, isChecked ->
|
||||||
Preferences.showOnlyBusyEvents = isChecked
|
Preferences.showOnlyBusyEvents = isChecked
|
||||||
MainWidget.updateWidget(requireContext())
|
updateCalendar()
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.actionShowDiffTime.setOnClickListener {
|
binding.actionShowDiffTime.setOnClickListener {
|
||||||
@ -254,6 +254,7 @@ class CalendarFragment : Fragment() {
|
|||||||
.addItem(getString(R.string.settings_widget_update_frequency_low), Constants.WidgetUpdateFrequency.LOW.rawValue)
|
.addItem(getString(R.string.settings_widget_update_frequency_low), Constants.WidgetUpdateFrequency.LOW.rawValue)
|
||||||
.addOnSelectItemListener { value ->
|
.addOnSelectItemListener { value ->
|
||||||
Preferences.widgetUpdateFrequency = value
|
Preferences.widgetUpdateFrequency = value
|
||||||
|
updateCalendar()
|
||||||
}.show()
|
}.show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,6 +135,8 @@ class PreferencesFragment : Fragment() {
|
|||||||
binding.showWeatherSwitch.setOnCheckedChangeListener { _, enabled: Boolean ->
|
binding.showWeatherSwitch.setOnCheckedChangeListener { _, enabled: Boolean ->
|
||||||
Preferences.showWeather = enabled
|
Preferences.showWeather = enabled
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
|
Preferences.weatherProviderError = ""
|
||||||
|
Preferences.weatherProviderLocationError = ""
|
||||||
WeatherReceiver.setUpdates(requireContext())
|
WeatherReceiver.setUpdates(requireContext())
|
||||||
} else {
|
} else {
|
||||||
WeatherReceiver.removeUpdates(requireContext())
|
WeatherReceiver.removeUpdates(requireContext())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user