forked from upstream-mirrors/OpenTracks
Optimization, reduce drive api calls when files are moved to a new directory in Google Drive
This commit is contained in:
@@ -132,7 +132,7 @@ public class SendDriveAsyncTask extends AbstractSendAsyncTask {
|
|||||||
*/
|
*/
|
||||||
private boolean updateDriveFile(Drive drive, File driveFile, String folderId, Track track)
|
private boolean updateDriveFile(Drive drive, File driveFile, String folderId, Track track)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
if (!SyncUtils.isValid(driveFile, folderId)) {
|
if (!SyncUtils.isInMyTracksAndValid(driveFile, folderId)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
long driveModifiedTime = driveFile.getModifiedDate().getValue();
|
long driveModifiedTime = driveFile.getModifiedDate().getValue();
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ import java.io.InputStream;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -219,7 +220,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
if (driveId != null && !driveId.equals("")) {
|
if (driveId != null && !driveId.equals("")) {
|
||||||
if (!track.isSharedWithMe()) {
|
if (!track.isSharedWithMe()) {
|
||||||
File driveFile = drive.files().get(driveId).execute();
|
File driveFile = drive.files().get(driveId).execute();
|
||||||
if (SyncUtils.isValid(driveFile, folderId)) {
|
if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)) {
|
||||||
merge(track, driveFile);
|
merge(track, driveFile);
|
||||||
result.add(driveId);
|
result.add(driveId);
|
||||||
} else {
|
} else {
|
||||||
@@ -260,7 +261,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
context, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
|
context, R.string.drive_deleted_list_key, PreferencesUtils.DRIVE_DELETED_LIST_DEFAULT);
|
||||||
|
|
||||||
Map<String, File> changes = new HashMap<String, File>();
|
Map<String, File> changes = new HashMap<String, File>();
|
||||||
largestChangeId = getDriveChangesInfo(folderId, largestChangeId, changes);
|
largestChangeId = getDriveChangesInfo(largestChangeId, changes);
|
||||||
|
|
||||||
Cursor cursor = null;
|
Cursor cursor = null;
|
||||||
try {
|
try {
|
||||||
@@ -279,7 +280,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
Log.d(TAG, "Delete local track " + track.getName());
|
Log.d(TAG, "Delete local track " + track.getName());
|
||||||
myTracksProviderUtils.deleteTrack(track.getId());
|
myTracksProviderUtils.deleteTrack(track.getId());
|
||||||
} else {
|
} else {
|
||||||
merge(track, driveFile);
|
if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)
|
||||||
|
|| SyncUtils.isInSharedWithMe(driveFile)) {
|
||||||
|
merge(track, driveFile);
|
||||||
|
} else {
|
||||||
|
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
changes.remove(driveId);
|
changes.remove(driveId);
|
||||||
} else {
|
} else {
|
||||||
@@ -287,22 +293,26 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
|
|
||||||
// Handle the case the track has changed
|
// Handle the case the track has changed
|
||||||
File driveFile = drive.files().get(driveId).execute();
|
File driveFile = drive.files().get(driveId).execute();
|
||||||
if (SyncUtils.isValid(driveFile, folderId)) {
|
if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)) {
|
||||||
merge(track, driveFile);
|
merge(track, driveFile);
|
||||||
} else {
|
|
||||||
/*
|
|
||||||
* Track has a drive id, but the drive id is no longer valid.
|
|
||||||
* E.g., the file is moved to another folder. Clear the drive
|
|
||||||
* id.
|
|
||||||
*/
|
|
||||||
SyncUtils.updateTrack(myTracksProviderUtils, track, null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (cursor.moveToNext());
|
} while (cursor.moveToNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Insert new tracks from new drive files
|
// Insert valid new drive file changes as new tracks
|
||||||
|
Iterator<String> iterator = changes.keySet().iterator();
|
||||||
|
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
String driveId = iterator.next();
|
||||||
|
File file = changes.get(driveId);
|
||||||
|
if (!SyncUtils.isInMyTracksAndValid(file, folderId)
|
||||||
|
&& !SyncUtils.isInSharedWithMeAndValid(file)) {
|
||||||
|
iterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
insertNewTracks(changes.values());
|
insertNewTracks(changes.values());
|
||||||
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
|
PreferencesUtils.setLong(context, R.string.drive_largest_change_id_key, largestChangeId);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -381,12 +391,11 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
/**
|
/**
|
||||||
* Gets the drive changes info in the My Tracks folder, including deleted files.
|
* Gets the drive changes info in the My Tracks folder, including deleted files.
|
||||||
*
|
*
|
||||||
* @param folderId the folder id
|
|
||||||
* @param changeId the largest change id
|
* @param changeId the largest change id
|
||||||
* @param changes a map of drive id to file for the changes
|
* @param changes a map of drive id to file for the changes
|
||||||
* @return an updated largest change id
|
* @return an updated largest change id
|
||||||
*/
|
*/
|
||||||
private long getDriveChangesInfo(String folderId, long changeId, Map<String, File> changes)
|
private long getDriveChangesInfo(long changeId, Map<String, File> changes)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
Changes.List request = drive.changes().list().setStartChangeId(changeId + 1);
|
Changes.List request = drive.changes().list().setStartChangeId(changeId + 1);
|
||||||
do {
|
do {
|
||||||
@@ -398,12 +407,10 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
changes.put(change.getFileId(), null);
|
changes.put(change.getFileId(), null);
|
||||||
} else {
|
} else {
|
||||||
File file = change.getFile();
|
File file = change.getFile();
|
||||||
if (SyncUtils.isInFolder(file, folderId) || SyncUtils.isSharedWithMe(file)) {
|
if (file.getLabels().getTrashed()) {
|
||||||
if (file.getLabels().getTrashed()) {
|
changes.put(change.getFileId(), null);
|
||||||
changes.put(change.getFileId(), null);
|
} else {
|
||||||
} else {
|
changes.put(change.getFileId(), file);
|
||||||
changes.put(change.getFileId(), file);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -439,7 +446,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
+ driveFile.getTitle());
|
+ driveFile.getTitle());
|
||||||
if (!updateTrack(track.getId(), driveFile)) {
|
if (!updateTrack(track.getId(), driveFile)) {
|
||||||
Log.e(TAG, "Unable to update drive change");
|
Log.e(TAG, "Unable to update drive change");
|
||||||
// The track could have been deleted in the unsuccessful update attempt
|
// The track could have been deleted in the unsuccessful update
|
||||||
track = myTracksProviderUtils.getTrack(track.getId());
|
track = myTracksProviderUtils.getTrack(track.getId());
|
||||||
if (track != null) {
|
if (track != null) {
|
||||||
track.setModifiedTime(driveModifiedTime);
|
track.setModifiedTime(driveModifiedTime);
|
||||||
@@ -552,12 +559,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
throws UserRecoverableAuthIOException {
|
throws UserRecoverableAuthIOException {
|
||||||
try {
|
try {
|
||||||
File driveFile = drive.files().get(driveId).execute();
|
File driveFile = drive.files().get(driveId).execute();
|
||||||
if (SyncUtils.isInFolder(driveFile, folderId)) {
|
if (SyncUtils.isInMyTracks(driveFile, folderId)) {
|
||||||
if (!driveFile.getLabels().getTrashed()) {
|
if (!driveFile.getLabels().getTrashed()) {
|
||||||
drive.files().trash(driveId).execute();
|
drive.files().trash(driveId).execute();
|
||||||
}
|
}
|
||||||
// if trashed, ignore
|
// if trashed, ignore
|
||||||
} else if (SyncUtils.isSharedWithMe(driveFile)) {
|
} else if (SyncUtils.isInSharedWithMe(driveFile)) {
|
||||||
if (!driveFile.getLabels().getTrashed()) {
|
if (!driveFile.getLabels().getTrashed()) {
|
||||||
drive.files().delete(driveId).execute();
|
drive.files().delete(driveId).execute();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,40 +222,13 @@ public class SyncUtils {
|
|||||||
return drive.files().insert(file).execute();
|
return drive.files().insert(file).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if a drive file is a Shared with me KML or KMZ file.
|
|
||||||
*
|
|
||||||
* @param driveFile the drive file
|
|
||||||
*/
|
|
||||||
public static boolean isSharedWithMe(File driveFile) {
|
|
||||||
if (driveFile == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String mimeType = driveFile.getMimeType();
|
|
||||||
if (!SyncUtils.KML_MIME_TYPE.equals(mimeType) && !SyncUtils.KMZ_MIME_TYPE.equals(mimeType)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return driveFile.getSharedWithMeDate() != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if a drive file is a KML or KMZ file in the My Tracks folder
|
|
||||||
* and not trashed.
|
|
||||||
*
|
|
||||||
* @param driveFile the drive file
|
|
||||||
* @param folderId the My Tracks folder id
|
|
||||||
*/
|
|
||||||
public static boolean isValid(File driveFile, String folderId) {
|
|
||||||
return isInFolder(driveFile, folderId) && !driveFile.getLabels().getTrashed();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if a drive file is a KML or KMZ file in the My Tracks folder.
|
* Returns true if a drive file is a KML or KMZ file in the My Tracks folder.
|
||||||
*
|
*
|
||||||
* @param driveFile the drive file
|
* @param driveFile the drive file
|
||||||
* @param folderId the My Tracks folder id
|
* @param folderId the My Tracks folder id
|
||||||
*/
|
*/
|
||||||
public static boolean isInFolder(File driveFile, String folderId) {
|
public static boolean isInMyTracks(File driveFile, String folderId) {
|
||||||
if (driveFile == null) {
|
if (driveFile == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -275,6 +248,44 @@ public class SyncUtils {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if a drive file is a KML or KMZ file in the My Tracks folder
|
||||||
|
* and not trashed.
|
||||||
|
*
|
||||||
|
* @param driveFile the drive file
|
||||||
|
* @param folderId the My Tracks folder id
|
||||||
|
*/
|
||||||
|
public static boolean isInMyTracksAndValid(File driveFile, String folderId) {
|
||||||
|
return isInMyTracks(driveFile, folderId) && !driveFile.getLabels().getTrashed();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if a drive file is a KML or KMZ file in the Shared with me
|
||||||
|
* directory.
|
||||||
|
*
|
||||||
|
* @param driveFile the drive file
|
||||||
|
*/
|
||||||
|
public static boolean isInSharedWithMe(File driveFile) {
|
||||||
|
if (driveFile == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String mimeType = driveFile.getMimeType();
|
||||||
|
if (!SyncUtils.KML_MIME_TYPE.equals(mimeType) && !SyncUtils.KMZ_MIME_TYPE.equals(mimeType)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return driveFile.getSharedWithMeDate() != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if a drive file is a KML or KMZ file in the Shared with me
|
||||||
|
* directory and is not trashed.
|
||||||
|
*
|
||||||
|
* @param driveFile the drive file
|
||||||
|
*/
|
||||||
|
public static boolean isInSharedWithMeAndValid(File driveFile) {
|
||||||
|
return isInSharedWithMe(driveFile) && !driveFile.getLabels().getTrashed();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts a drive file using info from a track.
|
* Inserts a drive file using info from a track.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user