Skip to content

Commit

Permalink
plusonelabs#338 Introduced "Auto refresh period, minutes" with defaul…
Browse files Browse the repository at this point in the history
…t value of 10 minutes. The refresh (and corresponding alarm) is not fired, when a device sleeps. So it doesn't spend much energy.
  • Loading branch information
yvolk committed Nov 17, 2019
1 parent bb77934 commit 004e458
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import org.andstatus.todoagenda.provider.EventProviderType;
import org.andstatus.todoagenda.util.DateUtil;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.andstatus.todoagenda.AppWidgetProvider.getWidgetIds;
Expand Down Expand Up @@ -48,13 +50,14 @@ public static void registerReceivers(Map<Integer, InstanceSettings> instances) {
if (oldReceiver != null) {
oldReceiver.unRegister(context);
}
scheduleNextAlarms(context, instances);
scheduleMidnightAlarms(context, instances);
schedulePeriodicAlarms(context, instances);

Log.i(TAG, "Registered receivers from " + instanceSettings.getContext().getClass().getName());
}
}

private static void scheduleNextAlarms(Context context, Map<Integer, InstanceSettings> instances) {
private static void scheduleMidnightAlarms(Context context, Map<Integer, InstanceSettings> instances) {
Set<DateTime> alarmTimes = new HashSet<>();
for (InstanceSettings settings : instances.values()) {
alarmTimes.add(DateUtil.now(settings.getTimeZone()).withTimeAtStartOfDay().plusDays(1));
Expand All @@ -67,11 +70,39 @@ private static void scheduleNextAlarms(Context context, Map<Integer, InstanceSet
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC, alarmTime.getMillis(), pendingIntent);
if (am != null) {
am.set(AlarmManager.RTC, alarmTime.getMillis(), pendingIntent);
}
counter++;
}
}

private static void schedulePeriodicAlarms(Context context, Map<Integer, InstanceSettings> instances) {
DateTime now = DateUtil.now(DateTimeZone.UTC).plusMinutes(1);
int periodMinutes = (int) TimeUnit.DAYS.toMinutes(1);
for (InstanceSettings settings : instances.values()) {
int period = settings.getRefreshPeriodMinutes();
if (period > 0 && period < periodMinutes) {
periodMinutes = period;
}
}
DateTime alarmTime = new DateTime(now.getYear(), now.getMonthOfYear(),
now.getDayOfMonth(), now.getHourOfDay(), now.getMinuteOfHour())
.plusMinutes(periodMinutes);

Intent intent = new Intent(context, EnvironmentChangedReceiver.class);
intent.setAction(RemoteViewsFactory.ACTION_PERIODIC_ALARM);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
RemoteViewsFactory.REQUEST_CODE_PERIODIC_ALARM,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (am != null) {
am.setInexactRepeating(AlarmManager.RTC, alarmTime.getMillis(),
TimeUnit.MINUTES.toMillis(periodMinutes), pendingIntent);
}
}

public static void forget() {
registeredReceiver.set(null);
}
Expand All @@ -98,6 +129,9 @@ public void onReceive(Context context, Intent intent) {
}
gotoPosition(context, widgetId, position2);
break;
case RemoteViewsFactory.ACTION_PERIODIC_ALARM:
updateAllWidgets(context);
break;
default:
int widgetId2 = intent == null
? 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ public class RemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory
private static final int REQUEST_CODE_EMPTY = 1;
private static final int REQUEST_CODE_ADD_EVENT = 2;
static final int REQUEST_CODE_MIDNIGHT_ALARM = REQUEST_CODE_ADD_EVENT + MAX_NUMBER_OF_WIDGETS;
static final int REQUEST_CODE_PERIODIC_ALARM = REQUEST_CODE_MIDNIGHT_ALARM + MAX_NUMBER_OF_WIDGETS;
static final String EXTRA_WIDGET_LIST_POSITION1 = "widgetListPosition1";
static final String EXTRA_WIDGET_LIST_POSITION2 = "widgetListPosition2";

private static final String PACKAGE = "org.andstatus.todoagenda";
static final String ACTION_GOTO_POSITIONS = PACKAGE + ".action.GOTO_TODAY";
static final String ACTION_REFRESH = PACKAGE + ".action.REFRESH";
static final String ACTION_PERIODIC_ALARM = PACKAGE + ".action.PERIODIC_ALARM";

private final Context context;
private final int widgetId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static void saveFromApplicationPreferences(Context context, Integer widge
settings.logMe(AllSettings.class, "saveFromApplicationPreferences put", widgetId);
instances.put(widgetId, settings);
}
EnvironmentChangedReceiver.registerReceivers(instances);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_MULTILINE_TITLE_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_PAST_EVENTS_BACKGROUND_COLOR;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_PAST_EVENTS_BACKGROUND_COLOR_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_REFRESH_PERIOD_MINUTES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_REFRESH_PERIOD_MINUTES_DEFAULT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_SHOW_DATE_ON_WIDGET_HEADER;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_SHOW_DAYS_WITHOUT_EVENTS;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_SHOW_DAY_HEADERS;
Expand Down Expand Up @@ -93,12 +95,13 @@ public static void fromInstanceSettings(Context context, Integer widgetId) {
setString(context, PREF_DATE_FORMAT, settings.getDateFormat());
setAbbreviateDates(context, settings.getAbbreviateDates());
setLockedTimeZoneId(context, settings.getLockedTimeZoneId());
setRefreshPeriodMinutes(context, settings.getRefreshPeriodMinutes());
setString(context, PREF_EVENT_ENTRY_LAYOUT, settings.getEventEntryLayout().value);
setBoolean(context, PREF_MULTILINE_TITLE, settings.isMultilineTitle());
setBoolean(context, PREF_MULTILINE_DETAILS, settings.isMultilineDetails());
setBoolean(context, PREF_SHOW_ONLY_CLOSEST_INSTANCE_OF_RECURRING_EVENT, settings
.getShowOnlyClosestInstanceOfRecurringEvent());
setBoolean(context, PREF_HIDE_DUPLICATES, settings.getHideDuplicates());
setHideDuplicates(context, settings.getHideDuplicates());
setBoolean(context, PREF_INDICATE_ALERTS, settings.getIndicateAlerts());
setBoolean(context, PREF_INDICATE_RECURRING, settings.getIndicateRecurring());
for (Map.Entry<TextShadingPref, TextShading> entry: settings.shadings.entrySet()) {
Expand All @@ -117,7 +120,7 @@ public static void save(Context context, int wigdetId) {
}

public static int getWidgetId(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getInt(PREF_WIDGET_ID, 0);
return getInt(context, PREF_WIDGET_ID, 0);
}

public static void setWidgetId(Context context, int value) {
Expand Down Expand Up @@ -163,8 +166,7 @@ public static void setEventsEnded(Context context, EndedSomeTimeAgo value) {
}

public static boolean getFillAllDayEvents(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_FILL_ALL_DAY, PREF_FILL_ALL_DAY_DEFAULT);
return getBoolean(context, PREF_FILL_ALL_DAY, PREF_FILL_ALL_DAY_DEFAULT);
}

private static void setFillAllDayEvents(Context context, boolean value) {
Expand All @@ -180,73 +182,63 @@ private static void setHideBasedOnKeywords(Context context, String value) {
}

public static int getWidgetHeaderBackgroundColor(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getInt(
PREF_WIDGET_HEADER_BACKGROUND_COLOR,
return getInt(context, PREF_WIDGET_HEADER_BACKGROUND_COLOR,
PREF_WIDGET_HEADER_BACKGROUND_COLOR_DEFAULT);
}

public static int getPastEventsBackgroundColor(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getInt(
PREF_PAST_EVENTS_BACKGROUND_COLOR,
return getInt(context, PREF_PAST_EVENTS_BACKGROUND_COLOR,
PREF_PAST_EVENTS_BACKGROUND_COLOR_DEFAULT);
}

public static int getTodaysEventsBackgroundColor(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getInt(
PREF_TODAYS_EVENTS_BACKGROUND_COLOR,
return getInt(context, PREF_TODAYS_EVENTS_BACKGROUND_COLOR,
PREF_TODAYS_EVENTS_BACKGROUND_COLOR_DEFAULT);
}

public static int getEventsBackgroundColor(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getInt(
PREF_EVENTS_BACKGROUND_COLOR,
return getInt(context, PREF_EVENTS_BACKGROUND_COLOR,
PREF_EVENTS_BACKGROUND_COLOR_DEFAULT);
}

public static boolean getHorizontalLineBelowDayHeader(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_HORIZONTAL_LINE_BELOW_DAY_HEADER, false);
return getBoolean(context, PREF_HORIZONTAL_LINE_BELOW_DAY_HEADER, false);
}

private static void setHorizontalLineBelowDayHeader(Context context, boolean value) {
setBoolean(context, PREF_HORIZONTAL_LINE_BELOW_DAY_HEADER, value);
}

public static boolean getShowDaysWithoutEvents(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_DAYS_WITHOUT_EVENTS, false);
return getBoolean(context, PREF_SHOW_DAYS_WITHOUT_EVENTS, false);
}

private static void setShowDaysWithoutEvents(Context context, boolean value) {
setBoolean(context, PREF_SHOW_DAYS_WITHOUT_EVENTS, value);
}

public static boolean getShowDayHeaders(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_DAY_HEADERS, true);
return getBoolean(context, PREF_SHOW_DAY_HEADERS, true);
}

private static void setShowDayHeaders(Context context, boolean value) {
setBoolean(context, PREF_SHOW_DAY_HEADERS, value);
}

public static boolean getShowPastEventsUnderOneHeader(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_PAST_EVENTS_UNDER_ONE_HEADER, false);
return getBoolean(context, PREF_SHOW_PAST_EVENTS_UNDER_ONE_HEADER, false);
}

private static void setShowPastEventsUnderOneHeader(Context context, boolean value) {
setBoolean(context, PREF_SHOW_PAST_EVENTS_UNDER_ONE_HEADER, value);
}

public static boolean getShowEventIcon(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_EVENT_ICON, false);
return getBoolean(context, PREF_SHOW_EVENT_ICON, false);
}

public static boolean getShowNumberOfDaysToEvent(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT, false);
return getBoolean(context, PREF_SHOW_NUMBER_OF_DAYS_TO_EVENT, false);
}

public static void setShowEventIcon(Context context, boolean value) {
Expand All @@ -258,32 +250,27 @@ public static void setShowNumberOfDaysToEvent(Context context, boolean value) {
}

public static boolean getShowPastEventsWithDefaultColor(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_PAST_EVENTS_WITH_DEFAULT_COLOR, false);
return getBoolean(context, PREF_SHOW_PAST_EVENTS_WITH_DEFAULT_COLOR, false);
}

public static void setShowPastEventsWithDefaultColor(Context context, boolean value) {
setBoolean(context, PREF_SHOW_PAST_EVENTS_WITH_DEFAULT_COLOR, value);
}

public static boolean getShowEndTime(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_END_TIME, PREF_SHOW_END_TIME_DEFAULT);
return getBoolean(context, PREF_SHOW_END_TIME, PREF_SHOW_END_TIME_DEFAULT);
}

public static boolean getShowLocation(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_LOCATION, PREF_SHOW_LOCATION_DEFAULT);
return getBoolean(context, PREF_SHOW_LOCATION, PREF_SHOW_LOCATION_DEFAULT);
}

