Go to the documentation of this file.00001 package com.riverlab.robotmanager.messages;
00002
00003 import java.util.ArrayList;
00004 import java.util.HashMap;
00005 import java.util.List;
00006
00007 import android.app.Activity;
00008 import android.content.Context;
00009 import android.media.AudioManager;
00010 import android.os.Bundle;
00011 import android.os.Handler;
00012 import android.os.Looper;
00013 import android.os.Message;
00014
00015 import android.view.View;
00016 import android.view.WindowManager;
00017 import android.widget.AdapterView;
00018 import android.widget.ImageView;
00019 import android.widget.TextView;
00020
00021 import com.google.android.glass.media.Sounds;
00022 import com.google.android.glass.widget.CardScrollView;
00023 import com.riverlab.robotmanager.R;
00024 import com.riverlab.robotmanager.RobotManagerApplication;
00025 import com.riverlab.robotmanager.voice_recognition.VoiceRecognitionThread;
00026
00027 public class MessageListActivity extends Activity implements AdapterView.OnItemClickListener
00028 {
00029
00030 List<RobotMessage> mMessages;
00031 CardScrollView mCardScrollView;
00032 RobotMessage mSelectedMessage;
00033 MessageCardScrollAdapter adapter;
00034 RobotManagerApplication mApplication;
00035
00036 Runnable close = new Runnable() {
00037 @Override
00038 public void run()
00039 {
00040 finish();
00041 }
00042 };
00043
00044 Runnable previous = new Runnable() {
00045 @Override
00046 public void run()
00047 {
00048 int current = mCardScrollView.getSelectedItemPosition();
00049 int next = current - 1;
00050
00051 if(next >= 0)
00052 {
00053 mCardScrollView.setSelection(next);
00054 }
00055 }
00056 };
00057
00058 Runnable next = new Runnable() {
00059 @Override
00060 public void run()
00061 {
00062 int last = mCardScrollView.getChildCount();
00063 int current = mCardScrollView.getSelectedItemPosition();
00064 int next = current + 1;
00065
00066 if(next < last)
00067 {
00068 mCardScrollView.setSelection(next);
00069 }
00070 }
00071 };
00072
00073 Runnable viewImage = new Runnable() {
00074 @Override
00075 public void run()
00076 {
00077 Runnable guiUpdate = new Runnable() {
00078 @Override
00079 public void run() {
00080 int index = mCardScrollView.getSelectedItemPosition();
00081 if (mMessages.get(index).getType() == "Text")
00082 return;
00083
00084 View curView = mCardScrollView.getChildAt(mCardScrollView.getSelectedItemPosition());
00085 ImageView imgView = (ImageView)curView.findViewById(R.id.imageView);
00086 TextView msgView = (TextView)curView.findViewById(R.id.messageText);
00087
00088 imgView.setVisibility(View.VISIBLE);
00089 msgView.setVisibility(View.INVISIBLE);
00090 }
00091 };
00092
00093 Handler handler = new Handler(Looper.getMainLooper());
00094 handler.post(guiUpdate);
00095 }
00096 };
00097
00098 Runnable viewText = new Runnable() {
00099 @Override
00100 public void run()
00101 {
00102 Runnable guiUpdate = new Runnable() {
00103 @Override
00104 public void run() {
00105 View curView = mCardScrollView.getChildAt(mCardScrollView.getSelectedItemPosition());
00106 ImageView imgView = (ImageView)curView.findViewById(R.id.imageView);
00107 TextView msgView = (TextView)curView.findViewById(R.id.messageText);
00108
00109 imgView.setVisibility(View.INVISIBLE);
00110 msgView.setVisibility(View.VISIBLE);
00111 }
00112 };
00113
00114 Handler handler = new Handler(Looper.getMainLooper());
00115 handler.post(guiUpdate);
00116 }
00117 };
00118
00119 @Override
00120 protected void onCreate(Bundle savedInstanceState) {
00121 super.onCreate(savedInstanceState);
00122
00123 mCardScrollView = new CardScrollView(this);
00124 mCardScrollView.activate();
00125 mCardScrollView.setOnItemClickListener(this);
00126 mApplication = ((RobotManagerApplication) this.getApplication());
00127 setContentView(mCardScrollView);
00128 }
00129
00130 @Override
00131 protected void onResume() {
00132 super.onResume();
00133
00134 mApplication.setMsgListActivity(this);
00135 mMessages = mApplication.getMessages();
00136 adapter = new MessageCardScrollAdapter(this, mMessages);
00137 mCardScrollView.setAdapter(adapter);
00138 mCardScrollView.setSelection(mMessages.size() - 1);
00139 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
00140
00141 HashMap<String, Runnable> commands = new HashMap<String, Runnable>();
00142 commands.put("Close", close);
00143 commands.put("Next", next);
00144 commands.put("Previous", previous);
00145 commands.put("Image", viewImage);
00146 commands.put("Text", viewText);
00147
00148 Handler voiceHandler = mApplication.getVoiceThreadHandler();
00149 Message msg = voiceHandler.obtainMessage(
00150 VoiceRecognitionThread.CHANGE_VOCAB_ACTION_MESSAGE,
00151 commands);
00152 voiceHandler.sendMessage(msg);
00153
00154 msg = voiceHandler.obtainMessage(
00155 VoiceRecognitionThread.CONTEXT_MESSAGE,
00156 this);
00157 voiceHandler.sendMessage(msg);
00158
00159 msg = voiceHandler.obtainMessage(
00160 VoiceRecognitionThread.LISTENING_MESSAGE,
00161 true);
00162 voiceHandler.sendMessage(msg);
00163 }
00164
00165 @Override
00166 protected void onPause()
00167 {
00168 super.onPause();
00169
00170 mApplication.setMsgListActivity(null);
00171 }
00172
00173 @Override
00174 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
00175 mSelectedMessage = (RobotMessage) mCardScrollView.getItemAtPosition(position);
00176 AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
00177 audio.playSoundEffect(Sounds.TAP);
00178 openOptionsMenu();
00179 }
00180
00181 public void onMessageAddition()
00182 {
00183 int curPosition = 0;
00184 curPosition = mCardScrollView.getSelectedItemPosition();
00185
00186 mMessages = mApplication.getMessages();
00187 adapter = new MessageCardScrollAdapter(this, mMessages);
00188 mCardScrollView.setAdapter(adapter);
00189
00190 if (mMessages.get(0).getPriority() == 0)
00191 {
00192 mCardScrollView.setSelection(curPosition);
00193 }
00194 else
00195 {
00196 mCardScrollView.setSelection(mMessages.size() - 1);
00197 }
00198 }
00199 }