/* (non-Javadoc)
  * @see android.support.v4.app.Fragment#onResume()
  */
 @Override
 public void onResume() {
   super.onResume();
   if (!UIUtils.isTablet(getSherlockActivity())) {
     clearSelection();
   }
 }
  /*
   * (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;
  }