Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 package edu.nimbus.glass;
00033
00034 import com.google.android.glass.timeline.LiveCard;
00035 import com.google.android.glass.timeline.TimelineManager;
00036
00037 import de.tavendo.autobahn.WebSocketConnection;
00038 import de.tavendo.autobahn.WebSocketException;
00039
00040 import android.app.PendingIntent;
00041 import android.app.Service;
00042 import android.content.Intent;
00043
00044 import android.os.Binder;
00045 import android.os.IBinder;
00046 import android.speech.tts.TextToSpeech;
00047 import android.util.Log;
00048
00049
00050
00054 public class TopicService extends Service {
00055 WebSocketConnection mConnection;
00056
00057 public static final String ALL_FIELDS = "ALL_FIELDS";
00058 public static final String GRAPH = "GRAPH";
00059 public static final String FIELD_FOCUS = "FIELD_FOCUS";
00060 public static final int NUM_DECIMALS = 2;
00061 private final TopicBinder mBinder = new TopicBinder();
00062
00063 private TopicManager mTopicManager;
00064 private TextToSpeech mSpeech;
00065
00066 private TimelineManager mTimelineManager;
00067 private LiveCard mLiveCard;
00068 private TopicRenderer mRenderer;
00069
00070 private String topic;
00071
00072
00073 private static final String LIVE_CARD_ID = "topic";
00075 public final static String HOST_ADDRESS = "ws://10.214.32.10:9090";
00076
00077
00082 public class TopicBinder extends Binder {
00083 public String getTopicText(){
00084 if(mTopicManager != null){
00085 return mTopicManager.getText();
00086 }else{
00087 return null;
00088 }
00089 }
00090
00091 public String getOriginalMessage(){
00092 if(mTopicManager != null){
00093 return mTopicManager.getOriginalMessage();
00094 }else{
00095 return null;
00096 }
00097 }
00098
00099 public String getCurrentTopic(){
00100 return topic;
00101
00102 }
00103 }
00104
00105
00106
00107 @Override
00108 public void onCreate() {
00109 super.onCreate();
00110
00111
00112 mSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
00113 @Override
00114 public void onInit(int status) {
00115
00116 }
00117
00118 });
00119 if(mConnection==null){
00120 mConnection = new WebSocketConnection();
00121 }
00122
00123
00124
00125 }
00126
00127 @Override
00128 public IBinder onBind(Intent intent) {
00129 return mBinder;
00130 }
00131
00141 @Override
00142 public int onStartCommand(Intent intent, int flags, int startId) {
00143
00144
00145
00146
00147 mTimelineManager = TimelineManager.from(this);
00148 String view_state = intent.getStringExtra("view_state");
00149 Log.d("State to enter", view_state);
00150
00151
00152
00153
00154 if(view_state.equals(TopicService.ALL_FIELDS)){
00155 String new_topic = intent.getStringExtra("topic");
00156 if(topic == null || !new_topic.equals(topic)){
00157
00158
00159
00160
00161 Log.d("TOPIC", new_topic);
00162 topic = new_topic;
00163 rosTopic(new_topic);
00164 }else{
00165 Log.d("Viewing previous topic", topic);
00166 mRenderer.setRendererAllFieldsState();
00167 }
00168
00169
00170
00171
00172 }else{
00173 assert topic != null;
00174 if(view_state.equals(TopicService.GRAPH)){
00175 Log.d("Graphing Topic: ", topic);
00176 mRenderer.setRendererGraphState(intent.getStringExtra("field"));
00177 }else if(view_state.equals(TopicService.FIELD_FOCUS)){
00178 Log.d("Text focus on topic: ", topic);
00179 mRenderer.setRendererFieldFocusState(intent.getStringExtra("field"));
00180 }else{
00181 Log.d("Incorrect menu option", view_state);
00182 }
00183 }
00184
00185 return START_STICKY;
00186 }
00187
00194 private void rosTopic(String new_topic){
00195 mTopicManager = new TopicManager(new_topic.trim() , HOST_ADDRESS, mConnection);
00196 try {
00197 mConnection.connect(HOST_ADDRESS, mTopicManager);
00198 } catch (WebSocketException e) {
00199 e.printStackTrace();
00200 }
00201 if (mLiveCard == null) {
00202 mLiveCard = mTimelineManager.createLiveCard(LIVE_CARD_ID);
00203 mRenderer = new TopicRenderer(this, mTopicManager);
00204
00205 mLiveCard.setDirectRenderingEnabled(true).getSurfaceHolder().addCallback(mRenderer);
00206
00207 Intent menuIntent = new Intent(this, TopicMenuActivity.class);
00208 menuIntent.putExtra("Destroy", true);
00209 menuIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
00210 mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
00211 mLiveCard.publish(LiveCard.PublishMode.REVEAL);
00212 }
00213 }
00214
00215 @Override
00216 public void onDestroy() {
00217
00218
00219 if (mLiveCard != null && mLiveCard.isPublished()) {
00220 mLiveCard.unpublish();
00221 mLiveCard.getSurfaceHolder().removeCallback(mRenderer);
00222 mLiveCard = null;
00223 }
00224
00225 mSpeech.shutdown();
00226 mConnection.disconnect();
00227 mConnection = null;
00228 mSpeech = null;
00229 mTopicManager = null;
00230
00231 super.onDestroy();
00232 }
00233
00234
00239 public String getTopic(){
00240 return topic;
00241 }
00242 }