/** * 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; } }
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); }
@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; } }