public static String getDateFormat(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(
PREF_DATE_FORMAT, PREF_DATE_FORMAT_DEFAULT);
return getString(context, PREF_DATE_FORMAT, PREF_DATE_FORMAT_DEFAULT);
}

public static boolean getAbbreviateDates(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_ABBREVIATE_DATES, PREF_ABBREVIATE_DATES_DEFAULT);
return getBoolean(context, PREF_ABBREVIATE_DATES, PREF_ABBREVIATE_DATES_DEFAULT);
}

public static void setAbbreviateDates(Context context, boolean value) {
Expand All @@ -298,36 +285,44 @@ public static void setLockedTimeZoneId(Context context, String value) {
setString(context, PREF_LOCKED_TIME_ZONE_ID, value);
}

public static void setRefreshPeriodMinutes(Context context, int value) {
setString(context, PREF_REFRESH_PERIOD_MINUTES, Integer.toString(value > 0
? value
: PREF_REFRESH_PERIOD_MINUTES_DEFAULT));
}

public static int getRefreshPeriodMinutes(Context context) {
int stored = getIntStoredAsString(context, PREF_REFRESH_PERIOD_MINUTES, PREF_REFRESH_PERIOD_MINUTES_DEFAULT);
return stored > 0 ? stored : PREF_REFRESH_PERIOD_MINUTES_DEFAULT;
}

public static boolean isTimeZoneLocked(Context context) {
return !TextUtils.isEmpty(getLockedTimeZoneId(context));
}

public static EventEntryLayout getEventEntryLayout(Context context) {
return EventEntryLayout.fromValue(PreferenceManager.getDefaultSharedPreferences(context).getString(
PREF_EVENT_ENTRY_LAYOUT, ""));
return EventEntryLayout.fromValue(
getString(context, PREF_EVENT_ENTRY_LAYOUT, ""));
}

public static boolean isMultilineTitle(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_MULTILINE_TITLE, PREF_MULTILINE_TITLE_DEFAULT);
return getBoolean(context, PREF_MULTILINE_TITLE, PREF_MULTILINE_TITLE_DEFAULT);
}

