/** When a task has ended, see if we are finished (no more tasks running). If so, finish. */
  @Override
  public void onTaskEnded(TaskManager manager, ManagedTask task) {
    int size;
    // System.out.println(task.getClass().getSimpleName() + "(" +  + task.getId() + ") FINISHED
    // starting");

    // Handle the result, and optionally queue another task
    if (task instanceof SearchThread) handleSearchTaskFinished((SearchThread) task);

    // Remove the finished task, and terminate if no more.
    synchronized (mRunningTasks) {
      mRunningTasks.remove(task);
      size = mRunningTasks.size();
      // for(ManagedTask t: mRunningTasks) {
      //	System.out.println(t.getClass().getSimpleName() + "(" +  + t.getId() + ") still running");
      // }
    }
    if (size == 0) {
      // Stop listening FIRST...otherwise, if sendResults() calls a listener that starts
      // a new task, we will stop listening for the new task.
      TaskManager.getMessageSwitch().removeListener(mTaskManager.getSenderId(), this);
      System.out.println("Not listening(1)");
      // Notify the listeners.
      sendResults();
    }
    // System.out.println(task.getClass().getSimpleName() + "(" +  + task.getId() + ") FINISHED
    // Exiting");
  }
  private void doSearch() {
    // List for task ends
    TaskManager.getMessageSwitch().addListener(mTaskManager.getSenderId(), this, false);
    // System.out.println("Listening");

    // We really want to ensure we get the same book from each, so if isbn is not present, do
    // these in series.

    boolean tasksStarted = false;
    mSearchingAsin = false;
    try {
      if (mIsbn != null && mIsbn.length() > 0) {
        if (IsbnUtils.isValid(mIsbn)) {
          // We have an ISBN, just do the search
          mWaitingForIsbn = false;
          tasksStarted = this.startSearches(mSearchFlags);
        } else {
          // Assume it's an ASIN, and just search Amazon
          mSearchingAsin = true;
          mWaitingForIsbn = false;
          // mSearchFlags = SEARCH_AMAZON;
          tasksStarted = startOneSearch(SEARCH_AMAZON);
          // tasksStarted = this.startSearches(mSearchFlags);
        }
      } else {
        // Run one at a time, startNext() defined the order.
        mWaitingForIsbn = true;
        tasksStarted = startNext();
      }
    } finally {
      if (!tasksStarted) {
        sendResults();
        TaskManager.getMessageSwitch().removeListener(mTaskManager.getSenderId(), this);
        // System.out.println("Not listening(2)");
      }
    }
  }