Merge remote-tracking branch 'origin/develop' into develop

This commit is contained in:
jannis 2020-01-17 20:58:18 +01:00
commit de95affa50
14 changed files with 135 additions and 164 deletions

View File

@ -82,6 +82,9 @@ dependencies {
implementation 'stax:stax-api:1.0.1'
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.8'
// File Utils
implementation 'commons-io:commons-io:2.6'
// Upload to OSM
implementation('de.westnordost:osmapi-traces:1.0')
configurations {

View File

@ -27,6 +27,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application

View File

@ -19,17 +19,14 @@
package de.tadris.fitness.activity;
import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.content.pm.PackageManager;
import android.util.TypedValue;
import androidx.annotation.StringRes;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import androidx.core.app.ActivityCompat;
import de.tadris.fitness.R;
@ -42,23 +39,6 @@ abstract public class FitoTrackActivity extends Activity {
return value.data;
}
void shareFile(Uri uri) {
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setDataAndType(uri, getContentResolver().getType(uri));
intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intentShareFile, getString(R.string.shareFile)));
Log.d("Export", uri.toString());
Log.d("Export", getContentResolver().getType(uri));
try {
Log.d("Export", new BufferedInputStream(getContentResolver().openInputStream(uri)).toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void showErrorDialog(Exception e, @StringRes int title, @StringRes int message) {
new AlertDialog.Builder(this)
.setTitle(title)
@ -67,5 +47,16 @@ abstract public class FitoTrackActivity extends Activity {
.create().show();
}
protected void requestStoragePermissions() {
if (!hasStoragePermission()) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 10);
}
}
protected boolean hasStoragePermission() {
return ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
}

View File

@ -20,7 +20,6 @@
package de.tadris.fitness.activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
@ -30,36 +29,15 @@ import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import androidx.annotation.StringRes;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import de.tadris.fitness.R;
import de.tadris.fitness.util.unit.UnitUtils;
public abstract class FitoTrackSettingsActivity extends PreferenceActivity {
protected void shareFile(Uri uri) {
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setDataAndType(uri, getContentResolver().getType(uri));
intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intentShareFile, getString(R.string.shareFile)));
Log.d("Export", uri.toString());
Log.d("Export", getContentResolver().getType(uri));
try {
Log.d("Export", new BufferedInputStream(getContentResolver().openInputStream(uri)).toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
protected void showErrorDialog(Exception e, @StringRes int title, @StringRes int message) {
new AlertDialog.Builder(this)
.setTitle(title)

View File

@ -43,6 +43,7 @@ import de.tadris.fitness.R;
import de.tadris.fitness.announcement.VoiceAnnouncements;
import de.tadris.fitness.export.BackupController;
import de.tadris.fitness.export.RestoreController;
import de.tadris.fitness.util.FileUtils;
import de.tadris.fitness.util.unit.UnitUtils;
import de.tadris.fitness.view.ProgressDialogController;
@ -102,6 +103,10 @@ public class SettingsActivity extends FitoTrackSettingsActivity {
}
private void showExportDialog() {
if (!hasPermission()) {
requestPermissions();
return;
}
new AlertDialog.Builder(this)
.setTitle(R.string.exportData)
.setMessage(R.string.exportDataSummary)
@ -115,7 +120,8 @@ public class SettingsActivity extends FitoTrackSettingsActivity {
new Thread(() -> {
try{
String file= getFilesDir().getAbsolutePath() + "/shared/backup.ftb";
if (!new File(file).getParentFile().mkdirs()) {
File parent = new File(file).getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IOException("Cannot write");
}
Uri uri= FileProvider.getUriForFile(getBaseContext(), "de.tadris.fitness.fileprovider", new File(file));
@ -125,7 +131,7 @@ public class SettingsActivity extends FitoTrackSettingsActivity {
mHandler.post(() -> {
dialogController.cancel();
shareFile(uri);
FileUtils.saveOrShareFile(this, uri, "ftb");
});
}catch (Exception e){
e.printStackTrace();

View File

@ -48,6 +48,7 @@ import de.tadris.fitness.data.WorkoutSample;
import de.tadris.fitness.osm.OAuthAuthentication;
import de.tadris.fitness.osm.OsmTraceUploader;
import de.tadris.fitness.util.DialogUtils;
import de.tadris.fitness.util.FileUtils;
import de.tadris.fitness.util.gpx.GpxExporter;
import de.tadris.fitness.util.unit.UnitUtils;
import de.tadris.fitness.view.ProgressDialogController;
@ -202,20 +203,25 @@ public class ShowWorkoutActivity extends WorkoutActivity implements DialogUtils.
}
private void exportToGpx(){
if (!hasStoragePermission()) {
requestStoragePermissions();
return;
}
ProgressDialogController dialogController= new ProgressDialogController(this, getString(R.string.exporting));
dialogController.setIndeterminate(true);
dialogController.show();
new Thread(() -> {
try{
String file= getFilesDir().getAbsolutePath() + "/shared/workout.gpx";
if (!new File(file).getParentFile().mkdirs()) {
File parent = new File(file).getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IOException("Cannot write to " + file);
}
Uri uri= FileProvider.getUriForFile(getBaseContext(), "de.tadris.fitness.fileprovider", new File(file));
GpxExporter.exportWorkout(getBaseContext(), workout, new File(file));
dialogController.cancel();
mHandler.post(() -> shareFile(uri));
mHandler.post(() -> FileUtils.saveOrShareFile(this, uri, "gpx"));
}catch (Exception e){
e.printStackTrace();
mHandler.post(() -> showErrorDialog(e, R.string.error, R.string.errorGpxExportFailed));

View File

@ -31,7 +31,6 @@ 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 {
@ -41,7 +40,6 @@ public class BackupController {
private final Context context;
private final File output;
private final ExportStatusListener listener;
private UserPreferences preferences;
private AppDatabase database;
private FitoTrackDataContainer dataContainer;
@ -55,10 +53,6 @@ public class BackupController {
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));
@ -69,32 +63,24 @@ public class BackupController {
}
private void init(){
preferences= Instance.getInstance(context).userPreferences;
database= Instance.getInstance(context).db;
UnitUtils.setUnit(context); // Ensure unit system is correct
newContainer();
}
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;
dataContainer.setVersion(VERSION);
dataContainer.setWorkouts(new ArrayList<>());
dataContainer.setSamples(new ArrayList<>());
}
private void saveWorkoutsToContainer(){
dataContainer.workouts.addAll(Arrays.asList(database.workoutDao().getWorkouts()));
dataContainer.getWorkouts().addAll(Arrays.asList(database.workoutDao().getWorkouts()));
}
private void saveSamplesToContainer(){
dataContainer.samples.addAll(Arrays.asList(database.workoutDao().getSamples()));
dataContainer.getSamples().addAll(Arrays.asList(database.workoutDao().getSamples()));
}
private void writeContainerToOutputFile() throws IOException {

View File

@ -31,18 +31,16 @@ import de.tadris.fitness.data.WorkoutSample;
@JsonIgnoreProperties(ignoreUnknown = true)
class FitoTrackDataContainer {
int version;
List<Workout> workouts;
List<WorkoutSample> samples;
FitoTrackSettings settings;
private int version;
private List<Workout> workouts;
private List<WorkoutSample> samples;
public FitoTrackDataContainer(){}
public FitoTrackDataContainer(int version, List<Workout> workouts, List<WorkoutSample> samples, FitoTrackSettings settings) {
public FitoTrackDataContainer(int version, List<Workout> workouts, List<WorkoutSample> samples) {
this.version = version;
this.workouts = workouts;
this.samples = samples;
this.settings = settings;
}
public int getVersion() {
@ -69,11 +67,5 @@ class FitoTrackDataContainer {
this.samples = samples;
}
public FitoTrackSettings getSettings() {
return settings;
}
public void setSettings(FitoTrackSettings settings) {
this.settings = settings;
}
}

View File

@ -1,59 +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.export;
public class FitoTrackSettings {
String preferredUnitSystem;
int weight;
String mapStyle;
public FitoTrackSettings(){}
public FitoTrackSettings(String preferredUnitSystem, int weight, String mapStyle) {
this.preferredUnitSystem = preferredUnitSystem;
this.weight = weight;
this.mapStyle = mapStyle;
}
public String getPreferredUnitSystem() {
return preferredUnitSystem;
}
public void setPreferredUnitSystem(String preferredUnitSystem) {
this.preferredUnitSystem = preferredUnitSystem;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getMapStyle() {
return mapStyle;
}
public void setMapStyle(String mapStyle) {
this.mapStyle = mapStyle;
}
}

View File

@ -19,10 +19,8 @@
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;
@ -54,9 +52,6 @@ public class RestoreController {
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));
}
@ -68,21 +63,11 @@ public class RestoreController {
}
private void checkVersion() throws UnsupportedVersionException {
if(dataContainer.version != 1){
throw new UnsupportedVersionException("Version Code" + dataContainer.version + " is unsupported!");
if (dataContainer.getVersion() != 1) {
throw new UnsupportedVersionException("Version Code" + dataContainer.getVersion() + " 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();
@ -97,8 +82,8 @@ public class RestoreController {
private void restoreWorkouts(){
listener.onStatusChanged(60, context.getString(R.string.workouts));
if(dataContainer.workouts != null){
for(Workout workout : dataContainer.workouts){
if (dataContainer.getWorkouts() != null) {
for (Workout workout : dataContainer.getWorkouts()) {
database.workoutDao().insertWorkout(workout);
}
}
@ -106,8 +91,8 @@ public class RestoreController {
private void restoreSamples(){
listener.onStatusChanged(80, context.getString(R.string.locationData));
if(dataContainer.samples != null){
for(WorkoutSample sample : dataContainer.samples){
if (dataContainer.getSamples() != null) {
for (WorkoutSample sample : dataContainer.getSamples()) {
database.workoutDao().insertSample(sample);
}
}

View File

@ -0,0 +1,78 @@
package de.tadris.fitness.util;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import org.apache.commons.io.IOUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import de.tadris.fitness.R;
public class FileUtils {
public static void saveOrShareFile(Activity activity, Uri uri, String suffix) {
String[] colors = {activity.getString(R.string.share), activity.getString(R.string.save)};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setItems(colors, (dialog, which) -> {
if (which == 0) {
shareFile(activity, uri);
} else {
try {
saveFile(activity, uri, suffix);
Toast.makeText(activity, R.string.savedToDownloads, Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(activity, R.string.savingFailed, Toast.LENGTH_LONG).show();
}
}
});
builder.show();
}
private static void saveFile(Activity activity, Uri fileUri, String suffix) throws IOException {
File target = new File(Environment.getExternalStorageDirectory(), "Download/fitotrack" + System.currentTimeMillis() + "." + suffix);
if (!target.createNewFile()) {
throw new IOException("Cannot write to file " + target);
}
copyFile(activity, fileUri, Uri.fromFile(target));
}
private static void copyFile(Activity activity, Uri sourceUri, Uri targetUri) throws IOException {
InputStream input = activity.getContentResolver().openInputStream(sourceUri);
if (input == null) {
throw new IOException("Source file not found");
}
OutputStream output = activity.getContentResolver().openOutputStream(targetUri);
IOUtils.copy(input, output);
}
private static void shareFile(Activity activity, Uri uri) {
Intent intentShareFile = new Intent(Intent.ACTION_SEND);
intentShareFile.setDataAndType(uri, activity.getContentResolver().getType(uri));
intentShareFile.putExtra(Intent.EXTRA_STREAM, uri);
intentShareFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(Intent.createChooser(intentShareFile, activity.getString(R.string.shareFile)));
Log.d("Export", uri.toString());
Log.d("Export", activity.getContentResolver().getType(uri));
try {
Log.d("Export", new BufferedInputStream(activity.getContentResolver().openInputStream(uri)).toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

View File

@ -35,7 +35,7 @@
<string name="errorImportFailed">Der Datenimport ist fehlgeschlagen.</string>
<string name="exportAsGpxFile">Als GPX-Datei exportieren</string>
<string name="exportData">Daten exportieren</string>
<string name="exportDataSummary">Erstellt ein Backup von all deinen Einstellungen und deiner Workoutdaten</string>
<string name="exportDataSummary">Erstellt ein Backup von all deinern Workoutdaten</string>
<string name="exporting">Exportieren</string>
<string name="finished">Fertig</string>
<string name="gps">GPS</string>

View File

@ -115,7 +115,6 @@
<string name="pref_unit_system">Sistema preferido de unidades</string>
<string name="settings">Ajustes</string>
<string name="exportData">Datos de exportación</string>
<string name="exportDataSummary">Esto toma una copia de seguridad de todas sus preferencias y datos de entrenamiento</string>
<string name="importBackup">Importar copia de seguridad</string>
<string name="importBackupSummary">Restaurar una copia de seguridad</string>
<string name="gps">GPS</string>

View File

@ -136,7 +136,7 @@
<string name="pref_unit_system">Preferred system of units</string>
<string name="settings">Settings</string>
<string name="exportData">Export Data</string>
<string name="exportDataSummary">This takes a backup of all your preferences and workout data</string>
<string name="exportDataSummary">This takes a backup of all your workout data</string>
<string name="importBackup">Import Data Backup</string>
<string name="importBackupSummary">Restore a taken backup</string>
<string name="gps">GPS</string>
@ -150,4 +150,9 @@
<string name="ttsNotAvailable">TextToSpeech is not available</string>
<string name="action_edit_comment">Edit Comment</string>
<string name="pref_announcement_mode">Announcement Mode</string>
<string name="save">Save</string>
<string name="share">Share</string>
<string name="savedToDownloads">Saved to Downloads</string>
<string name="savingFailed">Saving failed</string>
</resources>