Exemple #1
0
 /**
  * Block the current thread until the currently running DeckTask instance (if any) has finished.
  */
 public static void waitToFinish() {
   try {
     if ((instance != null) && (instance.getStatus() != AsyncTask.Status.FINISHED)) instance.get();
   } catch (Exception e) {
     return;
   }
 }
  @Override
  public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId()) {
      case CONTEXT_MENU_MARK:
        mPositionInCardsList = info.position;
        DeckTask.launchDeckTask(
            DeckTask.TASK_TYPE_MARK_CARD,
            mMarkCardHandler,
            new DeckTask.TaskData(0, mDeck, mSelectedCard));
        return true;
      case CONTEXT_MENU_SUSPEND:
        mPositionInCardsList = info.position;
        DeckTask.launchDeckTask(
            DeckTask.TASK_TYPE_SUSPEND_CARD,
            mSuspendCardHandler,
            new DeckTask.TaskData(0, mDeck, mSelectedCard));
        return true;
      case CONTEXT_MENU_DELETE:
        mPositionInCardsList = info.position;

        Dialog dialog;
        Resources res = getResources();
        StyledDialog.Builder builder = new StyledDialog.Builder(this);
        builder.setTitle(res.getString(R.string.delete_card_title));
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage(
            String.format(
                res.getString(R.string.delete_card_message),
                mCards.get(mPositionInCardsList).get("question"),
                mCards.get(mPositionInCardsList).get("answer")));
        builder.setPositiveButton(
            res.getString(R.string.yes),
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                DeckTask.launchDeckTask(
                    DeckTask.TASK_TYPE_DELETE_CARD,
                    mDeleteCardHandler,
                    new DeckTask.TaskData(0, mDeck, mSelectedCard));
              }
            });
        builder.setNegativeButton(res.getString(R.string.no), null);
        dialog = builder.create();
        dialog.show();
        return true;
      case CONTEXT_MENU_DETAILS:
        Themes.htmlOkDialog(
                this,
                getResources().getString(R.string.card_browser_card_details),
                mSelectedCard.getCardDetails(this, true))
            .show();
        return true;
      default:
        return super.onContextItemSelected(item);
    }
  }
Exemple #3
0
  public static DeckTask launchDeckTask(int type, TaskListener listener, TaskData... params) {
    oldInstance = instance;

    instance = new DeckTask();
    instance.listener = listener;
    instance.type = type;

    return (DeckTask) instance.execute(params);
  }
Exemple #4
0
  public static DeckTask launchDeckTask(int type, TaskListener listener, TaskData... params) {
    try {
      if ((instance != null) && (instance.getStatus() != AsyncTask.Status.FINISHED)) instance.get();
    } catch (Exception e) {
      e.printStackTrace();
    }

    instance = new DeckTask();
    instance.listener = listener;
    instance.type = type;

    return (DeckTask) instance.execute(params);
  }
 private void getCards() {
   DeckTask.launchDeckTask(
       DeckTask.TASK_TYPE_LOAD_CARDS,
       mLoadCardsHandler,
       new DeckTask.TaskData(
           mDeck,
           getResources().getStringArray(R.array.card_browser_order_values)[mSelectedOrder]));
 }
