TopicMenuActivity.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 
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             // Do nothing.
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                 //create a list of fields from the current topic and have the user select which field to view.
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                 //create a list of fields from the current topic and have the user select which field to view.
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              * In the event of the user selecting "graph" or "text" and then
00145              * a specific field for an in depth view of that field, an intent will
00146              * be sent to the service, which in turn will forward the information
00147              * to the renderer.
00148              * 
00149              * Messages will likely be of the form (0, field) for graphing and (1, field) for
00150              * in depth text and (2, null) for return to all topics view
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         // We must call finish() from this method to ensure that the activity ends either when an
00204         // item is selected from the menu or when the menu is dismissed by swiping down.
00205         finish();
00206     }
00207 }


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