public static boolean isMultilineDetails(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_MULTILINE_DETAILS, PREF_MULTILINE_DETAILS_DEFAULT);
return getBoolean(context, PREF_MULTILINE_DETAILS, PREF_MULTILINE_DETAILS_DEFAULT);
}

public static boolean getShowOnlyClosestInstanceOfRecurringEvent(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(PREF_SHOW_ONLY_CLOSEST_INSTANCE_OF_RECURRING_EVENT, false);
return getBoolean(context, PREF_SHOW_ONLY_CLOSEST_INSTANCE_OF_RECURRING_EVENT, false);
}

public static void setShowOnlyClosestInstanceOfRecurringEvent(Context context, boolean value) {
setBoolean(context, PREF_SHOW_ONLY_CLOSEST_INSTANCE_OF_RECURRING_EVENT, value);
}

public static boolean getHideDuplicates(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_HIDE_DUPLICATES, false);
return getBoolean(context, PREF_HIDE_DUPLICATES, false);
}

public static void setHideDuplicates(Context context, boolean value) {
Expand All @@ -341,6 +336,17 @@ private static void setString(Context context, String key, String value) {
editor.apply();
}

public static int getIntStoredAsString(Context context, String key, int defaultValue) {
try {
String stringValue = getString(context, key, "");
if (TextUtils.isEmpty(stringValue)) return defaultValue;

return Integer.parseInt(stringValue);
} catch (Exception e) {
return defaultValue;
}
}

public static String getString(Context context, String key, String defaultValue) {
return PreferenceManager.getDefaultSharedPreferences(context).getString(key, defaultValue);
}
Expand All @@ -363,6 +369,11 @@ private static void setInt(Context context, String key, int value) {
editor.apply();
}

public static int getInt(Context context, String key, int defaultValue) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getInt(key, defaultValue);
}

public static String getWidgetInstanceName(Context context) {
return getString(context, PREF_WIDGET_INSTANCE_NAME, "");
}
Expand Down
Loading

0 comments on commit 004e458

Please sign in to comment.