private void loadPreferences(View view) {
    RobopongApp.UserPrefs prefs = RobopongApp.getUserPrefs();

    mSpeedOsciConfig.setPosValues(prefs.mOsciValues);
    mSpeedOsciConfig.setPosRangeOffset(prefs.mOsciRangeOffset);

    mSpeedOsciConfig.setSpeedValues(prefs.mSpeedValues);
    mSpeedOsciConfig.setSpeedRangeOffset(prefs.mSpeedRangeOffset);

    mSpeedOsciConfig.setAngle(prefs.mHeadAngle);
    mLastAngleValue = prefs.mHeadAngle;
  }
  private void savePreferences() {
    RobopongApp.UserPrefs prefs = RobopongApp.getUserPrefs();
    prefs.mOsciValues = mSpeedOsciConfig.getPosValues();
    prefs.mOsciRangeOffset = mSpeedOsciConfig.getPosRangeOffset();

    prefs.mSpeedValues = mSpeedOsciConfig.getSpeedValues();
    prefs.mSpeedRangeOffset = mSpeedOsciConfig.getSpeedRangeOffset();

    prefs.mHeadAngle = mSpeedOsciConfig.getAngle();

    prefs.writeSettings();
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_manual_play, container, false);

    mButtonPlay = (Button) view.findViewById(R.id.buttonPlay);
    mButtonPlay.setOnClickListener(this);
    mButtonSave = (Button) view.findViewById(R.id.buttonSave);
    mButtonSave.setOnClickListener(this);

    mSpeedOsciConfig = (SpeedOsciConfiguratorView) view.findViewById(R.id.configuratorView);
    mSpeedOsciConfig.setOnValuesChangedListener(this);

    loadPreferences(view);

    final Context that = getActivity();

    /*FloatingActionButton showLogBtn = (FloatingActionButton) view.findViewById(R.id.actionBtnShowLog);
    showLogBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            createExercise();
            FragmentManager manager = getFragmentManager();
            ShowLogDialogFragment dlg = new ShowLogDialogFragment();
            IndentingStringBuilder xml= new IndentingStringBuilder();
            mExercise.toXml(xml);
            dlg.setLogContent(xml.toString());
            dlg.show(manager, "fragment_show_log"); //NON-NLS
        }
    });*/

    FloatingActionButton sendMailBtn =
        (FloatingActionButton) view.findViewById(R.id.actionBtnSendEmail);
    sendMailBtn.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            createExercise();
            IndentingStringBuilder xml = new IndentingStringBuilder();
            mExercise.toXml(xml);

            RobopongApp.sendEmail(
                that, RobopongApp.resourceBundle.getString("robopong.exercise"), xml.toString());
          }
        });

    return view;
  }
  private void createExercise() {
    int[] speedValues = mSpeedOsciConfig.getSpeedValues();
    int speedRangeOffset = mSpeedOsciConfig.getSpeedRangeOffset();
    int[] posValues = mSpeedOsciConfig.getPosValues();
    int posRangeOffset = mSpeedOsciConfig.getPosRangeOffset();
    int waitBetweenBallsMs = RobopongApp.getUserPrefs().getTimeAfterBallMs(); // TODO consider knob
    double angle = mSpeedOsciConfig.getAngle();

    mExercise = new Exercise();
    mExercise.setId("MANUAL_42"); // NON-NLS
    mExercise.setTitle("Manual Drill"); // NON-NLS
    RangeType range = new RangeType("" + (int) angle);
    mExercise.setAngle(range);

    if (speedValues.length > 0) {
      ChoicesStep choicesStep = new ChoicesStep();
      int choicePercentage = 100 / speedValues.length;

      for (int speedValue : speedValues) {
        SpeedStep step = new SpeedStep();
        step.setChoicePercentage(choicePercentage);

        range = new RangeType(speedValue, speedRangeOffset);
        step.setSpeed(range);
        choicesStep.addStep(step);
      }

      if (choicesStep.getSteps().size() > 1) {
        mExercise.addStep(choicesStep);
      } else if (choicesStep.getSteps().size() == 1) {
        mExercise.addStep(choicesStep.getSteps().get(0));
      }
    }

    if (posValues.length > 0) {
      ChoicesStep choicesStep = new ChoicesStep();
      int choicePercentage = 100 / posValues.length;

      for (int posValue : posValues) {
        PosStep step = new PosStep();
        step.setChoicePercentage(choicePercentage);

        range = new RangeType(posValue, posRangeOffset);
        step.setPos(range);
        choicesStep.addStep(step);
      }

      if (choicesStep.getSteps().size() > 1) {
        mExercise.addStep(choicesStep);
      } else if (choicesStep.getSteps().size() == 1) {
        mExercise.addStep(choicesStep.getSteps().get(0));
      }
    }

    PlayBallStep st = new PlayBallStep();
    st.setWaitAfterMs(waitBetweenBallsMs);
    st.setPos(null);
    st.setSpeed(null);

    st.setNoOfBalls(new RangeType("1"));

    mExercise.addStep(st);
  }