/** * Return the current status of the widget. * * @return an int array, containing due, progress, eta */ public static int[] getWidgetSmallStatus(Context context) { openDBIfClosed(context); Cursor cursor = null; try { cursor = mMetaDb.query( "smallWidgetStatus", new String[] {"progress", "left", "eta"}, null, null, null, null, null); while (cursor.moveToNext()) { return (new int[] {cursor.getInt(0), cursor.getInt(1), cursor.getInt(2)}); } } catch (SQLiteException e) { Timber.e(e, "Error while querying widgetStatus"); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } return null; }
/** * Stores the current state of the widget. * * <p>It replaces any stored state for the widget. * * @param decks an array of {@link DeckStatus} objects, one for each of the know decks. */ public static void storeWidgetStatus(Context context, DeckStatus[] decks) { openDBIfClosed(context); try { mMetaDb.beginTransaction(); try { // First clear all the existing content. mMetaDb.execSQL("DELETE FROM widgetStatus"); for (DeckStatus deck : decks) { mMetaDb.execSQL( "INSERT INTO widgetStatus(deckId, deckName, newCards, lrnCards, dueCards, progress, eta) " + "VALUES (?, ?, ?, ?, ?, ?, ?)", new Object[] { deck.mDeckId, deck.mDeckName, deck.mNewCards, deck.mLrnCards, deck.mDueCards, deck.mProgress, deck.mEta }); } mMetaDb.setTransactionSuccessful(); } finally { mMetaDb.endTransaction(); } } catch (IllegalStateException e) { Timber.e(e, "MetaDB.storeWidgetStatus: failed"); } catch (SQLiteException e) { Timber.e(e, "MetaDB.storeWidgetStatus: failed"); closeDB(); Timber.i("MetaDB:: Trying to reset Widget: " + resetWidget(context)); } }
/** * Stores a custom dictionary for a given deck. * * @param dictionary integer number of dictionary, -1 if not set (standard dictionary will be * used) */ public static void storeLookupDictionary(Context context, long did, int dictionary) { openDBIfClosed(context); Cursor cur = null; try { cur = mMetaDb.rawQuery("SELECT _id FROM customDictionary" + " WHERE did = " + did, null); if (cur.moveToNext()) { mMetaDb.execSQL( "UPDATE customDictionary " + "SET did = " + did + ", " + "dictionary=" + Integer.toString(dictionary) + " " + "WHERE _id=" + cur.getString(0) + ";"); Timber.i("MetaDB:: Store custom dictionary (%d) for deck %d", dictionary, did); } else { mMetaDb.execSQL( "INSERT INTO customDictionary (did, dictionary) VALUES (?, ?)", new Object[] {did, dictionary}); Timber.i("MetaDB:: Store custom dictionary (%d) for deck %d", dictionary, did); } } catch (Exception e) { Timber.e(e, "Error storing custom dictionary to MetaDB "); } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } }
/** * Stores the state of the whiteboard for a given deck. * * @param state 1 if the whiteboard should be shown, 0 otherwise */ public static void storeWhiteboardState(Context context, long did, boolean whiteboardState) { int state = (whiteboardState) ? 1 : 0; openDBIfClosed(context); Cursor cur = null; try { cur = mMetaDb.rawQuery("SELECT _id FROM whiteboardState" + " WHERE did = " + did, null); if (cur.moveToNext()) { mMetaDb.execSQL( "UPDATE whiteboardState " + "SET did = " + did + ", " + "state=" + Integer.toString(state) + " " + "WHERE _id=" + cur.getString(0) + ";"); Timber.d("Store whiteboard state (%d) for deck %d", state, did); } else { mMetaDb.execSQL( "INSERT INTO whiteboardState (did, state) VALUES (?, ?)", new Object[] {did, state}); Timber.d("Store whiteboard state (%d) for deck %d", state, did); } } catch (Exception e) { Timber.e(e, "Error storing whiteboard state in MetaDB "); } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } }
/** * Returns the language associated with the given deck, model and card model, for the given type. * * @param qa the part of the card for which to store the association, {@link * #LANGUAGES_QA_QUESTION}, {@link #LANGUAGES_QA_ANSWER}, or {@link #LANGUAGES_QA_UNDEFINED} * return the language associate with the type, as a two-characters, lowercase string, or the * empty string if no association is defined */ public static String getLanguage(Context context, long did, int ord, int qa) { openDBIfClosed(context); String language = ""; Cursor cur = null; try { String query = "SELECT language FROM languages " + "WHERE did = " + did + " AND ord = " + ord + " AND qa = " + qa + " " + "LIMIT 1"; cur = mMetaDb.rawQuery(query, null); Timber.v("getLanguage: %s", query); if (cur.moveToNext()) { language = cur.getString(0); } } catch (Exception e) { Timber.e(e, "Error fetching language "); } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } return language; }
public static void saveIntentInformation(Context context, String fields) { openDBIfClosed(context); try { mMetaDb.execSQL( "INSERT INTO intentInformation (fields) " + " VALUES (?);", new Object[] {fields}); Timber.i("MetaDB:: Store intentInformation: " + fields); } catch (Exception e) { Timber.e(e, "Error storing intentInformation in MetaDB "); } }
/** * Resets all the language associates for a given deck. * * @return whether an error occurred while resetting the language for the deck */ public static boolean resetDeckLanguages(Context context, long did) { openDBIfClosed(context); try { mMetaDb.execSQL("DELETE FROM languages WHERE did = " + did + ";"); Timber.i("MetaDB:: Resetting language assignment for deck %d", did); return true; } catch (Exception e) { Timber.e(e, "Error resetting deck language"); } return false; }
/** * Associates a language to a deck, model, and card model for a given type. * * @param qa the part of the card for which to store the association, {@link * #LANGUAGES_QA_QUESTION}, {@link #LANGUAGES_QA_ANSWER}, or {@link #LANGUAGES_QA_UNDEFINED} * @param language the language to associate, as a two-characters, lowercase string */ public static void storeLanguage(Context context, long did, int ord, int qa, String language) { openDBIfClosed(context); try { mMetaDb.execSQL( "INSERT INTO languages (did, ord, qa, language) " + " VALUES (?, ?, ?, ?);", new Object[] {did, ord, qa, language}); Timber.v("Store language for deck %d", did); } catch (Exception e) { Timber.e(e, "Error storing language in MetaDB "); } }
/** * Returns the state of the whiteboard for the given deck. * * @return 1 if the whiteboard should be shown, 0 otherwise */ public static boolean getWhiteboardState(Context context, long did) { openDBIfClosed(context); Cursor cur = null; try { cur = mMetaDb.rawQuery("SELECT state FROM whiteboardState" + " WHERE did = " + did, null); if (cur.moveToNext()) { return cur.getInt(0) > 0; } else { return false; } } catch (Exception e) { Timber.e(e, "Error retrieving whiteboard state from MetaDB "); return false; } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } }
/** * Returns a custom dictionary associated to a deck * * @return integer number of dictionary, -1 if not set (standard dictionary will be used) */ public static int getLookupDictionary(Context context, long did) { openDBIfClosed(context); Cursor cur = null; try { cur = mMetaDb.rawQuery("SELECT dictionary FROM customDictionary" + " WHERE did = " + did, null); if (cur.moveToNext()) { return cur.getInt(0); } else { return -1; } } catch (Exception e) { Timber.e(e, "Error retrieving custom dictionary from MetaDB "); return -1; } finally { if (cur != null && !cur.isClosed()) { cur.close(); } } }
/** * Return the current status of the widget. * * @return an array of {@link DeckStatus} objects, each representing the status of one of the * known decks */ public static DeckStatus[] getWidgetStatus(Context context) { openDBIfClosed(context); Cursor cursor = null; try { cursor = mMetaDb.query( "widgetStatus", new String[] { "deckId", "deckName", "newCards", "lrnCards", "dueCards", "progress", "eta" }, null, null, null, null, "deckName"); int count = cursor.getCount(); DeckStatus[] decks = new DeckStatus[count]; for (int index = 0; index < count; ++index) { if (!cursor.moveToNext()) { throw new SQLiteException("cursor count was incorrect"); } decks[index] = new DeckStatus( cursor.getLong(cursor.getColumnIndexOrThrow("deckId")), cursor.getString(cursor.getColumnIndexOrThrow("deckName")), cursor.getInt(cursor.getColumnIndexOrThrow("newCards")), cursor.getInt(cursor.getColumnIndexOrThrow("lrnCards")), cursor.getInt(cursor.getColumnIndexOrThrow("dueCards")), cursor.getInt(cursor.getColumnIndexOrThrow("progress")), cursor.getInt(cursor.getColumnIndexOrThrow("eta"))); } return decks; } catch (SQLiteException e) { Timber.e(e, "Error while querying widgetStatus"); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } return new DeckStatus[0]; }
public static ArrayList<HashMap<String, String>> getIntentInformation(Context context) { openDBIfClosed(context); Cursor cursor = null; ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); try { cursor = mMetaDb.query( "intentInformation", new String[] {"id", "fields"}, null, null, null, null, "id"); while (cursor.moveToNext()) { HashMap<String, String> item = new HashMap<String, String>(); item.put("id", Integer.toString(cursor.getInt(0))); String fields = cursor.getString(1); String[] split = Utils.splitFields(fields); String source = null; String target = null; for (int i = 0; i < split.length; i++) { if (source == null || source.length() == 0) { source = split[i]; } else if (target == null || target.length() == 0) { target = split[i]; } else { break; } } item.put("source", source); item.put("target", target); item.put("fields", fields); list.add(item); } } catch (SQLiteException e) { upgradeDB(mMetaDb, DATABASE_VERSION); Timber.e(e, "Error while querying intentInformation"); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } return list; }
public static void storeSmallWidgetStatus(Context context, float[] progress) { openDBIfClosed(context); try { mMetaDb.beginTransaction(); try { // First clear all the existing content. mMetaDb.execSQL("DELETE FROM smallWidgetStatus"); mMetaDb.execSQL( "INSERT INTO smallWidgetStatus(progress, left, eta) VALUES (?, ?, ?)", new Object[] {(int) (progress[1] * 1000), (int) progress[2], (int) progress[3]}); mMetaDb.setTransactionSuccessful(); } finally { mMetaDb.endTransaction(); } } catch (IllegalStateException e) { Timber.e(e, "MetaDB.storeSmallWidgetStatus: failed"); } catch (SQLiteException e) { Timber.e(e, "MetaDB.storeSmallWidgetStatus: failed"); closeDB(); Timber.i("MetaDB:: Trying to reset Widget: " + resetWidget(context)); } }
public static int getNotificationStatus(Context context) { openDBIfClosed(context); Cursor cursor = null; int due = 0; try { cursor = mMetaDb.query("smallWidgetStatus", new String[] {"left"}, null, null, null, null, null); if (cursor.moveToFirst()) { return cursor.getInt(0); } // while (cursor.moveToNext()) { // due += cursor.getInt(0) + cursor.getInt(1) + cursor.getInt(2); // } } catch (SQLiteException e) { Timber.e(e, "Error while querying widgetStatus"); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } return due; }
/** Reset the content of the meta-db, erasing all its content. */ public static boolean resetDB(Context context) { openDBIfClosed(context); try { mMetaDb.execSQL("DROP TABLE IF EXISTS languages;"); Timber.i("MetaDB:: Resetting all language assignment"); mMetaDb.execSQL("DROP TABLE IF EXISTS whiteboardState;"); Timber.i("MetaDB:: Resetting whiteboard state"); mMetaDb.execSQL("DROP TABLE IF EXISTS customDictionary;"); Timber.i("MetaDB:: Resetting custom Dictionary"); mMetaDb.execSQL("DROP TABLE IF EXISTS widgetStatus;"); Timber.i("MetaDB:: Resetting widget status"); mMetaDb.execSQL("DROP TABLE IF EXISTS smallWidgetStatus;"); Timber.i("MetaDB:: Resetting small widget status"); mMetaDb.execSQL("DROP TABLE IF EXISTS intentInformation;"); Timber.i("MetaDB:: Resetting intentInformation"); upgradeDB(mMetaDb, DATABASE_VERSION); return true; } catch (Exception e) { Timber.e(e, "Error resetting MetaDB "); } return false; }