4,11 → 4,6 |
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; |
20,19 → 15,18 |
import javax.xml.xpath.XPathFactory; |
|
import org.w3c.dom.Document; |
import org.w3c.dom.Element; |
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 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 |
43,20 → 37,15 |
private static final String URL_ECB = |
"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
|
private final CurrenciesActivity activityContext; |
private final Context activityContext; |
private ConverterThread updateThread = null; |
|
private final UpdateService service; |
|
/** |
* @param updateService |
* |
*/ |
public RatesUpdater(CurrenciesActivity activityContext, |
UpdateService updateService) |
public RatesUpdater(Context activityContext) |
{ |
this.activityContext = activityContext; |
this.service = updateService; |
} |
|
/** |
83,28 → 72,20 |
@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) |
{ |
TextView textUpdating = |
(TextView) this.activityContext |
.findViewById(R.id.currencies_text_updating); |
textUpdating.setVisibility(View.VISIBLE); |
// CurrenciesActivity.this.editValue1.setText("42"); |
|
DocumentBuilderFactory documentBuilderFactory = |
DocumentBuilderFactory.newInstance(); |
documentBuilderFactory.setNamespaceAware(true); |
DocumentBuilder builder; |
try |
{ |
builder = documentBuilderFactory.newDocumentBuilder(); |
Document doc; |
DocumentBuilder builder = |
documentBuilderFactory.newDocumentBuilder(); |
try |
{ |
doc = builder.parse(RatesUpdater.URL_ECB); |
Document doc = builder.parse(RatesUpdater.URL_ECB); |
XPathFactory xpathFactory = XPathFactory.newInstance(); |
XPath xpath = xpathFactory.newXPath(); |
// NamespaceContextHelper namespaceContext = |
119,95 → 100,80 |
* 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']"); |
NodeList nodes = (NodeList) |
.compile("./*[local-name() = 'Cube']/*[local-name() = 'Cube']/*[local-name() = 'Cube']"); //$NON-NLS-1$ |
Object result = |
expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
Element parentCube = (Element) nodes.item(0); |
if (parentCube == null) |
{ |
return; |
} |
NodeList nodes = (NodeList) result; |
|
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); |
} |
int len = nodes.getLength(); |
|
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; |
String ns = Context.NOTIFICATION_SERVICE; |
NotificationManager mNotificationManager = |
(NotificationManager) this.activityContext |
.getSystemService(ns); |
|
len = childCubes.getLength(); |
int icon = R.drawable.icon; |
CharSequence tickerText = "Found " + len + " nodes!"; |
long when = System.currentTimeMillis(); |
|
HashMap<String, ConversionData> conversionRates = |
this.activityContext.getConversionRates(); |
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) |
{ |
Element item = (Element) childCubes.item(i); |
String currency = item.getAttribute("currency"); |
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$ |
|
try |
{ |
Double rate = |
Double.parseDouble(item.getAttribute("rate")); //$NON-NLS-1$ |
conversionRates |
.put(currency, new ConversionData(rate, updated)); |
} |
catch (NumberFormatException e) |
{ |
|
} |
/* TODO: Update UI */ |
System.out.println(currency + ": " + rate); //$NON-NLS-1$ |
} |
|
this.activityContext.getDatabase().writeConversionsToDatabase(null); |
this.activityContext.fillTableRates(); |
} |
catch (XPathExpressionException e) |
{ |
Log.e(this.getClass().toString(), "Error in XPath expression", e); |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
} |
catch (SAXException e) |
{ |
Log.e(this.getClass().toString(), |
"Exception while parsing external XML resource", e); |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
catch (IOException e) |
{ |
Log.e(this.getClass().toString(), |
"I/O exception while parsing external XML resource", e); |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
} |
catch (ParserConfigurationException e) |
{ |
Log.e(this.getClass().toString(), |
"Document builder cannot be created", e); |
// TODO Auto-generated catch block |
e.printStackTrace(); |
} |
|
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); |
} |
} |
} |