/* * This file is part of the Remote Sensor Actuator Interface (ReSAI). * * Copyright(c) Andreas Kipp, Frederic Siepmann * http://opensource.cit-ec.de/projects/resai * * This file may be licensed under the terms of of the * GNU Lesser General Public License Version 3 (the ``LGPL''), * or (at your option) any later version. * * Software distributed under the License is distributed * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either * express or implied. See the LGPL for the specific language * governing rights and limitations. * * You should have received a copy of the LGPL along with this * program. If not, go to http://www.gnu.org/licenses/lgpl.html * or write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * The development of this software was supported by the * Excellence Cluster EXC 277 Cognitive Interaction Technology. * The Excellence Cluster EXC 277 is a grant of the Deutsche * Forschungsgemeinschaft (DFG) in the context of the German * Excellence Initiative. * */ /** * Configuration tab showing beacon info and component states. * * @author akipp */ package de.unibi.airobots.resaidroid.tabactivities; import java.io.FileInputStream; import java.io.IOException; import java.util.SortedMap; import java.util.TreeMap; import android.app.Activity; import android.app.TabActivity; import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TabHost; import android.widget.TextView; import de.unibi.airobots.resaidroid.R; import de.unibi.airobots.resaidroid.TabComponentMap; import de.unibi.airobots.resaidroid.communication.XmppConnection; import de.unibi.airobots.resaidroid.constants.Constants; import de.unibi.airobots.resaidroid.constants.Constants.Commands; import de.unibi.airobots.resaidroid.constants.Constants.Properties; import de.unibi.airobots.resaidroid.constants.ServerConfig; import de.unibi.airobots.resaidroid.helper.TimeConverter; import de.unibi.airobots.resaidroid.tabactivities.template.TabTemplate; public class TabConfig extends TabTemplate { private int tabNumber = 0; private TabActivity tabManager; private TextView lblServer; private TextView lblServerTime; private TextView lblClientTime; private TextView lblLastBeacon; private TextView lblBeaconDuration; private TextView lblStatus; private Button btnDisconnect; private FileInputStream fis; private TextView lblComponents; private TabComponentMap tcm; private SortedMap componentMap = new TreeMap(); private boolean tabsCreated = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.config); Resources res = getResources(); tcm = new TabComponentMap(res); tabManager = (TabActivity) getParent(); lblServer = (TextView) findViewById(R.id.lblServer); lblServerTime = (TextView) findViewById(R.id.lblServerTime); lblClientTime = (TextView) findViewById(R.id.lblClientTime); lblBeaconDuration = (TextView) findViewById(R.id.lblBeaconDuration); lblLastBeacon = (TextView) findViewById(R.id.lblLastBeacon); lblStatus = (TextView) findViewById(R.id.lblStatus); lblComponents = (TextView) findViewById(R.id.lblComponents); btnDisconnect = (Button) findViewById(R.id.btnDisconnect); btnDisconnect.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { addProperty("TAG", "ReSAIServerThread"); addProperty(Properties.CMD.name(), Commands.disconnect.name()); sendMessage(ServerConfig.RECIPIENT_FULL); returnWithError(Activity.RESULT_OK); } }); readConfig(); if (com.isLoggedIn()) { addProperty("TAG", "ComponentDispatcher"); addProperty(Properties.CMD.name(), Commands.requestComponentList.name()); sendMessage(ServerConfig.RECIPIENT_FULL); } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); messageHandler.removeCallbacks(StatusChecker); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); messageHandler.removeCallbacks(StatusChecker); messageHandler.postDelayed(StatusChecker, 2000); } @Override public void processProperties() { Log.i(TAG, "PROCCING DATA..."); messageHandler.post(new Runnable() { public void run() { try { synchronized (receivedProperties) { if (!tabsCreated) { for (String prop : receivedProperties.keySet()) { if (receivedProperties.get(prop) != null) { if (receivedProperties.get(prop).equals( Properties.COMPONENT.name())) { // Read all components and put into // sorted // map componentMap.put( cm.getTabPositionByThread(prop), prop); // TODO dirty hack for scripter if (prop.equals("ScripterThread")) { componentMap.put( cm.getTabPositionByThread(prop + "_2"), prop + "_2"); } } } } // Read sorted map and create all tabs for (int position : componentMap.keySet()) { addComponentTab(tcm.getTabClass(componentMap .get(position))); lblComponents.append(cm.getTabName(cm .getTabClass(componentMap.get(position))) + "\n"); } tabsCreated = true; } if (receivedProperties.containsKey("COMPONENT_STATES")) { lblComponents.setText(""); for (String prop : receivedProperties.keySet()) { if (prop.endsWith("STATE")) { String componentname = prop.substring(0, prop.indexOf("_")); String compstate = receivedProperties .get(prop); lblComponents.append(cm .getTabNameByComponentName(componentname) + " (" + compstate + ")\n"); } } } } } catch (Exception e) { // TODO Log.i(TAG, "### EXCEPTION " + e.getMessage()); } lblServerTime.setText(TimeConverter.toDateTime(XmppConnection .getServerTime())); lblClientTime.setText(TimeConverter.toDateTime(XmppConnection .getClientTime())); lblBeaconDuration.setText(String.valueOf(Math .abs(XmppConnection.getBeaconDuration())) + "ms"); } }); } private Runnable StatusChecker = new Runnable() { public void run() { messageHandler.post(new Runnable() { public void run() { lblServerTime.setText(TimeConverter .toDateTime(XmppConnection.getServerTime())); lblClientTime.setText(TimeConverter .toDateTime(XmppConnection.getClientTime())); lblLastBeacon.setText(TimeConverter .toDateTime(XmppConnection.getLastBeacon())); lblBeaconDuration.setText(String.valueOf(Math .abs(XmppConnection.getBeaconDuration())) + "ms"); } }); addProperty("TAG", "ComponentThread"); addProperty(Properties.CMD.name(), Commands.requestComponentStates.name()); sendMessage(ServerConfig.RECIPIENT_FULL); messageHandler.postDelayed(StatusChecker, 2000); } }; /** * reads the configuration file and fills the corresponding text boxes * */ private void readConfig() { try { fis = openFileInput(ServerConfig.CONFIGFILE); byte[] byteArray = new byte[102400]; int bytesRead = 0; while ((bytesRead = fis.read(byteArray)) != -1) { String configLine = new String(byteArray, 0, bytesRead); String[] configLines = configLine.split("\n"); for (String singleLine : configLines) { String[] config = singleLine.split("="); if (config[0].equals(Properties.SERVER_IP.name())) lblServer.setText(config[1]); if (config[0].equals(Properties.SERVER_PORT.name())) lblServer.append(":" + config[1]); } } } catch (IOException e) { addStatus("Configuration file not found."); addStatus(e.getMessage()); } } /** * Create a new Tab with given TabClass name * * @param tabClassName * given TabClass to add */ private void addComponentTab(final String tabClassName) { if (tabClassName != null) { messageHandler.post(new Runnable() { public void run() { try { ClassLoader loader = getClassLoader(); Class tabClass = loader .loadClass(Constants.TABACTIVITY_PATH + tabClassName); if (tabClass != null) { if (tabManager == null) { addStatus("tabwidget is null"); } else { TabHost tabHost = tabManager.getTabHost(); Intent intent; Context context = getApplicationContext(); intent = new Intent().setClass(context, tabClass); TabHost.TabSpec spec = tabHost .newTabSpec(tabClassName + "_" + tabNumber); spec.setContent(intent); spec.setIndicator(" " + cm.getTabName(tabClassName) + " "); try { tabHost.addTab(spec); } catch (Exception e) { addStatus("ERROR ADDING TAB: " + e.getMessage()); } } } else { addStatus("Class not found: " + tabClassName); } tabClass = null; } catch (ClassNotFoundException e) { addStatus("Could not add tab " + tabClassName); addStatus(e.getMessage()); Log.i(TAG, e.getMessage()); } } }); tabNumber++; } } private void addStatus(String message) { lblStatus.append("\n" + TimeConverter.toTime(System.currentTimeMillis()) + ": " + message); } }