forked from upstream-mirrors/OpenTracks
Use modified time to detect sync changes. Can not use md5 checksum to detect changes in zip file.
This commit is contained in:
@@ -90,14 +90,16 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
|
|||||||
String driveId = track.getDriveId();
|
String driveId = track.getDriveId();
|
||||||
if (driveId != null && !driveId.equals("")) {
|
if (driveId != null && !driveId.equals("")) {
|
||||||
File driveFile = drive.files().get(driveId).execute();
|
File driveFile = drive.files().get(driveId).execute();
|
||||||
if (SyncUtils.isValid(driveFile, folderId) && SyncUtils.updateDriveFile(
|
if (driveFile != null && updateDriveFile(drive, driveFile, folderId, track)) {
|
||||||
drive, driveFile, context, myTracksProviderUtils, track, false)) {
|
|
||||||
addPermission(drive, driveFile);
|
addPermission(drive, driveFile);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clear sync state
|
||||||
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// insert new drive file
|
||||||
File file = SyncUtils.insertDriveFile(
|
File file = SyncUtils.insertDriveFile(
|
||||||
drive, folderId, context, myTracksProviderUtils, track, false);
|
drive, folderId, context, myTracksProviderUtils, track, false);
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
@@ -119,6 +121,29 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
|
|||||||
return retryTask();
|
return retryTask();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates a drive file using info from a track. Returns true if successful.
|
||||||
|
*
|
||||||
|
* @param drive the drive
|
||||||
|
* @param driveFile the drive file
|
||||||
|
* @param folderId the folder id of the drive file
|
||||||
|
* @param track the track
|
||||||
|
*/
|
||||||
|
private boolean updateDriveFile(Drive drive, File driveFile, String folderId, Track track)
|
||||||
|
throws IOException {
|
||||||
|
if (!SyncUtils.isValid(driveFile, folderId)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
long driveModifiedTime = driveFile.getModifiedDate().getValue();
|
||||||
|
long modifiedTime = track.getModifiedTime();
|
||||||
|
|
||||||
|
if (driveModifiedTime != modifiedTime) {
|
||||||
|
return SyncUtils.updateDriveFile(
|
||||||
|
drive, driveFile, context, myTracksProviderUtils, track, true);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void invalidateToken() {}
|
protected void invalidateToken() {}
|
||||||
|
|||||||
@@ -47,13 +47,9 @@ import android.database.Cursor;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@@ -372,25 +368,14 @@ public class SyncUtils {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
file = SyncUtils.getTempFile(context, myTracksProviderUtils, track, true);
|
file = SyncUtils.getTempFile(context, myTracksProviderUtils, track, true);
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
Log.e(TAG, "Unable to update drive file. File is null for track " + track.getName());
|
Log.e(TAG, "Unable to update drive file. File is null for track " + track.getName());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String title = track.getName() + "." + KmzTrackExporter.KMZ_EXTENSION;
|
String title = track.getName() + "." + KmzTrackExporter.KMZ_EXTENSION;
|
||||||
File updatedFile;
|
File updatedFile = updateDriveFile(drive, driveFile, title, file, canRetry);
|
||||||
String digest = md5(file);
|
|
||||||
if (digest != null && digest.equals(driveFile.getMd5Checksum())) {
|
|
||||||
if (title.equals(driveFile.getTitle())) {
|
|
||||||
updatedFile = driveFile;
|
|
||||||
} else {
|
|
||||||
// Only update the title
|
|
||||||
updatedFile = updateDriveFile(drive, driveFile, title, null, canRetry);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
updatedFile = updateDriveFile(drive, driveFile, title, file, canRetry);
|
|
||||||
}
|
|
||||||
if (updatedFile == null) {
|
if (updatedFile == null) {
|
||||||
Log.e(
|
Log.e(
|
||||||
TAG, "Unable to update drive file. Updated file is null for track " + track.getName());
|
TAG, "Unable to update drive file. Updated file is null for track " + track.getName());
|
||||||
@@ -513,52 +498,4 @@ public class SyncUtils {
|
|||||||
: "");
|
: "");
|
||||||
myTracksProviderUtils.updateTrack(track);
|
myTracksProviderUtils.updateTrack(track);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the md5 digest for a file.
|
|
||||||
*
|
|
||||||
* @param file the file
|
|
||||||
*/
|
|
||||||
public static String md5(java.io.File file) {
|
|
||||||
if (file == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
InputStream in = null;
|
|
||||||
byte[] digest;
|
|
||||||
try {
|
|
||||||
in = new FileInputStream(file);
|
|
||||||
MessageDigest digester = MessageDigest.getInstance("MD5");
|
|
||||||
byte[] bytes = new byte[8192];
|
|
||||||
int byteCount;
|
|
||||||
while ((byteCount = in.read(bytes)) > 0) {
|
|
||||||
digester.update(bytes, 0, byteCount);
|
|
||||||
}
|
|
||||||
digest = digester.digest();
|
|
||||||
|
|
||||||
StringBuilder builder = new StringBuilder(digest.length * 2);
|
|
||||||
for (byte b : digest) {
|
|
||||||
if ((b & 0xFF) < 0x10) {
|
|
||||||
builder.append("0");
|
|
||||||
}
|
|
||||||
builder.append(Integer.toHexString(b & 0xFF));
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "IOException", e);
|
|
||||||
return null;
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
Log.e(TAG, "NoSuchAlgorithmException", e);
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
if (in != null) {
|
|
||||||
try {
|
|
||||||
in.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "Unable to close inputstream", e);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user