Skip to content

Commit

Permalink
plusonelabs#356 "Lock list of events (Snapshot mode)" option added al…
Browse files Browse the repository at this point in the history
…lowing to see not only live data, but a snapshot of events, taken on this on on another device. Snapshot is transferred using "Share events and settings for debugging" on one device -> and "Restore settings from a backup" on another device or a widget.
  • Loading branch information
yvolk committed Dec 30, 2019
1 parent 041c48a commit 2f01976
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.andstatus.todoagenda.prefs.InstanceSettings;
import org.andstatus.todoagenda.prefs.OrderedEventSource;
import org.andstatus.todoagenda.prefs.SettingsStorage;
import org.andstatus.todoagenda.prefs.SnapshotMode;
import org.andstatus.todoagenda.util.DateUtil;
import org.andstatus.todoagenda.util.RawResourceUtils;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -67,7 +68,10 @@ private MockCalendarContentProvider(Context context) {

public void updateAppSettings() {
InstanceSettings settings = AllSettings.instanceFromId(context, widgetId);
settings.setResultsStorage(results);
if (!results.getResults().isEmpty()) {
settings.setResultsStorage(results);
settings.setSnapshotMode(SnapshotMode.SNAPSHOT_TIME);
}
AllSettings.addNew(context, settings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_SHOW_ONLY_CLOSEST_INSTANCE_OF_RECURRING_EVENT;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_SHOW_PAST_EVENTS_UNDER_ONE_HEADER;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_SHOW_PAST_EVENTS_WITH_DEFAULT_COLOR;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_SNAPSHOT_MODE;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TASK_SCHEDULING;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TASK_WITHOUT_DATES;
import static org.andstatus.todoagenda.prefs.InstanceSettings.PREF_TEXT_SIZE_SCALE;
Expand Down Expand Up @@ -289,6 +290,10 @@ public static void setLockedTimeZoneId(Context context, String value) {
setString(context, PREF_LOCKED_TIME_ZONE_ID, value);
}

public static SnapshotMode getSnapshotMode(Context context) {
return SnapshotMode.fromValue(getString(context, PREF_SNAPSHOT_MODE, ""));
}

public static void setRefreshPeriodMinutes(Context context, int value) {
setString(context, PREF_REFRESH_PERIOD_MINUTES, Integer.toString(value > 0
? value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public class InstanceSettings {
static final String PREF_LOCK_TIME_ZONE = "lockTimeZone";
static final String PREF_LOCKED_TIME_ZONE_ID = "lockedTimeZoneId";
private String lockedTimeZoneId = "";
static final String PREF_SNAPSHOT_MODE = "snapshotMode";
private SnapshotMode snapshotMode = SnapshotMode.defaultValue;
public final static String PREF_REFRESH_PERIOD_MINUTES = "refreshPeriodMinutes";
public final static int PREF_REFRESH_PERIOD_MINUTES_DEFAULT = 10;
private int refreshPeriodMinutes = PREF_REFRESH_PERIOD_MINUTES_DEFAULT;
Expand Down Expand Up @@ -252,6 +254,9 @@ private InstanceSettings setFromJson(JSONObject json) {
if (json.has(PREF_LOCKED_TIME_ZONE_ID)) {
setLockedTimeZoneId(json.getString(PREF_LOCKED_TIME_ZONE_ID));
}
if (json.has(PREF_SNAPSHOT_MODE)) {
setSnapshotMode(SnapshotMode.fromValue(json.getString(PREF_SNAPSHOT_MODE)));
}
if (json.has(PREF_REFRESH_PERIOD_MINUTES)) {
setRefreshPeriodMinutes(json.getInt(PREF_REFRESH_PERIOD_MINUTES));
}
Expand Down Expand Up @@ -338,6 +343,7 @@ static InstanceSettings fromApplicationPreferences(Context context, int widgetId
settings.dateFormat = ApplicationPreferences.getDateFormat(context);
settings.abbreviateDates = ApplicationPreferences.getAbbreviateDates(context);
settings.setLockedTimeZoneId(ApplicationPreferences.getLockedTimeZoneId(context));
settings.setSnapshotMode(ApplicationPreferences.getSnapshotMode(context));
settings.setRefreshPeriodMinutes(ApplicationPreferences.getRefreshPeriodMinutes(context));
settings.eventEntryLayout = ApplicationPreferences.getEventEntryLayout(context);
settings.multilineTitle = ApplicationPreferences.isMultilineTitle(context);
Expand Down Expand Up @@ -423,6 +429,7 @@ public JSONObject toJson() {
json.put(PREF_DATE_FORMAT, dateFormat);
json.put(PREF_ABBREVIATE_DATES, abbreviateDates);
json.put(PREF_LOCKED_TIME_ZONE_ID, lockedTimeZoneId);
json.put(PREF_SNAPSHOT_MODE, snapshotMode.value);
json.put(PREF_REFRESH_PERIOD_MINUTES, refreshPeriodMinutes);
json.put(PREF_EVENT_ENTRY_LAYOUT, eventEntryLayout.value);
json.put(PREF_MULTILINE_TITLE, multilineTitle);
Expand Down Expand Up @@ -560,6 +567,18 @@ public String getLockedTimeZoneId() {
return lockedTimeZoneId;
}

public void setSnapshotMode(SnapshotMode snapshotMode) {
this.snapshotMode = snapshotMode;
}

public SnapshotMode getSnapshotMode() {
return resultsStorage == null ? SnapshotMode.LIVE_DATA : snapshotMode;
}

public boolean isLiveMode() {
return getSnapshotMode() == SnapshotMode.LIVE_DATA;
}

public void setRefreshPeriodMinutes(int refreshPeriodMinutes) {
if (refreshPeriodMinutes > 0) {
this.refreshPeriodMinutes = refreshPeriodMinutes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void onResume() {
super.onResume();
showLockTimeZone(true);
showWidgetInstanceName();
showSnapshotMode();
showRefreshPeriod();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
Expand All @@ -49,6 +50,13 @@ private void showLockTimeZone(boolean setAlso) {
}
}

private void showSnapshotMode() {
Preference preference = findPreference(InstanceSettings.PREF_SNAPSHOT_MODE);
if (preference != null) {
preference.setSummary(ApplicationPreferences.getSnapshotMode(getActivity()).valueResId);
}
}

private void showRefreshPeriod() {
EditTextPreference preference = (EditTextPreference) findPreference(InstanceSettings.PREF_REFRESH_PERIOD_MINUTES);

Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/org/andstatus/todoagenda/prefs/SnapshotMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.andstatus.todoagenda.prefs;

import androidx.annotation.StringRes;

import org.andstatus.todoagenda.R;

/**
* @author [email protected]
*/
public enum SnapshotMode {
LIVE_DATA("live_data", R.string.snapshot_mode_live_data),
SNAPSHOT_TIME("snapshot_time", R.string.snapshot_mode_time),
SNAPSHOT_NOW("snapshot_now", R.string.snapshot_mode_now);

public final static SnapshotMode defaultValue = LIVE_DATA;

public final String value;
@StringRes
public final int valueResId;

SnapshotMode(String value, int valueResId) {
this.value = value;
this.valueResId = valueResId;
}

public static SnapshotMode fromValue(String value) {
for (SnapshotMode item : SnapshotMode.values()) {
if (item.value.equals(value)) {
return item;
}
}
return defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public InstanceSettings getSettings() {
}

public boolean isPermissionNeeded(Context context, String permission) {
return getSettings().getResultsStorage() == null && PermissionsUtil.isPermissionNeeded(context, permission);
return getSettings().isLiveMode() && PermissionsUtil.isPermissionNeeded(context, permission);
}

public <R> R foldAvailableSources(@NonNull Uri uri, @Nullable String[] projection,
Expand All @@ -68,7 +68,7 @@ public <R> R foldAvailableSources(@NonNull Uri uri, @Nullable String[] projectio

private Cursor queryAvailableSources(@NonNull Uri uri, @Nullable String[] projection) {
try {
return widgetId == 0 || getSettings().getResultsStorage() == null
return widgetId == 0 || getSettings().isLiveMode()
? context.getContentResolver().query(uri, projection, null, null, null)
: getSettings().getResultsStorage().getResult(type, requestsCounter.incrementAndGet() - 1)
.map(r -> r.querySource(projection)).orElse(null);
Expand Down Expand Up @@ -112,7 +112,7 @@ public <R> R foldEvents(@NonNull Uri uri, @Nullable String[] projection, @Nullab

private Cursor queryForEvents(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
@Nullable String[] selectionArgs, @Nullable String sortOrder) {
return getSettings().getResultsStorage() == null
return getSettings().isLiveMode()
? context.getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder)
: getSettings().getResultsStorage().getResult(type, requestsCounter.incrementAndGet() - 1)
.map(r -> r.query(projection)).orElse(null);
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,14 @@
<item>debug</item>
<item>no_filtering</item>
</string-array>
<string-array name="pref_snapshot_mode_entries">
<item>@string/snapshot_mode_live_data</item>
<item>@string/snapshot_mode_time</item>
<item>@string/snapshot_mode_now</item>
</string-array>
<string-array name="pref_snapshot_mode_values" tools:ignore="MissingTranslation">
<item>live_data</item>
<item>snapshot_time</item>
<item>snapshot_now</item>
</string-array>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@
<string name="filter_mode_no_filtering">Ignore all filters (turn them off)</string>

<!-- Preference header: Other -->
<string name="snapshot_mode">Lock list of events (Snapshot mode)</string>
<string name="snapshot_mode_live_data">Live data from real sources</string>
<string name="snapshot_mode_time">Snapshot as seen at the time, when it was taken</string>
<string name="snapshot_mode_now">Stored snapshot shown at current time</string>
<string name="refresh_period_minutes">Auto refresh period, minutes</string>
<string name="refresh_period_minutes_desc">Automatically refresh the widget every %d minute(s)</string>

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences_other.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
android:title="@string/lock_time_zone_title">
</CheckBoxPreference>

<ListPreference
android:key="snapshotMode"
android:defaultValue="live_data"
android:entries="@array/pref_snapshot_mode_entries"
android:entryValues="@array/pref_snapshot_mode_values"
android:title="@string/snapshot_mode" />

<EditTextPreference
android:key="refreshPeriodMinutes"
android:title="@string/refresh_period_minutes"
Expand Down

0 comments on commit 2f01976

Please sign in to comment.