TopicService.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 The Android Open Source Project
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /*
00018 copyright 2014 UNL Nimbus Lab 
00019 
00020   Licensed under the Apache License, Version 2.0 (the "License");
00021      you may not use this file except in compliance with the License.
00022     You may obtain a copy of the License at
00023   
00024         http://www.apache.org/licenses/LICENSE-2.0
00025   
00026     Unless required by applicable law or agreed to in writing, software
00027   distributed under the License is distributed on an "AS IS" BASIS,
00028    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00029    See the License for the specific language governing permissions and
00030   limitations under the License.
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                 //Keep text to speech here but it is not used in our application
00112                 mSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
00113                         @Override
00114                         public void onInit(int status) {
00115                                 // Do nothing.
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                  * Will need to add logic to check if topicRenderer was already created.
00145                  * If it is, then switch the state that it is rendering.
00146                  */
00147                 mTimelineManager = TimelineManager.from(this);
00148                 String view_state = intent.getStringExtra("view_state");        //view_state -> {graph, field_focus, all_fields}
00149                 Log.d("State to enter", view_state);
00150                 /*
00151                  * If select all_fields and it isn't the same topic as before, have to reconnect.
00152                  * Otherwise just switch state back to all_fields
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                                  * I'm not sure if I should destroy the old one (if it exists) or reuse what I can.
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                          * Other options to interact with data: graph and focus on field
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                         //display the options menu when the live card is tapped.
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                 //Get rid of the livecard and clean everything up.
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 }


ros_glass_tools
Author(s):
autogenerated on Thu Aug 27 2015 14:47:21