Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
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
00083 for(String topic_name : topicNames) {
00084 Card card = new Card(this);
00085 card.setText(topic_name);
00086 card.setFootnote("Topic Card");
00087
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
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
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
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
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
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 }