Search in sources :

Example 36 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class CardTemplatePreviewer method onCollectionLoaded.

@Override
protected void onCollectionLoaded(Collection col) {
    super.onCollectionLoaded(col);
    if ((mCurrentCard == null) && (mCardList == null)) {
        Timber.d("onCollectionLoaded - incorrect state to load, closing");
        closeCardTemplatePreviewer();
        return;
    }
    if (mCardList != null && mOrdinal >= 0 && mOrdinal < mCardList.length) {
        mCurrentCard = new PreviewerCard(col, mCardList[mOrdinal]);
    }
    if (mNoteEditorBundle != null) {
        long newDid = mNoteEditorBundle.getLong("did");
        if (col.getDecks().isDyn(newDid)) {
            mCurrentCard.setODid(mCurrentCard.getDid());
        }
        mCurrentCard.setDid(newDid);
        Note currentNote = mCurrentCard.note();
        ArrayList<String> tagsList = mNoteEditorBundle.getStringArrayList("tags");
        NoteUtils.setTags(currentNote, tagsList);
        Bundle noteFields = mNoteEditorBundle.getBundle("editFields");
        if (noteFields != null) {
            for (String fieldOrd : noteFields.keySet()) {
                // In case the fields on the card are out of sync with the bundle
                int fieldOrdInt = Integer.parseInt(fieldOrd);
                if (fieldOrdInt < currentNote.getFields().length) {
                    currentNote.setField(fieldOrdInt, noteFields.getString(fieldOrd));
                }
            }
        }
    }
    displayCardQuestion();
    Timber.i("display card question while showing answer:%s", mShowingAnswer);
    if (mShowingAnswer) {
        displayCardAnswer();
    }
// showBackIcon();
}
Also used : Bundle(android.os.Bundle) Note(com.ichi2.libanki.Note)

Example 37 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class CustomStudyDialog method buildContextMenu.

/**
 * Build a context menu for custom study
 *
 * @param id
 * @return
 */
private MaterialDialog buildContextMenu(int id) {
    int[] listIds = getListIds(id);
    final boolean jumpToReviewer = getArguments().getBoolean("jumpToReviewer");
    return new MaterialDialog.Builder(this.getActivity()).title(R.string.custom_study).cancelable(true).itemsIds(listIds).items(ContextMenuHelper.getValuesFromKeys(getKeyValueMap(), listIds)).itemsCallback(new MaterialDialog.ListCallback() {

        @Override
        public void onSelection(MaterialDialog materialDialog, View view, int which, CharSequence charSequence) {
            AnkiActivity activity = getAnkiActivity();
            switch(view.getId()) {
                case DECK_OPTIONS:
                    {
                        // User asked to permanently change the deck options
                        Intent i = new Intent(activity, DeckOptions.class);
                        i.putExtra("did", getArguments().getLong("did"));
                        getActivity().startActivity(i);
                        break;
                    }
                case MORE_OPTIONS:
                    {
                        // User asked to see all custom study options
                        CustomStudyDialog d = CustomStudyDialog.newInstance(CONTEXT_MENU_STANDARD, getArguments().getLong("did"), jumpToReviewer, listener);
                        activity.showDialogFragment(d);
                        break;
                    }
                case CUSTOM_STUDY_TAGS:
                    {
                        /*
                                 * This is a special Dialog for CUSTOM STUDY, where instead of only collecting a
                                 * number, it is necessary to collect a list of tags. This case handles the creation
                                 * of that Dialog.
                                 */
                        long currentDeck = getArguments().getLong("did");
                        TagsDialog dialogFragment = TagsDialog.newInstance(TagsDialog.TYPE_CUSTOM_STUDY_TAGS, new ArrayList<String>(), new ArrayList<>(activity.getCol().getTags().byDeck(currentDeck, true)));
                        dialogFragment.setTagsDialogListener(CustomStudyDialog.this::customStudyFromTags);
                        activity.showDialogFragment(dialogFragment);
                        break;
                    }
                default:
                    {
                        // User asked for a standard custom study option
                        CustomStudyDialog d = CustomStudyDialog.newInstance(view.getId(), getArguments().getLong("did"), jumpToReviewer, listener);
                        getAnkiActivity().showDialogFragment(d);
                    }
            }
        }
    }).build();
}
Also used : MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ArrayList(java.util.ArrayList) Intent(android.content.Intent) View(android.view.View) TextView(android.widget.TextView) AnkiActivity(com.ichi2.anki.AnkiActivity) DeckOptions(com.ichi2.anki.DeckOptions)

Example 38 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class CardContentProvider method bulkInsertNotes.

/**
 * This implementation optimizes for when the notes are grouped according to model
 */
private int bulkInsertNotes(ContentValues[] valuesArr, long deckId) {
    if (valuesArr == null || valuesArr.length == 0) {
        return 0;
    }
    Collection col = CollectionHelper.getInstance().getCol(mContext);
    if (col == null) {
        throw new IllegalStateException(COL_NULL_ERROR_MSG);
    }
    if (col.getDecks().isDyn(deckId)) {
        throw new IllegalArgumentException("A filtered deck cannot be specified as the deck in bulkInsertNotes");
    }
    col.log(String.format(Locale.US, "bulkInsertNotes: %d items.\n%s", valuesArr.length, getLogMessage("bulkInsert", null)));
    // for caching model information (so we don't have to query for each note)
    long modelId = -1L;
    Model model = null;
    // is it okay to move this outside the for-loop? Is it needed at all?
    col.getDecks().flush();
    SupportSQLiteDatabase sqldb = col.getDb().getDatabase();
    try {
        int result = 0;
        sqldb.beginTransaction();
        for (int i = 0; i < valuesArr.length; i++) {
            ContentValues values = valuesArr[i];
            if (values == null) {
                continue;
            }
            String flds = values.getAsString(FlashCardsContract.Note.FLDS);
            if (flds == null) {
                continue;
            }
            Long thisModelId = values.getAsLong(FlashCardsContract.Note.MID);
            if (thisModelId == null || thisModelId < 0) {
                Timber.d("Unable to get model at index: " + i);
                continue;
            }
            String[] fldsArray = Utils.splitFields(flds);
            if (model == null || thisModelId != modelId) {
                // new modelId so need to recalculate model, modelId and invalidate duplicateChecker (which is based on previous model)
                model = col.getModels().get(thisModelId);
                modelId = thisModelId;
            }
            // Create empty note
            // for some reason we cannot pass modelId in here
            com.ichi2.libanki.Note newNote = new com.ichi2.libanki.Note(col, model);
            // Check that correct number of flds specified
            if (fldsArray.length != newNote.getFields().length) {
                throw new IllegalArgumentException("Incorrect flds argument : " + flds);
            }
            for (int idx = 0; idx < fldsArray.length; idx++) {
                newNote.setField(idx, fldsArray[idx]);
            }
            // Set tags
            String tags = values.getAsString(FlashCardsContract.Note.TAGS);
            if (tags != null) {
                newNote.setTagsFromStr(tags);
            }
            // Add to collection
            col.addNote(newNote);
            for (Card card : newNote.cards()) {
                card.setDid(deckId);
                card.flush();
            }
            result++;
        }
        col.save();
        sqldb.setTransactionSuccessful();
        return result;
    } finally {
        sqldb.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) Note(com.ichi2.libanki.Note) Card(com.ichi2.libanki.Card) SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection)

Example 39 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class CardContentProvider method insert.

@Override
public Uri insert(Uri uri, ContentValues values) {
    if (!hasReadWritePermission() && shouldEnforceQueryOrInsertSecurity()) {
        throwSecurityException("insert", uri);
    }
    Collection col = CollectionHelper.getInstance().getCol(mContext);
    if (col == null) {
        throw new IllegalStateException(COL_NULL_ERROR_MSG);
    }
    col.log(getLogMessage("insert", uri));
    // Find out what data the user is requesting
    int match = sUriMatcher.match(uri);
    switch(match) {
        case NOTES:
            {
                /* Insert new note with specified fields and tags
                 */
                Long modelId = values.getAsLong(FlashCardsContract.Note.MID);
                String flds = values.getAsString(FlashCardsContract.Note.FLDS);
                String tags = values.getAsString(FlashCardsContract.Note.TAGS);
                // Create empty note
                com.ichi2.libanki.Note newNote = new com.ichi2.libanki.Note(col, col.getModels().get(modelId));
                // Set fields
                String[] fldsArray = Utils.splitFields(flds);
                // Check that correct number of flds specified
                if (fldsArray.length != newNote.getFields().length) {
                    throw new IllegalArgumentException("Incorrect flds argument : " + flds);
                }
                for (int idx = 0; idx < fldsArray.length; idx++) {
                    newNote.setField(idx, fldsArray[idx]);
                }
                // Set tags
                if (tags != null) {
                    newNote.setTagsFromStr(tags);
                }
                // Add to collection
                col.addNote(newNote);
                col.save();
                return Uri.withAppendedPath(FlashCardsContract.Note.CONTENT_URI, Long.toString(newNote.getId()));
            }
        case NOTES_ID:
            // Note ID is generated automatically by libanki
            throw new IllegalArgumentException("Not possible to insert note with specific ID");
        case NOTES_ID_CARDS:
            // Cards are generated automatically by libanki
            throw new IllegalArgumentException("Not possible to insert cards directly (only through NOTES)");
        case NOTES_ID_CARDS_ORD:
            // Cards are generated automatically by libanki
            throw new IllegalArgumentException("Not possible to insert cards directly (only through NOTES)");
        case MODELS:
            // Get input arguments
            String modelName = values.getAsString(FlashCardsContract.Model.NAME);
            String css = values.getAsString(FlashCardsContract.Model.CSS);
            Long did = values.getAsLong(FlashCardsContract.Model.DECK_ID);
            String fieldNames = values.getAsString(FlashCardsContract.Model.FIELD_NAMES);
            Integer numCards = values.getAsInteger(FlashCardsContract.Model.NUM_CARDS);
            Integer sortf = values.getAsInteger(FlashCardsContract.Model.SORT_FIELD_INDEX);
            Integer type = values.getAsInteger(FlashCardsContract.Model.TYPE);
            String latexPost = values.getAsString(FlashCardsContract.Model.LATEX_POST);
            String latexPre = values.getAsString(FlashCardsContract.Model.LATEX_PRE);
            // Throw exception if required fields empty
            if (modelName == null || fieldNames == null || numCards == null) {
                throw new IllegalArgumentException("Model name, field_names, and num_cards can't be empty");
            }
            if (did != null && col.getDecks().isDyn(did)) {
                throw new IllegalArgumentException("Cannot set a filtered deck as default deck for a model");
            }
            // Create a new model
            Models mm = col.getModels();
            Model newModel = mm.newModel(modelName);
            try {
                // Add the fields
                String[] allFields = Utils.splitFields(fieldNames);
                for (String f : allFields) {
                    mm.addFieldInNewModel(newModel, mm.newField(f));
                }
                // Add some empty card templates
                for (int idx = 0; idx < numCards; idx++) {
                    JSONObject t = mm.newTemplate("Card " + (idx + 1));
                    t.put("qfmt", String.format("{{%s}}", allFields[0]));
                    String answerField = allFields[0];
                    if (allFields.length > 1) {
                        answerField = allFields[1];
                    }
                    t.put("afmt", String.format("{{FrontSide}}\\n\\n<hr id=answer>\\n\\n{{%s}}", answerField));
                    mm.addTemplateInNewModel(newModel, t);
                }
                // Add the CSS if specified
                if (css != null) {
                    newModel.put("css", css);
                }
                // Add the did if specified
                if (did != null) {
                    newModel.put("did", did);
                }
                if (sortf != null && sortf < allFields.length) {
                    newModel.put("sortf", sortf);
                }
                if (type != null) {
                    newModel.put("type", type);
                }
                if (latexPost != null) {
                    newModel.put("latexPost", latexPost);
                }
                if (latexPre != null) {
                    newModel.put("latexPre", latexPre);
                }
                // Add the model to collection (from this point on edits will require a full-sync)
                mm.add(newModel);
                col.save();
                // Get the mid and return a URI
                String mid = Long.toString(newModel.getLong("id"));
                return Uri.withAppendedPath(FlashCardsContract.Model.CONTENT_URI, mid);
            } catch (JSONException e) {
                Timber.e(e, "Could not set a field of new model %s", modelName);
                return null;
            }
        case MODELS_ID:
            // Model ID is generated automatically by libanki
            throw new IllegalArgumentException("Not possible to insert model with specific ID");
        case MODELS_ID_TEMPLATES:
            {
                Models models = col.getModels();
                Long mid = getModelIdFromUri(uri, col);
                Model existingModel = models.get(mid);
                if (existingModel == null) {
                    throw new IllegalArgumentException("model missing: " + mid);
                }
                String name = values.getAsString(CardTemplate.NAME);
                String qfmt = values.getAsString(CardTemplate.QUESTION_FORMAT);
                String afmt = values.getAsString(CardTemplate.ANSWER_FORMAT);
                String bqfmt = values.getAsString(CardTemplate.BROWSER_QUESTION_FORMAT);
                String bafmt = values.getAsString(CardTemplate.BROWSER_ANSWER_FORMAT);
                try {
                    JSONObject t = models.newTemplate(name);
                    t.put("qfmt", qfmt);
                    t.put("afmt", afmt);
                    t.put("bqfmt", bqfmt);
                    t.put("bafmt", bafmt);
                    models.addTemplate(existingModel, t);
                    models.save(existingModel);
                    col.save();
                    return ContentUris.withAppendedId(uri, t.getInt("ord"));
                } catch (ConfirmModSchemaException e) {
                    throw new IllegalArgumentException("Unable to add template without user requesting/accepting full-sync", e);
                } catch (JSONException e) {
                    throw new IllegalArgumentException("Unable to get ord from new template", e);
                }
            }
        case MODELS_ID_TEMPLATES_ID:
            throw new IllegalArgumentException("Not possible to insert template with specific ORD");
        case MODELS_ID_FIELDS:
            {
                Models models = col.getModels();
                Long mid = getModelIdFromUri(uri, col);
                Model existingModel = models.get(mid);
                if (existingModel == null) {
                    throw new IllegalArgumentException("model missing: " + mid);
                }
                String name = values.getAsString(FlashCardsContract.Model.FIELD_NAME);
                if (name == null) {
                    throw new IllegalArgumentException("field name missing for model: " + mid);
                }
                JSONObject field = models.newField(name);
                try {
                    models.addField(existingModel, field);
                    col.save();
                    JSONArray flds = existingModel.getJSONArray("flds");
                    return ContentUris.withAppendedId(uri, flds.length() - 1);
                } catch (ConfirmModSchemaException e) {
                    throw new IllegalArgumentException("Unable to insert field: " + name, e);
                } catch (JSONException e) {
                    throw new IllegalArgumentException("Unable to get newly created field: " + name, e);
                }
            }
        case SCHEDULE:
            // Doesn't make sense to insert an object into the schedule table
            throw new IllegalArgumentException("Not possible to perform insert operation on schedule");
        case DECKS:
            // Insert new deck with specified name
            String deckName = values.getAsString(FlashCardsContract.Deck.DECK_NAME);
            did = col.getDecks().id(deckName, false);
            if (did != null) {
                throw new IllegalArgumentException("Deck name already exists: " + deckName);
            }
            if (!Decks.isValidDeckName(deckName)) {
                throw new IllegalArgumentException("Invalid deck name '" + deckName + "'");
            }
            did = col.getDecks().id(deckName, true);
            Deck deck = col.getDecks().get(did);
            if (deck != null) {
                try {
                    String deckDesc = values.getAsString(FlashCardsContract.Deck.DECK_DESC);
                    if (deckDesc != null) {
                        deck.put("desc", deckDesc);
                    }
                } catch (JSONException e) {
                    Timber.e(e, "Could not set a field of new deck %s", deckName);
                    return null;
                }
            }
            col.getDecks().flush();
            return Uri.withAppendedPath(FlashCardsContract.Deck.CONTENT_ALL_URI, Long.toString(did));
        case DECK_SELECTED:
            // Can't have more than one selected deck
            throw new IllegalArgumentException("Selected deck can only be queried and updated");
        case DECKS_ID:
            // Deck ID is generated automatically by libanki
            throw new IllegalArgumentException("Not possible to insert deck with specific ID");
        default:
            // Unknown URI type
            throw new IllegalArgumentException("uri " + uri + " is not supported");
    }
}
Also used : Note(com.ichi2.libanki.Note) JSONArray(com.ichi2.utils.JSONArray) JSONException(com.ichi2.utils.JSONException) Deck(com.ichi2.libanki.Deck) JSONObject(com.ichi2.utils.JSONObject) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) ConfirmModSchemaException(com.ichi2.anki.exception.ConfirmModSchemaException) Models(com.ichi2.libanki.Models)

