diff --git a/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java b/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java index 36f65d86..62d7e93a 100644 --- a/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java +++ b/app/src/main/java/org/andstatus/todoagenda/RemoteViewsFactory.java @@ -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"); diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/AllSettings.java b/app/src/main/java/org/andstatus/todoagenda/prefs/AllSettings.java index 022c3dbb..ec17fa11 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/AllSettings.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/AllSettings.java @@ -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; @@ -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 instances = new ConcurrentHashMap<>(); - private static volatile List allowedWidgets = new CopyOnWriteArrayList<>(); @NonNull public static InstanceSettings instanceFromId(Context context, Integer widgetId) { @@ -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); @@ -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); } @@ -176,28 +170,17 @@ public static Map 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; } } diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/FeedbackPreferencesFragment.java b/app/src/main/java/org/andstatus/todoagenda/prefs/FeedbackPreferencesFragment.java index 5f4d8741..40ff3f63 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/FeedbackPreferencesFragment.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/FeedbackPreferencesFragment.java @@ -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; @@ -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; diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java b/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java index d368613b..71a18cee 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/InstanceSettings.java @@ -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; } @@ -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() { @@ -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; } diff --git a/app/src/main/java/org/andstatus/todoagenda/prefs/OtherPreferencesFragment.java b/app/src/main/java/org/andstatus/todoagenda/prefs/OtherPreferencesFragment.java index fe4054dc..022c5137 100644 --- a/app/src/main/java/org/andstatus/todoagenda/prefs/OtherPreferencesFragment.java +++ b/app/src/main/java/org/andstatus/todoagenda/prefs/OtherPreferencesFragment.java @@ -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; @@ -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; } diff --git a/app/src/main/java/org/andstatus/todoagenda/provider/QueryResultsStorage.java b/app/src/main/java/org/andstatus/todoagenda/provider/QueryResultsStorage.java index 78d387f7..b78faccf 100644 --- a/app/src/main/java/org/andstatus/todoagenda/provider/QueryResultsStorage.java +++ b/app/src/main/java/org/andstatus/todoagenda/provider/QueryResultsStorage.java @@ -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) {