Skip to content

Commit

Permalink
updateInterval
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
forrestguice committed Aug 11, 2024
1 parent f7ebe9e commit 4603e72
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -198,13 +199,18 @@ 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)
{
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds != null)
{
for (int appWidgetId : appWidgetIds) {
onUpdate(context, AppWidgetManager.getInstance(context), new int[] { appWidgetId });
setUpdateAlarm(context, appWidgetId);
}
}
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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 + ")");
}
}

Expand All @@ -741,23 +747,23 @@ 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();

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();
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<dimen name="widgetActivity_padding">0dp</dimen> <!-- padding around activity title + content -->
<dimen name="widgetActivity_padding_inner">8dp</dimen> <!-- padding around activity content -->

<integer name="widget_updateInterval">0</integer>
<integer name="widget_updateInterval">21600000</integer> <!-- 6 hours -->

<dimen name="aboutIcon_margin">0dip</dimen>

Expand Down

0 comments on commit 4603e72

Please sign in to comment.