MainActivity.java
Go to the documentation of this file.
00001 package com.riverlab.robotmanager;
00002 
00003 import android.app.Activity;
00004 import android.app.PendingIntent;
00005 import android.bluetooth.BluetoothAdapter;
00006 import android.bluetooth.BluetoothDevice;
00007 import android.bluetooth.BluetoothSocket;
00008 import android.content.Context;
00009 import android.content.Intent;
00010 import android.content.res.Resources;
00011 import android.media.AudioManager;
00012 import android.os.Bundle;
00013 import android.os.Handler;
00014 import android.os.Message;
00015 import android.os.Parcelable;
00016 import android.view.Menu;
00017 import android.view.MenuInflater;
00018 import android.view.MenuItem;
00019 import android.view.MotionEvent;
00020 import android.view.WindowManager;
00021 import android.widget.ImageView;
00022 import android.widget.RemoteViews;
00023 import android.widget.TextView;
00024 
00025 import com.google.android.glass.media.Sounds;
00026 import com.google.android.glass.timeline.LiveCard;
00027 import com.google.android.glass.touchpad.Gesture;
00028 import com.google.android.glass.touchpad.GestureDetector;
00029 import com.google.glass.input.VoiceInputHelper;
00030 import com.google.glass.input.VoiceListener;
00031 import com.google.glass.logging.FormattingLogger;
00032 import com.google.glass.logging.FormattingLoggers;
00033 import com.google.glass.logging.Log;
00034 import com.google.glass.voice.VoiceCommand;
00035 import com.google.glass.voice.VoiceConfig;
00036 import com.riverlab.robotmanager.bluetooth.BluetoothDevicesListActivity;
00037 import com.riverlab.robotmanager.bluetooth.ConnectedThread;
00038 import com.riverlab.robotmanager.messages.MessageActivity;
00039 import com.riverlab.robotmanager.messages.MessageListActivity;
00040 import com.riverlab.robotmanager.robot.ViewRobotListActivity;
00041 import com.riverlab.robotmanager.voice_recognition.VoiceRecognitionThread;
00042 
00043 import java.util.ArrayList;
00044 import java.util.Set;
00045 
00046 /*This is the Main Activity for the Robot Manager App.  It displays the
00047  * main GUI, and initializes the other parts of the application.
00048  */
00049 
00050 public class MainActivity extends Activity {
00051 
00052         //Thread global variables
00053         private ConnectedThread mConnectedThread;
00054         private VoiceRecognitionThread mVoiceThread;
00055         
00056         //Handler constants and definition
00057         public static final int MESSAGE_LIST_MESSAGE = 0;
00058         public static final int IMAGE_MESSAGE = 1;
00059         public static final int VIDEO_MESSAGE = 2;
00060         public static final int CONNECTION_MESSAGE = 3;
00061         public static final int FOCUS_MESSAGE = 4;
00062         public static final int COMMAND_MESSAGE = 5;
00063         public static final int SHUTDOWN_MESSAGE = 6;
00064         public static final int ROBOT_LIST_MESSAGE = 7;
00065         public static final int NEW_MESSAGE_MESSAGE = 8;
00066 
00067 
00068         //This Handler handles messages sent from the ConnectedThread for received bluetooth messages
00069         private final Handler mHandler = new Handler() {
00070                 @Override
00071                 public void handleMessage(Message msg) {
00072                         switch (msg.what) {
00073                         
00074                         case MESSAGE_LIST_MESSAGE:
00075                                 launchMessageListActivity();
00076                                 break;
00077                                 
00078                         case CONNECTION_MESSAGE:
00079                                 String text = (String)msg.obj;
00080                                 
00081                                 if (text.equals("connected"))
00082                                 {
00083                                         //Set the image view to show the bluetooth image
00084                                         imageView.setImageResource(R.drawable.ic_bluetooth_on_big);
00085                                 }
00086                                 else if (text.equals("disconnected"))
00087                                 {
00088                                         //Set the image view to show the not connected image
00089                                         imageView.setImageResource(R.drawable.not_connected_cross);
00090                                 }
00091                                 break;
00092                                 
00093                         case FOCUS_MESSAGE:
00094                                 String focus = (String)msg.obj;
00095                                 setFocusView(focus);
00096                                 break;
00097                                 
00098                         case COMMAND_MESSAGE:
00099                                 String cmdText = (String)msg.obj;
00100                                 setCommandView(cmdText);
00101                                 break;
00102                                 
00103                         case SHUTDOWN_MESSAGE:
00104                                 shutdown();
00105                                 break;
00106                                 
00107                         case ROBOT_LIST_MESSAGE:
00108                                 launchRobotListActivity();
00109                                 break;
00110                                 
00111                         case NEW_MESSAGE_MESSAGE:
00112                                 updateMessageView(1);
00113                         }
00114                 }
00115         };
00116 
00117         //Timeline global variables
00118         private RobotManagerApplication mApplication;
00119         private RemoteViews aRV;
00120         private LiveCard mLiveCard;
00121         private static final String LIVE_CARD_ID = "robot_manager";
00122 
00123         //GUI global variables
00124         private ImageView imageView;
00125         private GestureDetector mGestureDetector;
00126         private TextView commandView;
00127         private TextView focusView;
00128         private TextView messageView;
00129         private int numMsgs = 0;
00130         private int numNewMsgs = 0;
00131         private boolean msgFlag = false;
00132 
00133         
00134         /*
00135          * This function initializes the GUI and worker threads, 
00136          */
00137         @Override
00138         protected void onCreate(Bundle savedInstanceState) 
00139         {
00140                 super.onCreate(savedInstanceState);
00141 
00142                 //Uncomment the following line to use the ADT debugger
00143                 //android.os.Debug.waitForDebugger();
00144                 
00145                 mGestureDetector = createGestureDetector(this);
00146 
00147                 //Setup GUI resources
00148                 setContentView(R.layout.activity_main);
00149                 imageView = (ImageView) findViewById(R.id.imageView);
00150                 messageView = (TextView) findViewById(R.id.messageView);
00151                 commandView = (TextView)findViewById(R.id.commandView);
00152                 focusView = (TextView)findViewById(R.id.focusView);
00153                 mApplication = ((RobotManagerApplication) this.getApplication());
00154                 
00155                 //Initialize the image view with the not connected image
00156                 imageView.setImageResource(R.drawable.not_connected_cross);
00157                 
00158                 //Create and start the primary worker threads
00159                 mConnectedThread = new ConnectedThread(mHandler, mApplication);
00160                 mVoiceThread = new VoiceRecognitionThread(mApplication, this);
00161 
00162                 mConnectedThread.start();
00163                 mVoiceThread.start();
00164                 
00165                 //Wait until the worker threads have finished initializing, then
00166                 //store their handlers within the application
00167                 while(!mConnectedThread.isReady() && !mVoiceThread.isReady());
00168                 
00169                 mApplication.setMainThreadHandler(mHandler);
00170                 mApplication.setConnectedThreadHandler(mConnectedThread.getHandler());
00171                 mApplication.setVoiceThreadHandler(mVoiceThread.getHandler());
00172                 
00173                 //Provide the worker threads with each other's handlers
00174                 mConnectedThread.setHandlers(mHandler, mVoiceThread.getHandler());
00175                 mVoiceThread.setHandlers(mHandler, mConnectedThread.getHandler());
00176                 
00177                 //Name the threads for debugging purposes
00178                 Thread.currentThread().setName("Main Activity Thread");
00179                 mConnectedThread.setName("Connected Thread");
00180                 mVoiceThread.setName("Voice Recognition Thread");
00181         }
00182 
00183         /*
00184          * This function sets up the voice listener and resets new messages if needed
00185          */
00186         @Override
00187         protected void onResume() {
00188                 super.onResume();
00189                 //Do not turn off screen
00190                 getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
00191                 
00192                 //Set the voice listener thread to use the context of this activity,
00193                 //
00194                 Handler voiceHandler = mApplication.getVoiceThreadHandler();
00195                 
00196                 Message msg = voiceHandler.obtainMessage(
00197                                 VoiceRecognitionThread.CONTEXT_MESSAGE, 
00198                                 this);
00199                 voiceHandler.sendMessage(msg);
00200                 
00201                 //Configure voice listener to listen for default commands
00202                 msg = voiceHandler.obtainMessage(
00203                                 VoiceRecognitionThread.ENABLE_SYSTEM_CMD_MESSAGE, 
00204                                 true);
00205                 voiceHandler.sendMessage(msg);
00206                 
00207                 msg = voiceHandler.obtainMessage(
00208                                 VoiceRecognitionThread.CHANGE_VOCAB_MESSAGE,
00209                                 new ArrayList<String>());
00210                 voiceHandler.sendMessage(msg);
00211                 
00212                 msg = voiceHandler.obtainMessage(
00213                                 VoiceRecognitionThread.LISTENING_MESSAGE, 
00214                                 true);
00215                 voiceHandler.sendMessage(msg);
00216                 
00217                 //If we have just exited MessageListActivity reset new messages to 0
00218                 if (msgFlag)
00219                 {
00220                         numNewMsgs = 0;
00221                         msgFlag = false;
00222                         updateMessageView(0);
00223                 }
00224         }
00225 
00226         /*
00227          * This function pauses the voice listener and allows the screen to turn off
00228          */
00229         @Override
00230         protected void onPause() {
00231                 super.onPause();
00232                 
00233                 //Pause the voice listener
00234                 Handler voiceHandler = mApplication.getVoiceThreadHandler();
00235                 if (voiceHandler != null)
00236                 {
00237                         Message msg = voiceHandler.obtainMessage(VoiceRecognitionThread.LISTENING_MESSAGE, false);
00238                         voiceHandler.sendMessageAtFrontOfQueue(msg);
00239                 }
00240                 
00241                 //Allow the screen to turn off
00242                 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
00243         }
00244 
00245         /*
00246          * This function creates the menu
00247          */
00248         @Override
00249         public boolean onCreateOptionsMenu(Menu menu) {
00250                 MenuInflater inflater = getMenuInflater();
00251                 inflater.inflate(R.menu.main, menu);
00252                 return true;
00253         }
00254 
00255         /*
00256          * This function decides which menu item was selected and
00257          * applies the appropriate action
00258          */
00259         @Override
00260         public boolean onOptionsItemSelected(MenuItem item) {
00261                 // Handle item selection.
00262                 switch (item.getItemId()) {
00263                 case R.id.viewDevices:
00264                         launchBluetoothListActivity();
00265                         return true;
00266                 case R.id.viewRobots:
00267                         launchRobotListActivity();
00268                         return true;
00269                 case R.id.viewMessages:
00270                         launchMessageListActivity();
00271                         return true;
00272                 case R.id.close:
00273                         mApplication.onShutdown();
00274                         return true;
00275                 default:
00276                         return super.onOptionsItemSelected(item);
00277                 }
00278         }
00279 
00280         /*
00281          * This function listens for the user to tap on the Card and
00282          * then opens the menu on tap.
00283          */
00284         private GestureDetector createGestureDetector(Context context) {
00285                 GestureDetector gestureDetector = new GestureDetector(context);
00286                 //Create a base listener for generic gestures
00287                 gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
00288                         @Override
00289                         public boolean onGesture(Gesture gesture) {
00290                                 if (gesture == Gesture.TAP) {
00291                                         //On tap, play tap sound and open menu
00292                                         AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
00293                                         audio.playSoundEffect(Sounds.TAP);
00294                                         openOptionsMenu();
00295                                         return true;
00296                                 }
00297                                 return false;
00298                         }
00299                 });
00300                 return gestureDetector;
00301         }
00302 
00303         /*
00304          * Handles motion events
00305          */
00306         @Override
00307         public boolean onGenericMotionEvent(MotionEvent event) {
00308                 if (mGestureDetector != null) {
00309                         return mGestureDetector.onMotionEvent(event);
00310                 }
00311                 return false;
00312         }
00313         
00314         /*
00315          * This function sets the command text view to the
00316          * passed string
00317          */
00318         public void setCommandView(String cmdText)
00319         {
00320                 commandView.setText("Command: " + cmdText);
00321         }
00322         
00323         /*
00324          * This function sets the focus text view to the
00325          * passed string
00326          */
00327         public void setFocusView(String robotName)
00328         {
00329                 focusView.setText("Focus: " + robotName);
00330         }
00331         
00332         /*
00333          * This function sets the message text view to reflect
00334          * the total number and the number of new messages
00335          * after a change.  The change is given by the integer
00336          * argument
00337          */
00338         public void updateMessageView(int change) 
00339         {
00340                 numMsgs += change;
00341                 numNewMsgs += change;
00342                 
00343                 messageView.setText("\u2709\t" + numMsgs + "\tNew: " + numNewMsgs);
00344         }
00345         
00346         /*
00347          * This function starts the Bluetooth Devices List Activity
00348          */
00349         public void launchBluetoothListActivity()
00350         {
00351                 startActivity(new Intent(this, BluetoothDevicesListActivity.class));
00352         }
00353         
00354         /*
00355          * This function starts the Robot List Activity
00356          */
00357         public void launchRobotListActivity()
00358         {
00359                 startActivity(new Intent(this, ViewRobotListActivity.class));
00360         }
00361         
00362         /*
00363          * This function starts the Message List Activity
00364          */
00365         public void launchMessageListActivity()
00366         {
00367                 msgFlag = true;
00368                 startActivity(new Intent(this, MessageListActivity.class));
00369         }
00370         
00371         /*
00372          * This function shuts down the activity and kills the worker threads
00373          */
00374         public void shutdown()
00375         {
00376                 //Make sure all threads are done before shutting down
00377                 while (mApplication.getConnectedThreadHandler() != null &&
00378                                 mApplication.getVoiceThreadHandler() != null);
00379                 mConnectedThread.interrupt();
00380                 mVoiceThread.interrupt();
00381                 mConnectedThread = null;
00382                 mVoiceThread = null;
00383                 
00384                 finish();               
00385         }
00386 }


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