Search in sources :

Example 1 with HostNum

use of com.ichi2.libanki.sync.HostNum in project AnkiChinaAndroid by ankichinateam.

the class Connection method doInBackgroundLogin.

private Payload doInBackgroundLogin(Payload data) {
    String username = (String) data.data[0];
    String password = (String) data.data[1];
    HostNum hostNum = (HostNum) data.data[2];
    HttpSyncer server = new RemoteServer(this, null, hostNum);
    Response ret;
    try {
        ret = server.hostKey(username, password);
    } catch (UnknownHttpResponseException e) {
        data.success = false;
        data.result = new Object[] { "error", e.getResponseCode(), e.getMessage() };
        return data;
    } catch (Exception e2) {
        // Ask user to report all bugs which aren't timeout errors
        if (!timeoutOccurred(e2)) {
            AnkiDroidApp.sendExceptionReport(e2, "doInBackgroundLogin");
        }
        data.success = false;
        data.result = new Object[] { "connectionError", e2 };
        return data;
    }
    String hostkey = null;
    boolean valid = false;
    if (ret != null) {
        data.returnType = ret.code();
        Timber.d("doInBackgroundLogin - response from server: %d, (%s)", data.returnType, ret.message());
        if (data.returnType == 200) {
            try {
                JSONObject response = new JSONObject(ret.body().string());
                hostkey = response.getString("key");
                valid = (hostkey != null) && (hostkey.length() > 0);
            } catch (JSONException e) {
                valid = false;
            } catch (IllegalStateException | IOException | NullPointerException e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        Timber.e("doInBackgroundLogin - empty response from server");
    }
    if (valid) {
        data.success = true;
        data.data = new String[] { username, hostkey };
    } else {
        data.success = false;
    }
    return data;
}
Also used : JSONException(com.ichi2.utils.JSONException) HostNum(com.ichi2.libanki.sync.HostNum) IOException(java.io.IOException) 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) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) JSONObject(com.ichi2.utils.JSONObject) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

Example 2 with HostNum

use of com.ichi2.libanki.sync.HostNum 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 3 with HostNum

use of com.ichi2.libanki.sync.HostNum in project AnkiChinaAndroid by ankichinateam.

the class Connection method doInBackgroundCommonRequest.

private Payload doInBackgroundCommonRequest(Payload data) {
    HostNum hostNum = data.hostNum;
    HttpSyncer server = new RemoteServer(this, null, hostNum);
    Response ret;
    try {
        ret = data.RESTType == Payload.REST_TYPE_POST ? server.sendCommonPost(data.method, data.param, data.token) : data.RESTType == Payload.REST_TYPE_GET ? server.sendCommonGet(data.method, data.param, data.token) : server.sendCommonPUT(data.method, data.param, data.token);
    } catch (UnknownHttpResponseException e) {
        data.success = false;
        data.result = new Object[] { "error", e.getResponseCode(), e.getMessage() };
        return data;
    } catch (Exception e2) {
        // Ask user to report all bugs which aren't timeout errors
        if (!timeoutOccurred(e2)) {
            AnkiDroidApp.sendExceptionReport(e2, "doInBackgroundCommonRequest");
        }
        data.success = false;
        data.result = new Object[] { "connectionError" };
        return data;
    }
    int status_code = -1;
    String message = null;
    JSONObject bodyData = null;
    boolean valid = false;
    if (ret != null) {
        data.returnType = ret.code();
        Timber.d("doInBackgroundCommonRequest - response from server: %d, (%s)", data.returnType, ret.message());
        if (data.returnType == 200) {
            try {
                String bodyStr = ret.body().string();
                Timber.d("response body:%s", bodyStr);
                JSONObject jo = (new JSONObject(bodyStr));
                status_code = jo.getInt("status_code");
                message = jo.getString("message");
                bodyData = jo;
                valid = status_code == 0;
            } catch (JSONException e) {
                valid = false;
            } catch (IllegalStateException | IOException | NullPointerException e) {
                // throw new RuntimeException(e);
                e.printStackTrace();
            }
        }
    } else {
        Timber.e("doInBackgroundCommonRequest - empty response from server");
    }
    data.success = valid;
    data.message = message;
    data.statusCode = status_code;
    data.result = bodyData;
    return data;
}
Also used : JSONException(com.ichi2.utils.JSONException) HostNum(com.ichi2.libanki.sync.HostNum) IOException(java.io.IOException) 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) SuppressLint(android.annotation.SuppressLint) Response(okhttp3.Response) JSONObject(com.ichi2.utils.JSONObject) JSONObject(com.ichi2.utils.JSONObject) HttpSyncer(com.ichi2.libanki.sync.HttpSyncer) RemoteServer(com.ichi2.libanki.sync.RemoteServer)

