use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.
the class MultimediaCardEditorActivity method changeCurrentModel.
/**
* Change current model for the Note. Changes both MultimediaNote and the mEditorNote (Note Object) and copies
* existing values to both.
*
* @param newId
*/
protected void changeCurrentModel(long newId) {
try {
JSONObject currentModel = mCol.getModels().get(newId);
mCol.getModels().setCurrent(currentModel);
JSONObject cdeck = mCol.getDecks().current();
cdeck.put("mid", newId);
mCol.getDecks().save(cdeck);
int size = mNote.getNumberOfFields();
String[] oldValues = new String[size];
for (int i = 0; i < size; i++) {
oldValues[i] = mNote.getField(i).getFormattedValue();
}
mEditorNote = new Note(mCol, currentModel);
mEditorNote.model().put("did", mCurrentDid);
MultimediaEditableNote newNote = NoteService.createEmptyNote(currentModel);
for (int i = 0; i < newNote.getNumberOfFields(); i++) {
if (i < mNote.getNumberOfFields()) {
newNote.setField(i, mNote.getField(i));
}
}
mNote = newNote;
} catch (JSONException e) {
Log.e("Multimedia Editor", e.getMessage());
}
}
use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.
the class MultimediaCardEditorActivity method createEditorUI.
/**
* Creates the UI for editor area inside EditorLayout
*
* @param note
*/
private void createEditorUI(IMultimediaEditableNote note) {
try {
if (mNote == null) {
finishCancel();
return;
} else {
for (int i = 0; i < mNote.getNumberOfFields(); ++i) {
IField f = mNote.getField(i);
if (f == null) {
finishCancel();
return;
}
}
}
LinearLayout linearLayout = mEditorLayout;
linearLayout.removeAllViews();
for (int i = 0; i < note.getNumberOfFields(); ++i) {
createNewViewer(linearLayout, note.getField(i), i);
}
mModelButton.setText(gtxt(R.string.multimedia_editor_activity_note_type) + " : " + mEditorNote.model().getString("name"));
mDeckButton.setText(gtxt(R.string.multimedia_editor_activity_deck) + " : " + mCol.getDecks().get(mCurrentDid).getString("name"));
} catch (JSONException e) {
Log.e("Multimedia Editor", e.getMessage());
return;
}
}
use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.
the class Reviewer method onCreate.
// ----------------------------------------------------------------------------
// ANDROID METHODS
// ----------------------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
Themes.applyTheme(this);
super.onCreate(savedInstanceState);
// Remove the status bar and title bar
if (mPrefFullscreenReview) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Do not hide the title bar in Honeycomb, since it contains the action bar.
if (AnkiDroidApp.SDK_VERSION <= 11) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
mChangeBorderStyle = Themes.getTheme() == Themes.THEME_ANDROID_LIGHT || Themes.getTheme() == Themes.THEME_ANDROID_DARK;
// The hardware buttons should control the music volume while reviewing.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
Collection col = AnkiDroidApp.getCol();
if (col == null) {
reloadCollection(savedInstanceState);
return;
} else {
mSched = col.getSched();
mCollectionFilename = col.getPath();
mBaseUrl = Utils.getBaseUrl(col.getMedia().getDir());
restorePreferences();
setFullScreen(mPrefFullscreenReview);
registerExternalStorageListener();
if (mNightMode) {
mCurrentBackgroundColor = Themes.getNightModeCardBackground(this);
} else {
mCurrentBackgroundColor = Color.WHITE;
}
mUseQuickUpdate = shouldUseQuickUpdate();
initLayout(R.layout.flashcard);
try {
String[] title = mSched.getCol().getDecks().current().getString("name").split("::");
AnkiDroidApp.getCompat().setTitle(this, title[title.length - 1], mInvertedColors);
} catch (JSONException e) {
throw new RuntimeException(e);
}
AnkiDroidApp.getCompat().setSubtitle(this, "", mInvertedColors);
if (mPrefTextSelection) {
clipboardSetText("");
}
// Load the template for the card
try {
mCardTemplate = Utils.convertStreamToString(getAssets().open("card_template.html"));
} catch (IOException e) {
e.printStackTrace();
}
// Initialize text-to-speech. This is an asynchronous operation.
if (mSpeakText) {
ReadText.initializeTts(this);
}
// Get last whiteboard state
if (mPrefWhiteboard && mCurrentCard != null && MetaDB.getWhiteboardState(this, mCurrentCard.getDid()) == 1) {
mShowWhiteboard = true;
mWhiteboard.setVisibility(View.VISIBLE);
}
// Load the first card and start reviewing. Uses the answer card
// task to load a card, but since we send null
// as the card to answer, no card will be answered.
DeckTask.launchDeckTask(DeckTask.TASK_TYPE_ANSWER_CARD, mAnswerCardHandler, new DeckTask.TaskData(mSched, null, 0));
// Since we aren't actually answering a card, decrement the rep count
mSched.setReps(mSched.getReps() - 1);
}
}
use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundLoadTutorial.
private TaskData doInBackgroundLoadTutorial(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundLoadTutorial");
Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources();
Collection col = params[0].getCollection();
col.getDb().getDatabase().beginTransaction();
String title = res.getString(R.string.help_tutorial);
try {
// get deck or create it
long did = col.getDecks().id(title);
// reset todays counts
JSONObject d = col.getDecks().get(did);
for (String t : new String[] { "new", "rev", "lrn", "time" }) {
String k = t + "Today";
JSONArray ja = new JSONArray();
ja.put(col.getSched().getToday());
ja.put(0);
d.put(k, ja);
}
// save deck
col.getDecks().save(d);
if (col.getSched().cardCount("(" + did + ")") > 0) {
// deck does already exist. Remove all cards and recreate them
// to ensure the correct order
col.remCards(col.getDecks().cids(did));
}
JSONObject model = col.getModels().byName(title);
// }
if (model == null) {
model = col.getModels().addBasicModel(col, title);
}
model.put("did", did);
String[] questions = res.getStringArray(R.array.tutorial_questions);
String[] answers = res.getStringArray(R.array.tutorial_answers);
String[] sampleQuestions = res.getStringArray(R.array.tutorial_capitals_questions);
String[] sampleAnswers = res.getStringArray(R.array.tutorial_capitals_answers);
int len = Math.min(questions.length, answers.length);
for (int i = 0; i < len + Math.min(sampleQuestions.length, sampleAnswers.length); i++) {
Note note = col.newNote(model);
if (note.values().length < 2) {
return new TaskData(false);
}
note.values()[0] = (i < len) ? questions[i] : sampleQuestions[i - len];
note.values()[1] = (i < len) ? answers[i] : sampleAnswers[i - len];
col.addNote(note);
}
// deck.setSessionTimeLimit(0);
if (col.getSched().cardCount("(" + did + ")") == 0) {
// error, delete deck
col.getDecks().rem(did, true);
return new TaskData(false);
} else {
col.save();
col.getDecks().select(did);
col.getDb().getDatabase().setTransactionSuccessful();
return new TaskData(true);
}
} catch (SQLException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundLoadTutorial");
return new DeckTask.TaskData(false);
} catch (JSONException e) {
AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundLoadTutorial");
return new DeckTask.TaskData(false);
} finally {
col.getDb().getDatabase().endTransaction();
}
}
use of com.ichi2.utils.JSONException in project Anki-Android by Ramblurr.
the class DeckTask method doInBackgroundConfRemove.
private TaskData doInBackgroundConfRemove(TaskData... params) {
// Log.i(AnkiDroidApp.TAG, "doInBackgroundConfRemove");
Object[] data = params[0].getObjArray();
Collection col = (Collection) data[0];
JSONObject conf = (JSONObject) data[1];
try {
// When a conf is deleted, all decks using it revert to the default conf.
// Cards must be reordered according to the default conf.
int order = conf.getJSONObject("new").getInt("order");
int defaultOrder = col.getDecks().getConf(1).getJSONObject("new").getInt("order");
if (order != defaultOrder) {
conf.getJSONObject("new").put("order", defaultOrder);
col.getSched().resortConf(conf);
}
col.getDecks().remConf(conf.getLong("id"));
return new TaskData(true);
} catch (JSONException e) {
return new TaskData(false);
}
}
Aggregations