code cleanup: rename Importer classes

This commit is contained in:
Jimmy Shih
2013-08-28 12:17:17 -07:00
parent 0955e9f089
commit ee7376919e
9 changed files with 80 additions and 39 deletions
@@ -54,12 +54,12 @@ import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Abstract class for various importers like {@link GpxImporter} and
* {@link KmlImporter}.
* Abstract class for various file track importers like {@link GpxFileTrackImporter} and
* {@link KmlFileTrackImporter}.
*
* @author Jimmy Shih
*/
abstract class AbstractImporter extends DefaultHandler {
abstract class AbstractFileTrackImporter extends DefaultHandler implements TrackImporter {
/**
* Data for the current track.
@@ -131,12 +131,12 @@ abstract class AbstractImporter extends DefaultHandler {
* @param importTrackId the track id to import to. -1L to import to a new
* track.
*/
protected AbstractImporter(Context context, long importTrackId) {
protected AbstractFileTrackImporter(Context context, long importTrackId) {
this(context, importTrackId, MyTracksProviderUtils.Factory.get(context));
}
@VisibleForTesting
protected AbstractImporter(
protected AbstractFileTrackImporter(
Context context, long importTrackId, MyTracksProviderUtils myTracksProviderUtils) {
this.context = context;
this.importTrackId = importTrackId;
@@ -31,7 +31,7 @@ import org.xml.sax.SAXException;
*
* @author Jimmy Shih
*/
public class GpxImporter extends AbstractImporter {
public class GpxFileTrackImporter extends AbstractFileTrackImporter {
private static final String TAG_DESCRIPTION = "desc";
private static final String TAG_COMMENT = "cmt";
@@ -54,12 +54,12 @@ public class GpxImporter extends AbstractImporter {
* @param context the context
* @param importTrackId track id to import to. -1L to import to a new track.
*/
public GpxImporter(Context context, long importTrackId) {
public GpxFileTrackImporter(Context context, long importTrackId) {
super(context, importTrackId);
}
@VisibleForTesting
public GpxImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
public GpxFileTrackImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
super(context, -1L, myTracksProviderUtils);
}
@@ -158,10 +158,10 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
*/
private boolean importFile(final File file) {
try {
AbstractImporter importer = trackFileFormat == TrackFileFormat.KML ? new KmlImporter(
TrackImporter trackImporter = trackFileFormat == TrackFileFormat.KML ? new KmlFileTrackImporter(
context, -1L)
: new GpxImporter(context, -1L);
long trackIds[] = importer.importFile(new FileInputStream(file));
: new GpxFileTrackImporter(context, -1L);
long trackIds[] = trackImporter.importFile(new FileInputStream(file));
int length = trackIds.length;
if (length > 0) {
trackId = trackIds[length - 1];
@@ -36,7 +36,7 @@ import org.xml.sax.SAXException;
*
* @author Jimmy Shih
*/
public class KmlImporter extends AbstractImporter {
public class KmlFileTrackImporter extends AbstractFileTrackImporter {
private static final String CADENCE = "cadence";
private static final String HEART_RATE = "heart_rate";
@@ -74,12 +74,12 @@ public class KmlImporter extends AbstractImporter {
* @param context the context
* @param importTrackId track id to import to. -1L to import to a new track.
*/
public KmlImporter(Context context, long importTrackId) {
public KmlFileTrackImporter(Context context, long importTrackId) {
super(context, importTrackId);
}
@VisibleForTesting
public KmlImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
public KmlFileTrackImporter(Context context, MyTracksProviderUtils myTracksProviderUtils) {
super(context, -1L, myTracksProviderUtils);
}
@@ -0,0 +1,34 @@
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.io.file;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
* Interface for a track importer.
*
* @author Jimmy Shih
*/
public interface TrackImporter {
public long[] importFile(InputStream inputStream)
throws SAXException, IOException, ParserConfigurationException;
}
@@ -18,7 +18,7 @@ package com.google.android.apps.mytracks.io.sync;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.io.file.KmlImporter;
import com.google.android.apps.mytracks.io.file.KmlFileTrackImporter;
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.gms.auth.GoogleAuthException;
@@ -348,9 +348,9 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
if (driveFile != null) {
InputStream inputStream = downloadDriveFile(driveFile, true);
if (inputStream != null) {
KmlImporter kmlImporter = new KmlImporter(context, -1L);
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L);
try {
long[] trackIds = kmlImporter.importFile(inputStream);
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
if (trackIds.length == 1) {
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
if (track == null) {
@@ -494,9 +494,9 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName());
return false;
}
KmlImporter kmlImporter = new KmlImporter(context, track.getId());
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, track.getId());
try {
long[] trackIds = kmlImporter.importFile(inputStream);
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
if (trackIds.length == 1) {
Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]);
if (newTrack == null) {
@@ -42,11 +42,11 @@ import java.util.SimpleTimeZone;
import org.easymock.Capture;
/**
* Abstract class for testing importer.
* Abstract class for testing file track importers.
*
* @author Jimmy Shih.
*/
public class AbstractTestImporter extends AndroidTestCase {
public class AbstractTestFileTrackImporter extends AndroidTestCase {
protected static final String TRACK_NAME_0 = "blablub";
protected static final String TRACK_DESCRIPTION_0 = "s'Laebe isch koi Schlotzer";
@@ -31,11 +31,11 @@ import org.easymock.Capture;
import org.xml.sax.SAXException;
/**
* Tests for {@link GpxImporter}.
* Tests for {@link GpxFileTrackImporter}.
*
* @author Steffen Horlacher
*/
public class GpxImporterTest extends AbstractTestImporter {
public class GpxFileTrackImporterTest extends AbstractTestFileTrackImporter {
private static String getNameAndDescription(String name, String description) {
return "<name><![CDATA[" + name + "]]></name>" + "<desc><![CDATA[" + description + "]]></desc>";
@@ -107,8 +107,9 @@ public class GpxImporterTest extends AbstractTestImporter {
AndroidMock.replay(myTracksProviderUtils);
InputStream inputStream = new ByteArrayInputStream(VALID_ONE_TRACK_ONE_SEGMENT_GPX.getBytes());
GpxImporter gpxImporter = new GpxImporter(getContext(), myTracksProviderUtils);
long[] trackIds = gpxImporter.importFile(inputStream);
GpxFileTrackImporter gpxFileTrackImporter = new GpxFileTrackImporter(
getContext(), myTracksProviderUtils);
long[] trackIds = gpxFileTrackImporter.importFile(inputStream);
assertEquals(1, trackIds.length);
assertEquals(TRACK_ID_0, trackIds[0]);
@@ -140,8 +141,9 @@ public class GpxImporterTest extends AbstractTestImporter {
AndroidMock.replay(myTracksProviderUtils);
InputStream inputStream = new ByteArrayInputStream(VALID_ONE_TRACK_TWO_SEGMENTS_GPX.getBytes());
GpxImporter gpxImporter = new GpxImporter(getContext(), myTracksProviderUtils);
long[] trackIds = gpxImporter.importFile(inputStream);
GpxFileTrackImporter gpxFileTrackImporter = new GpxFileTrackImporter(
getContext(), myTracksProviderUtils);
long[] trackIds = gpxFileTrackImporter.importFile(inputStream);
assertEquals(1, trackIds.length);
assertEquals(TRACK_ID_0, trackIds[0]);
@@ -177,8 +179,9 @@ public class GpxImporterTest extends AbstractTestImporter {
InputStream inputStream = new ByteArrayInputStream(
VALID_ONE_TRACK_TWO_SEGMENTS_NO_TIME_GPX.getBytes());
GpxImporter gpxImporter = new GpxImporter(getContext(), myTracksProviderUtils);
long[] trackIds = gpxImporter.importFile(inputStream);
GpxFileTrackImporter gpxFileTrackImporter = new GpxFileTrackImporter(
getContext(), myTracksProviderUtils);
long[] trackIds = gpxFileTrackImporter.importFile(inputStream);
assertEquals(1, trackIds.length);
assertEquals(TRACK_ID_0, trackIds[0]);
@@ -212,8 +215,9 @@ public class GpxImporterTest extends AbstractTestImporter {
AndroidMock.replay(myTracksProviderUtils);
InputStream inputStream = new ByteArrayInputStream(VALID_TWO_TRACKS_GPX.getBytes());
GpxImporter gpxImporter = new GpxImporter(getContext(), myTracksProviderUtils);
long[] trackIds = gpxImporter.importFile(inputStream);
GpxFileTrackImporter gpxFileTrackImporter = new GpxFileTrackImporter(
getContext(), myTracksProviderUtils);
long[] trackIds = gpxFileTrackImporter.importFile(inputStream);
assertEquals(2, trackIds.length);
assertEquals(TRACK_ID_0, trackIds[0]);
assertEquals(TRACK_ID_1, trackIds[1]);
@@ -279,8 +283,9 @@ public class GpxImporterTest extends AbstractTestImporter {
InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
try {
GpxImporter gpxImporter = new GpxImporter(getContext(), myTracksProviderUtils);
gpxImporter.importFile(inputStream);
GpxFileTrackImporter gpxFileTrackImporter = new GpxFileTrackImporter(
getContext(), myTracksProviderUtils);
gpxFileTrackImporter.importFile(inputStream);
} catch (SAXException e) {
// expected
}
@@ -30,11 +30,11 @@ import java.io.InputStream;
import org.easymock.Capture;
/**
* Tests for {@link KmlImporter}.
* Tests for {@link KmlFileTrackImporter}.
*
* @author Jimmy Shih
*/
public class KmlImporterTest extends AbstractTestImporter {
public class KmlFileTrackImporterTest extends AbstractTestFileTrackImporter {
private static String getNameAndDescription(String name, String description) {
return "<name><![CDATA[" + name + "]]></name><description><![CDATA[" + description
@@ -85,8 +85,9 @@ public class KmlImporterTest extends AbstractTestImporter {
AndroidMock.replay(myTracksProviderUtils);
InputStream inputStream = new ByteArrayInputStream(VALID_ONE_TRACK_ONE_SEGMENT_GPX.getBytes());
KmlImporter kmlImporter = new KmlImporter(getContext(), myTracksProviderUtils);
long[] trackIds = kmlImporter.importFile(inputStream);
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
getContext(), myTracksProviderUtils);
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
assertEquals(1, trackIds.length);
assertEquals(TRACK_ID_0, trackIds[0]);
@@ -118,8 +119,9 @@ public class KmlImporterTest extends AbstractTestImporter {
AndroidMock.replay(myTracksProviderUtils);
InputStream inputStream = new ByteArrayInputStream(VALID_ONE_TRACK_TWO_SEGMENTS_GPX.getBytes());
KmlImporter kmlImporter = new KmlImporter(getContext(), myTracksProviderUtils);
long[] trackIds = kmlImporter.importFile(inputStream);
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
getContext(), myTracksProviderUtils);
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
assertEquals(1, trackIds.length);
assertEquals(TRACK_ID_0, trackIds[0]);