ROSMonitorService.java
Go to the documentation of this file.
00001 /*
00002 copyright 2014 UNL Nimbus Lab 
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 package edu.nimbus.glass;
00017 
00018 import android.app.PendingIntent;
00019 import android.app.Service;
00020 import android.content.Intent;
00021 import android.os.Binder;
00022 import android.os.IBinder;
00023 import android.widget.RemoteViews;
00024 import android.widget.TextView;
00025 
00026 import com.google.android.glass.timeline.LiveCard;
00027 import com.google.android.glass.timeline.TimelineManager;
00028 
00029 import de.tavendo.autobahn.WebSocketConnection;
00030 import de.tavendo.autobahn.WebSocketException;
00031 
00032 
00038 public class ROSMonitorService extends Service {
00039 
00040 
00042         private static final String LIVE_CARD_ID = "ROS_WARNING";
00044         public final static String HOST_ADDRESS = "ws://10.214.32.10:9090";
00045         
00046 
00048         private TimelineManager mTimelineManager;
00049         private LiveCard mLiveCard;
00050         private final ROSMonitorBinder mBinder = new ROSMonitorBinder();
00051 
00052         
00054         private ROSMonitorManager mROSMonitor;
00055         private WebSocketConnection mConnection;
00056 
00057 
00062         public class ROSMonitorBinder extends Binder {
00063 
00064                 public void remove_card() {
00065                                 ROSMonitorService.this.removeLiveCard();
00066                         }
00067 
00068 
00069         }
00070 
00071         @Override
00072         public void onCreate(){
00073                 if(mConnection==null){
00074                         mConnection = new WebSocketConnection();
00075                 }
00076 
00077         }
00078 
00088         @Override
00089         public int onStartCommand(Intent intent, int flags, int startId) {      
00090                 mTimelineManager = TimelineManager.from(this);
00091 
00092 
00093 
00094                 try {
00095                         mROSMonitor = new ROSMonitorManager(this, mConnection);
00096                         mConnection.connect(HOST_ADDRESS,mROSMonitor );
00097                 } catch (WebSocketException e) {
00098                         e.printStackTrace();
00099                 }
00100 
00101 
00102                 return START_STICKY;
00103         }
00104 
00105 
00106         @Override
00107         public void onDestroy() {
00108 
00109                 //Get rid of the livecard and clean everything up.
00110                 if (mLiveCard != null && mLiveCard.isPublished()) {
00111                         mLiveCard.unpublish();
00112                         mLiveCard = null;
00113                 }
00114 
00115                 mConnection.disconnect();
00116                 mConnection = null;
00117                 super.onDestroy();
00118         }
00119 
00120         @Override
00121         public IBinder onBind(Intent intent) {
00122                 return mBinder;
00123         }
00124 
00130         public void createWarning(String error_message) {
00131                 //if we have to live card create one
00132                 if (mLiveCard == null){
00133                         mLiveCard = mTimelineManager.createLiveCard(LIVE_CARD_ID);
00134                 }
00135                 //get rid of the old one
00136                 if(mLiveCard.isPublished()){
00137                         mLiveCard.unpublish();
00138                 }
00139                 //Create the view for the live card including the error message.
00140                 TextView text = new TextView(this);
00141                 text.setText(error_message);
00142                 RemoteViews views = new RemoteViews(getBaseContext().getPackageName(), R.layout.warn_card);
00143                 views.setTextViewText(R.id.warn_text, error_message     );
00144                 mLiveCard.setViews(views);
00145                 
00146                 //publish the live card and ensure that it takes up display
00147              Intent intent = new Intent(this, MenuActivity.class);
00148              mLiveCard.setAction(PendingIntent.getActivity(this, 0, intent, 0));
00149              mLiveCard.publish(LiveCard.PublishMode.REVEAL);
00150         }
00151         
00152         
00156         public void removeLiveCard(){
00157                 if(mLiveCard != null){
00158                         mLiveCard.unpublish();
00159                         mROSMonitor.setWarning("");
00160                 }
00161                 
00162         }
00163 }
00164 


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