Search in sources :

Example 6 with Download

use of com.ichi2.anki.Download in project Anki-Android by Ramblurr.

the class DownloadManagerService method resumeDownload.

// It could be part of the AIDL Interface but at the moment no Activity uses it directly
public void resumeDownload(Download download) {
    // Create tmp folder where the temporal decks are going to be stored
    new File(mDestination + "/tmp/").mkdirs();
    AnkiDroidApp.createNoMediaFileIfMissing(new File(mDestination));
    if (download instanceof SharedDeckDownload) {
        SharedDeckDownload sharedDeckDownload = (SharedDeckDownload) download;
        // numUpdatedCards and numTotalCards to get updated, so that progress is displayed correctly
        if (sharedDeckDownload.getStatus() == SharedDeckDownload.STATUS_PAUSED || sharedDeckDownload.getStatus() == SharedDeckDownload.STATUS_UPDATING) {
            new UpdateDeckTask().execute(new Payload(new Object[] { sharedDeckDownload }));
        } else {
            new DownloadSharedDeckTask().execute(sharedDeckDownload);
        }
    } else {
        // TODO: Check if there is already a deck with the same name, and if that's so
        // add the current milliseconds to the end of the name or notify the user
        new DownloadPersonalDeckTask().execute(download);
    }
}
Also used : Payload(com.ichi2.async.Connection.Payload) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) SharedDeckDownload(com.ichi2.anki.SharedDeckDownload)

Example 7 with Download

use of com.ichi2.anki.Download in project Anki-Android by Ramblurr.

the class FullSyncer method download.

@Override
public Object[] download() {
    InputStream cont;
    try {
        HttpResponse ret = super.req("download");
        if (ret == null) {
            return null;
        }
        cont = ret.getEntity().getContent();
    } catch (IllegalStateException e1) {
        throw new RuntimeException(e1);
    } catch (IOException e1) {
        return null;
    }
    String path = AnkiDroidApp.getCollectionPath();
    if (mCol != null) {
        mCol.close(false);
        mCol = null;
    }
    String tpath = path + ".tmp";
    if (!super.writeToFile(cont, tpath)) {
        return new Object[] { "sdAccessError" };
    }
    // first check, if account needs upgrade (from 1.2)
    try {
        FileInputStream fis = new FileInputStream(tpath);
        if (super.stream2String(fis, 15).equals("upgradeRequired")) {
            return new Object[] { "upgradeRequired" };
        }
    } catch (FileNotFoundException e1) {
        throw new RuntimeException(e1);
    }
    // check the received file is ok
    mCon.publishProgress(R.string.sync_check_download_file);
    try {
        AnkiDb d = AnkiDatabaseManager.getDatabase(tpath);
        if (!d.queryString("PRAGMA integrity_check").equalsIgnoreCase("ok")) {
            Log.e(AnkiDroidApp.TAG, "Full sync - downloaded file corrupt");
            return new Object[] { "remoteDbError" };
        }
    } catch (SQLiteDatabaseCorruptException e) {
        Log.e(AnkiDroidApp.TAG, "Full sync - downloaded file corrupt");
        return new Object[] { "remoteDbError" };
    } finally {
        AnkiDatabaseManager.closeDatabase(tpath);
    }
    // overwrite existing collection
    File newFile = new File(tpath);
    if (newFile.renameTo(new File(path))) {
        return new Object[] { "success" };
    } else {
        return new Object[] { "overwriteError" };
    }
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) HttpResponse(org.apache.http.HttpResponse) SQLiteDatabaseCorruptException(android.database.sqlite.SQLiteDatabaseCorruptException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with Download

use of com.ichi2.anki.Download in project Anki-Android by Ramblurr.

the class DeckPicker method sync.

/**
     * The mother of all syncing attempts. This might be called from sync() as first attempt to sync a collection OR
     * from the mSyncConflictResolutionListener if the first attempt determines that a full-sync is required. In the
     * second case, we have passed the mediaUsn that was obtained during the first attempt.
     * 
     * @param syncConflictResolution Either "upload" or "download", depending on the user's choice.
     * @param syncMediaUsn The media Usn, as determined during the prior sync() attempt that determined that full
     *            syncing was required.
     */
private void sync(String syncConflictResolution, int syncMediaUsn) {
    SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext());
    String hkey = preferences.getString("hkey", "");
    if (hkey.length() == 0) {
        showDialog(DIALOG_USER_NOT_LOGGED_IN_SYNC);
    } else {
        Connection.sync(mSyncListener, new Connection.Payload(new Object[] { hkey, preferences.getBoolean("syncFetchesMedia", true), syncConflictResolution, syncMediaUsn }));
    }
}
Also used : Payload(com.ichi2.async.Connection.Payload) SharedPreferences(android.content.SharedPreferences) Connection(com.ichi2.async.Connection) JSONObject(org.json.JSONObject)

Example 9 with Download

use of com.ichi2.anki.Download 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;
    }
}
Also used : FullSyncer(com.ichi2.libanki.sync.FullSyncer) OutOfMemoryError(java.lang.OutOfMemoryError) BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) FullSyncer(com.ichi2.libanki.sync.FullSyncer) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) BasicHttpSyncer(com.ichi2.libanki.sync.BasicHttpSyncer) Syncer(com.ichi2.libanki.sync.Syncer) Collection(com.ichi2.libanki.Collection) MediaSyncer(com.ichi2.libanki.sync.MediaSyncer) JSONObject(org.json.JSONObject) RemoteServer(com.ichi2.libanki.sync.RemoteServer) RemoteMediaServer(com.ichi2.libanki.sync.RemoteMediaServer)

Aggregations

File (java.io.File)6 JSONObject (org.json.JSONObject)5 SharedPreferences (android.content.SharedPreferences)4 Payload (com.ichi2.async.Connection.Payload)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 SQLiteDatabaseCorruptException (android.database.sqlite.SQLiteDatabaseCorruptException)2 AnkiDb (com.ichi2.anki.AnkiDb)2 SharedDeckDownload (com.ichi2.anki.SharedDeckDownload)2 Connection (com.ichi2.async.Connection)2 Collection (com.ichi2.libanki.Collection)2 BasicHttpSyncer (com.ichi2.libanki.sync.BasicHttpSyncer)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 HttpResponse (org.apache.http.HttpResponse)2 JSONException (org.json.JSONException)2