Example 40 with TAGS

use of com.ichi2.anki.CardBrowser.Column.TAGS in project AnkiChinaAndroid by ankichinateam.

the class CardContentProvider method update.

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    if (!hasReadWritePermission() && shouldEnforceUpdateSecurity(uri)) {
        throwSecurityException("update", uri);
    }
    Collection col = CollectionHelper.getInstance().getCol(mContext);
    if (col == null) {
        throw new IllegalStateException(COL_NULL_ERROR_MSG);
    }
    col.log(getLogMessage("update", uri));
    // Find out what data the user is requesting
    int match = sUriMatcher.match(uri);
    // Number of updated entries (return value)
    int updated = 0;
    switch(match) {
        case NOTES_V2:
        case NOTES:
            throw new IllegalArgumentException("Not possible to update notes directly (only through data URI)");
        case NOTES_ID:
            {
                /* Direct access note details
                 */
                Note currentNote = getNoteFromUri(uri, col);
                // the key of the ContentValues contains the column name
                // the value of the ContentValues contains the row value.
                Set<Map.Entry<String, Object>> valueSet = values.valueSet();
                for (Map.Entry<String, Object> entry : valueSet) {
                    String key = entry.getKey();
                    // when the client does not specify FLDS, then don't update the FLDS
                    if (key.equals(FlashCardsContract.Note.FLDS)) {
                        // Update FLDS
                        Timber.d("CardContentProvider: flds update...");
                        String newFldsEncoded = (String) entry.getValue();
                        String[] flds = Utils.splitFields(newFldsEncoded);
                        // Check that correct number of flds specified
                        if (flds.length != currentNote.getFields().length) {
                            throw new IllegalArgumentException("Incorrect flds argument : " + newFldsEncoded);
                        }
                        // Update the note
                        for (int idx = 0; idx < flds.length; idx++) {
                            currentNote.setField(idx, flds[idx]);
                        }
                        updated++;
                    } else if (key.equals(FlashCardsContract.Note.TAGS)) {
                        // Update tags
                        Timber.d("CardContentProvider: tags update...");
                        Object tags = entry.getValue();
                        if (tags != null) {
                            currentNote.setTagsFromStr(String.valueOf(tags));
                        }
                        updated++;
                    } else {
                        // Unsupported column
                        throw new IllegalArgumentException("Unsupported column: " + key);
                    }
                }
                Timber.d("CardContentProvider: Saving note...");
                currentNote.flush();
                break;
            }
        case NOTES_ID_CARDS:
            // TODO: To be implemented
            throw new UnsupportedOperationException("Not yet implemented");
        // break;
        case NOTES_ID_CARDS_ORD:
            {
                Card currentCard = getCardFromUri(uri, col);
                boolean isDeckUpdate = false;
                long did = -1;
                // the key of the ContentValues contains the column name
                // the value of the ContentValues contains the row value.
                Set<Map.Entry<String, Object>> valueSet = values.valueSet();
                for (Map.Entry<String, Object> entry : valueSet) {
                    // Only updates on deck id is supported
                    String key = entry.getKey();
                    isDeckUpdate = key.equals(FlashCardsContract.Card.DECK_ID);
                    did = values.getAsLong(key);
                }
                if (col.getDecks().isDyn(did)) {
                    throw new IllegalArgumentException("Cards cannot be moved to a filtered deck");
                }
                /* now update the card
                 */
                if ((isDeckUpdate) && (did >= 0)) {
                    Timber.d("CardContentProvider: Moving card to other deck...");
                    col.getDecks().flush();
                    currentCard.setDid(did);
                    currentCard.flush();
                    col.save();
                    updated++;
                } else {
                    // User tries an operation that is not (yet?) supported.
                    throw new IllegalArgumentException("Currently only updates of decks are supported");
                }
                break;
            }
        case MODELS:
            throw new IllegalArgumentException("Cannot update models in bulk");
        case MODELS_ID:
            // Get the input parameters
            String newModelName = values.getAsString(FlashCardsContract.Model.NAME);
            String newCss = values.getAsString(FlashCardsContract.Model.CSS);
            String newDid = values.getAsString(FlashCardsContract.Model.DECK_ID);
            String newFieldList = values.getAsString(FlashCardsContract.Model.FIELD_NAMES);
            if (newFieldList != null) {
                // Changing the field names would require a full-sync
                throw new IllegalArgumentException("Field names cannot be changed via provider");
            }
            Integer newSortf = values.getAsInteger(FlashCardsContract.Model.SORT_FIELD_INDEX);
            Integer newType = values.getAsInteger(FlashCardsContract.Model.TYPE);
            String newLatexPost = values.getAsString(FlashCardsContract.Model.LATEX_POST);
            String newLatexPre = values.getAsString(FlashCardsContract.Model.LATEX_PRE);
            // Get the original note JSON
            Model model = col.getModels().get(getModelIdFromUri(uri, col));
            try {
                // Update model name and/or css
                if (newModelName != null) {
                    model.put("name", newModelName);
                    updated++;
                }
                if (newCss != null) {
                    model.put("css", newCss);
                    updated++;
                }
                if (newDid != null) {
                    if (col.getDecks().isDyn(Long.parseLong(newDid))) {
                        throw new IllegalArgumentException("Cannot set a filtered deck as default deck for a model");
                    }
                    model.put("did", newDid);
                    updated++;
                }
                if (newSortf != null) {
                    model.put("sortf", newSortf);
                    updated++;
                }
                if (newType != null) {
                    model.put("type", newType);
                    updated++;
                }
                if (newLatexPost != null) {
                    model.put("latexPost", newLatexPost);
                    updated++;
                }
                if (newLatexPre != null) {
                    model.put("latexPre", newLatexPre);
                    updated++;
                }
                col.getModels().save(model);
                col.save();
            } catch (JSONException e) {
                Timber.e(e, "JSONException updating model");
            }
            break;
        case MODELS_ID_TEMPLATES:
            throw new IllegalArgumentException("Cannot update templates in bulk");
        case MODELS_ID_TEMPLATES_ID:
            Long mid = values.getAsLong(CardTemplate.MODEL_ID);
            Integer ord = values.getAsInteger(CardTemplate.ORD);
            String name = values.getAsString(CardTemplate.NAME);
            String qfmt = values.getAsString(CardTemplate.QUESTION_FORMAT);
            String afmt = values.getAsString(CardTemplate.ANSWER_FORMAT);
            String bqfmt = values.getAsString(CardTemplate.BROWSER_QUESTION_FORMAT);
            String bafmt = values.getAsString(CardTemplate.BROWSER_ANSWER_FORMAT);
            // Throw exception if read-only fields are included
            if (mid != null || ord != null) {
                throw new IllegalArgumentException("Updates to mid or ord are not allowed");
            }
            // Update the model
            try {
                Integer templateOrd = Integer.parseInt(uri.getLastPathSegment());
                Model existingModel = col.getModels().get(getModelIdFromUri(uri, col));
                JSONArray templates = existingModel.getJSONArray("tmpls");
                JSONObject template = templates.getJSONObject(templateOrd);
                if (name != null) {
                    template.put("name", name);
                    updated++;
                }
                if (qfmt != null) {
                    template.put("qfmt", qfmt);
                    updated++;
                }
                if (afmt != null) {
                    template.put("afmt", afmt);
                    updated++;
                }
                if (bqfmt != null) {
                    template.put("bqfmt", bqfmt);
                    updated++;
                }
                if (bafmt != null) {
                    template.put("bafmt", bafmt);
                    updated++;
                }
                // Save the model
                templates.put(templateOrd, template);
                existingModel.put("tmpls", templates);
                col.getModels().save(existingModel, true);
                col.save();
            } catch (JSONException e) {
                throw new IllegalArgumentException("Model is malformed", e);
            }
            break;
        case SCHEDULE:
            {
                Set<Map.Entry<String, Object>> valueSet = values.valueSet();
                int cardOrd = -1;
                long noteID = -1;
                int ease = -1;
                long timeTaken = -1;
                int bury = -1;
                int suspend = -1;
                for (Map.Entry<String, Object> entry : valueSet) {
                    String key = entry.getKey();
                    if (key.equals(FlashCardsContract.ReviewInfo.NOTE_ID)) {
                        noteID = values.getAsLong(key);
                    } else if (key.equals(FlashCardsContract.ReviewInfo.CARD_ORD)) {
                        cardOrd = values.getAsInteger(key);
                    } else if (key.equals(FlashCardsContract.ReviewInfo.EASE)) {
                        ease = values.getAsInteger(key);
                    } else if (key.equals(FlashCardsContract.ReviewInfo.TIME_TAKEN)) {
                        timeTaken = values.getAsLong(key);
                    } else if (key.equals(FlashCardsContract.ReviewInfo.BURY)) {
                        bury = values.getAsInteger(key);
                    } else if (key.equals(FlashCardsContract.ReviewInfo.SUSPEND)) {
                        suspend = values.getAsInteger(key);
                    }
                }
                if (cardOrd != -1 && noteID != -1) {
                    Card cardToAnswer = getCard(noteID, cardOrd, col);
                    if (cardToAnswer != null) {
                        if (bury == 1) {
                            // bury card
                            buryOrSuspendCard(col, col.getSched(), cardToAnswer, true);
                        } else if (suspend == 1) {
                            // suspend card
                            buryOrSuspendCard(col, col.getSched(), cardToAnswer, false);
                        } else {
                            answerCard(col, col.getSched(), cardToAnswer, ease, timeTaken);
                        }
                        updated++;
                    } else {
                        Timber.e("Requested card with noteId %d and cardOrd %d was not found. Either the provided " + "noteId/cardOrd were wrong or the card has been deleted in the meantime.", noteID, cardOrd);
                    }
                }
                break;
            }
        case DECKS:
            throw new IllegalArgumentException("Can't update decks in bulk");
        case DECKS_ID:
            // TODO: be sure to throw exception if change to the dyn value of a deck is requested
            throw new UnsupportedOperationException("Not yet implemented");
        case DECK_SELECTED:
            {
                Set<Map.Entry<String, Object>> valueSet = values.valueSet();
                for (Map.Entry<String, Object> entry : valueSet) {
                    String key = entry.getKey();
                    if (key.equals(FlashCardsContract.Deck.DECK_ID)) {
                        long deckId = values.getAsLong(key);
                        if (selectDeckWithCheck(col, deckId)) {
                            updated++;
                        }
                    }
                }
                col.save();
                break;
            }
        default:
            // Unknown URI type
            throw new IllegalArgumentException("uri " + uri + " is not supported");
    }
    return updated;
}
Also used : Set(java.util.Set) JSONArray(com.ichi2.utils.JSONArray) JSONException(com.ichi2.utils.JSONException) Card(com.ichi2.libanki.Card) JSONObject(com.ichi2.utils.JSONObject) Note(com.ichi2.libanki.Note) Model(com.ichi2.libanki.Model) Collection(com.ichi2.libanki.Collection) JSONObject(com.ichi2.utils.JSONObject) Map(java.util.Map)

Aggregations

JSONObject (com.ichi2.utils.JSONObject)27 Note (com.ichi2.libanki.Note)18 JSONArray (com.ichi2.utils.JSONArray)18 ArrayList (java.util.ArrayList)13 Card (com.ichi2.libanki.Card)12 Collection (com.ichi2.libanki.Collection)12 Model (com.ichi2.libanki.Model)11 Map (java.util.Map)10 HashMap (java.util.HashMap)9 SuppressLint (android.annotation.SuppressLint)8 Resources (android.content.res.Resources)8 Deck (com.ichi2.libanki.Deck)8 JSONException (com.ichi2.utils.JSONException)8 JSONException (org.json.JSONException)8 Intent (android.content.Intent)7 View (android.view.View)7 TextView (android.widget.TextView)7 Pair (android.util.Pair)5 ContentValues (android.content.ContentValues)4 Uri (android.net.Uri)4