forked from upstream-mirrors/OpenTracks
Codecleanup: for importer, make sure inputstreams are closed properly
This commit is contained in:
@@ -30,6 +30,7 @@ import android.util.Log;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -132,6 +133,9 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
publishProgress(i + 1, totalCount);
|
publishProgress(i + 1, totalCount);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "IOException", e);
|
||||||
|
return false;
|
||||||
} finally {
|
} finally {
|
||||||
if (wakeLock != null && wakeLock.isHeld()) {
|
if (wakeLock != null && wakeLock.isHeld()) {
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
@@ -159,7 +163,8 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
*
|
*
|
||||||
* @param file the file
|
* @param file the file
|
||||||
*/
|
*/
|
||||||
private boolean importFile(final File file) {
|
private boolean importFile(final File file) throws IOException {
|
||||||
|
FileInputStream fileInputStream = null;
|
||||||
try {
|
try {
|
||||||
TrackImporter trackImporter;
|
TrackImporter trackImporter;
|
||||||
if (trackFileFormat == TrackFileFormat.KML) {
|
if (trackFileFormat == TrackFileFormat.KML) {
|
||||||
@@ -178,7 +183,8 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
} else {
|
} else {
|
||||||
trackImporter = new GpxFileTrackImporter(context);
|
trackImporter = new GpxFileTrackImporter(context);
|
||||||
}
|
}
|
||||||
long trackIds[] = trackImporter.importFile(new FileInputStream(file));
|
fileInputStream = new FileInputStream(file);
|
||||||
|
long trackIds[] = trackImporter.importFile(fileInputStream);
|
||||||
int length = trackIds.length;
|
int length = trackIds.length;
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
trackId = trackIds[length - 1];
|
trackId = trackIds[length - 1];
|
||||||
@@ -187,6 +193,10 @@ public class ImportAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.d(TAG, "file: " + file.getAbsolutePath(), e);
|
Log.d(TAG, "file: " + file.getAbsolutePath(), e);
|
||||||
return false;
|
return false;
|
||||||
|
} finally {
|
||||||
|
if (fileInputStream != null) {
|
||||||
|
fileInputStream.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,8 @@ import org.xml.sax.SAXException;
|
|||||||
*/
|
*/
|
||||||
public class KmzTrackImporter implements TrackImporter {
|
public class KmzTrackImporter implements TrackImporter {
|
||||||
|
|
||||||
|
private static final int BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final String photoPath;
|
private final String photoPath;
|
||||||
|
|
||||||
@@ -51,18 +53,17 @@ public class KmzTrackImporter implements TrackImporter {
|
|||||||
@Override
|
@Override
|
||||||
public long[] importFile(InputStream inputStream)
|
public long[] importFile(InputStream inputStream)
|
||||||
throws IOException, ParserConfigurationException, SAXException {
|
throws IOException, ParserConfigurationException, SAXException {
|
||||||
long[] result = null;
|
|
||||||
ZipInputStream zipInputStream = null;
|
ZipInputStream zipInputStream = null;
|
||||||
try {
|
try {
|
||||||
zipInputStream = new ZipInputStream(inputStream);
|
long[] result = null;
|
||||||
ZipEntry zipEntry;
|
ZipEntry zipEntry;
|
||||||
|
|
||||||
|
zipInputStream = new ZipInputStream(inputStream);
|
||||||
|
|
||||||
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||||
String fileName = zipEntry.getName();
|
String fileName = zipEntry.getName();
|
||||||
if (fileName.equals(KmzTrackExporter.KML_FILE_NAME)) {
|
if (fileName.equals(KmzTrackExporter.KMZ_KML_FILE)) {
|
||||||
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
|
result = parseKml(zipInputStream);
|
||||||
context, -1L, photoPath);
|
|
||||||
byte[] buffer = readKml(zipInputStream);
|
|
||||||
result = kmlFileTrackImporter.importFile(new ByteArrayInputStream(buffer));
|
|
||||||
} else {
|
} else {
|
||||||
String prefix = KmzTrackExporter.KMZ_IMAGES_DIR + File.separatorChar;
|
String prefix = KmzTrackExporter.KMZ_IMAGES_DIR + File.separatorChar;
|
||||||
if (fileName.startsWith(prefix)) {
|
if (fileName.startsWith(prefix)) {
|
||||||
@@ -71,19 +72,33 @@ public class KmzTrackImporter implements TrackImporter {
|
|||||||
}
|
}
|
||||||
zipInputStream.closeEntry();
|
zipInputStream.closeEntry();
|
||||||
}
|
}
|
||||||
|
return result == null ? new long[0] : result;
|
||||||
} finally {
|
} finally {
|
||||||
if (zipInputStream != null) {
|
if (zipInputStream != null) {
|
||||||
zipInputStream.close();
|
zipInputStream.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result == null ? new long[0] : result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] readKml(ZipInputStream zipInputStream) throws IOException {
|
private long[] parseKml(ZipInputStream zipInputStream)
|
||||||
|
throws IOException, ParserConfigurationException, SAXException {
|
||||||
|
ByteArrayInputStream byteArrayInputStream = null;
|
||||||
|
try {
|
||||||
|
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L, photoPath);
|
||||||
|
byteArrayInputStream = new ByteArrayInputStream(getKml(zipInputStream));
|
||||||
|
return kmlFileTrackImporter.importFile(byteArrayInputStream);
|
||||||
|
} finally {
|
||||||
|
if (byteArrayInputStream != null) {
|
||||||
|
byteArrayInputStream.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] getKml(ZipInputStream zipInputStream) throws IOException {
|
||||||
ByteArrayOutputStream byteArrayOutputStream = null;
|
ByteArrayOutputStream byteArrayOutputStream = null;
|
||||||
try {
|
try {
|
||||||
byteArrayOutputStream = new ByteArrayOutputStream();
|
byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
byte[] buffer = new byte[KmzTrackExporter.BUFFER_SIZE];
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
int count;
|
int count;
|
||||||
while ((count = zipInputStream.read(buffer)) != -1) {
|
while ((count = zipInputStream.read(buffer)) != -1) {
|
||||||
byteArrayOutputStream.write(buffer, 0, count);
|
byteArrayOutputStream.write(buffer, 0, count);
|
||||||
@@ -100,7 +115,7 @@ public class KmzTrackImporter implements TrackImporter {
|
|||||||
FileOutputStream fileOutputStream = null;
|
FileOutputStream fileOutputStream = null;
|
||||||
try {
|
try {
|
||||||
fileOutputStream = new FileOutputStream(photoPath + File.separatorChar + fileName);
|
fileOutputStream = new FileOutputStream(photoPath + File.separatorChar + fileName);
|
||||||
byte[] buffer = new byte[KmzTrackExporter.BUFFER_SIZE];
|
byte[] buffer = new byte[BUFFER_SIZE];
|
||||||
int count;
|
int count;
|
||||||
while ((count = zipInputStream.read(buffer)) != -1) {
|
while ((count = zipInputStream.read(buffer)) != -1) {
|
||||||
fileOutputStream.write(buffer, 0, count);
|
fileOutputStream.write(buffer, 0, count);
|
||||||
|
|||||||
@@ -37,10 +37,10 @@ import java.util.zip.ZipOutputStream;
|
|||||||
*/
|
*/
|
||||||
public class KmzTrackExporter extends AbstractTrackExporter {
|
public class KmzTrackExporter extends AbstractTrackExporter {
|
||||||
|
|
||||||
public static final String KMZ_IMAGES_DIR = "images";
|
|
||||||
public static final String KMZ_EXTENSION = "kmz";
|
public static final String KMZ_EXTENSION = "kmz";
|
||||||
public static final int BUFFER_SIZE = 4096;
|
public static final String KMZ_IMAGES_DIR = "images";
|
||||||
public static final String KML_FILE_NAME = "doc.kml";
|
public static final String KMZ_KML_FILE = "doc.kml";
|
||||||
|
private static final int BUFFER_SIZE = 4096;
|
||||||
|
|
||||||
private final MyTracksProviderUtils myTracksProviderUtils;
|
private final MyTracksProviderUtils myTracksProviderUtils;
|
||||||
private final FileTrackExporter fileTrackExporter;
|
private final FileTrackExporter fileTrackExporter;
|
||||||
@@ -72,7 +72,7 @@ public class KmzTrackExporter extends AbstractTrackExporter {
|
|||||||
zipOutputStream = new ZipOutputStream(outputStream);
|
zipOutputStream = new ZipOutputStream(outputStream);
|
||||||
|
|
||||||
// Add kml file
|
// Add kml file
|
||||||
ZipEntry zipEntry = new ZipEntry(KML_FILE_NAME);
|
ZipEntry zipEntry = new ZipEntry(KMZ_KML_FILE);
|
||||||
zipOutputStream.putNextEntry(zipEntry);
|
zipOutputStream.putNextEntry(zipEntry);
|
||||||
|
|
||||||
fileTrackExporter.writeTrack(zipOutputStream);
|
fileTrackExporter.writeTrack(zipOutputStream);
|
||||||
|
|||||||
@@ -345,33 +345,40 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
*/
|
*/
|
||||||
private void insertNewDriveFiles(Collection<File> driveFiles) throws IOException {
|
private void insertNewDriveFiles(Collection<File> driveFiles) throws IOException {
|
||||||
for (File driveFile : driveFiles) {
|
for (File driveFile : driveFiles) {
|
||||||
if (driveFile != null) {
|
InputStream inputStream = null;
|
||||||
InputStream inputStream = downloadDriveFile(driveFile, true);
|
try {
|
||||||
if (inputStream != null) {
|
if (driveFile == null) {
|
||||||
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L, null);
|
continue;
|
||||||
try {
|
}
|
||||||
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
|
inputStream = downloadDriveFile(driveFile, true);
|
||||||
if (trackIds.length == 1) {
|
if (inputStream == null) {
|
||||||
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
|
continue;
|
||||||
if (track == null) {
|
}
|
||||||
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId());
|
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(context, -1L, null);
|
||||||
} else {
|
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
|
||||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
|
if (trackIds.length == 1) {
|
||||||
Log.d(TAG, "Add from Google Drive " + track.getName());
|
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
|
||||||
}
|
if (track == null) {
|
||||||
} else {
|
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId());
|
||||||
// Clean up if imported more than one track
|
continue;
|
||||||
for (int i = 0; i < trackIds.length; i++) {
|
|
||||||
myTracksProviderUtils.deleteTrack(trackIds[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SAXException e) {
|
|
||||||
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
|
|
||||||
} catch (ParserConfigurationException e) {
|
|
||||||
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
|
|
||||||
}
|
}
|
||||||
|
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
|
||||||
|
Log.d(TAG, "Add from Google Drive " + track.getName());
|
||||||
|
} else {
|
||||||
|
// Clean up if imported more than one track
|
||||||
|
for (int i = 0; i < trackIds.length; i++) {
|
||||||
|
myTracksProviderUtils.deleteTrack(trackIds[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SAXException e) {
|
||||||
|
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
|
||||||
|
} catch (ParserConfigurationException e) {
|
||||||
|
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId(), e);
|
||||||
|
} finally {
|
||||||
|
if (inputStream != null) {
|
||||||
|
inputStream.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -489,14 +496,15 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
InputStream inputStream = downloadDriveFile(driveFile, true);
|
InputStream inputStream = null;
|
||||||
if (inputStream == null) {
|
|
||||||
Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
|
|
||||||
context, track.getId(), null);
|
|
||||||
try {
|
try {
|
||||||
|
inputStream = downloadDriveFile(driveFile, true);
|
||||||
|
if (inputStream == null) {
|
||||||
|
Log.e(TAG, "Unable to update track. Input stream is null for track " + track.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
KmlFileTrackImporter kmlFileTrackImporter = new KmlFileTrackImporter(
|
||||||
|
context, track.getId(), null);
|
||||||
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
|
long[] trackIds = kmlFileTrackImporter.importFile(inputStream);
|
||||||
if (trackIds.length == 1) {
|
if (trackIds.length == 1) {
|
||||||
Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]);
|
Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]);
|
||||||
@@ -515,6 +523,10 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
Log.e(TAG, "Unable to merge", e);
|
Log.e(TAG, "Unable to merge", e);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Log.e(TAG, "Unable to merge", e);
|
Log.e(TAG, "Unable to merge", e);
|
||||||
|
} finally {
|
||||||
|
if (inputStream != null) {
|
||||||
|
inputStream.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,10 +111,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures the calorie preference.
|
* Configures the calorie preference.
|
||||||
*
|
|
||||||
* @param reference to configure
|
|
||||||
* @param key of the preference
|
|
||||||
* @param defaultValue default value of this preference
|
|
||||||
*/
|
*/
|
||||||
private void configCaloriePreference() {
|
private void configCaloriePreference() {
|
||||||
caloriePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
caloriePreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||||
@@ -160,11 +156,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures the weight preference.
|
* Configures the weight preference.
|
||||||
*
|
|
||||||
* @param preference to configure
|
|
||||||
* @param key of the preference
|
|
||||||
* @param defaultValue default value of this preference
|
|
||||||
* @param isEnable true means enable the weight preference
|
|
||||||
*/
|
*/
|
||||||
private void configWeightPreference() {
|
private void configWeightPreference() {
|
||||||
weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
weightPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
|
||||||
@@ -188,10 +179,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the weight summary.
|
* Updates the weight summary.
|
||||||
*
|
|
||||||
* @param preference the preference
|
|
||||||
* @param keyId the key id
|
|
||||||
* @param defaultValue the default value
|
|
||||||
*/
|
*/
|
||||||
private void updateWeightSummary() {
|
private void updateWeightSummary() {
|
||||||
boolean metricUnits = PreferencesUtils.isMetricUnits(this);
|
boolean metricUnits = PreferencesUtils.isMetricUnits(this);
|
||||||
@@ -201,10 +188,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
|||||||
/**
|
/**
|
||||||
* Updates the weight summary.
|
* Updates the weight summary.
|
||||||
*
|
*
|
||||||
* @param preference the preference
|
* @param metricUnits true if metric units
|
||||||
* @param keyId the key id
|
|
||||||
* @param defaultValue the default value
|
|
||||||
* @param metricUnits the status of metric units
|
|
||||||
*/
|
*/
|
||||||
private void updateWeightSummary(boolean metricUnits) {
|
private void updateWeightSummary(boolean metricUnits) {
|
||||||
int displayValue = getWeightDisplayValue(metricUnits);
|
int displayValue = getWeightDisplayValue(metricUnits);
|
||||||
@@ -216,8 +200,7 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
|||||||
* Gets the weight display value in metric or imperial depending on the
|
* Gets the weight display value in metric or imperial depending on the
|
||||||
* preferred units.
|
* preferred units.
|
||||||
*
|
*
|
||||||
* @param keyId the key id
|
* @param metricUnits true if metric units
|
||||||
* @param defaultValue the default value
|
|
||||||
*/
|
*/
|
||||||
private int getWeightDisplayValue(boolean metricUnits) {
|
private int getWeightDisplayValue(boolean metricUnits) {
|
||||||
int value = PreferencesUtils.getInt(this, R.string.stats_weight_key,
|
int value = PreferencesUtils.getInt(this, R.string.stats_weight_key,
|
||||||
@@ -231,8 +214,6 @@ public class StatsSettingsActivity extends AbstractSettingsActivity {
|
|||||||
/**
|
/**
|
||||||
* Stores the weight value, always in metric units.
|
* Stores the weight value, always in metric units.
|
||||||
*
|
*
|
||||||
* @param keyId the key id
|
|
||||||
* @param defaultValue the default value
|
|
||||||
* @param displayValue the display value
|
* @param displayValue the display value
|
||||||
*/
|
*/
|
||||||
private void storeWeightValue(String displayValue) {
|
private void storeWeightValue(String displayValue) {
|
||||||
|
|||||||
Reference in New Issue
Block a user