forked from upstream-mirrors/OpenTracks
Improve style and external storage access methods of ImportAllTracks
This commit is contained in:
@@ -145,14 +145,11 @@ limitations under the License.
|
|||||||
<!-- Format:
|
<!-- Format:
|
||||||
$1 : Successful imports
|
$1 : Successful imports
|
||||||
$2 : Attempted imports
|
$2 : Attempted imports
|
||||||
$3 : MyTracks directory
|
$3 : MyTracks gpx path
|
||||||
-->
|
-->
|
||||||
<string name="import_done">Imported %1$d of %2$d GPX files found in /sdcard/%3$s/gpx/</string>
|
<string name="import_done">Imported %1$d of %2$d GPX files from %3$s</string>
|
||||||
<!-- Format:
|
<string name="import_empty">No GPX files found in %s. SD card may be unreadable or directory may not contain GPX files.</string>
|
||||||
$1 : MyTracks directory
|
<string name="import_error">Import error for file %s</string>
|
||||||
-->
|
|
||||||
<string name="import_empty">No files found in /sdcard/%1$s/gpx/</string>
|
|
||||||
<string name="import_error">Import error for file</string>
|
|
||||||
|
|
||||||
<!-- Waypoints list -->
|
<!-- Waypoints list -->
|
||||||
<string name="waypointslist_btn_delete_all">Delete all markers</string>
|
<string name="waypointslist_btn_delete_all">Delete all markers</string>
|
||||||
|
|||||||
@@ -17,14 +17,10 @@ package com.google.android.apps.mytracks;
|
|||||||
|
|
||||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||||
import com.google.android.apps.mytracks.io.GpxImporter;
|
import com.google.android.apps.mytracks.io.GpxImporter;
|
||||||
|
import com.google.android.apps.mytracks.util.FileUtils;
|
||||||
import com.google.android.apps.mytracks.util.MyTracksUtils;
|
import com.google.android.apps.mytracks.util.MyTracksUtils;
|
||||||
import com.google.android.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
@@ -35,9 +31,21 @@ import android.os.PowerManager.WakeLock;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A class that will import all GPX tracks in /sdcard/MyTracks/gpx/
|
* A class that will import all GPX tracks in /sdcard/MyTracks/gpx/
|
||||||
*
|
*
|
||||||
* @author David Piggott
|
* @author David Piggott
|
||||||
*/
|
*/
|
||||||
public class ImportAllTracks {
|
public class ImportAllTracks {
|
||||||
@@ -45,12 +53,16 @@ public class ImportAllTracks {
|
|||||||
private final Activity activity;
|
private final Activity activity;
|
||||||
private WakeLock wakeLock;
|
private WakeLock wakeLock;
|
||||||
private ProgressDialog progress;
|
private ProgressDialog progress;
|
||||||
private int gpxFileCandidateCount;
|
private FileUtils fileUtils;
|
||||||
|
private String gpxPath;
|
||||||
|
private int gpxFileCount;
|
||||||
private int importSuccessCount;
|
private int importSuccessCount;
|
||||||
|
|
||||||
public ImportAllTracks(Activity activity) {
|
public ImportAllTracks(Activity activity) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
Log.i(MyTracksConstants.TAG, "ImportAllTracks: Starting");
|
Log.i(MyTracksConstants.TAG, "ImportAllTracks: Starting");
|
||||||
|
this.fileUtils = new FileUtils();
|
||||||
|
gpxPath = fileUtils.buildExternalDirectoryPath("gpx");
|
||||||
HandlerThread handlerThread;
|
HandlerThread handlerThread;
|
||||||
Handler handler;
|
Handler handler;
|
||||||
handlerThread = new HandlerThread("ImportAllTracks");
|
handlerThread = new HandlerThread("ImportAllTracks");
|
||||||
@@ -66,17 +78,14 @@ public class ImportAllTracks {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Makes sure that we keep the phone from sleeping.
|
* Makes sure that we keep the phone from sleeping. See if there is a current
|
||||||
* See if there is a current track. Acquire a wake lock if there is no
|
* track. Acquire a wake lock if there is no current track.
|
||||||
* current track.
|
|
||||||
*/
|
*/
|
||||||
private void aquireLocksAndImport() {
|
private void aquireLocksAndImport() {
|
||||||
SharedPreferences prefs =
|
SharedPreferences prefs = activity.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
|
||||||
activity.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
|
|
||||||
long recordingTrackId = -1;
|
long recordingTrackId = -1;
|
||||||
if (prefs != null) {
|
if (prefs != null) {
|
||||||
recordingTrackId =
|
recordingTrackId = prefs.getLong(activity.getString(R.string.recording_track_key), -1);
|
||||||
prefs.getLong(activity.getString(R.string.recording_track_key), -1);
|
|
||||||
}
|
}
|
||||||
if (recordingTrackId != -1) {
|
if (recordingTrackId != -1) {
|
||||||
wakeLock = MyTracksUtils.acquireWakeLock(activity, wakeLock);
|
wakeLock = MyTracksUtils.acquireWakeLock(activity, wakeLock);
|
||||||
@@ -94,17 +103,15 @@ public class ImportAllTracks {
|
|||||||
|
|
||||||
Log.i(MyTracksConstants.TAG, "ImportAllTracks: Done");
|
Log.i(MyTracksConstants.TAG, "ImportAllTracks: Done");
|
||||||
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||||
if (gpxFileCandidateCount == 0) {
|
if (gpxFileCount == 0) {
|
||||||
builder.setMessage(activity.getString(R.string.import_empty,
|
builder.setMessage(activity.getString(R.string.import_empty, gpxPath + "/"));
|
||||||
MyTracksConstants.SDCARD_TOP_DIR));
|
|
||||||
} else {
|
} else {
|
||||||
builder.setMessage(activity.getString(R.string.import_done,
|
builder.setMessage(activity.getString(R.string.import_done, importSuccessCount, gpxFileCount,
|
||||||
importSuccessCount, gpxFileCandidateCount,
|
gpxPath + "/"));
|
||||||
MyTracksConstants.SDCARD_TOP_DIR));
|
|
||||||
}
|
}
|
||||||
builder.setPositiveButton(R.string.ok, null);
|
builder.setPositiveButton(R.string.ok, null);
|
||||||
builder.show();
|
builder.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void makeProgressDialog(final int trackCount) {
|
private void makeProgressDialog(final int trackCount) {
|
||||||
String importMsg = activity.getString(R.string.tracklist_btn_import_all);
|
String importMsg = activity.getString(R.string.tracklist_btn_import_all);
|
||||||
@@ -118,15 +125,86 @@ public class ImportAllTracks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Actually import the tracks.
|
* Actually import the tracks. This should be called after the wake locks have
|
||||||
* This should be called after the wake locks have been acquired.
|
* been acquired.
|
||||||
*/
|
*/
|
||||||
private void importAll() {
|
private void importAll() {
|
||||||
MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory
|
MyTracksProviderUtils providerUtils = MyTracksProviderUtils.Factory.get(activity);
|
||||||
.get(activity);
|
|
||||||
LinkedList<File> gpxFiles = new LinkedList<File>();
|
if (!fileUtils.isSdCardAvailable()) {
|
||||||
File[] gpxFileCandidates = new File("/sdcard/"
|
return;
|
||||||
+ MyTracksConstants.SDCARD_TOP_DIR + "/gpx").listFiles();
|
}
|
||||||
|
|
||||||
|
List<File> gpxFiles = getGpxFiles();
|
||||||
|
gpxFileCount = gpxFiles.size();
|
||||||
|
if (gpxFileCount == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.i(MyTracksConstants.TAG, "ImportAllTracks: Importing: " + gpxFileCount + " tracks.");
|
||||||
|
activity.runOnUiThread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
makeProgressDialog(gpxFileCount);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Iterator<File> gpxFilesIterator = gpxFiles.iterator();
|
||||||
|
for (int currentFileNumber = 0; gpxFilesIterator.hasNext(); currentFileNumber++) {
|
||||||
|
File currentFile = gpxFilesIterator.next();
|
||||||
|
final int status = currentFileNumber;
|
||||||
|
activity.runOnUiThread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
synchronized (this) {
|
||||||
|
if (progress == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
progress.setProgress(status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (importFile(currentFile, providerUtils)) {
|
||||||
|
importSuccessCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (progress != null) {
|
||||||
|
synchronized (this) {
|
||||||
|
progress.dismiss();
|
||||||
|
progress = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to import a GPX file. Returns true on success, issues error
|
||||||
|
* notifications and returns false on failure.
|
||||||
|
*/
|
||||||
|
private boolean importFile(File gpxFile, MyTracksProviderUtils providerUtils) {
|
||||||
|
Log.i(MyTracksConstants.TAG, "ImportAllTracks: importing: " + gpxFile.getName());
|
||||||
|
try {
|
||||||
|
GpxImporter.importGPXFile(new FileInputStream(gpxFile), providerUtils);
|
||||||
|
return true;
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
Log.w(MyTracksConstants.TAG, "GPX file wasn't found/went missing: "
|
||||||
|
+ gpxFile.getAbsolutePath(), e);
|
||||||
|
} catch (ParserConfigurationException e) {
|
||||||
|
Log.w(MyTracksConstants.TAG, "Error parsing file: " + gpxFile.getAbsolutePath(), e);
|
||||||
|
} catch (SAXException e) {
|
||||||
|
Log.w(MyTracksConstants.TAG, "Error parsing file: " + gpxFile.getAbsolutePath(), e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.w(MyTracksConstants.TAG, "Error reading file: " + gpxFile.getAbsolutePath(), e);
|
||||||
|
}
|
||||||
|
Toast.makeText(activity, activity.getString(R.string.import_error, gpxFile.getName()),
|
||||||
|
Toast.LENGTH_LONG).show();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list of the GPX Files found in the GPX directory.
|
||||||
|
*/
|
||||||
|
private List<File> getGpxFiles() {
|
||||||
|
List<File> gpxFiles = new LinkedList<File>();
|
||||||
|
File[] gpxFileCandidates = new File(gpxPath).listFiles();
|
||||||
if (gpxFileCandidates != null) {
|
if (gpxFileCandidates != null) {
|
||||||
for (File file : gpxFileCandidates) {
|
for (File file : gpxFileCandidates) {
|
||||||
if (!file.isDirectory() && file.getName().endsWith(".gpx")) {
|
if (!file.isDirectory() && file.getName().endsWith(".gpx")) {
|
||||||
@@ -134,59 +212,6 @@ public class ImportAllTracks {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gpxFileCandidateCount = gpxFiles.size();
|
return gpxFiles;
|
||||||
|
|
||||||
if (gpxFileCandidateCount != 0) {
|
|
||||||
Log.i(MyTracksConstants.TAG, "ImportAllTracks: Importing: "
|
|
||||||
+ gpxFileCandidateCount + " tracks.");
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
makeProgressDialog(gpxFileCandidateCount);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Iterator<File> gpxFilesIterator = gpxFiles.iterator();
|
|
||||||
File currentFile;
|
|
||||||
int currentFileNumber = 0;
|
|
||||||
while (gpxFilesIterator.hasNext()) {
|
|
||||||
currentFile = gpxFilesIterator.next();
|
|
||||||
final int status = currentFileNumber;
|
|
||||||
activity.runOnUiThread(new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
synchronized (this) {
|
|
||||||
if (progress == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
progress.setProgress(status);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (importFile(currentFile, providerUtils)) {
|
|
||||||
importSuccessCount++;
|
|
||||||
}
|
|
||||||
currentFileNumber++;
|
|
||||||
}
|
|
||||||
if (progress != null) {
|
|
||||||
synchronized (this) {
|
|
||||||
progress.dismiss();
|
|
||||||
progress = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean importFile(File gpxFile, MyTracksProviderUtils providerUtils) {
|
|
||||||
Log.i(MyTracksConstants.TAG, "ImportAllTracks: importing: "
|
|
||||||
+ gpxFile.getName());
|
|
||||||
try {
|
|
||||||
GpxImporter.importGPXFile(new FileInputStream(gpxFile), providerUtils);
|
|
||||||
return true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.w("", e);
|
|
||||||
Toast.makeText(activity,
|
|
||||||
activity.getString(R.string.import_error) + " " + gpxFile.getName(),
|
|
||||||
Toast.LENGTH_LONG).show();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user