Backup and restore preferences

pull/820/head
Alex Baker 6 years ago
parent 49df4b9212
commit 1c944f15fd

@ -1,9 +1,11 @@
package org.tasks.backup;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import com.todoroo.astrid.data.Task;
import java.util.List;
import java.util.Map;
import org.tasks.backup.TasksJsonImporter.LegacyLocation;
import org.tasks.data.Alarm;
import org.tasks.data.CaldavAccount;
@ -30,6 +32,10 @@ class BackupContainer {
private final List<GoogleTaskAccount> googleTaskAccounts;
private final List<CaldavAccount> caldavAccounts;
private final List<CaldavCalendar> caldavCalendars;
private final Map<String, Integer> intPrefs;
private final Map<String, Long> longPrefs;
private final Map<String, String> stringPrefs;
private final Map<String, Boolean> boolPrefs;
BackupContainer(
List<TaskBackup> tasks,
@ -39,7 +45,11 @@ class BackupContainer {
List<GoogleTaskAccount> googleTaskAccounts,
List<GoogleTaskList> googleTaskLists,
List<CaldavAccount> caldavAccounts,
List<CaldavCalendar> caldavCalendars) {
List<CaldavCalendar> caldavCalendars,
Map<String, Integer> intPrefs,
Map<String, Long> longPrefs,
Map<String, String> stringPrefs,
Map<String, Boolean> boolPrefs) {
this.tasks = tasks;
this.places = places;
this.tags = tags;
@ -48,6 +58,10 @@ class BackupContainer {
this.googleTaskLists = googleTaskLists;
this.caldavAccounts = caldavAccounts;
this.caldavCalendars = caldavCalendars;
this.intPrefs = intPrefs;
this.longPrefs = longPrefs;
this.stringPrefs = stringPrefs;
this.boolPrefs = boolPrefs;
}
public List<TaskBackup> getTasks() {
@ -62,19 +76,19 @@ class BackupContainer {
return filters == null ? emptyList() : filters;
}
public List<GoogleTaskList> getGoogleTaskLists() {
List<GoogleTaskList> getGoogleTaskLists() {
return googleTaskLists == null ? emptyList() : googleTaskLists;
}
public List<CaldavAccount> getCaldavAccounts() {
List<CaldavAccount> getCaldavAccounts() {
return caldavAccounts == null ? emptyList() : caldavAccounts;
}
public List<CaldavCalendar> getCaldavCalendars() {
List<CaldavCalendar> getCaldavCalendars() {
return caldavCalendars == null ? emptyList() : caldavCalendars;
}
public List<GoogleTaskAccount> getGoogleTaskAccounts() {
List<GoogleTaskAccount> getGoogleTaskAccounts() {
return googleTaskAccounts == null ? emptyList() : googleTaskAccounts;
}
@ -82,6 +96,22 @@ class BackupContainer {
return places == null ? emptyList() : places;
}
Map<String, Integer> getIntPrefs() {
return intPrefs == null ? emptyMap() : intPrefs;
}
Map<String, Long> getLongPrefs() {
return longPrefs == null ? emptyMap() : longPrefs;
}
Map<String, String> getStringPrefs() {
return stringPrefs == null ? emptyMap() : stringPrefs;
}
Map<String, Boolean> getBoolPrefs() {
return boolPrefs == null ? emptyMap() : boolPrefs;
}
static class TaskBackup {
final Task task;

@ -202,7 +202,11 @@ public class TasksJsonExporter {
googleTaskListDao.getAccounts(),
googleTaskListDao.getAllLists(),
caldavDao.getAccounts(),
caldavDao.getCalendars()));
caldavDao.getCalendars(),
preferences.getPrefs(Integer.class),
preferences.getPrefs(Long.class),
preferences.getPrefs(String.class),
preferences.getPrefs(Boolean.class)));
OutputStreamWriter out = new OutputStreamWriter(os, UTF_8);
Gson gson = BuildConfig.DEBUG ? new GsonBuilder().setPrettyPrinting().create() : new Gson();

@ -14,11 +14,11 @@ import com.google.gson.JsonObject;
import com.todoroo.andlib.utility.DialogUtilities;
import com.todoroo.astrid.dao.TaskDao;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.helper.UUIDHelper;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map.Entry;
import javax.inject.Inject;
import org.tasks.LocalBroadcastManager;
import org.tasks.R;
@ -47,6 +47,7 @@ import org.tasks.data.TaskAttachmentDao;
import org.tasks.data.UserActivity;
import org.tasks.data.UserActivityDao;
import org.tasks.dialogs.DialogBuilder;
import org.tasks.preferences.Preferences;
import timber.log.Timber;
public class TasksJsonImporter {
@ -63,6 +64,7 @@ public class TasksJsonImporter {
private final FilterDao filterDao;
private final TaskAttachmentDao taskAttachmentDao;
private final CaldavDao caldavDao;
private final Preferences preferences;
private final LocationDao locationDao;
private Activity activity;
@ -87,7 +89,8 @@ public class TasksJsonImporter {
GoogleTaskListDao googleTaskListDao,
FilterDao filterDao,
TaskAttachmentDao taskAttachmentDao,
CaldavDao caldavDao) {
CaldavDao caldavDao,
Preferences preferences) {
this.tagDataDao = tagDataDao;
this.userActivityDao = userActivityDao;
this.dialogBuilder = dialogBuilder;
@ -101,6 +104,7 @@ public class TasksJsonImporter {
this.filterDao = filterDao;
this.taskAttachmentDao = taskAttachmentDao;
this.caldavDao = caldavDao;
this.preferences = preferences;
}
private void setProgressMessage(final String message) {
@ -232,6 +236,18 @@ public class TasksJsonImporter {
}
importCount++;
}
for (Entry<String, Integer> entry : backupContainer.getIntPrefs().entrySet()) {
preferences.setInt(entry.getKey(), entry.getValue());
}
for (Entry<String, Long> entry : backupContainer.getLongPrefs().entrySet()) {
preferences.setLong(entry.getKey(), entry.getValue());
}
for (Entry<String, String> entry : backupContainer.getStringPrefs().entrySet()) {
preferences.setString(entry.getKey(), entry.getValue());
}
for (Entry<String, Boolean> entry : backupContainer.getBoolPrefs().entrySet()) {
preferences.setBoolean(entry.getKey(), entry.getValue());
}
reader.close();
is.close();
} catch (IOException e) {

@ -421,6 +421,7 @@ public class BasicPreferences extends InjectingPreferenceActivity
} else if (requestCode == REQUEST_PICKER) {
if (resultCode == RESULT_OK) {
newImportTasksDialog(data.getData()).show(getFragmentManager(), FRAG_TAG_IMPORT_TASKS);
result.putBoolean(AppearancePreferences.EXTRA_RESTART, true);
}
} else if (requestCode == RC_DRIVE_BACKUP) {
((CheckBoxPreference) findPreference(R.string.p_google_drive_backup))

@ -1,6 +1,7 @@
package org.tasks.preferences;
import static android.content.SharedPreferences.Editor;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Sets.newHashSet;
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastKitKat;
@ -27,6 +28,9 @@ import com.todoroo.astrid.core.SortHelper;
import com.todoroo.astrid.data.Task;
import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import org.tasks.R;
import org.tasks.billing.Purchase;
@ -299,7 +303,7 @@ public class Preferences {
setBoolean(context.getString(keyResource), value);
}
void setBoolean(String key, boolean value) {
public void setBoolean(String key, boolean value) {
Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.apply();
@ -488,4 +492,14 @@ public class Preferences {
public boolean useGooglePlaces() {
return getInt(R.string.p_place_provider, 0) == 1;
}
public <T> Map<String, T> getPrefs(Class<T> c) {
Map<String, T> result = new HashMap<>();
Iterable<? extends Entry<String, ?>> entries =
filter(prefs.getAll().entrySet(), e -> c.isInstance(e.getValue()));
for (Entry<String, ?> entry : entries) {
result.put(entry.getKey(), (T) entry.getValue());
}
return result;
}
}

Loading…
Cancel
Save