/** * */ package de.pointedears.converter.net; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import de.pointedears.converter.R; import de.pointedears.converter.helpers.ConverterThread; /** * @author pelinux * */ public class RatesUpdater implements Runnable { private static final String URL_ECB = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ private final Context activityContext; private ConverterThread updateThread = null; /** * */ public RatesUpdater(Context activityContext) { this.activityContext = activityContext; } /** * @return the updateThread */ public ConverterThread getUpdateThread() { return this.updateThread; } /** * @param updateThread */ public void setUpdateThread(ConverterThread updateThread) { this.updateThread = updateThread; } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ @Override public void run() { if (this.getUpdateThread() != null) { // CurrenciesActivity.this.editValue1.setText("42"); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); try { DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); try { Document doc = builder.parse(RatesUpdater.URL_ECB); XPathFactory xpathFactory = XPathFactory.newInstance(); XPath xpath = xpathFactory.newXPath(); // NamespaceContextHelper namespaceContext = // new NamespaceContextHelper(); // namespaceContext.add("gesmes", // "http://www.gesmes.org/xml/2002-08-01"); // xpath.setNamespaceContext(namespaceContext); try { /* * FIXME: Why doesn't a simple "./Cube/Cube/Cube" work even with a * namespace resolver? */ XPathExpression expr = xpath .compile("./*[local-name() = 'Cube']/*[local-name() = 'Cube']/*[local-name() = 'Cube']"); //$NON-NLS-1$ Object result = expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); NodeList nodes = (NodeList) result; int len = nodes.getLength(); String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) this.activityContext .getSystemService(ns); int icon = R.drawable.icon; CharSequence tickerText = "Found " + len + " nodes!"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); Context applicationContext = this.activityContext.getApplicationContext(); CharSequence contentTitle = "Converter"; CharSequence contentText = "Found " + len + " nodes!"; Intent notificationIntent = new Intent(this.activityContext, this.activityContext.getClass()); PendingIntent contentIntent = PendingIntent.getActivity(this.activityContext, 0, notificationIntent, 0); notification.setLatestEventInfo(applicationContext, contentTitle, contentText, contentIntent); // private static final int HELLO_ID = 1; mNotificationManager.notify(1, notification); for (int i = 0; i < len; ++i) { Node item = nodes.item(i); NamedNodeMap attributes = item.getAttributes(); String currency = attributes .getNamedItem("currency").getNodeValue(); //$NON-NLS-1$ String rate = attributes.getNamedItem("rate").getNodeValue(); //$NON-NLS-1$ /* TODO: Update UI */ System.out.println(currency + ": " + rate); //$NON-NLS-1$ } } catch (XPathExpressionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }