Skip to content

Commit

Permalink
plusonelabs#356 Automatically create new snapshot, if it doesn't exis…
Browse files Browse the repository at this point in the history
…t yet, and option to lock events selected
  • Loading branch information
yvolk committed Jan 12, 2020
1 parent 77846f1 commit 14a3e15
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ public void onDataSetChanged() {
}

private void reload() {
if (!AllSettings.isWidgetAllowed(widgetId)) {
logEvent("reload, skip as the widget is not allowed");
return;
}
long prevReloadMillis = Math.abs(System.currentTimeMillis() - prevReloadFinishedAt);
if (prevReloadMillis < MIN_MILLIS_BETWEEN_RELOADS) {
logEvent("reload, skip as done " + prevReloadMillis + " ms ago");
Expand Down
49 changes: 16 additions & 33 deletions app/src/main/java/org/andstatus/todoagenda/prefs/AllSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
import org.andstatus.todoagenda.provider.WidgetData;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import static org.andstatus.todoagenda.AppWidgetProvider.getWidgetIds;
import static org.andstatus.todoagenda.prefs.SettingsStorage.loadJsonFromFile;
Expand All @@ -28,7 +26,6 @@ public class AllSettings {
private static final String TAG = AllSettings.class.getSimpleName();
private static volatile boolean instancesLoaded = false;
private static final Map<Integer, InstanceSettings> instances = new ConcurrentHashMap<>();
private static volatile List<Integer> allowedWidgets = new CopyOnWriteArrayList<>();

@NonNull
public static InstanceSettings instanceFromId(Context context, Integer widgetId) {
Expand All @@ -49,10 +46,7 @@ private static InstanceSettings newInstance(Context context, Integer widgetId) {
} else {
settings = new InstanceSettings(context, widgetId, "");
}
if (widgetId != 0 && isWidgetAllowed(widgetId)) {
settings.save();
settings.logMe(TAG, "newInstance put", widgetId);
instances.put(widgetId, settings);
if (save("newInstance", settings)) {
EventProviderType.initialize(context, true);
EnvironmentChangedReceiver.registerReceivers(instances);
EnvironmentChangedReceiver.updateWidget(context, widgetId);
Expand Down Expand Up @@ -93,27 +87,27 @@ public static void ensureLoadedFromFiles(Context context, boolean reInitialize)
}

public static void addNew(Context context, InstanceSettings settings) {
synchronized (instances) {
if (settings.widgetId == 0) {
settings.logMe(TAG, "Skipped addNew", settings.widgetId);
} else {
instances.put(settings.widgetId, settings);
settings.save();
settings.logMe(TAG, "addNew put", settings.widgetId);
}
save("addNew", settings);
}

/** @return true if success */
private static boolean save(String method, InstanceSettings settings) {
if (settings.isEmpty()) {
settings.logMe(TAG, "Skipped save empty from " + method, settings.widgetId);
} else if (settings.save(method)) {
instances.put(settings.widgetId, settings);
return true;
}
return false;
}

public static void saveFromApplicationPreferences(Context context, Integer widgetId) {
if (widgetId == 0) {
return;
}
if (widgetId == 0) return;

InstanceSettings settingsStored = instanceFromId(context, widgetId);
InstanceSettings settings = InstanceSettings.fromApplicationPreferences(context, widgetId, settingsStored);
if (settings.widgetId == widgetId && !settings.equals(settingsStored)) {
settings.save();
settings.logMe(TAG, "saveFromApplicationPreferences put", widgetId);
instances.put(widgetId, settings);
save("ApplicationPreferences", settings);
}
EnvironmentChangedReceiver.registerReceivers(instances);
}
Expand Down Expand Up @@ -176,28 +170,17 @@ public static Map<Integer, InstanceSettings> getLoadedInstances() {
return instances;
}

public static boolean isWidgetAllowed(int widgetId) {
return allowedWidgets.isEmpty() || allowedWidgets.contains(widgetId);
}

public static void forget() {
synchronized (instances) {
instances.clear();
instancesLoaded = false;
allowedWidgets.clear();
}
}

public static InstanceSettings restoreWidgetSettings(Activity activity, JSONObject json, int targetWidgetId) {
InstanceSettings settings = WidgetData.fromJson(json)
.getSettingsForWidget(activity, instances.get(targetWidgetId), targetWidgetId);
if (settings.isEmpty()) {
settings.logMe(TAG, "Skipped restoreWidgetSettings", settings.widgetId);
} else {
settings.save();
settings.logMe(TAG, "restoreWidgetSettings put", settings.widgetId);
instances.put(settings.widgetId, settings);
}
save("restoreWidgetSettings", settings);
return settings;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package org.andstatus.todoagenda.prefs;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.util.Log;

import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.provider.WidgetData;

import static android.content.Intent.ACTION_CREATE_DOCUMENT;
import static org.andstatus.todoagenda.WidgetConfigurationActivity.REQUEST_ID_BACKUP_SETTINGS;
Expand Down Expand Up @@ -38,29 +35,18 @@ public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preferen
.replaceAll("\\W+", "-") +
"-backup-" + formatLogDateTime(System.currentTimeMillis()) +
".json";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent intent = new Intent(ACTION_CREATE_DOCUMENT);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_TITLE, fileName);
intent.addCategory(Intent.CATEGORY_OPENABLE);
getActivity().startActivityForResult(intent, REQUEST_ID_BACKUP_SETTINGS);
} else {
String jsonSettings = WidgetData.fromSettings(settings.getContext(), settings).toJsonString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_SUBJECT, fileName);
intent.putExtra(Intent.EXTRA_TEXT, jsonSettings);
getActivity().startActivity(
Intent.createChooser(intent, getActivity().getText(R.string.backup_settings_title)));
Log.i(TAG, "onPreferenceTreeClick; Backed up \n" + jsonSettings);
}
Intent intent = new Intent(ACTION_CREATE_DOCUMENT);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_TITLE, fileName);
intent.addCategory(Intent.CATEGORY_OPENABLE);
getActivity().startActivityForResult(intent, REQUEST_ID_BACKUP_SETTINGS);
break;
case KEY_RESTORE_SETTINGS:
Intent intent = new Intent()
Intent intent2 = new Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE);
Intent withChooser = Intent.createChooser(intent,
Intent withChooser = Intent.createChooser(intent2,
getActivity().getText(R.string.restore_settings_title));
getActivity().startActivityForResult(withChooser, REQUEST_ID_RESTORE_SETTINGS);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,8 @@ static InstanceSettings fromApplicationPreferences(Context context, int widgetId

settings.clock().setLockedTimeZoneId(ApplicationPreferences.getLockedTimeZoneId(context));
settings.clock().setSnapshotMode(ApplicationPreferences.getSnapshotMode(context));
if (settingsStored != null) {
if (settingsStored.getResultsStorage() != null) {
settings.setResultsStorage(settingsStored.getResultsStorage());
}
if (settingsStored != null && settingsStored.hasResults()) {
settings.setResultsStorage(settingsStored.getResultsStorage());
}
return settings;
}
Expand All @@ -388,17 +386,21 @@ public boolean isEmpty() {
return widgetId == 0;
}

void save() {
/** @return true if success */
boolean save(String method) {
String msgLog = "save from " + method;
if (widgetId == 0) {
logMe(TAG, "Skipped save", widgetId);
return;
logMe(TAG, "Skipped " + msgLog, widgetId);
return false;
}
logMe(TAG, "save", widgetId);
logMe(TAG, msgLog, widgetId);
try {
saveJson(context, getStorageKey(widgetId), toJson());
return true;
} catch (IOException e) {
Log.e("save", toString(), e);
Log.e(TAG, msgLog + "\n" + toString(), e);
}
return false;
}

public JSONObject toJson() {
Expand Down Expand Up @@ -711,6 +713,10 @@ public InstanceSettings asForWidget(Context context, int targetWidgetId) {
return new InstanceSettings(context, targetWidgetId, newName).setFromJson(toJson());
}

public boolean hasResults() {
return resultsStorage != null && !resultsStorage.getResults().isEmpty();
}

public QueryResultsStorage getResultsStorage() {
return resultsStorage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.andstatus.todoagenda.MainActivity;
import org.andstatus.todoagenda.R;
import org.andstatus.todoagenda.provider.QueryResultsStorage;
import org.andstatus.todoagenda.util.DateUtil;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -101,6 +102,19 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
case InstanceSettings.PREF_REFRESH_PERIOD_MINUTES:
showRefreshPeriod();
break;
case InstanceSettings.PREF_SNAPSHOT_MODE:
SnapshotMode snapshotMode = ApplicationPreferences.getSnapshotMode(getActivity());
if (snapshotMode != SnapshotMode.LIVE_DATA) {
int widgetId = ApplicationPreferences.getWidgetId(getActivity());
InstanceSettings settings = AllSettings.instanceFromId(getActivity(), widgetId);
if (!settings.hasResults()) {
settings.setResultsStorage(QueryResultsStorage.getNewResults(getActivity(), widgetId));
settings.clock().setSnapshotMode(snapshotMode);
settings.save("newResultsForSnapshotMode");
}
}
showSnapshotMode();
break;
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,40 @@ public static boolean store(QueryResult result) {

public static void shareEventsForDebugging(Context context, int widgetId) {
final String method = "shareEventsForDebugging";
Log.i(TAG, method + " started");
InstanceSettings settings = AllSettings.instanceFromId(context, widgetId);
QueryResultsStorage storage = settings.isLiveMode() || !settings.hasResults()
? settings.getResultsStorage()
: getNewResults(context, widgetId);
String results = storage.toJsonString(context, widgetId);
if (TextUtils.isEmpty(results)) {
Log.i(TAG, method + "; Nothing to share");
} else {
String fileName = (settings.getWidgetInstanceName() + "-" + context.getText(R.string.app_name))
.replaceAll("\\W+", "-") +
"-shareEvents-" + formatLogDateTime(System.currentTimeMillis()) +
".json";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_SUBJECT, fileName);
intent.putExtra(Intent.EXTRA_TEXT, results);
context.startActivity(
Intent.createChooser(intent, context.getText(R.string.share_events_for_debugging_title)));
Log.i(TAG, method + "; Shared " + results);
}
}

public static QueryResultsStorage getNewResults(Context context, int widgetId) {
QueryResultsStorage resultsStorage;
try {
Log.i(TAG, method + " started");
setNeedToStoreResults(true, widgetId);
RemoteViewsFactory factory = new RemoteViewsFactory(context, widgetId);
factory.onDataSetChanged();
String results = theStorage.toJsonString(context, widgetId);
if (TextUtils.isEmpty(results)) {
Log.i(TAG, method + "; Nothing to share");
} else {
InstanceSettings settings = AllSettings.instanceFromId(context, widgetId);
String fileName = (settings.getWidgetInstanceName() + "-" + context.getText(R.string.app_name))
.replaceAll("\\W+", "-") +
"-shareEvents-" + formatLogDateTime(System.currentTimeMillis()) +
".json";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("application/json");
intent.putExtra(Intent.EXTRA_SUBJECT, fileName);
intent.putExtra(Intent.EXTRA_TEXT, results);
context.startActivity(
Intent.createChooser(intent, context.getText(R.string.share_events_for_debugging_title)));
Log.i(TAG, method + "; Shared " + results);
}
resultsStorage = QueryResultsStorage.theStorage;
} finally {
setNeedToStoreResults(false, widgetId);
}
return resultsStorage;
}

public static boolean getNeedToStoreResults(int widgetId) {
Expand Down

0 comments on commit 14a3e15

Please sign in to comment.