Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to the import feature #448

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand Down Expand Up @@ -152,30 +154,51 @@ public void determineProgressGoal(Uri importFileUri) {
mProgressAdmin.setContentLength(DEFAULT_UNKNOWN_FILESIZE);
if (importFileUri != null && importFileUri.getScheme().equals("file")) {
File file = new File(importFileUri.getPath());
mProgressAdmin.setContentLength(file.length());
if(!file.isDirectory()) mProgressAdmin.setContentLength(file.length());
}
}

public Uri importUri(Uri importFileUri) {
Uri result = null;
String trackName = null;
InputStream fis = null;
if (importFileUri.getScheme().equals("file")) {
trackName = importFileUri.getLastPathSegment();
}
try {
fis = mContentResolver.openInputStream(importFileUri);
} catch (IOException e) {
handleError(e, mContext.getString(R.string.error_importgpx_io));
File[] gpxFiles = new File[1];

// OI Filemanager returns a "content://" Uri, so check for "file://" does not work
if (importFileUri != null) {// && importFileUri.getScheme().equals("file")) {
File cFile = new File(importFileUri.getPath()); // make file object
if (cFile != null && cFile.exists()) {
if (cFile.isDirectory()) { // directory -> add all .gpx-files
gpxFiles = cFile.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".gpx");
}
});
} else if (cFile.getPath().endsWith(".gpx")) { // single file
gpxFiles[0] = cFile;
}
}
}

result = importTrack(fis, trackName);
// go through all files
for(File gpxFile : gpxFiles) {
if(gpxFile != null) {
try {
fis = new FileInputStream(gpxFile);
trackName = gpxFile.getName();
trackName = trackName.substring(0, trackName.length() - 4); // cut .gpx from name
result = importTrack(fis, trackName);
} catch (IOException e) {
handleError(e, mContext.getString(R.string.error_importgpx_io));
}
}
}

return result;
}

protected void handleError(Exception dialogException, String dialogErrorMessage) {
Timber.e(dialogException, "Unable to save ");
Timber.e(dialogException, "Unable to save: " + dialogErrorMessage);
mErrorDialogException = dialogException;
mErrorDialogMessage = dialogErrorMessage;
cancel(false);
Expand Down Expand Up @@ -218,7 +241,11 @@ public Uri importTrack(InputStream fis, String trackName) {
String attributeName;

Uri segmentUri = null;
while (eventType != XmlPullParser.END_DOCUMENT) {

// init track with date of first waypoint -> preparation
boolean updateTrackTime = true;

while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xmlParser.getName().equals(NAME_ELEMENT)) {
name = true;
Expand Down Expand Up @@ -265,21 +292,28 @@ public Uri importTrack(InputStream fis, String trackName) {
} else if (xmlParser.getName().equals(TIME_ELEMENT)) {
time = false;
} else if (xmlParser.getName().equals(SEGMENT_ELEMENT)) {
if (segmentUri == null) {
segmentUri = startSegment(trackUri);
if(!bulk.isEmpty()) {
if (segmentUri == null) {
segmentUri = startSegment(trackUri);
}

mContentResolver.bulkInsert(Uri.withAppendedPath(segmentUri, "waypoints"),
bulk.toArray(new ContentValues[bulk.size()]));
bulk.clear();
}
mContentResolver.bulkInsert(Uri.withAppendedPath(segmentUri, "waypoints"), bulk.toArray(new
ContentValues[bulk.size()]));
bulk.clear();
} else if (xmlParser.getName().equals(TRACK_ELEMENT)) {
if (!lastPosition.containsKey(Waypoints.TIME)) {
lastPosition.put(Waypoints.TIME, importDate);
}
if (!lastPosition.containsKey(Waypoints.SPEED)) {
lastPosition.put(Waypoints.SPEED, 0);
}

// very first waypoint -> update track to time of this waypoint
if(bulk.isEmpty() && updateTrackTime) lastPosition.put(Waypoints.UPDATE_TRACK_TIME, Waypoints.VALUE_YES);
bulk.add(lastPosition);
lastPosition = null;
updateTrackTime = false; // not again
}
} else if (eventType == XmlPullParser.TEXT) {
String text = xmlParser.getText();
Expand Down Expand Up @@ -383,4 +417,4 @@ public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ public static class Waypoints extends WaypointsColumns implements android.provid
*/
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.nl.sogeti.android.waypoint";

/**
* The flag, if the waypoint's time shall be the time of the track (actual value does not matter,
* but must be unique, of course).
*/
public static final String UPDATE_TRACK_TIME = "updatetracktime";

/**
* The value of the flag UPDATE_TRACK_TIME, just need "yes"
*/
public static final String VALUE_YES = "yes";

/**
* The name of this table, waypoints
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ int bulkInsertWaypoint(long trackId, long segmentId, ContentValues[] valuesArray
}
int inserted = 0;

// determine, if track time shall be updated
boolean updateTrackTime = valuesArray[0].getAsString(Waypoints.UPDATE_TRACK_TIME) != null;

// remove value to avoid trouble with the db
if(updateTrackTime) valuesArray[0].remove(Waypoints.UPDATE_TRACK_TIME);

SQLiteDatabase sqldb = getWritableDatabase();
sqldb.beginTransaction();
try {
Expand All @@ -142,6 +148,10 @@ int bulkInsertWaypoint(long trackId, long segmentId, ContentValues[] valuesArray

long id = sqldb.insert(Waypoints.TABLE, null, args);
if (id >= 0) {
if(inserted == 0 && updateTrackTime) { // first inserted waypoint -> update track time to this waypoint's time, if indicated
String trackTime = args.getAsString(Waypoints.TIME);
this.updateTrack(trackId, null, trackTime);
}
inserted++;
}
}
Expand Down Expand Up @@ -466,11 +476,12 @@ int deleteMedia(long mediaId) {
return affected;
}

int updateTrack(long trackId, String name) {
int updateTrack(long trackId, String name, String tDate) {
int updates;
String whereclause = Tracks._ID + " = " + trackId;
ContentValues args = new ContentValues();
args.put(Tracks.NAME, name);
if(name != null) args.put(Tracks.NAME, name);
if(tDate != null) args.put(Tracks.CREATION_TIME, tDate);

// Execute the query.
SQLiteDatabase mDb = getWritableDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,8 @@ public int update(Uri uri, ContentValues givenValues, String selection, String[]
case TRACK_ID:
trackId = Long.parseLong(uri.getLastPathSegment());
String name = givenValues.getAsString(Tracks.NAME);
updates = mDbHelper.updateTrack(trackId, name);
String tDate = givenValues.getAsString(Tracks.CREATION_TIME);
updates = mDbHelper.updateTrack(trackId, name, tDate);
break;
case TRACK_METADATA:
pathSegments = uri.getPathSegments();
Expand Down