public void testAlert() {
    // Check that values are ok.
    double threshold = 0;
    try {
      threshold = Double.parseDouble((alertThreshold.getText().toString()));
    } catch (NumberFormatException nfe) {
      Log.e(TAG, "Invalid number", nfe);
    }
    threshold = unitsConvertFromDisp(threshold);
    if (!verifyThreshold(threshold)) {
      return;
    }

    int timeStart = AlertType.toTime(startHour, startMinute);
    int timeEnd = AlertType.toTime(endHour, endMinute);

    boolean allDay = checkboxAllDay.isChecked();
    // if 23:59 was set, we increase it to 24:00
    if (timeStart == AlertType.toTime(23, 59)) {
      timeStart++;
    }
    if (timeEnd == AlertType.toTime(23, 59)) {
      timeEnd++;
    }
    if (timeStart == AlertType.toTime(0, 0) && timeEnd == AlertType.toTime(24, 0)) {
      allDay = true;
    }
    if (timeStart == timeEnd && (allDay == false)) {
      Toast.makeText(
              getApplicationContext(),
              "start time and end time of alert can not be equal",
              Toast.LENGTH_LONG)
          .show();
      return;
    }
    boolean overrideSilentMode = checkboxAlertOverride.isChecked();
    String mp3_file = audioPath;
    AlertType.testAlert(
        alertText.getText().toString(),
        above,
        threshold,
        allDay,
        1,
        mp3_file,
        timeStart,
        timeEnd,
        overrideSilentMode,
        defaultSnooze,
        mContext);
  }
  private boolean verifyThreshold(double threshold) {
    List<AlertType> lowAlerts = AlertType.getAll(false);
    List<AlertType> highAlerts = AlertType.getAll(true);

    if (threshold < MIN_ALERT || threshold > MAX_ALERT) {
      Toast.makeText(
              getApplicationContext(),
              "threshold has to be between " + MIN_ALERT + " and " + MAX_ALERT,
              Toast.LENGTH_LONG)
          .show();
      return false;
    }
    if (uuid == null) {
      // We want to make sure that for each threashold there is only one alert. Otherwise, which
      // file should we play.
      for (AlertType lowAlert : lowAlerts) {
        if (lowAlert.threshold == threshold) {
          Toast.makeText(
                  getApplicationContext(),
                  "Each alert should have it's own threshold. Please choose another threshold.",
                  Toast.LENGTH_LONG)
              .show();
          return false;
        }
      }
      for (AlertType highAlert : highAlerts) {
        if (highAlert.threshold == threshold) {
          Toast.makeText(
                  getApplicationContext(),
                  "Each alert should have it's own threshold. Please choose another threshold.",
                  Toast.LENGTH_LONG)
              .show();
          return false;
        }
      }
    }
    // high alerts have to be higher than all low alerts...
    if (above) {
      for (AlertType lowAlert : lowAlerts) {
        if (threshold < lowAlert.threshold) {
          Toast.makeText(
                  getApplicationContext(),
                  "High alert threshold has to be higher than all low alerts. Please choose another threshold.",
                  Toast.LENGTH_LONG)
              .show();
          return false;
        }
      }
    } else {
      // low alert has to be lower than all high alerts
      for (AlertType highAlert : highAlerts) {
        if (threshold > highAlert.threshold) {
          Toast.makeText(
                  getApplicationContext(),
                  "Low alert threshold has to be lower than all high alerts. Please choose another threshold.",
                  Toast.LENGTH_LONG)
              .show();
          return false;
        }
      }
    }

    return true;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.activity_edit_alert);

    viewHeader = (TextView) findViewById(R.id.view_alert_header);

    buttonSave = (Button) findViewById(R.id.edit_alert_save);
    buttonRemove = (Button) findViewById(R.id.edit_alert_remove);
    buttonTest = (Button) findViewById(R.id.edit_alert_test);
    buttonalertMp3 = (Button) findViewById(R.id.Button_alert_mp3_file);
    buttonPreSnooze = (Button) findViewById(R.id.edit_alert_pre_snooze);

    alertText = (EditText) findViewById(R.id.edit_alert_text);
    alertThreshold = (EditText) findViewById(R.id.edit_alert_threshold);
    alertMp3File = (EditText) findViewById(R.id.edit_alert_mp3_file);

    checkboxAllDay = (CheckBox) findViewById(R.id.check_alert_time);

    layoutTimeBetween = (LinearLayout) findViewById(R.id.time_between);
    timeInstructions = (LinearLayout) findViewById(R.id.time_instructions);
    timeInstructionsStart = (TextView) findViewById(R.id.time_instructions_start);
    timeInstructionsEnd = (TextView) findViewById(R.id.time_instructions_end);

    viewTimeStart = (TextView) findViewById(R.id.view_alert_time_start);
    viewTimeEnd = (TextView) findViewById(R.id.view_alert_time_end);
    editSnooze = (EditText) findViewById(R.id.edit_snooze);
    reraise = (EditText) findViewById(R.id.reraise);

    viewAlertOverrideText = (TextView) findViewById(R.id.view_alert_override_silent);
    checkboxAlertOverride = (CheckBox) findViewById(R.id.check_override_silent);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    addListenerOnButtons();

    if (BgGraphBuilder.isXLargeTablet(getApplicationContext())) {
      viewHeader.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      buttonSave.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      buttonRemove.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      buttonTest.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      buttonalertMp3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      buttonSave.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      buttonPreSnooze.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      alertText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      alertThreshold.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      alertMp3File.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);

      checkboxAllDay.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);

      viewTimeStart.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      viewTimeEnd.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      editSnooze.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      reraise.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      viewAlertOverrideText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);

      ((TextView) findViewById(R.id.view_alert_text)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      ((TextView) findViewById(R.id.view_alert_threshold))
          .setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      ((TextView) findViewById(R.id.view_alert_default_snooze))
          .setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      ((TextView) findViewById(R.id.view_alert_mp3_file))
          .setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      ((TextView) findViewById(R.id.view_alert_time)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
      ((TextView) findViewById(R.id.view_alert_time_between))
          .setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
    }
    SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    doMgdl = (prefs.getString("units", "mgdl").compareTo("mgdl") == 0);

    if (!doMgdl) {
      alertThreshold.setInputType(InputType.TYPE_CLASS_NUMBER);
      alertThreshold.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
      alertThreshold.setKeyListener(DigitsKeyListener.getInstance(false, true));
    }

    uuid = getExtra(savedInstanceState, "uuid");
    String status;
    if (uuid == null) {
      // This is a new alert
      above = Boolean.parseBoolean(getExtra(savedInstanceState, "above"));
      checkboxAllDay.setChecked(true);
      checkboxAlertOverride.setChecked(true);

      audioPath = "";
      alertMp3File.setText(shortPath(audioPath));
      alertMp3File.setKeyListener(null);
      defaultSnooze = SnoozeActivity.getDefaultSnooze(above);
      buttonRemove.setVisibility(View.GONE);
      // One can not snooze an alert that is still not in the database...
      buttonPreSnooze.setVisibility(View.GONE);
      status = "Adding " + (above ? "high" : "low") + " alert";
      startHour = 0;
      startMinute = 0;
      endHour = 23;
      endMinute = 59;
      alertReraise = 1;
    } else {
      // We are editing an alert
      AlertType at = AlertType.get_alert(uuid);
      if (at == null) {
        Log.wtf(TAG, "Error editing alert, when that alert does not exist...");
        Intent returnIntent = new Intent();
        setResult(RESULT_CANCELED, returnIntent);
        finish();
        return;
      }

      above = at.above;
      alertText.setText(at.name);
      alertThreshold.setText(unitsConvert2Disp(doMgdl, at.threshold));
      checkboxAllDay.setChecked(at.all_day);
      checkboxAlertOverride.setChecked(at.override_silent_mode);
      defaultSnooze = at.default_snooze;
      if (defaultSnooze == 0) {
        SnoozeActivity.getDefaultSnooze(above);
      }

      audioPath = at.mp3_file;
      alertMp3File.setText(shortPath(audioPath));

      status = "editing " + (above ? "high" : "low") + " alert";
      startHour = AlertType.time2Hours(at.start_time_minutes);
      startMinute = AlertType.time2Minutes(at.start_time_minutes);
      endHour = AlertType.time2Hours(at.end_time_minutes);
      endMinute = AlertType.time2Minutes(at.end_time_minutes);
      alertReraise = at.minutes_between;

      if (uuid.equals(AlertType.LOW_ALERT_55)) {
        // This is the 55 alert, can not be edited
        alertText.setKeyListener(null);
        alertThreshold.setKeyListener(null);
        buttonalertMp3.setEnabled(false);
        checkboxAllDay.setEnabled(false);
        checkboxAlertOverride.setEnabled(false);
        reraise.setEnabled(false);
      }
    }
    reraise.setText(String.valueOf(alertReraise));
    alertMp3File.setKeyListener(null);
    viewHeader.setText(status);
    setDefaultSnoozeSpinner();
    setPreSnoozeSpinner();
    enableAllDayControls();
    enableVibrateControls();
  }