Example 4 with HostNum

use of com.ichi2.libanki.sync.HostNum in project AnkiChinaAndroid by ankichinateam.

the class HttpTest method testLogin.

@Test
public void testLogin() {
    String username = "AnkiDroidInstrumentedTestUser";
    String password = "AnkiDroidInstrumentedTestInvalidPass";
    Connection.Payload invalidPayload = new Connection.Payload(new Object[] { username, password, new HostNum(null) });
    TestTaskListener testListener = new TestTaskListener(invalidPayload);
    // We have to carefully run things on the main thread here or the threading protections in BaseAsyncTask throw
    // The first one is just to run the static initializer, really
    Runnable onlineRunnable = () -> {
        try {
            Class.forName("com.ichi2.async.Connection");
        } catch (Exception e) {
            Assert.fail("Unable to load Connection class: " + e.getMessage());
        }
    };
    InstrumentationRegistry.getInstrumentation().runOnMainSync(onlineRunnable);
    // TODO simulate offline programmatically - currently exercised by manually toggling an emulator offline pre-test
    if (!Connection.isOnline()) {
        Connection.login(testListener, invalidPayload);
        Assert.assertFalse("Successful login despite being offline", testListener.getPayload().success);
        Assert.assertTrue("onDisconnected not called despite being offline", testListener.mDisconnectedCalled);
        return;
    }
    Runnable r = () -> {
        Connection conn = Connection.login(testListener, invalidPayload);
        try {
            // This forces us to synchronously wait for the AsyncTask to do it's work
            conn.get();
        } catch (Exception e) {
            Assert.fail("Caught exception while trying to login: " + e.getMessage());
        }
    };
    InstrumentationRegistry.getInstrumentation().runOnMainSync(r);
    Assert.assertFalse("Successful login despite invalid credentials", testListener.getPayload().success);
}
Also used : Connection(com.ichi2.async.Connection) HostNum(com.ichi2.libanki.sync.HostNum) Test(org.junit.Test)

Example 5 with HostNum

use of com.ichi2.libanki.sync.HostNum in project Anki-Android by ankidroid.

the class Connection method doInBackgroundSync.

/**
 * In the payload, success means that the sync did occur correctly and that a change did occur.
 * So success can be false without error, if no change occurred at all.
 */
