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
00035 import java.util.ArrayList;
00036 import java.util.Iterator;
00037
00038 import org.json.JSONException;
00039 import org.json.JSONObject;
00040
00041 import android.app.Activity;
00042 import android.content.ComponentName;
00043 import android.content.Intent;
00044 import android.content.ServiceConnection;
00045 import android.os.Bundle;
00046 import android.os.IBinder;
00047 import android.util.Log;
00048 import android.view.Menu;
00049 import android.view.MenuItem;
00050
00055 public class TopicMenuActivity extends Activity {
00056
00057 private TopicService.TopicBinder mCompassService;
00058 private boolean mResumed;
00059
00060 private ServiceConnection mConnection = new ServiceConnection() {
00061 @Override
00062 public void onServiceConnected(ComponentName name, IBinder service) {
00063 if (service instanceof TopicService.TopicBinder) {
00064 mCompassService = (TopicService.TopicBinder) service;
00065 openOptionsMenu();
00066 }
00067 }
00068
00069 @Override
00070 public void onServiceDisconnected(ComponentName name) {
00071
00072 }
00073
00074 public void remove_card(){
00075 this.remove_card();
00076 }
00077 };
00078
00079 @Override
00080 protected void onCreate(Bundle savedInstanceState) {
00081 super.onCreate(savedInstanceState);
00082 bindService(new Intent(this, TopicService.class), mConnection, 0);
00083 }
00084
00085 @Override
00086 protected void onResume() {
00087 super.onResume();
00088 mResumed = true;
00089 openOptionsMenu();
00090 }
00091
00092 @Override
00093 protected void onPause() {
00094 super.onPause();
00095 mResumed = false;
00096 }
00097
00098 @Override
00099 public void openOptionsMenu() {
00100 if (mResumed && mCompassService != null) {
00101 super.openOptionsMenu();
00102 }
00103 }
00104
00105 @Override
00106 public boolean onCreateOptionsMenu(Menu menu) {
00107 getMenuInflater().inflate(R.menu.topic, menu);
00108 return true;
00109 }
00110
00115 @Override
00116 public boolean onOptionsItemSelected(MenuItem item) {
00117 switch (item.getItemId()) {
00118 case R.id.stop:
00119 stopService(new Intent(this, TopicService.class));
00120 return true;
00121 case R.id.graph_view:
00122
00123 Log.d("Menu:", "Switching to graph view");
00124 ArrayList<String> messageFields = createFieldList(mCompassService.getOriginalMessage());
00125 Intent startIntent = new Intent(this, TopicFieldSelectActivity.class);
00126 startIntent.putStringArrayListExtra("field_names", messageFields);
00127 startIntent.putExtra("topic", mCompassService.getCurrentTopic());
00128 startActivity(startIntent);
00129 return true;
00130 case R.id.text_view:
00131
00132 Log.d("Menu:", "Switching to text view");
00133 ArrayList<String> mFields = createFieldList(mCompassService.getOriginalMessage());
00134 Intent sint = new Intent(this, TopicFieldSelectActivity.class);
00135 sint.putStringArrayListExtra("field_names", mFields);
00136 sint.putExtra("textual", true);
00137 sint.putExtra("topic", mCompassService.getCurrentTopic());
00138 startActivity(sint);
00139 return true;
00140 default:
00141 return super.onOptionsItemSelected(item);
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 }
00153 }
00154
00155
00156
00161 private ArrayList<String> createFieldList(String topicText) {
00162 ArrayList<String> arrList = new ArrayList<String>();
00163 JSONObject msg;
00164 try {
00165
00166 msg = new JSONObject(topicText);
00167 buildRecursiveList(msg.getJSONObject("msg"), arrList, "");
00168 return arrList;
00169 } catch (JSONException e) {
00170 return null;
00171 }
00172 }
00173
00174
00178 public void buildRecursiveList(JSONObject msg, ArrayList<String> arrList, String currentPrefix) throws JSONException{
00179 Iterator<String> fields = msg.keys();
00180
00181 while(fields.hasNext()){
00182 String val = fields.next();
00183 try{
00184 if(currentPrefix.equals("")){
00185 buildRecursiveList(msg.getJSONObject(val), arrList, val);
00186 }else{
00187 buildRecursiveList(msg.getJSONObject(val), arrList, currentPrefix + "." + val);
00188
00189 }
00190 }catch(Exception e){
00191 arrList.add(currentPrefix +"." + val);
00192 }
00193 }
00194 }
00195
00196
00197 @Override
00198 public void onOptionsMenuClosed(Menu menu) {
00199 super.onOptionsMenuClosed(menu);
00200
00201 unbindService(mConnection);
00202
00203
00204
00205 finish();
00206 }
00207 }