/** * Checks if the passed {@link StateModel} is valid. * * @param stateModel * A {@link StateModel} * @return True if {@link StateModel} is valid, false if not */ private Boolean stateModelIsOk(final StateModel stateModel) { Boolean returnBool = true; if (stateModel == null) { returnBool = false; if (loggingEnabled) { Log.e(TAG, "State Model doesn't exist."); } } else { if (stateModel.getStateMap() == null) { returnBool = false; if (loggingEnabled) { Log.e(TAG, "State Model has no StateMap."); } } else { if (!stateModel.getStateMap().containsKey(STATE_START)) { returnBool = false; if (loggingEnabled) { Log.e(TAG, "State Model has no State 'STATE_START'."); } } else if (!stateModel.getStateMap().containsKey(STATE_SLEEP)) { returnBool = false; if (loggingEnabled) { Log.e(TAG, "State Model has no State 'STATE_SLEEP'."); } } } } return returnBool; }
/** * Switch to the State with Id newStateId. * * @param oldStateId * @param newStateId */ public void changeState(final String oldStateId, final String newStateId) { interruptSleep(); if (ttsManager != null && ttsManager.getTextToSpeech() != null) { ttsManager.getTextToSpeech().stop(); } if (currentState != null) { currentState.directlyBeforeStateChange(oldStateId, newStateId); } // Send Broadcast that state will change Intent intent = new Intent(STATEMACHINE_WILL_CHANGE_STATE); intent.putExtra(STATEMACHINE_WILL_CHANGE_NEWSTATEID, newStateId); if (context != null) { context.sendBroadcast(intent); } if (stateModel != null && oldStateId != null && newStateId != null) { if (!stateModel.getStateMap().containsKey(newStateId)) { if (loggingEnabled) { Log.e(TAG, "Unknown state: " + newStateId); } } else { if (!isInState) { isInState = true; currentStateId = newStateId; if (stateModel.getStateMap() != null) { currentState = stateModel.getStateMap().get(newStateId); } currentState.executeInState(); isInState = false; } } } }