Search in sources :

Example 16 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class Reviewer method reloadCollection.

private void reloadCollection(Bundle savedInstanceState) {
    mSavedInstanceState = savedInstanceState;
    DeckTask.launchDeckTask(DeckTask.TASK_TYPE_OPEN_COLLECTION, new DeckTask.TaskListener() {

        @Override
        public void onPostExecute(DeckTask.TaskData result) {
            if (mOpenCollectionDialog.isShowing()) {
                try {
                    mOpenCollectionDialog.dismiss();
                } catch (Exception e) {
                    Log.e(AnkiDroidApp.TAG, "onPostExecute - Dialog dismiss Exception = " + e.getMessage());
                }
            }
            if (AnkiDroidApp.colIsOpen()) {
                onCreate(mSavedInstanceState);
            } else {
                finish();
            }
        }

        @Override
        public void onPreExecute() {
            mOpenCollectionDialog = StyledOpenCollectionDialog.show(Reviewer.this, getResources().getString(R.string.open_collection), new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    finish();
                }
            });
        }

        @Override
        public void onProgressUpdate(DeckTask.TaskData... values) {
        }
    }, new DeckTask.TaskData(AnkiDroidApp.getCurrentAnkiDroidDirectory() + AnkiDroidApp.COLLECTION_PATH, 0, true));
}
Also used : DialogInterface(android.content.DialogInterface) DeckTask(com.ichi2.async.DeckTask) JSONException(org.json.JSONException) IOException(java.io.IOException) OnCancelListener(android.content.DialogInterface.OnCancelListener)

Example 17 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class SearchImageActivity method postFinished.

public void postFinished(ImageSearchResponse response) {
    ArrayList<String> theImages = new ArrayList<String>();
    // No loop, just a good construct to break out from
    do {
        if (response == null)
            break;
        if (response.getOk() == false)
            break;
        ResponseData rdata = response.getResponseData();
        if (rdata == null)
            break;
        List<Result> results = rdata.getResults();
        if (results == null)
            break;
        for (Result result : results) {
            if (result == null) {
                continue;
            }
            String url = result.getUrl();
            if (url != null) {
                theImages.add(url);
            }
        }
        if (theImages.size() == 0)
            break;
        proceedWithImages(theImages);
        return;
    } while (false);
    returnFailure(gtxt(R.string.multimedia_editor_imgs_no_results));
}
Also used : ResponseData(com.ichi2.anki.multimediacard.googleimagesearch.json.ResponseData) ArrayList(java.util.ArrayList) Result(com.ichi2.anki.multimediacard.googleimagesearch.json.Result)

Example 18 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class GoogleTranslaterFilter method filter.

public Pair<String, String> filter(Pair<String, String> messages, SharedPreferences preferences) {
    Pair<String, String> result = new Pair<String, String>(messages.first, messages.second);
    Pattern pattern = Pattern.compile(SEARCH_PATTERN);
    Matcher matcher = pattern.matcher(getSearchText(messages));
    if (isCanBeExecuted(messages, preferences) && matcher.find()) {
        String translate = matcher.group(1);
        if (matcher.find()) {
            result = new Pair<String, String>(matcher.group(1), translate);
        }
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Pair(com.ichi2.anki.Pair)

Example 19 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundImportReplace.

private TaskData doInBackgroundImportReplace(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundImportReplace");
    Collection col = params[0].getCollection();
    String path = params[0].getString();
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    // extract the deck from the zip file
    String fileDir = AnkiDroidApp.getCurrentAnkiDroidDirectory() + "/tmpzip";
    File dir = new File(fileDir);
    if (dir.exists()) {
        BackupManager.removeDir(dir);
    }
    publishProgress(new TaskData(res.getString(R.string.import_unpacking)));
    // from anki2.py
    String colFile = fileDir + "/collection.anki2";
    ZipFile zip;
    try {
        zip = new ZipFile(new File(path), ZipFile.OPEN_READ);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - Error while unzipping: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace0");
        return new TaskData(false);
    }
    if (!Utils.unzipFiles(zip, fileDir, new String[] { "collection.anki2", "media" }, null) || !(new File(colFile)).exists()) {
        return new TaskData(-2, null, false);
    }
    Collection tmpCol = null;
    try {
        tmpCol = Storage.Collection(colFile);
        if (!tmpCol.validCollection()) {
            tmpCol.close();
            return new TaskData(-2, null, false);
        }
    } finally {
        if (tmpCol != null) {
            tmpCol.close();
        }
    }
    publishProgress(new TaskData(res.getString(R.string.importing_collection)));
    String colPath;
    if (col != null) {
        // unload collection and trigger a backup
        colPath = col.getPath();
        AnkiDroidApp.closeCollection(true);
        BackupManager.performBackup(colPath, true);
    }
    // overwrite collection
    colPath = AnkiDroidApp.getCollectionPath();
    File f = new File(colFile);
    f.renameTo(new File(colPath));
    int addedCount = -1;
    try {
        col = AnkiDroidApp.openCollection(colPath);
        // because users don't have a backup of media, it's safer to import new
        // data and rely on them running a media db check to get rid of any
        // unwanted media. in the future we might also want to duplicate this step
        // import media
        HashMap<String, String> nameToNum = new HashMap<String, String>();
        HashMap<String, String> numToName = new HashMap<String, String>();
        File mediaMapFile = new File(fileDir, "media");
        if (mediaMapFile.exists()) {
            JsonReader jr = new JsonReader(new FileReader(mediaMapFile));
            jr.beginObject();
            String name;
            String num;
            while (jr.hasNext()) {
                num = jr.nextName();
                name = jr.nextString();
                nameToNum.put(name, num);
                numToName.put(num, name);
            }
            jr.endObject();
            jr.close();
        }
        String mediaDir = col.getMedia().getDir();
        int total = nameToNum.size();
        int i = 0;
        for (Map.Entry<String, String> entry : nameToNum.entrySet()) {
            String file = entry.getKey();
            String c = entry.getValue();
            File of = new File(mediaDir, file);
            if (!of.exists()) {
                Utils.unzipFiles(zip, mediaDir, new String[] { c }, numToName);
            }
            ++i;
            publishProgress(new TaskData(res.getString(R.string.import_media_count, (i + 1) * 100 / total)));
        }
        zip.close();
        // delete tmp dir
        BackupManager.removeDir(dir);
        publishProgress(new TaskData(res.getString(R.string.import_update_counts)));
        // Update the counts
        DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col));
        if (result == null) {
            return null;
        }
        return new TaskData(addedCount, result.getObjArray(), true);
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - RuntimeException: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace1");
        return new TaskData(false);
    } catch (FileNotFoundException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - FileNotFoundException: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace2");
        return new TaskData(false);
    } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - IOException: ", e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace3");
        return new TaskData(false);
    }
}
Also used : HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) Collection(com.ichi2.libanki.Collection) JsonReader(com.google.gson.stream.JsonReader) FileReader(java.io.FileReader) Resources(android.content.res.Resources) ZipFile(java.util.zip.ZipFile) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 20 with Result

