/**
   * Return the event info for a given position in the adapter
   *
   * @param positionInListView
   * @param returnEventStartDay If true, return actual event startday. Otherwise return agenda
   *     date-header date as the startDay. The two will differ for multi-day events after the first
   *     day.
   * @return
   */
  public AgendaItem getAgendaItemByPosition(
      final int positionInListView, boolean returnEventStartDay) {
    if (DEBUGLOG) Log.e(TAG, "getEventByPosition " + positionInListView);
    if (positionInListView < 0) {
      return null;
    }

    final int positionInAdapter = positionInListView - OFF_BY_ONE_BUG;
    DayAdapterInfo info = getAdapterInfoByPosition(positionInAdapter);
    if (info == null) {
      return null;
    }

    int cursorPosition = info.dayAdapter.getCursorPosition(positionInAdapter - info.offset);
    if (cursorPosition == Integer.MIN_VALUE) {
      return null;
    }

    boolean isDayHeader = false;
    if (cursorPosition < 0) {
      cursorPosition = -cursorPosition;
      isDayHeader = true;
    }

    if (cursorPosition < info.cursor.getCount()) {
      AgendaItem item = buildAgendaItemFromCursor(info.cursor, cursorPosition, isDayHeader);
      if (!returnEventStartDay && !isDayHeader) {
        item.startDay = info.dayAdapter.findJulianDayFromPosition(positionInAdapter - info.offset);
      }
      return item;
    }
    return null;
  }
  private AgendaItem buildAgendaItemFromCursor(
      final Cursor cursor, int cursorPosition, boolean isDayHeader) {
    if (cursorPosition == -1) {
      cursor.moveToFirst();
    } else {
      cursor.moveToPosition(cursorPosition);
    }
    AgendaItem agendaItem = new AgendaItem();
    agendaItem.begin = cursor.getLong(AgendaWindowAdapter.INDEX_BEGIN);
    agendaItem.end = cursor.getLong(AgendaWindowAdapter.INDEX_END);
    agendaItem.startDay = cursor.getInt(AgendaWindowAdapter.INDEX_START_DAY);
    agendaItem.allDay = cursor.getInt(AgendaWindowAdapter.INDEX_ALL_DAY) != 0;
    if (agendaItem.allDay) { // UTC to Local time conversion
      Time time = new Time(mTimeZone);
      time.setJulianDay(Time.getJulianDay(agendaItem.begin, 0));
      agendaItem.begin = time.toMillis(false /* use isDst */);
    } else if (isDayHeader) { // Trim to midnight.
      Time time = new Time(mTimeZone);
      time.set(agendaItem.begin);
      time.hour = 0;
      time.minute = 0;
      time.second = 0;
      agendaItem.begin = time.toMillis(false /* use isDst */);
    }

    // If this is not a day header, then it's an event.
    if (!isDayHeader) {
      agendaItem.id = cursor.getLong(AgendaWindowAdapter.INDEX_EVENT_ID);
      if (agendaItem.allDay) {
        Time time = new Time(mTimeZone);
        time.setJulianDay(Time.getJulianDay(agendaItem.end, 0));
        agendaItem.end = time.toMillis(false /* use isDst */);
      }
    }
    return agendaItem;
  }