From 193e5eccbbadf4021e09d7ccfeb3275ab7cd2c0c Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Fri, 29 Nov 2019 22:23:07 +0100 Subject: [PATCH] Cleanup. --- .../io/file/exporter/FileTrackExporter.java | 2 +- .../io/file/exporter/TrackExporter.java | 3 +-- .../io/file/importer/ImportAsyncTask.java | 17 +++++++++------- .../io/file/importer/KmzTrackImporter.java | 20 ++++++------------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java index c92e2655b..9ac91b231 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/FileTrackExporter.java @@ -64,7 +64,7 @@ public class FileTrackExporter implements TrackExporter { } @Override - public boolean writeTrack(Context context, @NonNull OutputStream outputStream) { + public boolean writeTrack(@NonNull Context context, @NonNull OutputStream outputStream) { try { trackWriter.prepare(outputStream); trackWriter.writeHeader(tracks); diff --git a/src/main/java/de/dennisguse/opentracks/io/file/exporter/TrackExporter.java b/src/main/java/de/dennisguse/opentracks/io/file/exporter/TrackExporter.java index 71befbb26..417cd0f93 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/exporter/TrackExporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/exporter/TrackExporter.java @@ -19,7 +19,6 @@ package de.dennisguse.opentracks.io.file.exporter; import android.content.Context; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import java.io.OutputStream; @@ -37,5 +36,5 @@ public interface TrackExporter { * @param context the context * @param outputStream the output stream */ - boolean writeTrack(@Nullable Context context, @NonNull OutputStream outputStream); + boolean writeTrack(@NonNull Context context, @NonNull OutputStream outputStream); } diff --git a/src/main/java/de/dennisguse/opentracks/io/file/importer/ImportAsyncTask.java b/src/main/java/de/dennisguse/opentracks/io/file/importer/ImportAsyncTask.java index ffa33eb21..f6fc1ace1 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/importer/ImportAsyncTask.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/importer/ImportAsyncTask.java @@ -45,7 +45,9 @@ public class ImportAsyncTask extends AsyncTask { private static final String TAG = ImportAsyncTask.class.getSimpleName(); private final TrackFileFormat trackFileFormat; private final String path; - private final Context context; + // TODO Can be removed, or? + private Context context; + // TODO Use weak reference private ImportActivity importActivity; private WakeLock wakeLock; @@ -100,7 +102,7 @@ public class ImportAsyncTask extends AsyncTask { @Override protected Boolean doInBackground(Void... params) { 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 boolean isRecording = PreferencesUtils.isRecording(importActivity); boolean isPaused = PreferencesUtils.isRecordingTrackPaused(importActivity); @@ -149,9 +151,11 @@ public class ImportAsyncTask extends AsyncTask { @Override protected void onCancelled() { + context = null; completed = true; if (importActivity != null) { importActivity.onAsyncTaskCompleted(importTrackCount, totalTrackCount); + importActivity = null; } } @@ -163,18 +167,17 @@ public class ImportAsyncTask extends AsyncTask { private boolean importFile(final File file) { TrackImporter trackImporter; if (trackFileFormat == TrackFileFormat.GPX) { - trackImporter = new GpxFileTrackImporter(context); + trackImporter = new GpxFileTrackImporter(importActivity); } else { //KML or KMZ String extension = FileUtils.getExtension(file.getName()); if (TrackFileFormat.KML_ONLY_TRACK.getExtension().equals(extension)) { - trackImporter = new KmlFileTrackImporter(context, -1L); + trackImporter = new KmlFileTrackImporter(importActivity, -1L); } else { - ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); - ; + ContentProviderUtils contentProviderUtils = new ContentProviderUtils(importActivity); Uri uri = contentProviderUtils.insertTrack(new Track()); long newId = Long.parseLong(uri.getLastPathSegment()); - trackImporter = new KmzTrackImporter(context, newId); + trackImporter = new KmzTrackImporter(importActivity, newId); } } diff --git a/src/main/java/de/dennisguse/opentracks/io/file/importer/KmzTrackImporter.java b/src/main/java/de/dennisguse/opentracks/io/file/importer/KmzTrackImporter.java index 2755f24ac..7b98556eb 100644 --- a/src/main/java/de/dennisguse/opentracks/io/file/importer/KmzTrackImporter.java +++ b/src/main/java/de/dennisguse/opentracks/io/file/importer/KmzTrackImporter.java @@ -72,7 +72,7 @@ public class KmzTrackImporter implements TrackImporter { return -1L; } String fileName = zipEntry.getName(); - if (fileName.equals(KmzTrackExporter.KMZ_KML_FILE)) { + if (KmzTrackExporter.KMZ_KML_FILE.equals(fileName)) { trackId = parseKml(zipInputStream); if (trackId == -1L) { Log.d(TAG, "Unable to parse kml in kmz"); @@ -103,7 +103,6 @@ public class KmzTrackImporter implements TrackImporter { private void cleanImport(long trackId) { if (PreferencesUtils.isRecording(trackId)) { ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); - ; contentProviderUtils.deleteTrack(context, trackId); } @@ -126,15 +125,10 @@ public class KmzTrackImporter implements TrackImporter { * @return the imported track id or -1L */ private long parseKml(ZipInputStream zipInputStream) throws IOException { - ByteArrayInputStream byteArrayInputStream = null; - try { - KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, importTrackId); - byteArrayInputStream = new ByteArrayInputStream(getKml(zipInputStream)); + KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, importTrackId); + + try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(getKml(zipInputStream))) { return kmlFileTrackImporter.importFile(byteArrayInputStream); - } finally { - if (byteArrayInputStream != null) { - byteArrayInputStream.close(); - } } } @@ -161,12 +155,10 @@ public class KmzTrackImporter implements TrackImporter { * @param fileName the file name */ private void readImageFile(ZipInputStream zipInputStream, String fileName) throws IOException { - if (importTrackId == -1L) { - return; - } - if (fileName.equals("")) { + if (importTrackId == -1L || fileName.equals("")) { return; } + File dir = FileUtils.getPhotoDir(importTrackId); FileUtils.ensureDirectoryExists(dir); File file = new File(dir, fileName);