Subversion Repositories ES

Compare Revisions

Last modification

Ignore whitespace Rev 16 → Rev 17

/trunk/src/de/pointedears/converter/net/RatesUpdater.java
4,6 → 4,11
package de.pointedears.converter.net;
 
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
15,18 → 20,19
import javax.xml.xpath.XPathFactory;
 
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
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 android.util.Log;
import android.view.View;
import android.widget.TextView;
import de.pointedears.converter.R;
import de.pointedears.converter.app.CurrenciesActivity;
import de.pointedears.converter.db.ConversionData;
import de.pointedears.converter.helpers.ConverterThread;
import de.pointedears.converter.helpers.UpdateService;
 
/**
* @author pelinux
37,15 → 43,20
private static final String URL_ECB =
"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$
 
private final Context activityContext;
private final CurrenciesActivity activityContext;
private ConverterThread updateThread = null;
 
private final UpdateService service;
 
/**
* @param updateService
*
*/
public RatesUpdater(Context activityContext)
public RatesUpdater(CurrenciesActivity activityContext,
UpdateService updateService)
{
this.activityContext = activityContext;
this.service = updateService;
}
 
/**
72,20 → 83,28
@Override
public void run()
{
int len = 0;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$
Date updated = new Date();
 
if (this.getUpdateThread() != null)
{
// CurrenciesActivity.this.editValue1.setText("42");
TextView textUpdating =
(TextView) this.activityContext
.findViewById(R.id.currencies_text_updating);
textUpdating.setVisibility(View.VISIBLE);
 
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder builder;
try
{
DocumentBuilder builder =
documentBuilderFactory.newDocumentBuilder();
builder = documentBuilderFactory.newDocumentBuilder();
Document doc;
try
{
Document doc = builder.parse(RatesUpdater.URL_ECB);
doc = builder.parse(RatesUpdater.URL_ECB);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
// NamespaceContextHelper namespaceContext =
100,80 → 119,95
* FIXME: Why doesn't a simple "./Cube/Cube/Cube" work even with a
* namespace resolver?
*/
@SuppressWarnings("nls")
XPathExpression expr =
xpath
.compile("./*[local-name() = 'Cube']/*[local-name() = 'Cube']/*[local-name() = 'Cube']"); //$NON-NLS-1$
Object result =
.compile("./*[local-name() = 'Cube']/*[local-name() = 'Cube']");
NodeList nodes = (NodeList)
expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
Element parentCube = (Element) nodes.item(0);
if (parentCube == null)
{
return;
}
 
int len = nodes.getLength();
try
{
updated = df.parse(parentCube.getAttribute("time"));
}
catch (ParseException e)
{
Log.e(this.getClass().toString(),
"Could not parse the `time' attribute into a Date", e);
}
 
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) this.activityContext
.getSystemService(ns);
expr =
xpath
.compile("./*[local-name()='Cube' and (@currency='CHF' or @currency='USD')]"); //$NON-NLS-1$
nodes =
(NodeList) expr.evaluate(parentCube, XPathConstants.NODESET);
NodeList childCubes = nodes;
 
int icon = R.drawable.icon;
CharSequence tickerText = "Found " + len + " nodes!";
long when = System.currentTimeMillis();
len = childCubes.getLength();
 
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);
 
HashMap<String, ConversionData> conversionRates =
this.activityContext.getConversionRates();
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$
Element item = (Element) childCubes.item(i);
String currency = item.getAttribute("currency");
 
/* TODO: Update UI */
System.out.println(currency + ": " + rate); //$NON-NLS-1$
try
{
Double rate =
Double.parseDouble(item.getAttribute("rate")); //$NON-NLS-1$
conversionRates
.put(currency, new ConversionData(rate, updated));
}
catch (NumberFormatException e)
{
 
}
}
 
this.activityContext.getDatabase().writeConversionsToDatabase(null);
this.activityContext.fillTableRates();
}
catch (XPathExpressionException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(this.getClass().toString(), "Error in XPath expression", e);
}
}
catch (SAXException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(this.getClass().toString(),
"Exception while parsing external XML resource", e);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(this.getClass().toString(),
"I/O exception while parsing external XML resource", e);
}
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
Log.e(this.getClass().toString(),
"Document builder cannot be created", e);
}
 
if (len > 0)
{
/*
* Notify the activity that we are done (causes a notification to be
* shown)
*/
Intent intent = new Intent(UpdateService.ACTION_UPDATE);
intent.putExtra(UpdateService.EXTRA_NUM_RATES, len);
intent.putExtra(UpdateService.EXTRA_DATE, updated);
this.service.sendBroadcast(intent);
}
 
textUpdating.setVisibility(View.GONE);
}
}
}