Go to the documentation of this file.00001 package com.riverlab.robotmanager.voice_recognition;
00002
00003 import java.util.ArrayList;
00004
00005 import com.riverlab.robotmanager.MainActivity;
00006 import com.riverlab.robotmanager.RobotManagerApplication;
00007 import com.riverlab.robotmanager.robot.Robot;
00008
00009 import android.os.Handler;
00010 import android.os.HandlerThread;
00011 import android.os.Message;
00012
00013 public class VoiceHelperThread extends HandlerThread
00014 {
00015 RobotManagerApplication mApplication;
00016 String expression;
00017 String rootPhrase;
00018 String targetRobotName;
00019 Handler voiceRecognitionThreadHandler;
00020
00021 String newPhrase;
00022 boolean newPhraseReceived = false;
00023 boolean reset;
00024
00025 public static final int RECEIVE_PHRASE_MESSAGE = 0;
00026
00027 private Handler mHandler;
00028 private Thread worker;
00029
00030 public VoiceHelperThread(RobotManagerApplication app, String rootPhrase, String targetRobotName, Handler voiceRecognitionThreadHandler)
00031 {
00032 super("Voice Helper Thread");
00033 this.mApplication = app;
00034 this.rootPhrase = rootPhrase;
00035 this.targetRobotName = targetRobotName;
00036 this.voiceRecognitionThreadHandler = voiceRecognitionThreadHandler;
00037 }
00038
00039 @Override
00040 public void start()
00041 {
00042 super.start();
00043
00044 mHandler = new Handler(){
00045 @Override
00046 public void handleMessage(Message msg) {
00047 switch (msg.what) {
00048 case RECEIVE_PHRASE_MESSAGE:
00049 String phraseText = (String)msg.obj;
00050 receivePhrase(phraseText);
00051 break;
00052 }
00053
00054 }
00055 };
00056
00057 worker = new Thread()
00058 {
00059 @Override
00060 public void run()
00061 {
00062 createExpression();
00063 }
00064 };
00065 worker.start();
00066 }
00067
00068 public Handler getHandler()
00069 {
00070 return mHandler;}
00071
00072 public void createExpression()
00073 {
00074 ArrayList<String> previous = new ArrayList<String>();
00075 ArrayList<Robot> allRobots = new ArrayList<Robot>(mApplication.getRobots());
00076
00077 Robot targetRobot = mApplication.getRobot(targetRobotName);
00078
00079 newPhrase = rootPhrase;
00080 newPhraseReceived = true;
00081
00082 outerloop:
00083 while (!reset)
00084 {
00085 ArrayList<String> nextPhrases = new ArrayList<String>();
00086 boolean isRequired = false;
00087
00088 if (newPhraseReceived)
00089 {
00090 previous.add(newPhrase);
00091 newPhraseReceived = false;
00092 sendUIUpdate(previous, true);
00093
00094
00095 if (targetRobotName.equals("All"))
00096 {
00097 reset = true;
00098 ArrayList<String> defaultList = new ArrayList<String>();
00099
00100 for (Robot robot : allRobots)
00101 {
00102 NewPhrasesMessage msg = robot.getNextPhrases(new ArrayList<String>(previous));
00103
00104 if (msg.reset)
00105 {
00106
00107 reset &= msg.reset;
00108 defaultList.addAll(msg.newVocab);
00109 }
00110 else
00111 {
00112 reset = false;
00113 nextPhrases.addAll(msg.newVocab);
00114
00115
00116 isRequired |= msg.isRequired;
00117 }
00118 }
00119
00120 if (reset)
00121 {
00122 nextPhrases = defaultList;
00123 }
00124 }
00125 else
00126 {
00127 NewPhrasesMessage msg = targetRobot.getNextPhrases(new ArrayList<String>(previous));
00128 nextPhrases.addAll(msg.newVocab);
00129 isRequired = msg.isRequired;
00130 reset = msg.reset;
00131 }
00132
00133 if (!reset)
00134 {
00135 nextPhrases.add("Cancel");
00136 }
00137 sendNewVocabMessage(nextPhrases);
00138
00139 if (reset)
00140 {
00141 break outerloop;
00142 }
00143
00144 if (!isRequired)
00145 {
00146 long start = System.currentTimeMillis();
00147
00148
00149 while (!newPhraseReceived && (System.currentTimeMillis() - start < 1000));
00150
00151
00152 if (newPhraseReceived)
00153 {
00154 continue outerloop;
00155 }
00156 else
00157 {
00158 break outerloop;
00159 }
00160 }
00161 }
00162 }
00163
00164
00165
00166 sendResetMessage(previous);
00167 }
00168
00169 public void receivePhrase(String phrase)
00170 {
00171 newPhrase = phrase;
00172 newPhraseReceived = true;
00173 }
00174
00175
00176 private void sendNewVocabMessage(ArrayList<String> newVocab)
00177 {
00178 Message msg = voiceRecognitionThreadHandler.obtainMessage(VoiceRecognitionThread.CHANGE_VOCAB_MESSAGE, newVocab);
00179 voiceRecognitionThreadHandler.sendMessageAtFrontOfQueue(msg);
00180 }
00181
00182 private void sendResetMessage(ArrayList<String> previousPhrases)
00183 {
00184 String sent = sendUIUpdate(previousPhrases, false);
00185 String fullString = targetRobotName + ": " + sent;
00186
00187 Message voiceMsg = voiceRecognitionThreadHandler.obtainMessage(VoiceRecognitionThread.RESET_MESSAGE, fullString);
00188 voiceRecognitionThreadHandler.sendMessageAtFrontOfQueue(voiceMsg);
00189 }
00190
00191 private String sendUIUpdate(ArrayList<String> previousPhrases, boolean more)
00192 {
00193 String commandViewText = "";
00194
00195 for (String phrase : previousPhrases)
00196 {
00197 commandViewText += (phrase + " ");
00198 }
00199 if (more)
00200 {
00201 commandViewText += ("_");
00202 }
00203 else
00204 {
00205 commandViewText = commandViewText.substring(0, commandViewText.length() - 1);
00206 }
00207
00208 Handler mainHandler = mApplication.getMainActivityHandler();
00209 Message mainMsg = mainHandler.obtainMessage(MainActivity.COMMAND_MESSAGE, commandViewText);
00210 mainHandler.sendMessage(mainMsg);
00211
00212 return commandViewText;
00213 }
00214
00215 public synchronized boolean isReady()
00216 {
00217 return mHandler != null;
00218 }
00219
00220 }