/**
   * Fills the list with the available recordings. Only the recordings that are scheduled are added
   * to the list.
   */
  private void populateList() {
    // Clear the list and add the recordings
    adapter.clear();
    TVHClientApplication app = (TVHClientApplication) activity.getApplication();
    for (SeriesRecording srec : app.getSeriesRecordings()) {
      adapter.add(srec);
    }
    // Show the newest scheduled recordings first
    adapter.sort(Constants.RECORDING_SORT_DESCENDING);
    adapter.notifyDataSetChanged();

    if (toolbar != null) {
      onPrepareToolbarMenu(toolbar.getMenu());
      toolbar.setTitle(getString(R.string.series_recordings));
      if (adapter.getCount() > 0) {
        toolbar.setSubtitle(adapter.getCount() + " " + getString(R.string.items_available));
      } else {
        toolbar.setSubtitle(R.string.no_recordings_scheduled);
      }
    }
    // Inform the listeners that the channel list is populated.
    // They could then define the preselected list item.
    if (fragmentStatusInterface != null) {
      fragmentStatusInterface.onListPopulated(TAG);
    }
  }
  @Override
  public void setInitialSelection(int position) {
    setSelection(position, 0);

    // Set the position in the adapter so that we can show the selected
    // recording in the theme with the arrow.
    if (adapter != null) {
      SeriesRecording srec = null;
      if (adapter.getCount() > position) {
        adapter.setPosition(position);
        srec = (SeriesRecording) adapter.getItem(position);
      }

      // Simulate a click in the list item to inform the activity
      // It will then show the details fragment if dual pane is active
      if (isDualPane) {
        if (fragmentStatusInterface != null) {
          fragmentStatusInterface.onListItemSelected(position, srec, TAG);
        }
      }
    }
  }
  /**
   * @param item
   * @return
   */
  private boolean onToolbarItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_record_cancel_all:
        // Show a confirmation dialog before deleting all recordings
        new AlertDialog.Builder(activity)
            .setTitle(R.string.menu_record_cancel_all)
            .setMessage(getString(R.string.cancel_all_recordings))
            .setPositiveButton(
                android.R.string.yes,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    removeAllRecordings();
                  }
                })
            .setNegativeButton(
                android.R.string.no,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int which) {
                    // NOP
                  }
                })
            .show();
        return true;

      case R.id.menu_add:
        Bundle args = new Bundle();
        DialogFragment newFragment = SeriesRecordingAddFragment.newInstance(args);
        newFragment.show(activity.getSupportFragmentManager(), "dialog");
        return true;

      case R.id.menu_refresh:
        fragmentStatusInterface.reloadData(TAG);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }
  /**
   * Adds all programs from the program guide to the current view. Only those programs that are
   * within the defined time slot are added. If the last program was reached, a call to load more
   * programs is made.
   */
  public void addPrograms() {

    // Clear all previously shown programs
    layout.removeAllViews();

    // Show that no programs are available
    if (channel == null || channel.epg.isEmpty()) {
      addEmptyProgramToView();
      return;
    }

    // Reset the usable width for the layout of a single program guide item
    // to the entire display width. After each adding of a program item the
    // width will be reduced.
    displayWidthRemaining = displayWidth;

    // Indicates that at least one program has been added
    boolean programAdded = false;

    // Indicated that the last program in the list has
    // been found. More program need to be loaded.
    boolean lastProgramFound = false;

    // Defaults
    int programType = PROGRAM_TIMESLOT_ERROR;
    int programsAddedCounter = 0;

    try {
      // Go through all programs and add them to the view
      synchronized (channel.epg) {
        Iterator<Program> it = channel.epg.iterator();
        Program p = null;
        while (it.hasNext()) {
          p = it.next();

          // Get the type of the program and add it to the view
          programType = getProgramType(p);
          addCurrentProgram(p, programType, programsAddedCounter);

          // The program is only considered as added when it was
          // somehow within the timeslot. Required further down to
          // check if it was the last one added
          programAdded =
              ((programType != PROGRAM_BEFORE_TIMESLOT)
                  || (programType != PROGRAM_AFTER_TIMESLOT)
                  || (programType != PROGRAM_UNKNOWN_TIMESLOT));

          // Increase the counter which is required to fill in placeholder
          // programs in case the first program in the guide data is
          // already within the time slot and not one that moves into one.
          if (programAdded) {
            programsAddedCounter += 1;
          }
          // Check if there is more guide data available
          lastProgramFound = !it.hasNext();

          // Stop adding more programs if the last program is within the
          // time slot and no more data is available or the program added
          // is the last one that fits into or overlaps the time slot.
          if ((programType == PROGRAM_IS_WITHIN_TIMESLOT && lastProgramFound)
              || programType == PROGRAM_MOVES_OUT_OF_TIMESLOT
              || programType == PROGRAM_OVERLAPS_TIMESLOT) {
            break;
          }
        }
      }
    } catch (NoSuchElementException e) {
      Log.e(TAG, "The selected channel contains no programs.");
    }

    TVHClientApplication app = (TVHClientApplication) activity.getApplication();

    // Add the loading indication only when the channel is not blocked and
    // the program is the last one and overlaps the timeslot somehow.
    // Otherwise show that no program data is available.
    if (!app.isChannelBlocked(channel)
        && lastProgramFound
        && (programType == PROGRAM_MOVES_INTO_TIMESLOT
            || programType == PROGRAM_IS_WITHIN_TIMESLOT)) {
      addLoadingIndication();
    } else {
      addEmptyProgramToView();
    }

    // If the program that was last added was added in the view and it was
    // the last program in the guide then try to load more programs.
    // Also load programs when no program at all was added.
    if (!app.isChannelBlocked(channel) && ((programAdded && lastProgramFound) || !programAdded)) {
      if (fragmentStatusInterface != null) {
        fragmentStatusInterface.moreDataRequired(channel, TAG);
      }
    }
  }