/** Insert a set of flashcards (linked list) in a single transaction. */ private void insertFlashcardSet(Flashcard head) { openDB(); SQLiteStatement stmt = getCompiledStatement(SQL_INSERT_FC); boolean gotError = false; _curDB.beginTransaction(); try { for (Flashcard fc = head; fc != null; fc = Flashcard.Chain.getNext(fc)) { stmt.bindLong(1, _importState.getRandomId()); stmt.bindString(2, fc.getLang1Str()); stmt.bindString(3, fc.getLang2Str()); stmt.execute(); } _curDB.setTransactionSuccessful(); } catch (SQLException e) { gotError = true; MsgDispatcher.sendMessageToUI(MsgType.MSG_SHOW_ERROR_MSG, 0, 0, "unable to insert fc"); } finally { _curDB.endTransaction(); } Flashcard.Chain.releaseChain(head); // tell controller to send us more data if insert went fine. if (!gotError) { MsgDispatcher.sendMessageToController(MsgType.MSG_CONTINUE_IMPORT, 0, 0, null); } }
/** * handle a vertical or horizontal fling from within this Activity. If we have only shown the * first half of the current card so far, then the second half is shown to the user. Otherwise, a * new card is displayed. * * <p>Called by {@link AxialFlingListener} when it detects a vertical or horizontal fling. */ private void handleFling(boolean isCorrectGuess) { if (FlashcardApp.getInstance().isShowEntireCard()) { // if we are already waiting for a card, ignore extra fling. if (_waitingForNextCard) { return; } _waitingForNextCard = true; MsgDispatcher.sendMessageToController(MsgType.MSG_GET_NEXT_FC, 0, 0, null); Flashcard fc = FlashcardApp.getInstance().getCurrentFlashcard(); if (fc != null) { // save current fc as the previous fc. must clone it as the one // we send to update guess count may be released soon. if (_prevFlashcard != null) _prevFlashcard.release(); _prevFlashcard = fc.clone(); int arg1 = isCorrectGuess ? 1 : 0; MsgDispatcher.sendMessageToController(MsgType.MSG_FC_GUESS_RESULT, arg1, 0, fc); } } else { FlashcardApp.getInstance().setShowEntireCard(true); updateDisplay(); } }
public FlashcardDB() { super("db"); this.start(); Handler h = new Handler(this.getLooper(), this); MsgDispatcher.setDbHandler(h); }
/** update the level and count for the given flashcard in the database. */ private void updateFlashcard(Flashcard fc) { openDB(); _curDB.beginTransaction(); try { // Delete old entry if any. SQLiteStatement dStmt = getCompiledStatement(SQL_DELETE_FCS); dStmt.bindString(1, fc.getLang1Str()); dStmt.execute(); // insert new state entry. SQLiteStatement iStmt = getCompiledStatement(SQL_INSERT_FCS); iStmt.bindString(1, fc.getLang1Str()); iStmt.bindLong(2, fc.getLevel()); iStmt.bindLong(3, fc.getRightGuessCount()); iStmt.execute(); _curDB.setTransactionSuccessful(); } catch (SQLException e) { MsgDispatcher.sendMessageToUI( MsgType.MSG_SHOW_ERROR_MSG, 0, 0, "unable to update id=" + fc.getID()); } finally { _curDB.endTransaction(); } // this flashcard is no longer used by anyone so release it. fc.release(); }
/** create the database schema (tables). Assumes database is already open. */ private void createSchema() { try { _curDB.execSQL(SQL_CREATE_FC_TBL); _curDB.execSQL(SQL_CREATE_FCS_TBL); } catch (SQLException e) { MsgDispatcher.sendMessageToUI(MsgType.MSG_SHOW_ERROR_MSG, 0, 0, "unable to create schema"); } }
/** handle selection of a menu item from menu.xml. */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_import: MsgDispatcher.sendMessageToController(MsgType.MSG_MENU_IMPORT, 0, 0, null); return true; } return super.onOptionsItemSelected(item); }
/** * perform a query of the database for the next set of flashcards at the given level and starting * at given id. At first, a query from the current minId is performed. If insufficient results are * returned, then a query from the beginning of the table is tried. */ private void queryFlashcardSet(int level, int minId) { openDB(); int requiredCount = AppConfig._maxQuerySetSize; QueryResult qr = new QueryResult(); qr.level = level; // perform the first query. If insufficient data was returned, // perform a second query from the beginning of the table. queryNoWrap(qr, level, minId, requiredCount); qr.log(0); if (qr.count < requiredCount && minId > 1) { qr.maxId = 0; queryNoWrap(qr, level, 0, requiredCount); qr.log(1); } // send the query result (potentially partial] to controller. MsgDispatcher.sendMessageToController(MsgType.MSG_RESULT_FC_SET, 0, 0, qr); }
@Override public void onCreate(Bundle savedInstanceState) { if (LP.LOG_LIFECYCLE_EVENTS) Log.d(LP.TAG, "[MA] OnCreate() called."); super.onCreate(savedInstanceState); setContentView(R.layout.main); // create the UI handler here so it will be associated with the main // UI thread. MsgDispatcher.setUiHandler(new UiHandler()); _topText = (TextView) findViewById(R.id.text_top); _bottomText = (TextView) findViewById(R.id.text_bottom); _statusText = (TextView) findViewById(R.id.text_status); _gestureDetector = new GestureDetector(this, new AxialFlingListener()); // save the singleton instance. make sure to remove this instance // when this activity is destroyed. setInstance(this); // TODO: send out request for next flashcard to prime the pump. }