Go to the documentation of this file.00001 package com.riverlab.robotmanager.robot;
00002
00003 import android.app.Activity;
00004 import android.bluetooth.BluetoothAdapter;
00005 import android.bluetooth.BluetoothDevice;
00006 import android.bluetooth.BluetoothSocket;
00007 import android.content.Context;
00008 import android.content.Intent;
00009 import android.media.AudioManager;
00010 import android.os.Bundle;
00011 import android.os.Handler;
00012 import android.os.Message;
00013 import android.speech.RecognizerIntent;
00014 import android.util.Log;
00015 import android.view.KeyEvent;
00016 import android.view.Menu;
00017 import android.view.MenuInflater;
00018 import android.view.MenuItem;
00019 import android.view.MotionEvent;
00020 import android.view.View;
00021 import android.view.WindowManager;
00022 import android.widget.AdapterView;
00023 import android.widget.ScrollView;
00024
00025 import com.google.android.glass.media.Sounds;
00026 import com.google.android.glass.touchpad.Gesture;
00027 import com.google.android.glass.touchpad.GestureDetector;
00028 import com.google.android.glass.widget.CardScrollView;
00029 import com.riverlab.robotmanager.RobotManagerApplication;
00030 import com.riverlab.robotmanager.voice_recognition.VoiceRecognitionThread;
00031
00032 import java.io.IOException;
00033 import java.io.InputStream;
00034 import java.io.OutputStream;
00035 import java.lang.reflect.Array;
00036 import java.lang.reflect.Method;
00037 import java.util.ArrayList;
00038 import java.util.Collection;
00039 import java.util.HashMap;
00040 import java.util.List;
00041 import java.util.Set;
00042
00043 public class ViewRobotListActivity extends Activity implements AdapterView.OnItemClickListener {
00044
00045 private static final int SPEECH_REQUEST = 0;
00046
00047 CardScrollView mCardScrollView;
00048 BluetoothDevice mSelectedDevice;
00049 RobotCardScrollAdapter adapter;
00050 GestureDetector mGestureDetector;
00051 RobotManagerApplication mApplication;
00052
00053 Runnable close = new Runnable() {
00054 @Override
00055 public void run()
00056 {
00057 finish();
00058 }
00059 };
00060
00061 Runnable previous = new Runnable() {
00062 @Override
00063 public void run()
00064 {
00065 int current = mCardScrollView.getSelectedItemPosition();
00066 int next = current - 1;
00067
00068 if(next >= 0)
00069 {
00070 mCardScrollView.setSelection(next);
00071 }
00072 }
00073 };
00074
00075 Runnable next = new Runnable() {
00076 @Override
00077 public void run()
00078 {
00079 int last = mCardScrollView.getChildCount();
00080 int current = mCardScrollView.getSelectedItemPosition();
00081 int next = current + 1;
00082
00083 if(next < last)
00084 {
00085 mCardScrollView.setSelection(next);
00086 }
00087 }
00088 };
00089
00090 @Override
00091 protected void onCreate(Bundle savedInstanceState) {
00092 super.onCreate(savedInstanceState);
00093
00094 mGestureDetector = createGestureDetector(this);
00095 mCardScrollView = new CardScrollView(this);
00096 mCardScrollView.activate();
00097 mCardScrollView.setOnItemClickListener(this);
00098 setContentView(mCardScrollView);
00099 }
00100
00101 @Override
00102 protected void onResume() {
00103 super.onResume();
00104
00105 Collection<Robot> mRobots = ((RobotManagerApplication)this.getApplication()).getRobots();
00106 adapter = new RobotCardScrollAdapter(this, mRobots);
00107 mCardScrollView.setAdapter(adapter);
00108 mApplication = (RobotManagerApplication)this.getApplication();
00109 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
00110
00111 HashMap<String, Runnable> commands = new HashMap<String, Runnable>();
00112 commands.put("Close", close);
00113 commands.put("Next", next);
00114 commands.put("Previous", previous);
00115
00116 Handler voiceHandler = mApplication.getVoiceThreadHandler();
00117 Message msg = voiceHandler.obtainMessage(
00118 VoiceRecognitionThread.CHANGE_VOCAB_ACTION_MESSAGE,
00119 commands);
00120 voiceHandler.sendMessage(msg);
00121
00122 msg = voiceHandler.obtainMessage(
00123 VoiceRecognitionThread.CONTEXT_MESSAGE,
00124 this);
00125 voiceHandler.sendMessage(msg);
00126
00127 msg = voiceHandler.obtainMessage(
00128 VoiceRecognitionThread.LISTENING_MESSAGE,
00129 true);
00130 voiceHandler.sendMessage(msg);
00131 }
00132
00133 @Override
00134 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
00135 AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
00136 audio.playSoundEffect(Sounds.TAP);
00137 displaySpeechRecognizer();
00138 }
00139
00140 private GestureDetector createGestureDetector(Context context) {
00141 GestureDetector gestureDetector = new GestureDetector(context);
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
00170
00171 @Override
00172 public void onFingerCountChanged(int previousCount, int currentCount) {
00173 if(currentCount == 2){
00174 mCardScrollView.deactivate();
00175 }else{
00176
00177 mCardScrollView.activate();
00178 mCardScrollView.requestFocus();
00179 }
00180 }
00181 });
00182 return gestureDetector;
00183 }
00184
00185
00186
00187
00188 @Override
00189 public boolean onGenericMotionEvent(MotionEvent event) {
00190 if (mGestureDetector != null) {
00191 return mGestureDetector.onMotionEvent(event);
00192 }
00193 return false;
00194 }
00195
00196 private void displaySpeechRecognizer() {
00197 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
00198 startActivityForResult(intent, SPEECH_REQUEST);
00199 }
00200
00201 @Override
00202 protected void onActivityResult(int requestCode, int resultCode,
00203 Intent data) {
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257 }
00258 }