forked from upstream-mirrors/OpenTracks
Handle the case that sync is active while deleting all tracks
This commit is contained in:
@@ -76,6 +76,16 @@ public class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
MyTracksProviderUtils myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
||||||
|
|
||||||
if (trackIds.length == 1 && trackIds[0] == -1L) {
|
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(
|
PreferencesUtils.setBoolean(
|
||||||
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
|
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
|
||||||
SyncUtils.disableSync(context);
|
SyncUtils.disableSync(context);
|
||||||
@@ -84,9 +94,10 @@ public class DeleteAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
for (long id : trackIds) {
|
for (long id : trackIds) {
|
||||||
if (!isCancelled()) {
|
if (isCancelled()) {
|
||||||
myTracksProviderUtils.deleteTrack(id);
|
return false;
|
||||||
}
|
}
|
||||||
|
myTracksProviderUtils.deleteTrack(id);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -353,8 +353,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
long[] trackIds = kmlImporter.importFile(inputStream);
|
long[] trackIds = kmlImporter.importFile(inputStream);
|
||||||
if (trackIds.length == 1) {
|
if (trackIds.length == 1) {
|
||||||
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
|
Track track = myTracksProviderUtils.getTrack(trackIds[0]);
|
||||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, track, driveFile);
|
if (track == null) {
|
||||||
Log.d(TAG, "Add from Google Drive " + track.getName());
|
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 {
|
} else {
|
||||||
// Clean up if imported more than one track
|
// Clean up if imported more than one track
|
||||||
for (int i = 0; i < trackIds.length; i++) {
|
for (int i = 0; i < trackIds.length; i++) {
|
||||||
@@ -495,8 +499,12 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
long[] trackIds = kmlImporter.importFile(inputStream);
|
long[] trackIds = kmlImporter.importFile(inputStream);
|
||||||
if (trackIds.length == 1) {
|
if (trackIds.length == 1) {
|
||||||
Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]);
|
Track newTrack = myTracksProviderUtils.getTrack(trackIds[0]);
|
||||||
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, newTrack, driveFile);
|
if (newTrack == null) {
|
||||||
return true;
|
Log.e(TAG, "Unable to merge, imported track is null");
|
||||||
|
} else {
|
||||||
|
SyncUtils.updateTrackWithDriveFileInfo(myTracksProviderUtils, newTrack, driveFile);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "Unable to merge, imported size is not 1");
|
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.
|
* Enables sync.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user