Handle the case that sync is active while deleting all tracks

This commit is contained in:
Jimmy Shih
2013-07-12 14:26:09 -07:00
parent a6e9412a69
commit 64018fb02c
3 changed files with 40 additions and 6 deletions
@@ -76,6 +76,16 @@ public class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
if (trackIds.length == 1 && trackIds[0] == -1L) {
try {
while (SyncUtils.isSyncActive(context)) {
if (isCancelled()) {
return false;
}
Thread.sleep(1000);
}
} catch (InterruptedException e) {
return false;
}
PreferencesUtils.setBoolean(
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
SyncUtils.disableSync(context);
@@ -84,9 +94,10 @@ public class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
return true;
} else {
for (long id : trackIds) {
if (!isCancelled()) {
myTracksProviderUtils.deleteTrack(id);
if (isCancelled()) {
return false;
}
myTracksProviderUtils.deleteTrack(id);
}
return true;
}
@@ -353,8 +353,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
long[] trackIds = kmlImporter.importFile(inputStream);
if (trackIds.length == 1) {
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
Log.d(TAG, "Add from Google Drive " + track.getName());
if (track == null) {
Log.e(TAG, "Unable to insert new drive file for " + driveFile.getId());
} else {
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
Log.d(TAG, "Add from Google Drive " + track.getName());
}
} else {
// Clean up if imported more than one track
for (int i = 0; i < trackIds.length; i++) {
@@ -495,8 +499,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
long[] trackIds = kmlImporter.importFile(inputStream);
if (trackIds.length == 1) {
Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]);
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, newTrack, driveFile);
return true;
if (newTrack == null) {
Log.e(TAG, "Unable to merge, imported track is null");
} else {
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, newTrack, driveFile);
return true;
}
} else {
Log.e(TAG, "Unable to merge, imported size is not 1");
}
@@ -127,6 +127,21 @@ public class SyncUtils {
}
}
/**
* Returns true if sync is active.
*
* @param context the context
*/
public static boolean isSyncActive(Context context) {
Account[] accounts = AccountManager.get(context).getAccountsByType(Constants.ACCOUNT_TYPE);
for (Account account : accounts) {
if (ContentResolver.isSyncActive(account, SYNC_AUTHORITY)) {
return true;
}
}
return false;
}
/**
* Enables sync.
*