mirror of
https://github.com/russok/FitoTrack.git
synced 2025-10-29 00:32:11 -07:00
Refactor Backup/Restore Process
This commit is contained in:
parent
db0690a351
commit
7b15571e1b
@ -50,7 +50,8 @@ import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import de.tadris.fitness.R;
|
||||
import de.tadris.fitness.util.export.Exporter;
|
||||
import de.tadris.fitness.export.BackupController;
|
||||
import de.tadris.fitness.export.RestoreController;
|
||||
import de.tadris.fitness.util.unit.UnitUtils;
|
||||
import de.tadris.fitness.view.ProgressDialogController;
|
||||
|
||||
@ -185,8 +186,8 @@ public class SettingsActivity extends PreferenceActivity {
|
||||
new File(file).getParentFile().mkdirs();
|
||||
Uri uri= FileProvider.getUriForFile(getBaseContext(), "de.tadris.fitness.fileprovider", new File(file));
|
||||
|
||||
Exporter.exportData(getBaseContext(), new File(file),
|
||||
(progress, action) -> mHandler.post(() -> dialogController.setProgress(progress, action)));
|
||||
BackupController backupController= new BackupController(getBaseContext(), new File(file), (progress, action) -> mHandler.post(() -> dialogController.setProgress(progress, action)));
|
||||
backupController.exportData();
|
||||
|
||||
mHandler.post(() -> {
|
||||
dialogController.cancel();
|
||||
@ -254,10 +255,10 @@ public class SettingsActivity extends PreferenceActivity {
|
||||
dialogController.show();
|
||||
new Thread(() -> {
|
||||
try{
|
||||
Exporter.importData(getBaseContext(), uri,
|
||||
RestoreController restoreController= new RestoreController(getBaseContext(), uri,
|
||||
(progress, action) -> mHandler.post(() -> dialogController.setProgress(progress, action)));
|
||||
restoreController.restoreData();
|
||||
|
||||
// DO on backup finished
|
||||
mHandler.post(dialogController::cancel);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
|
||||
@ -0,0 +1,90 @@
|
||||
package de.tadris.fitness.export;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import de.tadris.fitness.Instance;
|
||||
import de.tadris.fitness.R;
|
||||
import de.tadris.fitness.data.AppDatabase;
|
||||
import de.tadris.fitness.data.UserPreferences;
|
||||
import de.tadris.fitness.util.unit.UnitUtils;
|
||||
|
||||
public class BackupController {
|
||||
|
||||
public static final int VERSION= 1;
|
||||
|
||||
private Context context;
|
||||
private File output;
|
||||
private ExportStatusListener listener;
|
||||
private UserPreferences preferences;
|
||||
private AppDatabase database;
|
||||
|
||||
private FitoTrackDataContainer dataContainer;
|
||||
|
||||
public BackupController(Context context, File output, ExportStatusListener listener) {
|
||||
this.context = context;
|
||||
this.output = output;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void exportData() throws IOException {
|
||||
listener.onStatusChanged(0, context.getString(R.string.initialising));
|
||||
init();
|
||||
listener.onStatusChanged(10, context.getString(R.string.preferences));
|
||||
newContainer();
|
||||
|
||||
saveSettingsToContainer();
|
||||
listener.onStatusChanged(20, context.getString(R.string.workouts));
|
||||
saveWorkoutsToContainer();
|
||||
listener.onStatusChanged(40, context.getString(R.string.locationData));
|
||||
saveSamplesToContainer();
|
||||
listener.onStatusChanged(60, context.getString(R.string.converting));
|
||||
writeContainerToOutputFile();
|
||||
listener.onStatusChanged(100, context.getString(R.string.finished));
|
||||
}
|
||||
|
||||
private void init(){
|
||||
preferences= Instance.getInstance(context).userPreferences;
|
||||
database= Instance.getInstance(context).db;
|
||||
UnitUtils.setUnit(context); // Ensure unit system is correct
|
||||
}
|
||||
|
||||
private void newContainer(){
|
||||
dataContainer= new FitoTrackDataContainer();
|
||||
dataContainer.version= VERSION;
|
||||
dataContainer.workouts= new ArrayList<>();
|
||||
dataContainer.samples= new ArrayList<>();
|
||||
}
|
||||
|
||||
private void saveSettingsToContainer(){
|
||||
FitoTrackSettings settings= new FitoTrackSettings();
|
||||
settings.weight= preferences.getUserWeight();
|
||||
settings.mapStyle= preferences.getMapStyle();
|
||||
settings.preferredUnitSystem= String.valueOf(UnitUtils.CHOSEN_SYSTEM.getId());
|
||||
dataContainer.settings= settings;
|
||||
}
|
||||
|
||||
private void saveWorkoutsToContainer(){
|
||||
dataContainer.workouts.addAll(Arrays.asList(database.workoutDao().getWorkouts()));
|
||||
}
|
||||
|
||||
private void saveSamplesToContainer(){
|
||||
dataContainer.samples.addAll(Arrays.asList(database.workoutDao().getSamples()));
|
||||
}
|
||||
|
||||
private void writeContainerToOutputFile() throws IOException {
|
||||
XmlMapper mapper= new XmlMapper();
|
||||
mapper.writeValue(output, dataContainer);
|
||||
}
|
||||
|
||||
public interface ExportStatusListener{
|
||||
void onStatusChanged(int progress, String action);
|
||||
}
|
||||
|
||||
}
|
||||
@ -17,7 +17,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.tadris.fitness.util.export;
|
||||
package de.tadris.fitness.export;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
@ -17,7 +17,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.tadris.fitness.util.export;
|
||||
package de.tadris.fitness.export;
|
||||
|
||||
public class FitoTrackSettings {
|
||||
|
||||
@ -0,0 +1,107 @@
|
||||
package de.tadris.fitness.export;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import de.tadris.fitness.Instance;
|
||||
import de.tadris.fitness.R;
|
||||
import de.tadris.fitness.data.AppDatabase;
|
||||
import de.tadris.fitness.data.Workout;
|
||||
import de.tadris.fitness.data.WorkoutSample;
|
||||
|
||||
public class RestoreController {
|
||||
|
||||
private Context context;
|
||||
private Uri input;
|
||||
private ImportStatusListener listener;
|
||||
private FitoTrackDataContainer dataContainer;
|
||||
private AppDatabase database;
|
||||
|
||||
public RestoreController(Context context, Uri input, ImportStatusListener listener) {
|
||||
this.context = context;
|
||||
this.input = input;
|
||||
this.listener = listener;
|
||||
this.database= Instance.getInstance(context).db;
|
||||
}
|
||||
|
||||
public void restoreData() throws IOException, UnsupportedVersionException{
|
||||
listener.onStatusChanged(0, context.getString(R.string.loadingFile));
|
||||
loadDataFromFile();
|
||||
checkVersion();
|
||||
listener.onStatusChanged(40, context.getString(R.string.preferences));
|
||||
restoreSettings();
|
||||
|
||||
restoreDatabase();
|
||||
listener.onStatusChanged(100, context.getString(R.string.finished));
|
||||
}
|
||||
|
||||
private void loadDataFromFile() throws IOException {
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
xmlMapper.configure(JsonParser.Feature.IGNORE_UNDEFINED, true);
|
||||
dataContainer = xmlMapper.readValue(context.getContentResolver().openInputStream(input), FitoTrackDataContainer.class);
|
||||
}
|
||||
|
||||
private void checkVersion() throws UnsupportedVersionException {
|
||||
if(dataContainer.version != 1){
|
||||
throw new UnsupportedVersionException("Version Code" + dataContainer.version + " is unsupported!");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("ApplySharedPref")
|
||||
private void restoreSettings(){
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.edit().clear()
|
||||
.putInt("weight", dataContainer.settings.weight)
|
||||
.putString("unitSystem", dataContainer.settings.preferredUnitSystem)
|
||||
.putBoolean("firstStart", false).putString("mapStyle", dataContainer.settings.mapStyle)
|
||||
.commit();
|
||||
}
|
||||
|
||||
private void restoreDatabase(){
|
||||
database.runInTransaction(() -> {
|
||||
resetDatabase();
|
||||
restoreWorkouts();
|
||||
restoreSamples();
|
||||
});
|
||||
}
|
||||
|
||||
private void resetDatabase(){
|
||||
database.clearAllTables();
|
||||
}
|
||||
|
||||
private void restoreWorkouts(){
|
||||
listener.onStatusChanged(60, context.getString(R.string.workouts));
|
||||
if(dataContainer.workouts != null){
|
||||
for(Workout workout : dataContainer.workouts){
|
||||
database.workoutDao().insertWorkout(workout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void restoreSamples(){
|
||||
listener.onStatusChanged(80, context.getString(R.string.locationData));
|
||||
if(dataContainer.samples != null){
|
||||
for(WorkoutSample sample : dataContainer.samples){
|
||||
database.workoutDao().insertSample(sample);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface ImportStatusListener{
|
||||
void onStatusChanged(int progress, String action);
|
||||
}
|
||||
|
||||
public static class UnsupportedVersionException extends Exception{
|
||||
public UnsupportedVersionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,132 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2019 Jannis Scheibe <jannis@tadris.de>
|
||||
*
|
||||
* This file is part of FitoTrack
|
||||
*
|
||||
* FitoTrack is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* FitoTrack is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.tadris.fitness.util.export;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import de.tadris.fitness.Instance;
|
||||
import de.tadris.fitness.R;
|
||||
import de.tadris.fitness.data.AppDatabase;
|
||||
import de.tadris.fitness.data.UserPreferences;
|
||||
import de.tadris.fitness.data.Workout;
|
||||
import de.tadris.fitness.data.WorkoutSample;
|
||||
import de.tadris.fitness.util.unit.UnitUtils;
|
||||
|
||||
public class Exporter {
|
||||
|
||||
public static final int VERSION= 1;
|
||||
|
||||
public static void exportData(Context context, File output, ExportStatusListener listener) throws IOException {
|
||||
listener.onStatusChanged(0, context.getString(R.string.initialising));
|
||||
UserPreferences preferences= Instance.getInstance(context).userPreferences;
|
||||
AppDatabase database= Instance.getInstance(context).db;
|
||||
UnitUtils.setUnit(context);
|
||||
|
||||
FitoTrackDataContainer container= new FitoTrackDataContainer();
|
||||
container.version= VERSION;
|
||||
container.workouts= new ArrayList<>();
|
||||
container.samples= new ArrayList<>();
|
||||
|
||||
listener.onStatusChanged(10, context.getString(R.string.preferences));
|
||||
FitoTrackSettings settings= new FitoTrackSettings();
|
||||
settings.weight= preferences.getUserWeight();
|
||||
settings.mapStyle= preferences.getMapStyle();
|
||||
settings.preferredUnitSystem= String.valueOf(UnitUtils.CHOSEN_SYSTEM.getId());
|
||||
container.settings= settings;
|
||||
|
||||
listener.onStatusChanged(20, context.getString(R.string.workouts));
|
||||
container.workouts.addAll(Arrays.asList(database.workoutDao().getWorkouts()));
|
||||
listener.onStatusChanged(40, context.getString(R.string.locationData));
|
||||
container.samples.addAll(Arrays.asList(database.workoutDao().getSamples()));
|
||||
|
||||
listener.onStatusChanged(60, context.getString(R.string.converting));
|
||||
|
||||
XmlMapper mapper= new XmlMapper();
|
||||
mapper.writeValue(output, container);
|
||||
|
||||
listener.onStatusChanged(100, context.getString(R.string.finished));
|
||||
}
|
||||
|
||||
@SuppressLint("ApplySharedPref")
|
||||
public static void importData(Context context, Uri input, ExportStatusListener listener) throws IOException, UnsupportedVersionException {
|
||||
listener.onStatusChanged(0, context.getString(R.string.loadingFile));
|
||||
XmlMapper xmlMapper = new XmlMapper();
|
||||
xmlMapper.configure(JsonParser.Feature.IGNORE_UNDEFINED, true);
|
||||
FitoTrackDataContainer container = xmlMapper.readValue(context.getContentResolver().openInputStream(input), FitoTrackDataContainer.class);
|
||||
|
||||
if(container.version != 1){
|
||||
throw new UnsupportedVersionException("Version Code" + container.version + " is unsupported!");
|
||||
}
|
||||
|
||||
listener.onStatusChanged(40, context.getString(R.string.preferences));
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.edit().clear()
|
||||
.putInt("weight", container.settings.weight)
|
||||
.putString("unitSystem", container.settings.preferredUnitSystem)
|
||||
.putBoolean("firstStart", false).putString("mapStyle", container.settings.mapStyle)
|
||||
.commit();
|
||||
|
||||
AppDatabase database= Instance.getInstance(context).db;
|
||||
|
||||
database.runInTransaction(() -> {
|
||||
database.clearAllTables();
|
||||
|
||||
listener.onStatusChanged(60, context.getString(R.string.workouts));
|
||||
if(container.workouts != null){
|
||||
for(Workout workout : container.workouts){
|
||||
database.workoutDao().insertWorkout(workout);
|
||||
}
|
||||
}
|
||||
|
||||
listener.onStatusChanged(80, context.getString(R.string.locationData));
|
||||
if(container.samples != null){
|
||||
for(WorkoutSample sample : container.samples){
|
||||
database.workoutDao().insertSample(sample);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
listener.onStatusChanged(100, context.getString(R.string.finished));
|
||||
}
|
||||
|
||||
|
||||
public interface ExportStatusListener{
|
||||
void onStatusChanged(int progress, String action);
|
||||
}
|
||||
|
||||
public static class UnsupportedVersionException extends Exception{
|
||||
public UnsupportedVersionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user