Search in sources :

Example 36 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class AudioField method setFormattedString.

@Override
public void setFormattedString(String value) {
    Pattern p = Pattern.compile(PATH_REGEX);
    Matcher m = p.matcher(value);
    String res = "";
    if (m.find()) {
        res = m.group(1);
    }
    Collection col = AnkiDroidApp.getCol();
    String mediaDir = col.getMedia().getDir() + "/";
    setAudioPath(mediaDir + res);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Collection(com.ichi2.libanki.Collection)

Example 37 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class StudyOptionsFragment method initAllContentViews.

private void initAllContentViews(LayoutInflater inflater) {
    mStudyOptionsView = inflater.inflate(R.layout.studyoptions, null);
    Themes.setContentStyle(mStudyOptionsView, Themes.CALLER_STUDYOPTIONS);
    mTextDeckName = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_deck_name);
    mTextDeckDescription = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_deck_description);
    mButtonStart = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_start);
    mButtonCustomStudy = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_custom);
    mDeckOptions = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_options);
    mCramOptions = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_options_cram);
    if (AnkiDroidApp.colIsOpen() && AnkiDroidApp.getCol().getDecks().isDyn(AnkiDroidApp.getCol().getDecks().selected())) {
        Button rebBut = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_rebuild_cram);
        rebBut.setOnClickListener(mButtonClickListener);
        Button emptyBut = (Button) mStudyOptionsView.findViewById(R.id.studyoptions_empty_cram);
        emptyBut.setOnClickListener(mButtonClickListener);
        // If dynamic deck then enable the cram buttons group, and disable the new filtered deck / ordinary study options buttons group
        ((LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_cram_buttons)).setVisibility(View.VISIBLE);
        ((LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_regular_buttons)).setVisibility(View.GONE);
    }
    if (!mFragmented) {
        // Standard non-fragmented view for non-tablets, using standard layout file (in ./res/layout/)
        mAddNote = (ImageButton) mStudyOptionsView.findViewById(R.id.studyoptions_add);
        if (AnkiDroidApp.colIsOpen()) {
            Collection col = AnkiDroidApp.getCol();
            if (col.getDecks().isDyn(col.getDecks().selected())) {
                mAddNote.setEnabled(false);
            }
        }
        mCardBrowser = (ImageButton) mStudyOptionsView.findViewById(R.id.studyoptions_card_browser);
        mStatisticsButton = (ImageButton) mStudyOptionsView.findViewById(R.id.studyoptions_statistics);
        mAddNote.setOnClickListener(mButtonClickListener);
        mCardBrowser.setOnClickListener(mButtonClickListener);
        mStatisticsButton.setOnClickListener(mButtonClickListener);
    } else {
    // Fragmented view for 10" tablets, which is different from smaller devices due to larger layout file (in ./res/layout-xlarge/)
    // This tablet view shows the study options fragment simultaneously with the deck picker, and has different buttons from standard        	
    }
    // Code common to both fragmented and non-fragmented view      
    mGlobalBar = (View) mStudyOptionsView.findViewById(R.id.studyoptions_global_bar);
    mGlobalMatBar = (View) mStudyOptionsView.findViewById(R.id.studyoptions_global_mat_bar);
    mBarsMax = (View) mStudyOptionsView.findViewById(R.id.studyoptions_progressbar_content);
    mTextTodayNew = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_new);
    mTextTodayLrn = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_lrn);
    mTextTodayRev = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_rev);
    mTextNewTotal = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_total_new);
    mTextTotal = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_total);
    mTextETA = (TextView) mStudyOptionsView.findViewById(R.id.studyoptions_eta);
    mSmallChart = (LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_mall_chart);
    mGlobalMatBar.setVisibility(View.INVISIBLE);
    mGlobalBar.setVisibility(View.INVISIBLE);
    mDeckCounts = (LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_deckcounts);
    mDeckChart = (LinearLayout) mStudyOptionsView.findViewById(R.id.studyoptions_chart);
    mButtonStart.setOnClickListener(mButtonClickListener);
    mButtonCustomStudy.setOnClickListener(mButtonClickListener);
    mDeckOptions.setOnClickListener(mButtonClickListener);
    mCramOptions.setOnClickListener(mButtonClickListener);
    //        mButtonUp.setOnClickListener(mButtonClickListener);
    //        mButtonDown.setOnClickListener(mButtonClickListener);
    //        mToggleLimitToggle.setOnClickListener(mButtonClickListener);
    // mToggleCram.setOnClickListener(mButtonClickListener);
    // mToggleNight.setOnClickListener(mButtonClickListener);
    // The view that shows the congratulations view.
    mCongratsView = inflater.inflate(R.layout.studyoptions_congrats, null);
    // The view that shows the learn more options
    mCustomStudyDetailsView = inflater.inflate(R.layout.styled_custom_study_details_dialog, null);
    mCustomStudyTextView1 = (TextView) mCustomStudyDetailsView.findViewById(R.id.custom_study_details_text1);
    mCustomStudyTextView2 = (TextView) mCustomStudyDetailsView.findViewById(R.id.custom_study_details_text2);
    mCustomStudyEditText = (EditText) mCustomStudyDetailsView.findViewById(R.id.custom_study_details_edittext2);
    /* When creating a new filtered deck after reviewing, there are several options.
         * For selecting several tags, we need a new, different dialog, that allows to select
         * a list of tags:
         */
    Themes.setWallpaper(mCongratsView);
    mTextCongratsMessage = (TextView) mCongratsView.findViewById(R.id.studyoptions_congrats_message);
    Themes.setTextViewStyle(mTextCongratsMessage);
    mTextCongratsMessage.setOnClickListener(mButtonClickListener);
    mButtonCongratsUndo = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_undo);
    mButtonCongratsUnbury = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_unbury);
    mButtonCongratsCustomStudy = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_customstudy);
    mButtonCongratsOpenOtherDeck = (Button) mCongratsView.findViewById(R.id.studyoptions_congrats_open_other_deck);
    if (mFragmented) {
        mButtonCongratsOpenOtherDeck.setVisibility(View.GONE);
    }
    mButtonCongratsUndo.setOnClickListener(mButtonClickListener);
    mButtonCongratsUnbury.setOnClickListener(mButtonClickListener);
    mButtonCongratsCustomStudy.setOnClickListener(mButtonClickListener);
    mButtonCongratsOpenOtherDeck.setOnClickListener(mButtonClickListener);
}
Also used : ImageButton(android.widget.ImageButton) RadioButton(android.widget.RadioButton) Button(android.widget.Button) Collection(com.ichi2.libanki.Collection) LinearLayout(android.widget.LinearLayout)

