Bug fix, do not cache folder id across sync requests to handle the case of switching account

This commit is contained in:
Jimmy Shih
2013-12-05 17:33:48 -08:00
parent 07edfde08d
commit 61bc8f5bbe
@@ -79,7 +79,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
private Drive drive; private Drive drive;
private String driveAccountName; // the account name associated with the drive private String driveAccountName; // the account name associated with the drive
private String folderId; private String folderId;
public SyncAdapter(Context context) { public SyncAdapter(Context context) {
super(context, true); super(context, true);
this.context = context; this.context = context;
@@ -119,6 +119,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
drive = SyncUtils.getDriveService(credential); drive = SyncUtils.getDriveService(credential);
driveAccountName = account.name; driveAccountName = account.name;
} }
folderId = getFolderId();
long largestChangeId = PreferencesUtils.getLong( long largestChangeId = PreferencesUtils.getLong(
context, R.string.drive_largest_change_id_key); context, R.string.drive_largest_change_id_key);
@@ -145,17 +146,15 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
* Gets the folder id.. * Gets the folder id..
*/ */
private String getFolderId() throws IOException { private String getFolderId() throws IOException {
if (folderId == null) { File folder = SyncUtils.getMyTracksFolder(context, drive);
File folder = SyncUtils.getMyTracksFolder(context, drive); if (folder == null) {
if (folder == null) { throw new IOException("folder is null");
throw new IOException("folder is null");
}
folderId = folder.getId();
if (folderId == null) {
throw new IOException("folder id is null");
}
} }
return folderId; String id = folder.getId();
if (id == null) {
throw new IOException("folder id is null");
}
return id;
} }
/** /**
@@ -168,8 +167,8 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
long largestChangeId = about.getLargestChangeId(); long largestChangeId = about.getLargestChangeId();
// Get all the KML/KMZ files in the "My Drive:/My Tracks" folder // Get all the KML/KMZ files in the "My Drive:/My Tracks" folder
Files.List myTracksFolderRequest = drive.files().list() Files.List myTracksFolderRequest = drive.files()
.setQ(String.format(Locale.US, SyncUtils.MY_TRACKS_FOLDER_FILES_QUERY, getFolderId())); .list().setQ(String.format(Locale.US, SyncUtils.MY_TRACKS_FOLDER_FILES_QUERY, folderId));
Map<String, File> myTracksFolderMap = getFiles(myTracksFolderRequest, true); Map<String, File> myTracksFolderMap = getFiles(myTracksFolderRequest, true);
// Handle tracks that are already uploaded to Google Drive // Handle tracks that are already uploaded to Google Drive
@@ -227,7 +226,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.isInMyTracksAndValid(driveFile, getFolderId())) { if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)) {
merge(track, driveFile); merge(track, driveFile);
result.add(driveId); result.add(driveId);
} else { } else {
@@ -287,14 +286,14 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
continue; continue;
} }
File driveFile = drive.files().get(driveId).execute(); File driveFile = drive.files().get(driveId).execute();
if (SyncUtils.isInMyTracksAndValid(driveFile, getFolderId())) { if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)) {
merge(track, driveFile); merge(track, driveFile);
} }
} }
PreferencesUtils.setString( PreferencesUtils.setString(
context, R.string.drive_edited_list_key, PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT); context, R.string.drive_edited_list_key, PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT);
} }
// Handle changes from Google Drive // Handle changes from Google Drive
Map<String, File> changes = new HashMap<String, File>(); Map<String, File> changes = new HashMap<String, File>();
long newLargestChangeId = getDriveChangesInfo(largestChangeId, changes); long newLargestChangeId = getDriveChangesInfo(largestChangeId, changes);
@@ -316,7 +315,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
Log.d(TAG, "Delete local track " + track.getName()); Log.d(TAG, "Delete local track " + track.getName());
myTracksProviderUtils.deleteTrack(context, track.getId()); myTracksProviderUtils.deleteTrack(context, track.getId());
} else { } else {
if (SyncUtils.isInMyTracksAndValid(driveFile, getFolderId()) if (SyncUtils.isInMyTracksAndValid(driveFile, folderId)
|| SyncUtils.isInSharedWithMe(driveFile)) { || SyncUtils.isInSharedWithMe(driveFile)) {
merge(track, driveFile); merge(track, driveFile);
} else { } else {
@@ -334,7 +333,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
while (iterator.hasNext()) { while (iterator.hasNext()) {
String driveId = iterator.next(); String driveId = iterator.next();
File file = changes.get(driveId); File file = changes.get(driveId);
if (!SyncUtils.isInMyTracksAndValid(file, getFolderId()) if (!SyncUtils.isInMyTracksAndValid(file, folderId)
&& !SyncUtils.isInSharedWithMeAndValid(file)) { && !SyncUtils.isInSharedWithMeAndValid(file)) {
iterator.remove(); iterator.remove();
} }
@@ -366,8 +365,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
continue; continue;
} }
// If not successful, the next sync will retry again // If not successful, the next sync will retry again
SyncUtils.insertDriveFile( SyncUtils.insertDriveFile(drive, folderId, context, myTracksProviderUtils, track, true);
drive, getFolderId(), context, myTracksProviderUtils, track, true);
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
} finally { } finally {
@@ -416,14 +414,14 @@ 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 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(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 {
ChangeList changeList = request.execute(); ChangeList changeList = request.execute();
@@ -499,7 +497,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
} }
File updatedDriveFile; File updatedDriveFile;
String trackName = FileUtils.getName(driveFile.getTitle()); String trackName = FileUtils.getName(driveFile.getTitle());
if (SyncUtils.isInMyTracks(driveFile, getFolderId()) && !track.getName().equals(trackName)) { if (SyncUtils.isInMyTracks(driveFile, folderId) && !track.getName().equals(trackName)) {
track.setName(trackName); track.setName(trackName);
/* /*
@@ -558,7 +556,7 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
if (useKmz) { if (useKmz) {
if (trackId == -1L) { if (trackId == -1L) {
Uri uri = myTracksProviderUtils.insertTrack(new Track()); Uri uri = myTracksProviderUtils.insertTrack(new Track());
trackId = Long.parseLong(uri.getLastPathSegment()); trackId = Long.parseLong(uri.getLastPathSegment());
} }
trackImporter = new KmzTrackImporter(context, trackId); trackImporter = new KmzTrackImporter(context, trackId);
} else { } else {
@@ -592,13 +590,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
* *
* @param driveId the drive id * @param driveId the drive id
* @param canRetry true if can retry the request * @param canRetry true if can retry the request
* @throws IOException * @throws IOException
*/ */
private void deleteDriveFile(String driveId, boolean canRetry) private void deleteDriveFile(String driveId, boolean canRetry) throws IOException {
throws IOException {
try { try {
File driveFile = drive.files().get(driveId).execute(); File driveFile = drive.files().get(driveId).execute();
if (SyncUtils.isInMyTracks(driveFile, getFolderId())) { if (SyncUtils.isInMyTracks(driveFile, folderId)) {
if (!driveFile.getLabels().getTrashed()) { if (!driveFile.getLabels().getTrashed()) {
drive.files().trash(driveId).execute(); drive.files().trash(driveId).execute();
} }