From 4603e72316f7c8b2e0975164791c311de0be508d Mon Sep 17 00:00:00 2001 From: Forrest Guice Date: Sun, 11 Aug 2024 10:58:39 -0700 Subject: [PATCH] updateInterval assign widget `updateInterval` so that in addition to the normal update loop, the system sends a broadcast every ~6 hours, restoring exact widget update alarms (that may have been killed due to battery opt) (#806) --- .../suntimeswidget/SuntimesWidget0.java | 24 ++++++++++++------- app/src/main/res/values/dimens.xml | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesWidget0.java b/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesWidget0.java index 6610b205b..599b1a2b0 100644 --- a/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesWidget0.java +++ b/app/src/main/java/com/forrestguice/suntimeswidget/SuntimesWidget0.java @@ -27,6 +27,7 @@ import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.util.Log; import android.view.View; import android.content.Context; @@ -198,6 +199,10 @@ public void onReceive(@NonNull Context context, @NonNull Intent intent) // TODO: handle TIME_SET better .. when automatic/network time is enabled this thing fires /frequently/ ... } else if (action != null && action.equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) { + /** + * ACTION_APPWIDGET_UPDATE should be broadcast by the system at `R.dimen.widget_updateInterval`, + * or whenever the system feels like updating widgets. + */ Log.d(TAG, "onReceive: ACTION_APPWIDGET_UPDATE :: " + getClass()); if (extras != null) { @@ -205,6 +210,7 @@ public void onReceive(@NonNull Context context, @NonNull Intent intent) if (appWidgetIds != null) { for (int appWidgetId : appWidgetIds) { + onUpdate(context, AppWidgetManager.getInstance(context), new int[] { appWidgetId }); setUpdateAlarm(context, appWidgetId); } } @@ -704,8 +710,8 @@ protected void setUpdateAlarm( Context context, int alarmID ) } else { alarmManager.setWindow(AlarmManager.RTC, updateTime, 5 * 1000, alarmIntent); } - Log.d(TAG, "setUpdateAlarm: " + utils.calendarDateTimeDisplayString(context, updateTime).toString() + " --> " + getUpdateIntentFilter() + "(" + alarmID + ") :: " + utils.timeDeltaLongDisplayString(getUpdateInterval(), true) ); - } else Log.d(TAG, "setUpdateAlarm: skipping " + alarmID); + Log.d(TAG, "SuntimesWidget.setUpdateAlarm: " + utils.calendarDateTimeDisplayString(context, updateTime).toString() + " --> " + getUpdateIntentFilter() + "(" + alarmID + ") :: " + utils.timeDeltaLongDisplayString(getUpdateInterval(), true) ); + } else Log.w(TAG, "SuntimesWidget.setUpdateAlarm: skipping " + alarmID); } } @@ -720,7 +726,7 @@ protected void unsetUpdateAlarm( Context context, int alarmID ) { PendingIntent alarmIntent = getUpdateIntent(context, alarmID); alarmManager.cancel(alarmIntent); - Log.d(TAG, "unsetUpdateAlarm: unset alarm --> " + getUpdateIntentFilter() + "(" + alarmID + ")"); + Log.d(TAG, "SuntimesWidget.unsetUpdateAlarm: unset alarm --> " + getUpdateIntentFilter() + "(" + alarmID + ")"); } } @@ -741,7 +747,7 @@ protected long getUpdateTimeMillis( Context context, int appWidgetID ) } } - protected long getUpdateTimeMillis(Calendar suggestedUpdateTime) + protected long getUpdateTimeMillis(@Nullable Calendar suggestedUpdateTime) { Calendar updateTime = Calendar.getInstance(); Calendar now = Calendar.getInstance(); @@ -749,15 +755,15 @@ protected long getUpdateTimeMillis(Calendar suggestedUpdateTime) if (now.before(suggestedUpdateTime)) { updateTime.setTimeInMillis(suggestedUpdateTime.getTimeInMillis()); - Log.d(TAG, "getUpdateTimeMillis: next update is at: " + updateTime.getTimeInMillis()); + Log.d(TAG, "SuntimesWidget.getUpdateTimeMillis: next update is at: " + updateTime.getTimeInMillis()); } else { - updateTime.set(Calendar.MILLISECOND, 0); // reset seconds, minutes, and hours to 0 - updateTime.set(Calendar.MINUTE, 0); + updateTime.set(Calendar.MILLISECOND, 0); + updateTime.set(Calendar.MINUTE, 1); updateTime.set(Calendar.SECOND, 0); updateTime.set(Calendar.HOUR_OF_DAY, 0); - updateTime.add(Calendar.DAY_OF_MONTH, 1); // and increment the date by 1 day - Log.d(TAG, "getUpdateTimeMillis: next update is at midnight: " + updateTime.getTimeInMillis()); + updateTime.add(Calendar.DAY_OF_YEAR, 1); + Log.d(TAG, "SuntimesWidget.getUpdateTimeMillis: next update is at midnight: " + updateTime.getTimeInMillis()); } return updateTime.getTimeInMillis(); } diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 726b23743..3d6ea5e1e 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -131,7 +131,7 @@ 0dp 8dp - 0 + 21600000 0dip