@Override
    protected Payload doInBackground(Payload... args) {

      Payload data = doInBackgroundLoadDeck(args);
      if (data.returnType == DeckTask.DECK_LOADED) {
        double now = System.currentTimeMillis();
        HashMap<String, Object> results = (HashMap<String, Object>) data.result;
        Deck deck = (Deck) results.get("deck");
        // deck.beforeUpdateCards();
        // deck.updateAllCards();
        SharedDeckDownload download = (SharedDeckDownload) args[0].data[0];
        SharedPreferences pref = PrefSettings.getSharedPrefs(getBaseContext());
        String updatedCardsPref =
            "numUpdatedCards:" + mDestination + "/tmp/" + download.getTitle() + ".anki.updating";
        long totalCards = deck.retrieveCardCount();
        download.setNumTotalCards((int) totalCards);
        long updatedCards = pref.getLong(updatedCardsPref, 0);
        download.setNumUpdatedCards((int) updatedCards);
        long batchSize = Math.max(100, totalCards / 200);
        mRecentBatchTimings = new long[sRunningAvgLength];
        mTotalBatches = ((double) totalCards) / batchSize;
        int currentBatch = (int) (updatedCards / batchSize);
        long runningAvgCount = 0;
        long batchStart;
        mElapsedTime = 0;
        while (updatedCards < totalCards
            && download.getStatus() == SharedDeckDownload.STATUS_UPDATING) {
          batchStart = System.currentTimeMillis();
          updatedCards = deck.updateAllCardsFromPosition(updatedCards, batchSize);
          Editor editor = pref.edit();
          editor.putLong(updatedCardsPref, updatedCards);
          editor.commit();
          download.setNumUpdatedCards((int) updatedCards);
          publishProgress();
          estimateTimeToCompletion(
              download, currentBatch, runningAvgCount, System.currentTimeMillis() - batchStart);
          currentBatch++;
          runningAvgCount++;
        }
        if (download.getStatus() == SharedDeckDownload.STATUS_UPDATING) {
          data.success = true;
        } else if (download.getStatus() == SharedDeckDownload.STATUS_PAUSED) {
          Editor editor = pref.edit();
          String pausedPref =
              "paused:" + mDestination + "/tmp/" + download.getTitle() + ".anki.updating";
          editor.putBoolean(pausedPref, true);
          editor.commit();
          data.success = false;
          Log.i(AnkiDroidApp.TAG, "pausing deck " + download.getTitle());
        } else if (download.getStatus() == SharedDeckDownload.STATUS_CANCELLED) {
          data.success = false;
        }
        // Log.i(AnkiDroidApp.TAG, "Time to update deck = " + download.getEstTimeToCompletion() + "
        // sec.");
        // deck.afterUpdateCards();
      } else {
        data.success = false;
      }
      return data;
    }