VoiceHelperThread.java
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                                         //Obtain next set of commands
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                                                                 //All robots must agree to reset before reset can be true
00107                                                                 reset &= msg.reset;
00108                                                                 defaultList.addAll(msg.newVocab);
00109                                                         }
00110                                                         else
00111                                                         {
00112                                                                 reset = false;
00113                                                                 nextPhrases.addAll(msg.newVocab);
00114                                                                 //Only one robot must need a required command before a next
00115                                                                 //command to be required
00116                                                                 isRequired |= msg.isRequired;
00117                                                         }
00118                                                 }
00119 
00120                                                 if (reset)
00121                                                 {
00122                                                         nextPhrases = defaultList;
00123                                                 }
00124                                         }
00125                                         else //If specific robot is in focus
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                                         //If a command is not finished, add cancel to interrupt command
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) //If command is not required, set time limit
00145                                         {
00146                                                 long start = System.currentTimeMillis();  
00147 
00148                                                 //Pause thread execution until a phrase has been recieved or a second has elapsed
00149                                                 while (!newPhraseReceived && (System.currentTimeMillis() - start < 1000));
00150 
00151                                                 //Has execution resumed because a new command has been given?
00152                                                 if (newPhraseReceived)
00153                                                 {
00154                                                         continue outerloop;
00155                                                 }
00156                                                 else //Time limit reached
00157                                                 {
00158                                                         break outerloop;
00159                                                 }
00160                                         }
00161                                 }
00162                         }
00163 
00164                 //The full command has been said, compile phrases into an expression and
00165                 //send with a reset message
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) //Add continuation character
00200                 {
00201                         commandViewText += ("_");
00202                 }
00203                 else //Remove extra space
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 }


google_glass_driver
Author(s): Nicholas Otero
autogenerated on Fri Aug 28 2015 10:51:44