forked from upstream-mirrors/OpenTracks
Cleanup: move instantiation of TrackImporter.
This commit is contained in:
+16
-12
@@ -90,10 +90,14 @@ public class ExportImportTest {
|
|||||||
private Track.Id trackId;
|
private Track.Id trackId;
|
||||||
private Track.Id importTrackId;
|
private Track.Id importTrackId;
|
||||||
|
|
||||||
|
private TrackImporter trackImporter;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void fileSetup() throws IOException {
|
public void fileSetup() throws IOException {
|
||||||
tmpFile = File.createTempFile("test", "test", context.getFilesDir());
|
tmpFile = File.createTempFile("test", "test", context.getFilesDir());
|
||||||
tmpFileUri = Uri.fromFile(tmpFile);
|
tmpFileUri = Uri.fromFile(tmpFile);
|
||||||
|
|
||||||
|
trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(10), Distance.of(200), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
@@ -169,8 +173,8 @@ public class ExportImportTest {
|
|||||||
|
|
||||||
// 2. import
|
// 2. import
|
||||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// 1. track
|
// 1. track
|
||||||
@@ -209,8 +213,8 @@ public class ExportImportTest {
|
|||||||
|
|
||||||
// 2. import
|
// 2. import
|
||||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// 1. track
|
// 1. track
|
||||||
@@ -248,8 +252,8 @@ public class ExportImportTest {
|
|||||||
contentProviderUtils.deleteTrack(context, trackId);
|
contentProviderUtils.deleteTrack(context, trackId);
|
||||||
|
|
||||||
// 2. import
|
// 2. import
|
||||||
KmzTrackImporter trackImporter = new KmzTrackImporter();
|
KmzTrackImporter importer = new KmzTrackImporter(context, trackImporter);
|
||||||
importTrackId = trackImporter.importFile(context, tmpFileUri).get(0);
|
importTrackId = importer.importFile(tmpFileUri).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// 1. track
|
// 1. track
|
||||||
@@ -288,8 +292,8 @@ public class ExportImportTest {
|
|||||||
|
|
||||||
// 2. import
|
// 2. import
|
||||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Track importedTrack = contentProviderUtils.getTrack(importTrackId);
|
Track importedTrack = contentProviderUtils.getTrack(importTrackId);
|
||||||
@@ -313,8 +317,8 @@ public class ExportImportTest {
|
|||||||
|
|
||||||
// 2. import
|
// 2. import
|
||||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||||
XMLImporter trackImporter = new XMLImporter(new GpxTrackImporter(context, contentProviderUtils));
|
XMLImporter importer = new XMLImporter(new GpxTrackImporter(context, trackImporter));
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// 1. track
|
// 1. track
|
||||||
@@ -362,8 +366,8 @@ public class ExportImportTest {
|
|||||||
|
|
||||||
// 2. import
|
// 2. import
|
||||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||||
XMLImporter trackImporter = new XMLImporter(new GpxTrackImporter(context, contentProviderUtils));
|
XMLImporter importer = new XMLImporter(new GpxTrackImporter(context, trackImporter));
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// 1. track
|
// 1. track
|
||||||
|
|||||||
+15
-6
@@ -7,6 +7,7 @@ import androidx.test.filters.LargeTest;
|
|||||||
import androidx.test.platform.app.InstrumentationRegistry;
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
@@ -15,6 +16,7 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.content.data.Distance;
|
||||||
import de.dennisguse.opentracks.content.data.TestDataUtil;
|
import de.dennisguse.opentracks.content.data.TestDataUtil;
|
||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||||
@@ -34,8 +36,15 @@ public class LegacyImportTest {
|
|||||||
private final Context context = ApplicationProvider.getApplicationContext();
|
private final Context context = ApplicationProvider.getApplicationContext();
|
||||||
private final ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
|
private final ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
|
||||||
|
|
||||||
|
private TrackImporter trackImporter;
|
||||||
|
|
||||||
private Track.Id importTrackId;
|
private Track.Id importTrackId;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(10), Distance.of(200), true);
|
||||||
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
if (importTrackId != null) {
|
if (importTrackId != null) {
|
||||||
@@ -51,12 +60,12 @@ public class LegacyImportTest {
|
|||||||
@Test
|
@Test
|
||||||
public void kml_with_statistics_marker() throws IOException {
|
public void kml_with_statistics_marker() throws IOException {
|
||||||
// given
|
// given
|
||||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||||
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getResources().openRawResource(de.dennisguse.opentracks.debug.test.R.raw.legacy_kml_statistics_marker);
|
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getResources().openRawResource(de.dennisguse.opentracks.debug.test.R.raw.legacy_kml_statistics_marker);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
// 1. import
|
// 1. import
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// 1. track
|
// 1. track
|
||||||
@@ -93,11 +102,11 @@ public class LegacyImportTest {
|
|||||||
@Test(expected = ImportParserException.class)
|
@Test(expected = ImportParserException.class)
|
||||||
public void kml_without_locations() throws IOException {
|
public void kml_without_locations() throws IOException {
|
||||||
// given
|
// given
|
||||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||||
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getResources().openRawResource(de.dennisguse.opentracks.debug.test.R.raw.legacy_kml_empty);
|
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getResources().openRawResource(de.dennisguse.opentracks.debug.test.R.raw.legacy_kml_empty);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,12 +116,12 @@ public class LegacyImportTest {
|
|||||||
@Test
|
@Test
|
||||||
public void gpx_with_pause_resume() throws IOException {
|
public void gpx_with_pause_resume() throws IOException {
|
||||||
// given
|
// given
|
||||||
XMLImporter trackImporter = new XMLImporter(new GpxTrackImporter(context));
|
XMLImporter importer = new XMLImporter(new GpxTrackImporter(context, trackImporter));
|
||||||
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getResources().openRawResource(de.dennisguse.opentracks.debug.test.R.raw.legacy_gpx_pause_resume);
|
InputStream inputStream = InstrumentationRegistry.getInstrumentation().getContext().getResources().openRawResource(de.dennisguse.opentracks.debug.test.R.raw.legacy_gpx_pause_resume);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
// 1. import
|
// 1. import
|
||||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
importTrackId = importer.importFile(inputStream).get(0);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
// 1. track
|
// 1. track
|
||||||
|
|||||||
@@ -17,11 +17,8 @@
|
|||||||
package de.dennisguse.opentracks.io.file.importer;
|
package de.dennisguse.opentracks.io.file.importer;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.VisibleForTesting;
|
|
||||||
|
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.Locator;
|
import org.xml.sax.Locator;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
@@ -38,8 +35,6 @@ import de.dennisguse.opentracks.content.data.Marker;
|
|||||||
import de.dennisguse.opentracks.content.data.Speed;
|
import de.dennisguse.opentracks.content.data.Speed;
|
||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
|
||||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
|
||||||
import de.dennisguse.opentracks.util.StringUtils;
|
import de.dennisguse.opentracks.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -115,20 +110,9 @@ public class GpxTrackImporter extends DefaultHandler implements XMLImporter.Trac
|
|||||||
|
|
||||||
private final TrackImporter trackImporter;
|
private final TrackImporter trackImporter;
|
||||||
|
|
||||||
public GpxTrackImporter(Context context) {
|
public GpxTrackImporter(Context context, TrackImporter trackImporter) {
|
||||||
this(context, new ContentProviderUtils(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
GpxTrackImporter(Context context, ContentProviderUtils contentProviderUtils) {
|
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
this.trackImporter = trackImporter;
|
||||||
//TODO move this to instantiation of this class
|
|
||||||
SharedPreferences sharedPreferences = PreferencesUtils.getSharedPreferences(context);
|
|
||||||
Distance maxRecordingDistance = PreferencesUtils.getMaxRecordingDistance(sharedPreferences, context);
|
|
||||||
Distance recordingDistanceInterval = PreferencesUtils.getRecordingDistanceInterval(sharedPreferences, context);
|
|
||||||
boolean preventReimport = PreferencesUtils.getPreventReimportTracks(sharedPreferences, context);
|
|
||||||
this.trackImporter = new TrackImporter(context, contentProviderUtils, recordingDistanceInterval, maxRecordingDistance, preventReimport);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package de.dennisguse.opentracks.io.file.importer;
|
|||||||
import android.app.job.JobService;
|
import android.app.job.JobService;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.ResultReceiver;
|
import android.os.ResultReceiver;
|
||||||
@@ -16,9 +17,12 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
|
import de.dennisguse.opentracks.content.data.Distance;
|
||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
|
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
||||||
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
||||||
import de.dennisguse.opentracks.util.FileUtils;
|
import de.dennisguse.opentracks.util.FileUtils;
|
||||||
|
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||||
|
|
||||||
public class ImportService extends JobIntentService {
|
public class ImportService extends JobIntentService {
|
||||||
|
|
||||||
@@ -50,13 +54,19 @@ public class ImportService extends JobIntentService {
|
|||||||
|
|
||||||
String fileExtension = FileUtils.getExtension(file);
|
String fileExtension = FileUtils.getExtension(file);
|
||||||
try {
|
try {
|
||||||
|
SharedPreferences sharedPreferences = PreferencesUtils.getSharedPreferences(this);
|
||||||
|
Distance maxRecordingDistance = PreferencesUtils.getMaxRecordingDistance(sharedPreferences, this);
|
||||||
|
Distance recordingDistanceInterval = PreferencesUtils.getRecordingDistanceInterval(sharedPreferences, this);
|
||||||
|
boolean preventReimport = PreferencesUtils.getPreventReimportTracks(sharedPreferences, this);
|
||||||
|
|
||||||
|
TrackImporter trackImporter = new TrackImporter(this, new ContentProviderUtils(this), recordingDistanceInterval, maxRecordingDistance, preventReimport);
|
||||||
|
|
||||||
if (TrackFileFormat.GPX.getExtension().equals(fileExtension)) {
|
if (TrackFileFormat.GPX.getExtension().equals(fileExtension)) {
|
||||||
trackIds.addAll(new XMLImporter(new GpxTrackImporter(this)).importFile(this, file.getUri()));
|
trackIds.addAll(new XMLImporter(new GpxTrackImporter(this, trackImporter)).importFile(this, file.getUri()));
|
||||||
} else if (TrackFileFormat.KML_WITH_TRACKDETAIL_AND_SENSORDATA.getExtension().equals(fileExtension)) {
|
} else if (TrackFileFormat.KML_WITH_TRACKDETAIL_AND_SENSORDATA.getExtension().equals(fileExtension)) {
|
||||||
trackIds.addAll(new XMLImporter(new KmlTrackImporter(this)).importFile(this, file.getUri()));
|
trackIds.addAll(new XMLImporter(new KmlTrackImporter(this, trackImporter)).importFile(this, file.getUri()));
|
||||||
} else if (TrackFileFormat.KMZ_WITH_TRACKDETAIL_AND_SENSORDATA_AND_PICTURES.getExtension().equals(fileExtension)) {
|
} else if (TrackFileFormat.KMZ_WITH_TRACKDETAIL_AND_SENSORDATA_AND_PICTURES.getExtension().equals(fileExtension)) {
|
||||||
trackIds.addAll(new KmzTrackImporter().importFile(this, file.getUri()));
|
trackIds.addAll(new KmzTrackImporter(this, trackImporter).importFile(file.getUri()));
|
||||||
} else {
|
} else {
|
||||||
Log.d(TAG, "Unsupported file format.");
|
Log.d(TAG, "Unsupported file format.");
|
||||||
sendResult(ImportServiceResultReceiver.RESULT_CODE_ERROR, null, file, getString(R.string.import_unsupported_format));
|
sendResult(ImportServiceResultReceiver.RESULT_CODE_ERROR, null, file, getString(R.string.import_unsupported_format));
|
||||||
|
|||||||
@@ -17,7 +17,6 @@
|
|||||||
package de.dennisguse.opentracks.io.file.importer;
|
package de.dennisguse.opentracks.io.file.importer;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -36,9 +35,7 @@ import de.dennisguse.opentracks.content.data.Marker;
|
|||||||
import de.dennisguse.opentracks.content.data.Speed;
|
import de.dennisguse.opentracks.content.data.Speed;
|
||||||
import de.dennisguse.opentracks.content.data.Track;
|
import de.dennisguse.opentracks.content.data.Track;
|
||||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||||
import de.dennisguse.opentracks.content.provider.ContentProviderUtils;
|
|
||||||
import de.dennisguse.opentracks.io.file.exporter.KMLTrackExporter;
|
import de.dennisguse.opentracks.io.file.exporter.KMLTrackExporter;
|
||||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
|
||||||
import de.dennisguse.opentracks.util.StringUtils;
|
import de.dennisguse.opentracks.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,15 +104,9 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
|
|||||||
|
|
||||||
private final TrackImporter trackImporter;
|
private final TrackImporter trackImporter;
|
||||||
|
|
||||||
public KmlTrackImporter(Context context) {
|
public KmlTrackImporter(Context context, TrackImporter trackImporter) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
this.trackImporter = trackImporter;
|
||||||
//TODO move this to instantiation of this class
|
|
||||||
SharedPreferences sharedPreferences = PreferencesUtils.getSharedPreferences(context);
|
|
||||||
Distance maxRecordingDistance = PreferencesUtils.getMaxRecordingDistance(sharedPreferences, context);
|
|
||||||
Distance recordingDistanceInterval = PreferencesUtils.getRecordingDistanceInterval(sharedPreferences, context);
|
|
||||||
boolean preventReimport = PreferencesUtils.getPreventReimportTracks(sharedPreferences, context);
|
|
||||||
this.trackImporter = new TrackImporter(context, new ContentProviderUtils(context), recordingDistanceInterval, maxRecordingDistance, preventReimport);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -51,11 +51,16 @@ public class KmzTrackImporter {
|
|||||||
|
|
||||||
private static final List<String> KMZ_IMAGES_EXT = Arrays.asList("jpeg", "jpg", "png");
|
private static final List<String> KMZ_IMAGES_EXT = Arrays.asList("jpeg", "jpg", "png");
|
||||||
|
|
||||||
private Context context;
|
private final Context context;
|
||||||
|
private final TrackImporter trackImporter;
|
||||||
|
|
||||||
|
public KmzTrackImporter(Context context, TrackImporter trackImporter) {
|
||||||
|
this.context = context;
|
||||||
|
this.trackImporter = trackImporter;
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public List<Track.Id> importFile(Context context, Uri fileUri) throws IOException {
|
public List<Track.Id> importFile(Uri fileUri) throws IOException {
|
||||||
this.context = context;
|
|
||||||
List<Track.Id> trackIds = findAndParseKmlFile(fileUri);
|
List<Track.Id> trackIds = findAndParseKmlFile(fileUri);
|
||||||
|
|
||||||
List<Track.Id> trackIdsWithImages = new ArrayList<>();
|
List<Track.Id> trackIdsWithImages = new ArrayList<>();
|
||||||
@@ -205,7 +210,7 @@ public class KmzTrackImporter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<Track.Id> parseKml(ZipInputStream zipInputStream) throws IOException {
|
private List<Track.Id> parseKml(ZipInputStream zipInputStream) throws IOException {
|
||||||
XMLImporter kmlFileTrackImporter = new XMLImporter(new KmlTrackImporter(context));
|
XMLImporter kmlFileTrackImporter = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||||
|
|
||||||
InputStream nonClosableInputStream = new FilterInputStream(zipInputStream) {
|
InputStream nonClosableInputStream = new FilterInputStream(zipInputStream) {
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user