/* * File: TSetup.java * This file is part of Tico, an application to create and perform * interactive communication boards to be used by people with * severe motor disabilities. * * Author: Eduardo Ferrer * * Date: Jan, 2012 * * Company: Dept. of Computer Sciences and Systems Engineering, Universidad de Zaragoza, Spain * * * License: * This program is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package configuration; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; import java.io.OutputStreamWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import android.TICO.R; import android.TICO.TICO; import android.content.Context; import android.os.Environment; /** * Static class that manages the application configuration. It allows to * write and read from a XML file (tico.conf). * * @author Eduardo Ferrer * @version 1.0 Jan, 2012 */ public class TSetup { private static String CONFIGURATION_FILE_PATH = "tico.conf"; private static String interpreterHomeDirectory = ""; private static String cellTextPosition = ""; private static float volume=1.0f; private static String selectionMode = "direct"; private static int scanWaitTime = 1000; private static boolean alternativeSound=false; private static String controlScanning=""; /** * Loads the configuration file tico.conf * @param context the Context of the application * * @throws ParserConfigurationException If there are syntactic error * in the XML document * @throws SAXException If there are problems transforming the * text to a XML document * @throws IOException If there are file problems */ public static void load(Context context) throws ParserConfigurationException, SAXException, IOException, Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(context.openFileInput(CONFIGURATION_FILE_PATH)); Element preferencesElement = doc.getDocumentElement(); if (preferencesElement.getTagName().equals("preferences")) { NodeList interpreterNodeList = preferencesElement.getElementsByTagName("interpreterHomeDirectory"); if (interpreterNodeList.getLength() > 0) { String interpreterHomeDirectory = interpreterNodeList.item(0).getChildNodes().item(0).getNodeValue(); if (interpreterHomeDirectory != null) if (directoryExists(interpreterHomeDirectory)) setInterpreterHome(interpreterHomeDirectory); } NodeList textCellPositionNodeList = preferencesElement.getElementsByTagName("cellTextPosition"); if (textCellPositionNodeList.getLength() > 0) { cellTextPosition = textCellPositionNodeList.item(0).getChildNodes().item(0).getNodeValue(); }else{ cellTextPosition="down"; } NodeList volumeNodeList = preferencesElement.getElementsByTagName("volume"); if (volumeNodeList.getLength() > 0) { volume = Float.parseFloat(volumeNodeList.item(0).getChildNodes().item(0).getNodeValue()); }else{ volume=1.0f; } NodeList selectionModeNodeList = preferencesElement.getElementsByTagName("selectionMode"); if (selectionModeNodeList.getLength() > 0) { selectionMode = selectionModeNodeList.item(0).getChildNodes().item(0).getNodeValue(); }else{ selectionMode="direct"; } NodeList scanWaitTimeNodeList = preferencesElement.getElementsByTagName("scanWaitTime"); if (scanWaitTimeNodeList.getLength() > 0) { scanWaitTime = Integer.parseInt(scanWaitTimeNodeList.item(0).getChildNodes().item(0).getNodeValue()); }else{ scanWaitTime=TICO.res.getInteger(R.integer.barrido_espera_min_milisegundos); } NodeList alternativeSoundNodeList = preferencesElement.getElementsByTagName("alternativeSound"); if (alternativeSoundNodeList.getLength() > 0) { alternativeSound = Boolean.parseBoolean(alternativeSoundNodeList.item(0).getChildNodes().item(0).getNodeValue()); }else{ alternativeSound=false; } NodeList controlScanningNodeList = preferencesElement.getElementsByTagName("controlScanning"); if (controlScanningNodeList.getLength() > 0) { controlScanning = controlScanningNodeList.item(0).getChildNodes().item(0).getNodeValue(); }else{ controlScanning="last"; } } } /** * Saves the configuration file tico.conf * * @param context the Context of the application * * @throws ParserConfigurationException If there are syntactic error * in the XML document * @throws IOException If there are file problems * @throws TransformerException If there are problems transforming the * XML document to text */ public static void save(Context context) throws ParserConfigurationException, IOException, TransformerException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder domBuilder = domFactory.newDocumentBuilder(); Document doc = domBuilder.newDocument(); // Create properties node Element projectElement = doc.createElement("preferences"); // Create interpreter home node if (!getInterpreterHome().equals("")){ Element interpreterHomeElement = doc.createElement("interpreterHomeDirectory"); interpreterHomeElement.appendChild(doc.createTextNode(getInterpreterHome())); // Append interpreter home node projectElement.appendChild(interpreterHomeElement); } // Create cell text position node if (!getCellTextPosition().equals("")){ Element cellTextPositionElement = doc.createElement("cellTextPosition"); cellTextPositionElement.appendChild(doc.createTextNode(getCellTextPosition())); // Append cell text position projectElement.appendChild(cellTextPositionElement); } // Create volume node if (!(getVolume()<0.0f) && !(getVolume()>1.0f)){ Element volumeElement = doc.createElement("volume"); volumeElement.appendChild(doc.createTextNode(Float.toString(getVolume()))); // Append volume projectElement.appendChild(volumeElement); } // Create selection mode node if (!getSelectionMode().equals("")){ Element selectionModeElement = doc.createElement("selectionMode"); selectionModeElement.appendChild(doc.createTextNode(getSelectionMode())); // Append selection mode projectElement.appendChild(selectionModeElement); } // Create scan wait time node if (!(getScanWaitTime()TICO.res.getInteger(R.integer.barrido_espera_max_milisegundos))){ Element scanWaitTimeElement = doc.createElement("scanWaitTime"); scanWaitTimeElement.appendChild(doc.createTextNode(Integer.toString(getScanWaitTime()))); // Append scan wait time projectElement.appendChild(scanWaitTimeElement); } // Create alternative sound node Element alternativeSoundElement = doc.createElement("alternativeSound"); alternativeSoundElement.appendChild(doc.createTextNode(Boolean.toString(getAlternativeSound()))); // Append alternative sound projectElement.appendChild(alternativeSoundElement); // Create control scanning node if (!getControlScanning().equals("")){ Element controlScanningElement = doc.createElement("controlScanning"); controlScanningElement.appendChild(doc.createTextNode(getControlScanning())); // Append alternative sound projectElement.appendChild(controlScanningElement); } // Append preferences node doc.appendChild(projectElement); // Write the file BufferedWriter bufferWriter = new BufferedWriter( new OutputStreamWriter( context.openFileOutput(CONFIGURATION_FILE_PATH, Context.MODE_PRIVATE), "UTF-8")); // Use a Transformer for output TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(bufferWriter); transformer.transform(source, result); bufferWriter.close(); } /** * Returns the interpreter home directory. * * @return The interpreter current home directory */ public static String getInterpreterHome() { return interpreterHomeDirectory; } /** * Sets the interpreter home directory. * * @param pathInterpreter The interpreter current home directory to set */ public static void setInterpreterHome(String pathInterpreter) { TSetup.interpreterHomeDirectory = pathInterpreter; } /** * Returns the cell text position. * * @return The (up down noText) position of the text in the cell */ public static String getCellTextPosition() { return cellTextPosition; } /** * Sets the cell text position. * * @param position The cell text position (up down noText) */ public static void setCellTextPosition(String position) { TSetup.cellTextPosition = position; } /** * Returns the volume level * * @return The volume level */ public static float getVolume() { return volume; } /** * Sets the volume level. * * @param volume The desired volume level */ public static void setVolume(float volume) { TSetup.volume = volume; } /** * Returns the selection mode * * @return selectionMode (direct or automatic) */ public static String getSelectionMode() { return selectionMode; } /** * Sets the selection mode selectionMode * * @param mode The desired selection mode (direct or automatic) */ public static void setSelectionMode(String mode) { TSetup.selectionMode = mode; } /** * Returns the scan wait time in milliseconds * * @return scanWaitTime */ public static int getScanWaitTime() { return scanWaitTime; } /** * Sets the automatic scan wait time scanWaitTime * * @param time The desired scan wait time in automatic scanning in milliseconds */ public static void setScanWaitTime(int time) { TSetup.scanWaitTime = time; } /** * Returns if is enabled alternative sound or not * * @return the configuration enabled or not of the alternative sound */ public static boolean getAlternativeSound() { return alternativeSound; } /** * Sets if enabled or not the alternative sound * * @param enable true or false for enabled alternative sound */ public static void setAlternativeSound(boolean enable) { TSetup.alternativeSound = enable; } /** * Returns if is enabled or not the scanning of outboard control cells in automatic scanning mode * * @return the configuration enabled or not the scanning of outboard control cells in automatic scanning mode */ public static String getControlScanning() { return controlScanning; } /** * Sets if outboard control cells in automatic scanning mode are scanning first, last or not scanned * * @param order String with "first" "last" or "no_scanning" for the order of scanning of outboard control cells in automatic scanning mode */ public static void setControlScanning(String order) { TSetup.controlScanning = order; } /** * Returns if is the given directory path exists or not * @param directoryPath String which contains the directory path * * @return boolean true if the given directory path exists, otherwise false */ private static boolean directoryExists(String directoryPath){ File file = new File(directoryPath); return file.exists(); } /** * Creates the configuration file tico.conf with the initial values * @param context the Context of the application * */ public static void createInitialConfig(Context context) { interpreterHomeDirectory = Environment.getExternalStorageDirectory().getAbsolutePath(); cellTextPosition = TICO.res.getString(R.string.position_text_default); volume=1.0f; selectionMode = TICO.res.getString(R.string.selection_mode_default); scanWaitTime = TICO.res.getInteger(R.integer.barrido_espera_min_milisegundos); alternativeSound=TICO.res.getBoolean(R.bool.alternative_sound_default); try { save(context); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }