Search in sources :

Example 6 with MediaSyncException

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();
    }
}
Also used : HostNum(com.ichi2.libanki.sync.HostNum) FullSyncer(com.ichi2.libanki.sync.FullSyncer) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) JSONException(com.ichi2.utils.JSONException) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) IOException(java.io.IOException) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) FullSyncer(com.ichi2.libanki.sync.FullSyncer) Syncer(com.ichi2.libanki.sync.Syncer) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) MediaSyncException(com.ichi2.anki.exception.MediaSyncException) Collection(com.ichi2.libanki.Collection) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) JSONObject(com.ichi2.utils.JSONObject) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) RemoteServer(com.ichi2.libanki.sync.RemoteServer) RemoteMediaServer(com.ichi2.libanki.sync.RemoteMediaServer) NoEnoughServerSpaceException(com.ichi2.anki.exception.NoEnoughServerSpaceException)

Example 7 with MediaSyncException

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);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException)

Example 8 with MediaSyncException

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);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException)

Example 9 with MediaSyncException

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);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 10 with MediaSyncException

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);
    }
}
Also used : Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) IOException(java.io.IOException)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)11 IOException (java.io.IOException)10 Response (okhttp3.Response)8 MediaSyncException (com.ichi2.anki.exception.MediaSyncException)2 NoEnoughServerSpaceException (com.ichi2.anki.exception.NoEnoughServerSpaceException)2 UnknownHttpResponseException (com.ichi2.anki.exception.UnknownHttpResponseException)2 Collection (com.ichi2.libanki.Collection)2 FullSyncer (com.ichi2.libanki.sync.FullSyncer)2 HostNum (com.ichi2.libanki.sync.HostNum)2 HttpSyncer (com.ichi2.libanki.sync.HttpSyncer)2 MediaSyncer (com.ichi2.libanki.sync.MediaSyncer)2 RemoteMediaServer (com.ichi2.libanki.sync.RemoteMediaServer)2 RemoteServer (com.ichi2.libanki.sync.RemoteServer)2 Syncer (com.ichi2.libanki.sync.Syncer)2 JSONException (com.ichi2.utils.JSONException)2 FileInputStream (java.io.FileInputStream)2 SQLException (android.database.SQLException)1 Pair (android.util.Pair)1 ConflictResolution (com.ichi2.async.Connection.ConflictResolution)1 CustomSyncServerUrlException (com.ichi2.libanki.sync.CustomSyncServerUrlException)1