TopicSelectActivity.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 java.util.ArrayList;
00019 import java.util.List;
00020 
00021 import org.json.JSONException;
00022 import org.json.JSONObject;
00023 
00024 import android.app.Activity;
00025 import android.content.Intent;
00026 import android.os.Bundle;
00027 import android.util.Log;
00028 import android.view.View;
00029 import android.view.ViewGroup;
00030 import android.widget.AdapterView;
00031 import android.widget.AdapterView.OnItemClickListener;
00032 
00033 import com.google.android.glass.app.Card;
00034 import com.google.android.glass.widget.CardScrollAdapter;
00035 import com.google.android.glass.widget.CardScrollView;
00036 
00037 import de.tavendo.autobahn.WebSocketConnection;
00038 import de.tavendo.autobahn.WebSocketHandler;
00039 
00040 
00049 public class TopicSelectActivity extends Activity {
00050         private List<Card> mCards;
00051         private CardScrollView mCardScrollView;
00052         private String[] stringArr = null;
00053         
00054         @Override
00055         protected void onCreate(Bundle savedInstanceState) {
00056                 super.onCreate(savedInstanceState);
00057                 Intent intt = new Intent(TopicSelectActivity.this, TopicService.class);         
00058                 //kill the service if it is already running because we want to subscrive to a new topic.  
00059                 stopService(intt);
00060                 createTopics();
00061 
00062         }
00063         
00068         private ArrayList<String> getTopics() {
00069                 ArrayList<String> topic_names = new ArrayList<String>();
00070                 if(stringArr != null){
00071                         for(int i=0;i < stringArr.length;i++){
00072                                 topic_names.add(stringArr[i]);
00073                         }
00074                 }else{
00075                         topic_names.add("ERROR NO Topics");
00076                 }
00077                 return topic_names;
00078         }
00079         
00080         private void createCards(ArrayList<String> topicNames) {
00081                 mCards = new ArrayList<Card>();
00082                 //TODO: Handle no topics gracefully.
00083                 for(String topic_name : topicNames) {
00084                         Card card = new Card(this);
00085                         card.setText(topic_name);
00086                         card.setFootnote("Topic Card");
00087                         //card.setInfo("Topic card");
00088                         mCards.add(card);
00089                 }
00090         }
00091         
00092         
00097         private class TopicCardScrollAdapter extends CardScrollAdapter {
00098                 @Override
00099                 public int findIdPosition(Object id) {
00100                         return -1;
00101                 }
00102                 
00103                 @Override
00104                 public int findItemPosition(Object item) {
00105                         return mCards.indexOf(item);
00106                 }
00107                 
00108                 @Override
00109                 public int getCount() {
00110                         return mCards.size();
00111                 }
00112                 
00113                 @Override
00114                 public Object getItem(int position) {
00115                         return mCards.get(position);
00116                 }
00117                 
00118                 @Override
00119                 public View getView(int position, View convertView, ViewGroup parent) {
00120                         return mCards.get(position).toView();
00121                 }
00122         }
00123         
00124         
00128         private void createTopics() {
00129 
00130                 final String url = TopicService.HOST_ADDRESS;
00131                 
00132                 final WebSocketConnection mConnection = new WebSocketConnection();
00133 
00134                 try{
00135                         Log.d("Socket", "Atempting connection");
00136                         mConnection.connect(url, new WebSocketHandler(){
00137                                 boolean message_received = false;
00138                                 @Override
00139                                 public void onOpen() {
00140                                         
00141                                         //send the request
00142                                         JSONObject jsonRequest = new JSONObject();
00143 
00144                                         try {
00145                                                 jsonRequest.put("op", "call_service");
00146                                                 jsonRequest.put("service", "/list_topics");
00147                                         } catch(Exception e) {
00148 
00149                                         }
00150                                         Log.d("Main Connection", "Status: Connected to " + url);
00151                                         mConnection.sendTextMessage(jsonRequest.toString());
00152 
00153                                 }
00154 
00155                                 @Override
00156                                 public void onTextMessage(String payload) {
00157                                         message_received = true;
00158                                         Log.d("Main Payload", payload);
00159                                         //We got a message back from the server so lets create the cards for selection.
00160                                         JSONObject obj;
00161                                         String topics = "n/a";
00162                                         try {
00163                                                 obj = new JSONObject(payload);
00164                                                 topics = obj.getJSONObject("values").getString("topics");
00165 
00166                                         } catch (JSONException e) {
00167                                                 topics = "n/a";
00168                                 }
00169                                         if(topics.equals("n/a")){
00170                                                 Card card1 = new Card(getBaseContext());
00171                                         card1.setText("No Topics Returned Check Setup.");
00172                                                 View card1View = card1.toView();
00173                                         setContentView(card1View);
00174                                         }else{
00175                                         //Split topics and create cards.
00176                                         stringArr = topics.split(",");
00177                                         ArrayList<String> topicNames = getTopics();
00178 
00179                                         createCards(topicNames);
00180 
00181                                         mCardScrollView = new CardScrollView(TopicSelectActivity.this);
00182                                         TopicCardScrollAdapter adapter = new TopicCardScrollAdapter();
00183                                         mCardScrollView.setAdapter(adapter);
00184                                         mCardScrollView.setOnItemClickListener(new OnItemClickListener(){
00185 
00190                                                 @Override
00191                                                 public void onItemClick(AdapterView<?> parent, View view,
00192                                                                 int position, long id) {
00193                                                                 
00194                                                                 String topic = ((Card) parent.getItemAtPosition(position)).getText();
00195                                                                 
00196                                                                 //bindService(new Intent(TopicSelectActivity.this, TopicService.class), mServiceConnection, 0);
00197                                                                 Intent intt = new Intent(TopicSelectActivity.this, TopicService.class); 
00198                                                                 intt.putExtra("view_state", TopicService.ALL_FIELDS);
00199                                                                 intt.putExtra("topic",topic);
00200                                                                 startService(intt);
00201                                                 }
00202                                                 
00203                                         });
00204                                         
00205                                         //Show the scroll cards and close the connection.
00206                                         mCardScrollView.activate();
00207                                         setContentView(mCardScrollView);
00208                                         mConnection.disconnect();
00209 
00210                                         }
00211                                 }
00212 
00213                                 @Override
00214                                 public void onClose(int code, String reason) {
00215                                         if(!message_received){
00216                                                 Card card1 = new Card(getBaseContext());
00217                                         card1.setText("No Web Connection");
00218                                                 View card1View = card1.toView();
00219                                         setContentView(card1View);
00220                                         }
00221                                 }
00222 
00223                         });
00224                         
00225                 }catch (Exception e){
00226                         e.printStackTrace();
00227                 }
00228         }
00229         
00230 }


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