use of com.ichi2.libanki.sync.Syncer in project Anki-Android by Ramblurr.
the class Connection method doInBackgroundSync.
private Payload doInBackgroundSync(Payload data) {
// for for doInBackgroundLoadDeckCounts if any
DeckTask.waitToFinish();
String hkey = (String) data.data[0];
boolean media = (Boolean) data.data[1];
String conflictResolution = (String) data.data[2];
int mediaUsn = (Integer) data.data[3];
boolean colCorruptFullSync = false;
Collection col = AnkiDroidApp.getCol();
if (!AnkiDroidApp.colIsOpen()) {
if (conflictResolution != null && conflictResolution.equals("download")) {
colCorruptFullSync = true;
} else {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
}
String path = AnkiDroidApp.getCollectionPath();
BasicHttpSyncer server = new RemoteServer(this, hkey);
Syncer client = new Syncer(col, server);
// run sync and check state
boolean noChanges = false;
if (conflictResolution == null) {
Log.i(AnkiDroidApp.TAG, "Sync - starting sync");
publishProgress(R.string.sync_prepare_syncing);
Object[] ret = client.sync(this);
data.message = client.getSyncMsg();
mediaUsn = client.getmMediaUsn();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
String retCode = (String) ret[0];
if (!retCode.equals("noChanges") && !retCode.equals("success")) {
data.success = false;
data.result = ret;
// note mediaUSN for later
data.data = new Object[] { mediaUsn };
return data;
}
// save and note success state
if (retCode.equals("noChanges")) {
// publishProgress(R.string.sync_no_changes_message);
noChanges = true;
} else {
// publishProgress(R.string.sync_database_success);
}
} else {
try {
server = new FullSyncer(col, hkey, this);
if (conflictResolution.equals("upload")) {
Log.i(AnkiDroidApp.TAG, "Sync - fullsync - upload collection");
publishProgress(R.string.sync_preparing_full_sync_message);
Object[] ret = server.upload();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
AnkiDroidApp.openCollection(path);
return data;
}
if (!((String) ret[0]).equals(BasicHttpSyncer.ANKIWEB_STATUS_OK)) {
data.success = false;
data.result = ret;
AnkiDroidApp.openCollection(path);
return data;
}
} else if (conflictResolution.equals("download")) {
Log.i(AnkiDroidApp.TAG, "Sync - fullsync - download collection");
publishProgress(R.string.sync_downloading_message);
Object[] ret = server.download();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
AnkiDroidApp.openCollection(path);
return data;
}
if (!((String) ret[0]).equals("success")) {
data.success = false;
data.result = ret;
if (!colCorruptFullSync) {
AnkiDroidApp.openCollection(path);
}
return data;
}
}
col = AnkiDroidApp.openCollection(path);
} catch (OutOfMemoryError e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
data.success = false;
data.result = new Object[] { "OutOfMemoryError" };
data.data = new Object[] { mediaUsn };
return data;
} catch (RuntimeException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-fullSync");
data.success = false;
data.result = new Object[] { "IOException" };
data.data = new Object[] { mediaUsn };
return data;
}
}
// clear undo to avoid non syncing orphans (because undo resets usn too
if (!noChanges) {
col.clearUndo();
}
// then move on to media sync
boolean noMediaChanges = false;
String mediaError = null;
if (media) {
server = new RemoteMediaServer(hkey, this);
MediaSyncer mediaClient = new MediaSyncer(col, (RemoteMediaServer) server);
String ret;
try {
ret = mediaClient.sync(mediaUsn, this);
if (ret == null) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
} else {
if (ret.equals("noChanges")) {
publishProgress(R.string.sync_media_no_changes);
noMediaChanges = true;
}
if (ret.equals("sanityFailed")) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
} else {
publishProgress(R.string.sync_media_success);
}
}
} catch (RuntimeException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundSync-mediaSync");
mediaError = e.getLocalizedMessage();
}
}
if (noChanges && noMediaChanges) {
data.success = false;
data.result = new Object[] { "noChanges" };
return data;
} else {
data.success = true;
TreeSet<Object[]> decks = col.getSched().deckDueTree();
int[] counts = new int[] { 0, 0, 0 };
for (Object[] deck : decks) {
if (((String[]) deck[0]).length == 1) {
counts[0] += (Integer) deck[2];
counts[1] += (Integer) deck[3];
counts[2] += (Integer) deck[4];
}
}
Object[] dc = col.getSched().deckCounts();
data.result = dc[0];
data.data = new Object[] { conflictResolution, col, dc[1], dc[2], mediaError };
return data;
}
}
Aggregations