This commit is contained in:
Dennis Guse
2019-11-29 22:23:07 +01:00
parent bf7fc28bb0
commit 193e5eccbb
4 changed files with 18 additions and 24 deletions
@@ -64,7 +64,7 @@ public class FileTrackExporter implements TrackExporter {
} }
@Override @Override
public boolean writeTrack(Context context, @NonNull OutputStream outputStream) { public boolean writeTrack(@NonNull Context context, @NonNull OutputStream outputStream) {
try { try {
trackWriter.prepare(outputStream); trackWriter.prepare(outputStream);
trackWriter.writeHeader(tracks); trackWriter.writeHeader(tracks);
@@ -19,7 +19,6 @@ package de.dennisguse.opentracks.io.file.exporter;
import android.content.Context; import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.OutputStream; import java.io.OutputStream;
@@ -37,5 +36,5 @@ public interface TrackExporter {
* @param context the context * @param context the context
* @param outputStream the output stream * @param outputStream the output stream
*/ */
boolean writeTrack(@Nullable Context context, @NonNull OutputStream outputStream); boolean writeTrack(@NonNull Context context, @NonNull OutputStream outputStream);
} }
@@ -45,7 +45,9 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
private static final String TAG = ImportAsyncTask.class.getSimpleName(); private static final String TAG = ImportAsyncTask.class.getSimpleName();
private final TrackFileFormat trackFileFormat; private final TrackFileFormat trackFileFormat;
private final String path; private final String path;
private final Context context; // TODO Can be removed, or?
private Context context;
// TODO Use weak reference
private ImportActivity importActivity; private ImportActivity importActivity;
private WakeLock wakeLock; private WakeLock wakeLock;
@@ -100,7 +102,7 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
try { try {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); Thread.currentThread().setPriority(Thread.MAX_PRIORITY); // TODO Should be set to previous level at the end of this method.
// Get the wake lock if not recording or paused // Get the wake lock if not recording or paused
boolean isRecording = PreferencesUtils.isRecording(importActivity); boolean isRecording = PreferencesUtils.isRecording(importActivity);
boolean isPaused = PreferencesUtils.isRecordingTrackPaused(importActivity); boolean isPaused = PreferencesUtils.isRecordingTrackPaused(importActivity);
@@ -149,9 +151,11 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override @Override
protected void onCancelled() { protected void onCancelled() {
context = null;
completed = true; completed = true;
if (importActivity != null) { if (importActivity != null) {
importActivity.onAsyncTaskCompleted(importTrackCount, totalTrackCount); importActivity.onAsyncTaskCompleted(importTrackCount, totalTrackCount);
importActivity = null;
} }
} }
@@ -163,18 +167,17 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
private boolean importFile(final File file) { private boolean importFile(final File file) {
TrackImporter trackImporter; TrackImporter trackImporter;
if (trackFileFormat == TrackFileFormat.GPX) { if (trackFileFormat == TrackFileFormat.GPX) {
trackImporter = new GpxFileTrackImporter(context); trackImporter = new GpxFileTrackImporter(importActivity);
} else { //KML or KMZ } else { //KML or KMZ
String extension = FileUtils.getExtension(file.getName()); String extension = FileUtils.getExtension(file.getName());
if (TrackFileFormat.KML_ONLY_TRACK.getExtension().equals(extension)) { if (TrackFileFormat.KML_ONLY_TRACK.getExtension().equals(extension)) {
trackImporter = new KmlFileTrackImporter(context, -1L); trackImporter = new KmlFileTrackImporter(importActivity, -1L);
} else { } else {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); ContentProviderUtils contentProviderUtils = new ContentProviderUtils(importActivity);
;
Uri uri = contentProviderUtils.insertTrack(new Track()); Uri uri = contentProviderUtils.insertTrack(new Track());
long newId = Long.parseLong(uri.getLastPathSegment()); long newId = Long.parseLong(uri.getLastPathSegment());
trackImporter = new KmzTrackImporter(context, newId); trackImporter = new KmzTrackImporter(importActivity, newId);
} }
} }
@@ -72,7 +72,7 @@ public class KmzTrackImporter implements TrackImporter {
return -1L; return -1L;
} }
String fileName = zipEntry.getName(); String fileName = zipEntry.getName();
if (fileName.equals(KmzTrackExporter.KMZ_KML_FILE)) { if (KmzTrackExporter.KMZ_KML_FILE.equals(fileName)) {
trackId = parseKml(zipInputStream); trackId = parseKml(zipInputStream);
if (trackId == -1L) { if (trackId == -1L) {
Log.d(TAG, "Unable to parse kml in kmz"); Log.d(TAG, "Unable to parse kml in kmz");
@@ -103,7 +103,6 @@ public class KmzTrackImporter implements TrackImporter {
private void cleanImport(long trackId) { private void cleanImport(long trackId) {
if (PreferencesUtils.isRecording(trackId)) { if (PreferencesUtils.isRecording(trackId)) {
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
;
contentProviderUtils.deleteTrack(context, trackId); contentProviderUtils.deleteTrack(context, trackId);
} }
@@ -126,15 +125,10 @@ public class KmzTrackImporter implements TrackImporter {
* @return the imported track id or -1L * @return the imported track id or -1L
*/ */
private long parseKml(ZipInputStream zipInputStream) throws IOException { private long parseKml(ZipInputStream zipInputStream) throws IOException {
ByteArrayInputStream byteArrayInputStream = null; KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, importTrackId);
try {
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, importTrackId); try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(getKml(zipInputStream))) {
byteArrayInputStream = new ByteArrayInputStream(getKml(zipInputStream));
return kmlFileTrackImporter.importFile(byteArrayInputStream); return kmlFileTrackImporter.importFile(byteArrayInputStream);
} finally {
if (byteArrayInputStream != null) {
byteArrayInputStream.close();
}
} }
} }
@@ -161,12 +155,10 @@ public class KmzTrackImporter implements TrackImporter {
* @param fileName the file name * @param fileName the file name
*/ */
private void readImageFile(ZipInputStream zipInputStream, String fileName) throws IOException { private void readImageFile(ZipInputStream zipInputStream, String fileName) throws IOException {
if (importTrackId == -1L) { if (importTrackId == -1L || fileName.equals("")) {
return;
}
if (fileName.equals("")) {
return; return;
} }
File dir = FileUtils.getPhotoDir(importTrackId); File dir = FileUtils.getPhotoDir(importTrackId);
FileUtils.ensureDirectoryExists(dir); FileUtils.ensureDirectoryExists(dir);
File file = new File(dir, fileName); File file = new File(dir, fileName);