00001 package com.riverlab.robotmanager; 00002 00003 import java.util.ArrayList; 00004 import java.util.Collection; 00005 import java.util.HashMap; 00006 import java.util.Set; 00007 00008 import com.google.android.glass.media.Sounds; 00009 import com.google.glass.logging.Log; 00010 import com.riverlab.robotmanager.bluetooth.ConnectedThread; 00011 import com.riverlab.robotmanager.messages.MessageListActivity; 00012 import com.riverlab.robotmanager.messages.RobotMessage; 00013 import com.riverlab.robotmanager.robot.Robot; 00014 import com.riverlab.robotmanager.voice_recognition.VoiceRecognitionThread; 00015 00016 import android.app.Application; 00017 import android.content.Context; 00018 import android.media.AudioManager; 00019 import android.os.Handler; 00020 import android.os.Message; 00021 00022 00023 public class RobotManagerApplication extends Application 00024 { 00025 private Handler mMainThreadHandler; 00026 private Handler mConnectedThreadHandler; 00027 private Handler mVoiceThreadHandler; 00028 private HashMap<String, Robot> mRobotMap = new HashMap<String, Robot>(); 00029 private boolean isConnected; 00030 private Robot robotInFocus; 00031 private ArrayList<RobotMessage> mMessages = new ArrayList<RobotMessage>(); 00032 private MessageListActivity msgListActivity = null; 00033 00034 public Handler getMainActivityHandler() 00035 { 00036 return mMainThreadHandler; 00037 } 00038 00039 public Handler getConnectedThreadHandler() 00040 { 00041 return mConnectedThreadHandler; 00042 } 00043 00044 public Handler getVoiceThreadHandler() 00045 { 00046 return mVoiceThreadHandler; 00047 } 00048 00049 00050 public void setMainThreadHandler(Handler mainHandler) 00051 { 00052 mMainThreadHandler = mainHandler; 00053 } 00054 00055 public void setConnectedThreadHandler(Handler connectedHandler) 00056 { 00057 mConnectedThreadHandler = connectedHandler; 00058 } 00059 00060 public void setVoiceThreadHandler(Handler voiceHandler) 00061 { 00062 mVoiceThreadHandler = voiceHandler; 00063 } 00064 00065 public void setMsgListActivity(MessageListActivity mla) 00066 { 00067 msgListActivity = mla; 00068 } 00069 00070 public boolean getConnectionStatus() 00071 { 00072 return isConnected; 00073 } 00074 00075 00076 public void setConnectionStatus(boolean isConnected) 00077 { 00078 this.isConnected = isConnected; 00079 } 00080 00081 00082 public Set<String> getRobotNames() 00083 { 00084 return mRobotMap.keySet(); 00085 } 00086 00087 public Collection<Robot> getRobots() 00088 { 00089 return mRobotMap.values(); 00090 } 00091 00092 public Robot getRobot(String name) 00093 { 00094 return mRobotMap.get(name); 00095 } 00096 00097 public void addRobot(Robot newRobot) 00098 { 00099 mRobotMap.put(newRobot.getName(), newRobot); 00100 00101 Message msg = mVoiceThreadHandler.obtainMessage(VoiceRecognitionThread.ADD_VOCAB_MESSAGE, newRobot.getName()); 00102 mVoiceThreadHandler.sendMessageAtFrontOfQueue(msg); 00103 00104 msg = mVoiceThreadHandler.obtainMessage(VoiceRecognitionThread.UPDATE_MESSAGE); 00105 mVoiceThreadHandler.sendMessage(msg); 00106 00107 Log.i("Application", "Finished addRobot"); 00108 } 00109 00110 public void removeRobot(Robot robot) 00111 { 00112 mRobotMap.remove(robot.getName()); 00113 00114 Message msg = mVoiceThreadHandler.obtainMessage(VoiceRecognitionThread.REMOVE_VOCAB_MESSAGE, robot.getName()); 00115 mVoiceThreadHandler.sendMessageAtFrontOfQueue(msg); 00116 } 00117 00118 public Robot getRobotInFocus() 00119 { 00120 return robotInFocus; 00121 } 00122 00123 public void setRobotInFocus(String robotName) 00124 { 00125 if (robotName.equals("All")) 00126 { 00127 robotInFocus = null; 00128 } 00129 else 00130 { 00131 robotInFocus = mRobotMap.get(robotName); 00132 } 00133 } 00134 00135 public void addMessage(RobotMessage newMsg) 00136 { 00137 mMessages.add(newMsg); 00138 if (msgListActivity != null) 00139 { 00140 Runnable task = new Runnable() { 00141 public void run() { 00142 msgListActivity.onMessageAddition(); 00143 } 00144 }; 00145 00146 mMainThreadHandler.post(task); 00147 } 00148 00149 Message msg = mMainThreadHandler.obtainMessage(MainActivity.NEW_MESSAGE_MESSAGE); 00150 mMainThreadHandler.sendMessage(msg); 00151 00152 AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 00153 audio.playSoundEffect(Sounds.SUCCESS); 00154 } 00155 00156 public ArrayList<RobotMessage> getMessages() 00157 { 00158 return mMessages; 00159 } 00160 00161 public void onShutdown() 00162 { 00163 mRobotMap = null; 00164 mMessages = null; 00165 00166 Message msgMain = mMainThreadHandler.obtainMessage(); 00167 msgMain.what = MainActivity.SHUTDOWN_MESSAGE; 00168 mMainThreadHandler.sendMessageAtFrontOfQueue(msgMain); 00169 00170 Message msgConnected = mConnectedThreadHandler.obtainMessage(ConnectedThread.SHUTDOWN_MESSAGE); 00171 msgConnected.what = ConnectedThread.SHUTDOWN_MESSAGE; 00172 mConnectedThreadHandler.sendMessageAtFrontOfQueue(msgConnected); 00173 00174 Message msgVoice = mVoiceThreadHandler.obtainMessage(VoiceRecognitionThread.SHUTDOWN_MESSAGE); 00175 msgVoice.what = VoiceRecognitionThread.SHUTDOWN_MESSAGE; 00176 mVoiceThreadHandler.sendMessageAtFrontOfQueue(msgVoice); 00177 } 00178 }