Example 38 with Collection

use of com.ichi2.libanki.Collection 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)

Example 39 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundAddNote.

private TaskData doInBackgroundAddNote(TaskData[] params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundAddNote");
    Note note = params[0].getNote();
    Collection col = note.getCol();
    try {
        AnkiDb ankiDB = col.getDb();
        ankiDB.getDatabase().beginTransaction();
        try {
            publishProgress(new TaskData(col.addNote(note)));
            ankiDB.getDatabase().setTransactionSuccessful();
        } finally {
            ankiDB.getDatabase().endTransaction();
        }
    } catch (RuntimeException e) {
        Log.e(AnkiDroidApp.TAG, "doInBackgroundAddNote - RuntimeException on adding fact: " + e);
        AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundAddNote");
        return new TaskData(false);
    }
    return new TaskData(true);
}
Also used : AnkiDb(com.ichi2.anki.AnkiDb) Note(com.ichi2.libanki.Note) Collection(com.ichi2.libanki.Collection)

Example 40 with Collection

use of com.ichi2.libanki.Collection in project Anki-Android by Ramblurr.

the class DeckTask method doInBackgroundConfSetSubdecks.

private TaskData doInBackgroundConfSetSubdecks(TaskData... params) {
    Log.i(AnkiDroidApp.TAG, "doInBackgroundConfSetSubdecks");
    Object[] data = params[0].getObjArray();
    Collection col = (Collection) data[0];
    JSONObject deck = (JSONObject) data[1];
    JSONObject conf = (JSONObject) data[2];
    try {
        TreeMap<String, Long> children = col.getDecks().children(deck.getLong("id"));
        for (Map.Entry<String, Long> entry : children.entrySet()) {
            JSONObject child = col.getDecks().get(entry.getValue());
            if (child.getInt("dyn") == 1) {
                continue;
            }
            TaskData newParams = new TaskData(new Object[] { col, child, conf });
            boolean changed = doInBackgroundConfChange(newParams).getBoolean();
            if (!changed) {
                return new TaskData(false);
            }
        }
        return new TaskData(true);
    } catch (JSONException e) {
        return new TaskData(false);
    }
}
Also used : JSONObject(org.json.JSONObject) Collection(com.ichi2.libanki.Collection) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

Collection (com.ichi2.libanki.Collection)40 JSONObject (org.json.JSONObject)20 File (java.io.File)13 JSONException (org.json.JSONException)12 IOException (java.io.IOException)11 Resources (android.content.res.Resources)10 DialogInterface (android.content.DialogInterface)6 DeckTask (com.ichi2.async.DeckTask)6 TaskData (com.ichi2.async.DeckTask.TaskData)6 StyledDialog (com.ichi2.themes.StyledDialog)6 AnkiDb (com.ichi2.anki.AnkiDb)5 FileInputStream (java.io.FileInputStream)5 ArrayList (java.util.ArrayList)5 Intent (android.content.Intent)4 Note (com.ichi2.libanki.Note)4 FileNotFoundException (java.io.FileNotFoundException)4 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 JSONArray (org.json.JSONArray)4 OnCancelListener (android.content.DialogInterface.OnCancelListener)3