use of com.ichi2.anki.multimediacard.googleimagesearch.json.Result in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundOpenCollection.

private TaskData doInBackgroundOpenCollection(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundOpenCollection");
    long time = Utils.intNow(1000);
    Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
    String collectionFile = params[0].getString();
    SharedPreferences prefs = AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext());
    // see, if a collection is still opened
    Collection oldCol = AnkiDroidApp.getCol();
    Collection col = null;
    publishProgress(new TaskData(res.getString(R.string.open_collection)));
    if (!(AnkiDroidApp.colIsOpen() && oldCol.getPath().equals(collectionFile))) {
        // android's delete db bug
        if (BackupManager.safetyBackupNeeded(collectionFile)) {
            publishProgress(new TaskData(res.getString(R.string.backup_collection)));
            BackupManager.performBackup(collectionFile);
        }
        publishProgress(new TaskData(res.getString(R.string.open_collection)));
        // load collection
        try {
            col = AnkiDroidApp.openCollection(collectionFile);
        } catch (RuntimeException e) {
            BackupManager.restoreCollectionIfMissing(collectionFile);
            Log.e(AnkiDroidApp.TAG, "doInBackgroundOpenCollection - RuntimeException on opening collection: " + e);
            AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundOpenCollection");
            return new TaskData(false);
        }
        // create tutorial deck if needed
        if (prefs.contains("createTutorial") && prefs.getBoolean("createTutorial", false)) {
            prefs.edit().remove("createTutorial").commit();
            publishProgress(new TaskData(res.getString(R.string.tutorial_load)));
            doInBackgroundLoadTutorial(new TaskData(col));
        }
    } else {
        Log.i(AnkiDroidApp.TAG, "doInBackgroundOpenCollection: collection still open - reusing it");
        col = oldCol;
    }
    Object[] counts = null;
    DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col));
    if (result != null) {
        counts = result.getObjArray();
    }
    if (prefs.getBoolean("splashScreen", false)) {
        long millies = Utils.intNow(1000) - time;
        if (millies < 1000) {
            try {
                Thread.sleep(1200 - millies);
            } catch (InterruptedException e) {
            }
        }
    }
    return new TaskData(col, counts);
}
Also used : SharedPreferences(android.content.SharedPreferences) Collection(com.ichi2.libanki.Collection) JSONObject(org.json.JSONObject) Resources(android.content.res.Resources)

Aggregations

IOException (java.io.IOException)7 Intent (android.content.Intent)6 Resources (android.content.res.Resources)6 DeckTask (com.ichi2.async.DeckTask)6 Collection (com.ichi2.libanki.Collection)6 JSONException (org.json.JSONException)6 DialogInterface (android.content.DialogInterface)5 TaskData (com.ichi2.async.DeckTask.TaskData)5 File (java.io.File)5 OnCancelListener (android.content.DialogInterface.OnCancelListener)4 SharedPreferences (android.content.SharedPreferences)4 OnClickListener (android.view.View.OnClickListener)4 NotFoundException (android.content.res.Resources.NotFoundException)3 View (android.view.View)3 Payload (com.ichi2.async.Connection.Payload)3 StyledDialog (com.ichi2.themes.StyledDialog)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 JSONObject (org.json.JSONObject)3