use of com.ichi2.anki.exception.MediaSyncException in project AnkiChinaAndroid by ankichinateam.
the class Connection method doInBackgroundSync.
private Payload doInBackgroundSync(Payload data) {
sIsCancellable = true;
Timber.d("doInBackgroundSync()");
// Block execution until any previous background task finishes, or timeout after 5s
boolean ok = CollectionTask.waitToFinish(5);
String hkey = (String) data.data[0];
boolean media = (Boolean) data.data[1];
String conflictResolution = (String) data.data[2];
HostNum hostNum = (HostNum) data.data[3];
long restServerSpace = (long) data.data[4];
// Use safe version that catches exceptions so that full sync is still possible
Collection col = CollectionHelper.getInstance().getColSafe(AnkiDroidApp.getInstance());
boolean colCorruptFullSync = false;
if (!CollectionHelper.getInstance().colIsOpen() || !ok) {
if (conflictResolution != null && "download".equals(conflictResolution)) {
colCorruptFullSync = true;
} else {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
}
try {
CollectionHelper.getInstance().lockCollection();
HttpSyncer server = new RemoteServer(this, hkey, hostNum);
Syncer client = new Syncer(col, server, hostNum);
// run sync and check state
boolean noChanges = false;
if (conflictResolution == null) {
Timber.i("Sync - starting sync");
publishProgress(R.string.sync_prepare_syncing);
Object[] ret = client.sync(this, restServerSpace);
data.message = client.getSyncMsg();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
String retCode = (String) ret[0];
if (!"noChanges".equals(retCode) && !"success".equals(retCode)) {
data.success = false;
data.result = ret;
// Check if there was a sanity check error
if ("sanityCheckError".equals(retCode)) {
// Force full sync next time
col.modSchemaNoCheck();
col.save();
}
return data;
}
// save and note success state
if ("noChanges".equals(retCode)) {
// publishProgress(R.string.sync_no_changes_message);
noChanges = true;
}
restServerSpace = client.getRestSpace();
} else {
try {
// Disable sync cancellation for full-sync
sIsCancellable = false;
server = new FullSyncer(col, hkey, this, hostNum);
if ("upload".equals(conflictResolution)) {
Timber.i("Sync - fullsync - upload collection");
publishProgress(R.string.sync_preparing_full_sync_message);
Object[] ret = server.upload(restServerSpace);
col.reopen();
if (ret == null) {
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
if (!ret[0].equals(HttpSyncer.ANKIWEB_STATUS_OK)) {
data.success = false;
data.result = ret;
return data;
}
} else if ("download".equals(conflictResolution)) {
Timber.i("Sync - fullsync - download collection");
publishProgress(R.string.sync_downloading_message);
Object[] ret = server.download();
if (ret == null) {
Timber.w("Sync - fullsync - unknown error");
data.success = false;
data.result = new Object[] { "genericError" };
return data;
}
if ("success".equals(ret[0])) {
data.success = true;
col.reopen();
}
if (!"success".equals(ret[0])) {
Timber.w("Sync - fullsync - download failed");
data.success = false;
data.result = ret;
if (!colCorruptFullSync) {
col.reopen();
}
return data;
}
}
} catch (OutOfMemoryError e) {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
data.success = false;
data.result = new Object[] { "OutOfMemoryError" };
return data;
} catch (RuntimeException e) {
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
} else {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
data.result = new Object[] { "IOException", e };
}
data.success = false;
return data;
}
}
// clear undo to avoid non syncing orphans (because undo resets usn too
if (!noChanges) {
col.clearUndo();
}
// then move on to media sync
sIsCancellable = true;
boolean noMediaChanges = false;
boolean enoughServerSpace = true;
String mediaError = null;
if (media) {
server = new RemoteMediaServer(col, hkey, this, hostNum);
MediaSyncer mediaClient = new MediaSyncer(col, (RemoteMediaServer) server, this);
String ret;
try {
// ������ܻ���Ϊ�ϴ��ļ������ռ��С���׳��쳣���ڴ�֮ǰ��Ҫ����ʣ��ռ��С
ret = mediaClient.sync(restServerSpace);
Timber.e("sync media ret is null");
if (ret == null) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
} else {
if ("corruptMediaDB".equals(ret)) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_db_error);
noMediaChanges = true;
}
if ("noChanges".equals(ret)) {
publishProgress(R.string.sync_media_no_changes);
noMediaChanges = true;
}
if ("sanityFailed".equals(ret)) {
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
} else {
publishProgress(R.string.sync_media_success);
}
}
} catch (RuntimeException e) {
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
}
mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error) + "\n\n" + e.getLocalizedMessage();
}
}
if (noChanges && (!media || noMediaChanges)) {
data.success = false;
data.result = new Object[] { "noChanges" };
return data;
} else {
data.success = true;
data.data = new Object[] { conflictResolution, col, mediaError };
return data;
}
} catch (MediaSyncException e) {
Timber.e("Media sync rejected by server");
data.success = false;
data.result = new Object[] { "mediaSyncServerError", e };
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
return data;
} catch (UnknownHttpResponseException e) {
Timber.e(e, "doInBackgroundSync -- unknown response code error");
data.success = false;
int code = e.getResponseCode();
String msg = e.getLocalizedMessage();
data.result = new Object[] { "error", code, msg };
return data;
} catch (NoEnoughServerSpaceException e) {
Timber.e("NoEnoughServerSpaceException ");
e.printStackTrace();
data.success = false;
data.result = new Object[] { "noServerSpace", e.rest, e.need };
return data;
} catch (Exception e) {
// Global error catcher.
// Try to give a human readable error, otherwise print the raw error message
Timber.e(e, "doInBackgroundSync error");
data.success = false;
if (timeoutOccurred(e)) {
data.result = new Object[] { "connectionError", e };
} else if ("UserAbortedSync".equals(e.getMessage())) {
data.result = new Object[] { "UserAbortedSync", e };
} else {
AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
data.result = new Object[] { e.getLocalizedMessage(), e };
}
return data;
} finally {
Timber.i("Sync Finished - Closing Collection");
// don't bump mod time unless we explicitly save
if (col != null) {
col.close(false);
}
CollectionHelper.getInstance().unlockCollection();
}
}
use of com.ichi2.anki.exception.MediaSyncException in project AnkiChinaAndroid by ankichinateam.
the class RemoteMediaServer method mediaChanges.
// args: lastUsn
public JSONArray mediaChanges(int lastUsn) throws UnknownHttpResponseException, MediaSyncException {
try {
mPostVars = new HashMap<>();
mPostVars.put("sk", mSKey);
Response resp = super.req("mediaChanges", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("lastUsn", lastUsn))));
JSONObject jresp = new JSONObject(resp.body().string());
return _dataOnly(jresp, JSONArray.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.anki.exception.MediaSyncException in project AnkiChinaAndroid by ankichinateam.
the class RemoteMediaServer method begin.
public JSONObject begin() throws UnknownHttpResponseException, MediaSyncException {
try {
mPostVars = new HashMap<>();
mPostVars.put("k", mHKey);
mPostVars.put("v", String.format(Locale.US, "anki,%s,%s", VersionUtils.getPkgVersionNameFake(), Utils.platDesc()));
Response resp = super.req("begin", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject())));
JSONObject jresp = new JSONObject(resp.body().string());
JSONObject ret = _dataOnly(jresp, JSONObject.class);
mSKey = ret.getString("sk");
return ret;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.anki.exception.MediaSyncException in project Anki-Android by ankidroid.
the class RemoteMediaServer method uploadChanges.
public JSONArray uploadChanges(File zip) throws UnknownHttpResponseException, MediaSyncException {
try {
// no compression, as we compress the zip file instead
Response resp = super.req("uploadChanges", new FileInputStream(zip), 0);
JSONObject jresp = new JSONObject(resp.body().string());
return _dataOnly(jresp, JSONArray.class);
} catch (IOException | NullPointerException e) {
throw new RuntimeException(e);
}
}
use of com.ichi2.anki.exception.MediaSyncException in project Anki-Android by ankidroid.
the class RemoteMediaServer method mediaSanity.
// args: local
public String mediaSanity(int lcnt) throws UnknownHttpResponseException, MediaSyncException {
try {
Response resp = super.req("mediaSanity", HttpSyncer.getInputStream(Utils.jsonToString(new JSONObject().put("local", lcnt))));
JSONObject jresp = new JSONObject(resp.body().string());
return _dataOnly(jresp, String.class);
} catch (IOException | NullPointerException e) {
throw new RuntimeException(e);
}
}
Aggregations