private Payload doInBackgroundSync(Payload data) {
    sIsCancellable = true;
    Timber.d("doInBackgroundSync()");
    // Block execution until any previous background task finishes, or timeout after 5s
    boolean ok = TaskManager.waitToFinish(5);
    // Unique key allowing to identify the user to AnkiWeb without password
    String hkey = (String) data.data[0];
    // Whether media should be synced too
    boolean media = (Boolean) data.data[1];
    // If normal sync can't occur, what to do
    ConflictResolution conflictResolution = (ConflictResolution) data.data[2];
    // A number AnkiWeb told us to send back. Probably to choose the best server for the user
    HostNum hostNum = (HostNum) data.data[3];
    // 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 (FULL_DOWNLOAD == conflictResolution) {
            colCorruptFullSync = true;
        } else {
            return returnGenericError(data);
        }
    }
    try {
        CollectionHelper.getInstance().lockCollection();
        RemoteServer remoteServer = new RemoteServer(this, hkey, hostNum);
        Syncer client = new Syncer(col, remoteServer, hostNum);
        // run sync and check state
        boolean noChanges = false;
        if (conflictResolution == null) {
            Timber.i("Sync - starting sync");
            publishProgress(R.string.sync_prepare_syncing);
            Pair<ConnectionResultType, Object> ret = client.sync(this);
            data.message = client.getSyncMsg();
            if (ret == null) {
                return returnGenericError(data);
            }
            if (NO_CHANGES != ret.first && SUCCESS != ret.first) {
                data.success = false;
                data.resultType = ret.first;
                data.result = new Object[] { ret.second };
                // Check if there was a sanity check error
                if (SANITY_CHECK_ERROR == ret.first) {
                    // Force full sync next time
                    col.modSchemaNoCheck();
                    col.save();
                }
                return data;
            }
            // save and note success state
            if (NO_CHANGES == ret.first) {
                // publishProgress(R.string.sync_no_changes_message);
                noChanges = true;
            }
        } else {
            try {
                // Disable sync cancellation for full-sync
                sIsCancellable = false;
                FullSyncer fullSyncServer = new FullSyncer(col, hkey, this, hostNum);
                switch(conflictResolution) {
                    case FULL_UPLOAD:
                        {
                            Timber.i("Sync - fullsync - upload collection");
                            publishProgress(R.string.sync_preparing_full_sync_message);
                            Pair<ConnectionResultType, Object[]> ret = fullSyncServer.upload();
                            col.reopen();
                            if (ret == null) {
                                return returnGenericError(data);
                            }
                            if (ret.first == ARBITRARY_STRING && !ret.second[0].equals(HttpSyncer.ANKIWEB_STATUS_OK)) {
                                data.success = false;
                                data.resultType = ret.first;
                                data.result = ret.second;
                                return data;
                            }
                            break;
                        }
                    case FULL_DOWNLOAD:
                        {
                            Timber.i("Sync - fullsync - download collection");
                            publishProgress(R.string.sync_downloading_message);
                            ConnectionResultType ret = fullSyncServer.download();
                            if (ret == null) {
                                Timber.w("Sync - fullsync - unknown error");
                                return returnGenericError(data);
                            }
                            if (SUCCESS == ret) {
                                data.success = true;
                                col.reopen();
                            }
                            if (SUCCESS != ret) {
                                Timber.w("Sync - fullsync - download failed");
                                data.success = false;
                                data.resultType = ret;
                                if (!colCorruptFullSync) {
                                    col.reopen();
                                }
                                return data;
                            }
                            break;
                        }
                    default:
                }
            } catch (OutOfMemoryError e) {
                Timber.w(e);
                AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
                data.success = false;
                data.resultType = OUT_OF_MEMORY_ERROR;
                data.result = new Object[0];
                return data;
            } catch (RuntimeException e) {
                Timber.w(e);
                if (timeoutOccurred(e)) {
                    data.resultType = CONNECTION_ERROR;
                } else if (USER_ABORTED_SYNC.toString().equals(e.getMessage())) {
                    data.resultType = USER_ABORTED_SYNC;
                } else {
                    AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync-fullSync");
                    data.resultType = IO_EXCEPTION;
                }
                data.result = new Object[] { 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;
        String mediaError = null;
        if (media) {
            RemoteMediaServer mediaServer = new RemoteMediaServer(col, hkey, this, hostNum);
            MediaSyncer mediaClient = new MediaSyncer(col, mediaServer, this);
            Pair<ConnectionResultType, String> ret;
            try {
                Timber.i("Sync - Performing media sync");
                ret = mediaClient.sync();
                if (ret == null || ret.first == null) {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error);
                } else {
                    if (CORRUPT == ret.first) {
                        mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_db_error);
                        noMediaChanges = true;
                    }
                    if (NO_CHANGES == ret.first) {
                        publishProgress(R.string.sync_media_no_changes);
                        noMediaChanges = true;
                    }
                    if (MEDIA_SANITY_FAILED == ret.first) {
                        mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_sanity_failed);
                    } else {
                        publishProgress(R.string.sync_media_success);
                    }
                }
            } catch (RuntimeException e) {
                Timber.w(e);
                if (timeoutOccurred(e)) {
                    data.resultType = CONNECTION_ERROR;
                    data.result = new Object[] { e };
                } else if (USER_ABORTED_SYNC.toString().equals(e.getMessage())) {
                    data.resultType = USER_ABORTED_SYNC;
                    data.result = new Object[] { e };
                }
                int downloadedCount = mediaClient.getDownloadCount();
                int uploadedCount = mediaClient.getUploadCount();
                if (downloadedCount == 0 && uploadedCount == 0) {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_error) + "\n\n" + e.getLocalizedMessage();
                } else {
                    mediaError = AnkiDroidApp.getAppResources().getString(R.string.sync_media_partial_updated, downloadedCount, uploadedCount) + "\n\n" + e.getLocalizedMessage();
                }
            }
        }
        if (noChanges && (!media || noMediaChanges)) {
            // This means that there is no change at all, neither media nor collection. Not that there was an error.
            data.success = false;
            data.resultType = NO_CHANGES;
            data.result = new Object[0];
        } 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.resultType = MEDIA_SYNC_SERVER_ERROR;
        data.result = new Object[] { 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.resultType = ERROR;
        data.result = new Object[] { code, msg };
        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.resultType = CONNECTION_ERROR;
            data.result = new Object[] { e };
        } else if (USER_ABORTED_SYNC.toString().equals(e.getMessage())) {
            data.resultType = USER_ABORTED_SYNC;
            data.result = new Object[] { e };
        } else {
            AnkiDroidApp.sendExceptionReport(e, "doInBackgroundSync");
            data.resultType = ARBITRARY_STRING;
            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 : ConnectionResultType(com.ichi2.libanki.sync.Syncer.ConnectionResultType) HostNum(com.ichi2.libanki.sync.HostNum) FullSyncer(com.ichi2.libanki.sync.FullSyncer) UnknownHttpResponseException(com.ichi2.anki.exception.UnknownHttpResponseException) CustomSyncServerUrlException(com.ichi2.libanki.sync.CustomSyncServerUrlException) JSONException(com.ichi2.utils.JSONException) 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) ConflictResolution(com.ichi2.async.Connection.ConflictResolution) RemoteServer(com.ichi2.libanki.sync.RemoteServer) RemoteMediaServer(com.ichi2.libanki.sync.RemoteMediaServer) Pair(android.util.Pair)

Aggregations

HostNum (com.ichi2.libanki.sync.HostNum)7 MediaSyncException (com.ichi2.anki.exception.MediaSyncException)5 UnknownHttpResponseException (com.ichi2.anki.exception.UnknownHttpResponseException)5 RemoteServer (com.ichi2.libanki.sync.RemoteServer)5 JSONException (com.ichi2.utils.JSONException)5 JSONObject (com.ichi2.utils.JSONObject)5 IOException (java.io.IOException)5 HttpSyncer (com.ichi2.libanki.sync.HttpSyncer)4 NoEnoughServerSpaceException (com.ichi2.anki.exception.NoEnoughServerSpaceException)3 Response (okhttp3.Response)3 Connection (com.ichi2.async.Connection)2 Collection (com.ichi2.libanki.Collection)2 CustomSyncServerUrlException (com.ichi2.libanki.sync.CustomSyncServerUrlException)2 FullSyncer (com.ichi2.libanki.sync.FullSyncer)2 MediaSyncer (com.ichi2.libanki.sync.MediaSyncer)2 RemoteMediaServer (com.ichi2.libanki.sync.RemoteMediaServer)2 Syncer (com.ichi2.libanki.sync.Syncer)2 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 Pair (android.util.Pair)1