/**
  * Cursor to timer.
  *
  * @param c the c
  * @return the timer´
  * @author RayBa
  * @date 07.04.2013
  */
 private Timer cursorToTimer(Cursor c) {
   String name = c.getString(c.getColumnIndex(ChannelTbl.NAME));
   long channelID = c.getLong(c.getColumnIndex(ChannelTbl.CHANNEL_ID));
   String epgTitle =
       !c.isNull(c.getColumnIndex(EpgTbl.TITLE))
           ? c.getString(c.getColumnIndex(EpgTbl.TITLE))
           : name;
   long epgStart = c.getLong(c.getColumnIndex(EpgTbl.START));
   long epgEnd = c.getLong(c.getColumnIndex(EpgTbl.END));
   DVBViewerPreferences prefs = new DVBViewerPreferences(getSherlockActivity());
   int epgBefore = prefs.getPrefs().getInt(DVBViewerPreferences.KEY_TIMER_TIME_BEFORE, 5);
   int epgAfter = prefs.getPrefs().getInt(DVBViewerPreferences.KEY_TIMER_TIME_AFTER, 5);
   Date start = epgStart > 0 ? new Date(epgStart) : new Date();
   Date end = epgEnd > 0 ? new Date(epgEnd) : new Date(start.getTime() + (1000 * 60 * 120));
   Log.i(ChannelList.class.getSimpleName(), "start: " + start.toString());
   Log.i(ChannelList.class.getSimpleName(), "end: " + end.toString());
   start = DateUtils.addMinutes(start, 0 - epgBefore);
   end = DateUtils.addMinutes(end, epgAfter);
   Timer timer = new Timer();
   timer.setTitle(epgTitle);
   timer.setChannelId(channelID);
   timer.setChannelName(name);
   timer.setStart(start);
   timer.setEnd(end);
   timer.setTimerAction(
       prefs.getPrefs().getInt(DVBViewerPreferences.KEY_TIMER_DEF_AFTER_RECORD, 0));
   return timer;
 }
 /* (non-Javadoc)
  * @see java.lang.Comparable#compareTo(java.lang.Object)
  */
 public int compareTo(Timer comparator) {
   return this.start.compareTo(comparator.getStart());
 }
  /*
   * (non-Javadoc)
   *
   * @see
   * android.support.v4.app.Fragment#onContextItemSelected(android.view.MenuItem
   * )
   */
  public boolean onContextItemSelected(MenuItem item) {
    if (item.getMenuInfo() != null) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      selectedPosition = info.position;
    }
    Cursor c = mAdapter.getCursor();
    c.moveToPosition(selectedPosition);
    Channel chan = cursorToChannel(c);
    Timer timer;
    switch (item.getItemId()) {
      case R.id.menuTimer:
        timer = cursorToTimer(c);
        if (UIUtils.isTablet(getActivity())) {
          TimerDetails timerdetails = TimerDetails.newInstance();
          Bundle args = new Bundle();
          args.putString(TimerDetails.EXTRA_TITLE, timer.getTitle());
          args.putString(TimerDetails.EXTRA_CHANNEL_NAME, timer.getChannelName());
          args.putLong(TimerDetails.EXTRA_CHANNEL_ID, timer.getChannelId());
          args.putLong(TimerDetails.EXTRA_START, timer.getStart().getTime());
          args.putLong(TimerDetails.EXTRA_END, timer.getEnd().getTime());
          args.putInt(TimerDetails.EXTRA_ACTION, timer.getTimerAction());
          args.putBoolean(TimerDetails.EXTRA_ACTIVE, true);
          timerdetails.setArguments(args);
          timerdetails.show(
              getSherlockActivity().getSupportFragmentManager(), TimerDetails.class.getName());
        } else {
          Intent timerIntent = new Intent(getActivity(), TimerDetailsActivity.class);
          timerIntent.putExtra(TimerDetails.EXTRA_TITLE, timer.getTitle());
          timerIntent.putExtra(TimerDetails.EXTRA_CHANNEL_NAME, timer.getChannelName());
          timerIntent.putExtra(TimerDetails.EXTRA_CHANNEL_ID, timer.getChannelId());
          timerIntent.putExtra(TimerDetails.EXTRA_START, timer.getStart().getTime());
          timerIntent.putExtra(TimerDetails.EXTRA_END, timer.getEnd().getTime());
          timerIntent.putExtra(TimerDetails.EXTRA_ACTION, timer.getTimerAction());
          timerIntent.putExtra(TimerDetails.EXTRA_ACTIVE, !timer.isFlagSet(Timer.FLAG_DISABLED));
          startActivity(timerIntent);
        }
        return true;
      case R.id.menuStream:
        if (UIUtils.isTablet(getActivity())) {
          StreamConfig cfg = StreamConfig.newInstance();
          Bundle arguments = new Bundle();
          arguments.putInt(StreamConfig.EXTRA_FILE_ID, chan.getPosition());
          arguments.putInt(StreamConfig.EXTRA_FILE_TYPE, StreamConfig.FILE_TYPE_LIVE);
          arguments.putInt(StreamConfig.EXTRA_DIALOG_TITLE_RES, R.string.streamConfig);
          cfg.setArguments(arguments);
          cfg.show(getSherlockActivity().getSupportFragmentManager(), StreamConfig.class.getName());
        } else {
          Intent streamConfig = new Intent(getActivity(), StreamConfigActivity.class);
          streamConfig.putExtra(StreamConfig.EXTRA_FILE_ID, chan.getPosition());
          streamConfig.putExtra(StreamConfig.EXTRA_FILE_TYPE, StreamConfig.FILE_TYPE_LIVE);
          streamConfig.putExtra(StreamConfig.EXTRA_DIALOG_TITLE_RES, R.string.streamConfig);
          startActivity(streamConfig);
        }
        return true;
      case R.id.menuSwitch:
        String switchRequest = ServerConsts.URL_SWITCH_COMMAND + chan.getPosition();
        DVBViewerCommand command = new DVBViewerCommand(switchRequest);
        Thread exexuterTHread = new Thread(command);
        exexuterTHread.start();
        return true;
      case R.id.menuRecord:
        timer = cursorToTimer(c);
        String url =
            timer.getId() < 0l ? ServerConsts.URL_TIMER_CREATE : ServerConsts.URL_TIMER_EDIT;
        String title = timer.getTitle();
        String days = String.valueOf(DateUtils.getDaysSinceDelphiNull(timer.getStart()));
        String start = String.valueOf(DateUtils.getMinutesOfDay(timer.getStart()));
        String stop = String.valueOf(DateUtils.getMinutesOfDay(timer.getEnd()));
        String endAction = String.valueOf(timer.getTimerAction());
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("ch", String.valueOf(timer.getChannelId())));
        params.add(new BasicNameValuePair("dor", days));
        params.add(new BasicNameValuePair("encoding", "255"));
        params.add(new BasicNameValuePair("enable", "1"));
        params.add(new BasicNameValuePair("start", start));
        params.add(new BasicNameValuePair("stop", stop));
        params.add(new BasicNameValuePair("title", title));
        params.add(new BasicNameValuePair("endact", endAction));
        if (timer.getId() >= 0) {
          params.add(new BasicNameValuePair("id", String.valueOf(timer.getId())));
        }

        String query = URLEncodedUtils.format(params, "utf-8");
        String request = url + query;
        RecordingServiceGet rsGet = new RecordingServiceGet(request);
        Thread executionThread = new Thread(rsGet);
        executionThread.start();
        return true;

      default:
        break;
    }
    return false;
  }