diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/AbstractTrackExporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/AbstractTrackExporter.java deleted file mode 100644 index 6cd9692c7..000000000 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/AbstractTrackExporter.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.export; - -import android.util.Log; - -import java.io.IOException; -import java.io.OutputStream; - -/** - * Abstract class for {@link TrackExporter}. - * - * @author Jimmy Shih - */ -abstract class AbstractTrackExporter implements TrackExporter { - - private static final String TAG = AbstractTrackExporter.class.getSimpleName(); - - private Thread writeThread; - private boolean success = false; - - @Override - public void writeTrack(final OutputStream outputStream) { - writeThread = new Thread() { - @Override - public void run() { - try { - performWrite(outputStream); - success = true; - } catch (InterruptedException e) { - Log.e(TAG, "Unable to perform write", e); - success = false; - } catch (IOException e) { - Log.e(TAG, "Unable to perform write", e); - success = false; - } - } - }; - writeThread.start(); - try { - writeThread.join(); - } catch (InterruptedException e) { - Log.e(TAG, "Interrupted while waiting for write thread to finish", e); - success = false; - } - } - - @Override - public boolean isSuccess() { - return success; - } - - abstract void performWrite(OutputStream outputStream) throws InterruptedException, IOException; -} diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporter.java index 7320b3706..97bd57a0c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporter.java @@ -29,6 +29,7 @@ import com.google.common.annotations.VisibleForTesting; import android.content.Context; import android.database.Cursor; import android.location.Location; +import android.util.Log; import java.io.OutputStream; @@ -38,7 +39,9 @@ import java.io.OutputStream; * @author Sandor Dornbush * @author Rodrigo Damazio */ -public class FileTrackExporter extends AbstractTrackExporter { +public class FileTrackExporter implements TrackExporter { + + private static final String TAG = FileTrackExporter.class.getSimpleName(); private final MyTracksProviderUtils myTracksProviderUtils; private final Track[] tracks; @@ -71,17 +74,23 @@ public class FileTrackExporter extends AbstractTrackExporter { } @Override - void performWrite(OutputStream outputStream) throws InterruptedException { - trackWriter.prepare(outputStream); - trackWriter.writeHeader(tracks[0]); - long startTime = tracks[0].getTripStatistics().getStartTime(); - for (int i = 0; i < tracks.length; i++) { - writeWaypoints(tracks[i]); - long offset = tracks[i].getTripStatistics().getStartTime() - startTime; - writeLocations(tracks[i], offset); + public boolean writeTrack(OutputStream outputStream) { + try { + trackWriter.prepare(outputStream); + trackWriter.writeHeader(tracks[0]); + long startTime = tracks[0].getTripStatistics().getStartTime(); + for (int i = 0; i < tracks.length; i++) { + writeWaypoints(tracks[i]); + long offset = tracks[i].getTripStatistics().getStartTime() - startTime; + writeLocations(tracks[i], offset); + } + trackWriter.writeFooter(); + trackWriter.close(); + return true; + } catch (InterruptedException e) { + Log.e(TAG, "Thread interrupted", e); + return false; } - trackWriter.writeFooter(); - trackWriter.close(); } /** diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/KmzTrackExporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/KmzTrackExporter.java index e92591462..751e7b8d4 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/KmzTrackExporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/KmzTrackExporter.java @@ -22,6 +22,7 @@ import com.google.android.apps.mytracks.content.Waypoint; import android.database.Cursor; import android.net.Uri; +import android.util.Log; import java.io.File; import java.io.FileInputStream; @@ -35,11 +36,13 @@ import java.util.zip.ZipOutputStream; * * @author Jimmy Shih */ -public class KmzTrackExporter extends AbstractTrackExporter { - +public class KmzTrackExporter implements TrackExporter { + public static final String KMZ_EXTENSION = "kmz"; public static final String KMZ_IMAGES_DIR = "images"; public static final String KMZ_KML_FILE = "doc.kml"; + + private static final String TAG = KmzTrackExporter.class.getSimpleName(); private static final int BUFFER_SIZE = 4096; private final MyTracksProviderUtils myTracksProviderUtils; @@ -61,12 +64,7 @@ public class KmzTrackExporter extends AbstractTrackExporter { } @Override - public boolean isSuccess() { - return fileTrackExporter.isSuccess() && super.isSuccess(); - } - - @Override - void performWrite(OutputStream outputStream) throws InterruptedException, IOException { + public boolean writeTrack(OutputStream outputStream) { ZipOutputStream zipOutputStream = null; try { zipOutputStream = new ZipOutputStream(outputStream); @@ -75,17 +73,29 @@ public class KmzTrackExporter extends AbstractTrackExporter { ZipEntry zipEntry = new ZipEntry(KMZ_KML_FILE); zipOutputStream.putNextEntry(zipEntry); - fileTrackExporter.writeTrack(zipOutputStream); + boolean success = fileTrackExporter.writeTrack(zipOutputStream); zipOutputStream.closeEntry(); - if (!fileTrackExporter.isSuccess()) { - throw new IOException(); + if (!success) { + Log.e(TAG, "Unable to write kml in kmz"); + return false; } // Add photos addImages(zipOutputStream); + return true; + } catch (InterruptedException e) { + Log.e(TAG, "Unable to write track", e); + return false; + } catch (IOException e) { + Log.e(TAG, "Unable to write track", e); + return false; } finally { if (zipOutputStream != null) { - zipOutputStream.close(); + try { + zipOutputStream.close(); + } catch (IOException e) { + Log.e(TAG, "Unable to close zip input stream", e);; + } } } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/SaveAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/SaveAsyncTask.java index 708b18ac2..e455937b7 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/SaveAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/SaveAsyncTask.java @@ -215,15 +215,14 @@ public class SaveAsyncTask extends AsyncTask { FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); - trackExporter.writeTrack(fileOutputStream); - - if (trackExporter.isSuccess()) { + if (trackExporter.writeTrack(fileOutputStream)) { savedPath = file.getAbsolutePath(); return true; } else { if (!file.delete()) { - Log.w(TAG, "Failed to delete file " + file.getAbsolutePath()); + Log.d(TAG, "Unable to delete file"); } + Log.e(TAG, "Unable to export track"); return false; } } catch (FileNotFoundException e) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/TrackExporter.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/TrackExporter.java index 6b6c6156f..e84a742d9 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/export/TrackExporter.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/export/TrackExporter.java @@ -24,17 +24,11 @@ import java.io.OutputStream; * @author Jimmy Shih */ public interface TrackExporter { - + /** * Write track to an output stream * * @param outputStream the output stream */ - void writeTrack(OutputStream outputStream); - - /** - * Returns true if {@link #writeTrack(OutputStream)} completed successfully. - */ - boolean isSuccess(); - + boolean writeTrack(OutputStream outputStream); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java index d7dd5e82f..ab62a0162 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sync/SyncUtils.java @@ -447,8 +447,7 @@ public class SyncUtils { FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); - fileTrackExporter.writeTrack(fileOutputStream); - if (fileTrackExporter.isSuccess()) { + if (fileTrackExporter.writeTrack(fileOutputStream)) { return file; } else { if (!file.delete()) { diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/GpxFileTrackImporterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/GpxFileTrackImporterTest.java index e5b50c1f2..1bc791b3d 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/GpxFileTrackImporterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/GpxFileTrackImporterTest.java @@ -65,11 +65,6 @@ public class GpxFileTrackImporterTest extends AbstractTestFileTrackImporter { + getNameAndDescription(TRACK_NAME_0, TRACK_DESCRIPTION_0) + "" + getTrackPoint(0, null) + getTrackPoint(1, null) + "" + getTrackPoint(2, null) + getTrackPoint(3, null) + ""; - private static final String VALID_TWO_TRACKS_GPX = "" - + getNameAndDescription(TRACK_NAME_0, TRACK_DESCRIPTION_0) + "" - + getTrackPoint(0, TRACK_TIME_0) + "" - + getNameAndDescription(TRACK_NAME_1, TRACK_DESCRIPTION_1) + "" - + getTrackPoint(1, TRACK_TIME_1) + ""; private static final String INVALID_XML_GPX = VALID_ONE_TRACK_ONE_SEGMENT_GPX.substring( 0, VALID_ONE_TRACK_ONE_SEGMENT_GPX.length() - 50); diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporterTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporterTest.java index 54cf6e378..6e15bb99e 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporterTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/export/FileTrackExporterTest.java @@ -7,9 +7,6 @@ import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.MyTracksProviderUtils.Factory; import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Waypoint; -import com.google.android.apps.mytracks.io.file.export.FileTrackExporter; -import com.google.android.apps.mytracks.io.file.export.TrackExporterListener; -import com.google.android.apps.mytracks.io.file.export.TrackWriter; import com.google.android.apps.mytracks.services.TrackRecordingServiceTest.MockContext; import com.google.android.apps.mytracks.testing.TestingProviderUtilsFactory; @@ -101,9 +98,7 @@ public class FileTrackExporterTest extends AndroidTestCase { trackWriter.writeFooter(); mocksControl.replay(); - fileTrackExporter.writeTrack(outputStream); - - assertTrue(fileTrackExporter.isSuccess()); + assertTrue(fileTrackExporter.writeTrack(outputStream)); mocksControl.verify(); } @@ -132,9 +127,8 @@ public class FileTrackExporterTest extends AndroidTestCase { trackWriter.writeFooter(); mocksControl.replay(); - fileTrackExporter.writeTrack(outputStream); - - assertTrue(fileTrackExporter.isSuccess()); + + assertTrue(fileTrackExporter.writeTrack(outputStream)); mocksControl.verify(); } @@ -196,9 +190,8 @@ public class FileTrackExporterTest extends AndroidTestCase { trackWriter.writeFooter(); mocksControl.replay(); - fileTrackExporter.writeTrack(outputStream); - - assertTrue(fileTrackExporter.isSuccess()); + + assertTrue(fileTrackExporter.writeTrack(outputStream)); mocksControl.verify(); }