Exemple #6
0
  @Override
  protected TaskData doInBackground(TaskData... params) {
    // Wait for previous thread (if any) to finish before continuing
    try {
      if ((oldInstance != null) && (oldInstance.getStatus() != AsyncTask.Status.FINISHED))
        oldInstance.get();
    } catch (Exception e) {
      Log.e(
          TAG,
          "doInBackground - Got exception while waiting for thread to finish: " + e.getMessage());
    }

    switch (type) {
      case TASK_TYPE_LOAD_DECK:
        return doInBackgroundLoadDeck(params);

      case TASK_TYPE_LOAD_DECK_AND_UPDATE_CARDS:
        TaskData taskData = doInBackgroundLoadDeck(params);
        if (taskData.integer == AnkiDroid.DECK_LOADED) {
          taskData.deck.updateAllCards();
          taskData.card = taskData.deck.getCurrentCard();
        }
        return taskData;

      case TASK_TYPE_ANSWER_CARD:
        return doInBackgroundAnswerCard(params);

      case TASK_TYPE_SUSPEND_CARD:
        return doInBackgroundSuspendCard(params);

      case TASK_TYPE_UPDATE_FACT:
        return doInBackgroundUpdateFact(params);

      default:
        return null;
    }
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == EDIT_CARD && resultCode == RESULT_OK) {
      Log.i(AnkiDroidApp.TAG, "Saving card...");
      DeckTask.launchDeckTask(
          DeckTask.TASK_TYPE_UPDATE_FACT,
          mUpdateCardHandler,
          new DeckTask.TaskData(0, mDeck, mSelectedCard));
      // TODO: code to save the changes made to the current card.
    } else if (requestCode == ADD_FACT && resultCode == RESULT_OK) {
      getCards();
    }
  }
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
   switch (item.getItemId()) {
     case MENU_UNDO:
       DeckTask.launchDeckTask(
           DeckTask.TASK_TYPE_UNDO, mUndoRedoHandler, new DeckTask.TaskData(0, mDeck, 0, true));
       return true;
     case MENU_REDO:
       DeckTask.launchDeckTask(
           DeckTask.TASK_TYPE_REDO, mUndoRedoHandler, new DeckTask.TaskData(0, mDeck, 0, true));
       return true;
     case MENU_ADD_FACT:
       startActivityForResult(new Intent(CardBrowser.this, FactAdder.class), ADD_FACT);
       if (Integer.valueOf(android.os.Build.VERSION.SDK) > 4) {
         ActivityTransitionAnimation.slide(CardBrowser.this, ActivityTransitionAnimation.LEFT);
       }
       return true;
     case MENU_SHOW_MARKED:
       mShowOnlyMarSus = true;
       mSearchEditText.setHint(R.string.card_browser_show_marked);
       mCards.clear();
       for (int i = 0; i < mAllCards.size(); i++) {
         if ((mAllCards
                         .get(i)
                         .get("question")
                         .toLowerCase()
                         .indexOf(mSearchEditText.getText().toString().toLowerCase())
                     != -1
                 || mAllCards
                         .get(i)
                         .get("answer")
                         .toLowerCase()
                         .indexOf(mSearchEditText.getText().toString().toLowerCase())
                     != -1)
             && mAllCards.get(i).get("marSus").subSequence(0, 1).equals("1")) {
           mCards.add(mAllCards.get(i));
         }
       }
       updateList();
       return true;
     case MENU_SELECT_SUSPENDED:
       mShowOnlyMarSus = true;
       mSearchEditText.setHint(R.string.card_browser_show_suspended);
       mCards.clear();
       for (int i = 0; i < mAllCards.size(); i++) {
         if ((mAllCards
                         .get(i)
                         .get("question")
                         .toLowerCase()
                         .indexOf(mSearchEditText.getText().toString().toLowerCase())
                     != -1
                 || mAllCards
                         .get(i)
                         .get("answer")
                         .toLowerCase()
                         .indexOf(mSearchEditText.getText().toString().toLowerCase())
                     != -1)
             && mAllCards.get(i).get("marSus").subSequence(1, 2).equals("1")) {
           mCards.add(mAllCards.get(i));
         }
       }
       updateList();
       return true;
     case MENU_SELECT_TAG:
       recreateTagsDialog();
       mTagsDialog.show();
       return true;
     case MENU_CHANGE_ORDER:
       showDialog(DIALOG_ORDER);
       return true;
   }
   return false;
 }
Exemple #9
0
 public void closeDeck() {
   DeckTask.waitToFinish(); // Wait for any thread working on the deck to finish.
   if (modifiedSinceSave()) commitToDB();
   AnkiDb.closeDatabase();
 }