public void nextStates() {

    switch (state) {
      case IDLE:
        if (currentCmd != null) state = BUSY;
        break;

      case BUSY:
        if (Constants.CMD_CLOSE.equalsIgnoreCase(currentCmd)
            || Constants.CMD_CALL.equalsIgnoreCase(currentCmd)) {

          state = CONFIRM;
          contact = nlu.getContact();

          confirm(currentCmd);

        } else if (Constants.CMD_SMS.equalsIgnoreCase(currentCmd)) {

          state = OPTION;
          option = 0;
          String msg = "";
          int j = 0;
          contact = nlu.getContact();

          for (int i = 0; i < sms_options.length; i++) {
            j = i + 1;
            msg += j + ", " + sms_options[i] + ".\n";
          }
          toSay = "Please choose an option." + "\n" + (msg);
          // toSay = "Please choose an option";
        } else {
          state = RUN;
        }
        break;

      case CONFIRM:
        // System.out.println("Confirm branch: \nState " + state + "\nAnswer " + answer);
        if ("yes".equalsIgnoreCase(answer)) {
          state = RUN;
          runCmd(currentCmd);
        } else if ("no".equalsIgnoreCase(answer)) {
          cancelCmd();
        }
        break;

      case OPTION:
        if (option != 0) {
          state = CONFIRM;
          contact = nlu.getContact();
          message = sms_options[option - 1];
          confirm(currentCmd);
        }
        break;
      case RUN:
        runCmd(currentCmd);
        break;
    }
    // System.out.println("Current state :" + state);
  }
  public void processSpeech(ArrayList<String> speech) {

    nlu.getCommand(speech);

    selectedLine = nlu.getSelectedLine();

    if (selectedLine != null) {

      System.out.println(selectedLine);
      // sleepTimer.cancel();

      if (state == IDLE) {
        currentCmd = nlu.getCmd();

        if (currentCmd != null) {

          System.out.println("Command <" + currentCmd + "> heard");

          // a command was detected and the current state is
          state = BUSY;

        } else {
          // the system does not understand what was said
          // <<Buzz

        }
      }

      // These commands can be called any time
      if (Constants.CMD_CANCEL.equalsIgnoreCase(nlu.getCmd())) {
        cancelCmd();
      } else if (Constants.CMD_REPEAT.equalsIgnoreCase(nlu.getCmd())) {
        runCmd("repeat");
      }

      if (state == CONFIRM) {
        // check if it is an allowed answer Y or N
        answer = selectedLine;
      } else if (state == OPTION) {
        // check if best result is a digit
        if (digitPattern.matcher(selectedLine).matches()) {
          option = Integer.valueOf(selectedLine);
        } else {
          // not number from 1 to 4
          toSay = "Please say a number between 1 and 4";
        }
      }

    } else {
      // nothing was selected from the speech engine results
      toSay = "I didn't understand what you say";
    }

    nextStates();

    lastUtterance = toSay;
  }
 public void initialise() {
   state = IDLE; /*SLEEPING */
   currentCmd = null;
   selectedLine = null;
   // toSay 			= "";
   answer = "";
   option = 0;
   contact = null;
   nlu.setName(null);
   // sleepTimer.schedule(new silenceTimer(), SLEEP_TIMEOUT);
 }