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 importTrackId;
|
||||
|
||||
private TrackImporter trackImporter;
|
||||
|
||||
@Before
|
||||
public void fileSetup() throws IOException {
|
||||
tmpFile = File.createTempFile("test", "test", context.getFilesDir());
|
||||
tmpFileUri = Uri.fromFile(tmpFile);
|
||||
|
||||
trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(10), Distance.of(200), true);
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -169,8 +173,8 @@ public class ExportImportTest {
|
||||
|
||||
// 2. import
|
||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
|
||||
// then
|
||||
// 1. track
|
||||
@@ -209,8 +213,8 @@ public class ExportImportTest {
|
||||
|
||||
// 2. import
|
||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
|
||||
// then
|
||||
// 1. track
|
||||
@@ -248,8 +252,8 @@ public class ExportImportTest {
|
||||
contentProviderUtils.deleteTrack(context, trackId);
|
||||
|
||||
// 2. import
|
||||
KmzTrackImporter trackImporter = new KmzTrackImporter();
|
||||
importTrackId = trackImporter.importFile(context, tmpFileUri).get(0);
|
||||
KmzTrackImporter importer = new KmzTrackImporter(context, trackImporter);
|
||||
importTrackId = importer.importFile(tmpFileUri).get(0);
|
||||
|
||||
// then
|
||||
// 1. track
|
||||
@@ -288,8 +292,8 @@ public class ExportImportTest {
|
||||
|
||||
// 2. import
|
||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||
XMLImporter trackImporter = new XMLImporter(new KmlTrackImporter(context));
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
XMLImporter importer = new XMLImporter(new KmlTrackImporter(context, trackImporter));
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
|
||||
// then
|
||||
Track importedTrack = contentProviderUtils.getTrack(importTrackId);
|
||||
@@ -313,8 +317,8 @@ public class ExportImportTest {
|
||||
|
||||
// 2. import
|
||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||
XMLImporter trackImporter = new XMLImporter(new GpxTrackImporter(context, contentProviderUtils));
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
XMLImporter importer = new XMLImporter(new GpxTrackImporter(context, trackImporter));
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
|
||||
// then
|
||||
// 1. track
|
||||
@@ -362,8 +366,8 @@ public class ExportImportTest {
|
||||
|
||||
// 2. import
|
||||
InputStream inputStream = context.getContentResolver().openInputStream(tmpFileUri);
|
||||
XMLImporter trackImporter = new XMLImporter(new GpxTrackImporter(context, contentProviderUtils));
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
XMLImporter importer = new XMLImporter(new GpxTrackImporter(context, trackImporter));
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
|
||||
// then
|
||||
// 1. track
|
||||
|
||||
+15
-6
@@ -7,6 +7,7 @@ import androidx.test.filters.LargeTest;
|
||||
import androidx.test.platform.app.InstrumentationRegistry;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
@@ -15,6 +16,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import de.dennisguse.opentracks.content.data.Distance;
|
||||
import de.dennisguse.opentracks.content.data.TestDataUtil;
|
||||
import de.dennisguse.opentracks.content.data.Track;
|
||||
import de.dennisguse.opentracks.content.data.TrackPoint;
|
||||
@@ -34,8 +36,15 @@ public class LegacyImportTest {
|
||||
private final Context context = ApplicationProvider.getApplicationContext();
|
||||
private final ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
|
||||
|
||||
private TrackImporter trackImporter;
|
||||
|
||||
private Track.Id importTrackId;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
trackImporter = new TrackImporter(context, contentProviderUtils, Distance.of(10), Distance.of(200), true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (importTrackId != null) {
|
||||
@@ -51,12 +60,12 @@ public class LegacyImportTest {
|
||||
@Test
|
||||
public void kml_with_statistics_marker() throws IOException {
|
||||
// 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);
|
||||
|
||||
// when
|
||||
// 1. import
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
|
||||
// then
|
||||
// 1. track
|
||||
@@ -93,11 +102,11 @@ public class LegacyImportTest {
|
||||
@Test(expected = ImportParserException.class)
|
||||
public void kml_without_locations() throws IOException {
|
||||
// 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);
|
||||
|
||||
// when
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,12 +116,12 @@ public class LegacyImportTest {
|
||||
@Test
|
||||
public void gpx_with_pause_resume() throws IOException {
|
||||
// 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);
|
||||
|
||||
// when
|
||||
// 1. import
|
||||
importTrackId = trackImporter.importFile(inputStream).get(0);
|
||||
importTrackId = importer.importFile(inputStream).get(0);
|
||||
|
||||
// then
|
||||
// 1. track
|
||||
|
||||
@@ -17,11 +17,8 @@
|
||||
package de.dennisguse.opentracks.io.file.importer;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.Locator;
|
||||
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.Track;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -115,20 +110,9 @@ public class GpxTrackImporter extends DefaultHandler implements XMLImporter.Trac
|
||||
|
||||
private final TrackImporter trackImporter;
|
||||
|
||||
public GpxTrackImporter(Context context) {
|
||||
this(context, new ContentProviderUtils(context));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
GpxTrackImporter(Context context, ContentProviderUtils contentProviderUtils) {
|
||||
public GpxTrackImporter(Context context, TrackImporter trackImporter) {
|
||||
this.context = context;
|
||||
|
||||
//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);
|
||||
this.trackImporter = trackImporter;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.dennisguse.opentracks.io.file.importer;
|
||||
import android.app.job.JobService;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.ResultReceiver;
|
||||
@@ -16,9 +17,12 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.content.data.Distance;
|
||||
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.util.FileUtils;
|
||||
import de.dennisguse.opentracks.util.PreferencesUtils;
|
||||
|
||||
public class ImportService extends JobIntentService {
|
||||
|
||||
@@ -50,13 +54,19 @@ public class ImportService extends JobIntentService {
|
||||
|
||||
String fileExtension = FileUtils.getExtension(file);
|
||||
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)) {
|
||||
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)) {
|
||||
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)) {
|
||||
trackIds.addAll(new KmzTrackImporter().importFile(this, file.getUri()));
|
||||
trackIds.addAll(new KmzTrackImporter(this, trackImporter).importFile(file.getUri()));
|
||||
} else {
|
||||
Log.d(TAG, "Unsupported file 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;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Location;
|
||||
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.Track;
|
||||
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.util.PreferencesUtils;
|
||||
import de.dennisguse.opentracks.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -107,15 +104,9 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
|
||||
|
||||
private final TrackImporter trackImporter;
|
||||
|
||||
public KmlTrackImporter(Context context) {
|
||||
public KmlTrackImporter(Context context, TrackImporter trackImporter) {
|
||||
this.context = context;
|
||||
|
||||
//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);
|
||||
this.trackImporter = trackImporter;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -51,11 +51,16 @@ public class KmzTrackImporter {
|
||||
|
||||
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
|
||||
public List<Track.Id> importFile(Context context, Uri fileUri) throws IOException {
|
||||
this.context = context;
|
||||
public List<Track.Id> importFile(Uri fileUri) throws IOException {
|
||||
List<Track.Id> trackIds = findAndParseKmlFile(fileUri);
|
||||
|
||||
List<Track.Id> trackIdsWithImages = new ArrayList<>();
|
||||
@@ -205,7 +210,7 @@ public class KmzTrackImporter {
|
||||
}
|
||||
|
||||
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) {
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user