Rev 16 | Rev 18 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 16 | Rev 17 | ||
---|---|---|---|
1 | /**
|
1 | /**
|
2 | *
|
2 | *
|
3 | */
|
3 | */
|
4 | package de.pointedears.converter.net; |
4 | package de.pointedears.converter.net; |
5 | 5 | ||
6 | import java.io.IOException; |
6 | import java.io.IOException; |
- | 7 | import java.text.DateFormat; |
|
- | 8 | import java.text.ParseException; |
|
- | 9 | import java.text.SimpleDateFormat; |
|
- | 10 | import java.util.Date; |
|
- | 11 | import java.util.HashMap; |
|
7 | 12 | ||
8 | import javax.xml.parsers.DocumentBuilder; |
13 | import javax.xml.parsers.DocumentBuilder; |
9 | import javax.xml.parsers.DocumentBuilderFactory; |
14 | import javax.xml.parsers.DocumentBuilderFactory; |
10 | import javax.xml.parsers.ParserConfigurationException; |
15 | import javax.xml.parsers.ParserConfigurationException; |
11 | import javax.xml.xpath.XPath; |
16 | import javax.xml.xpath.XPath; |
12 | import javax.xml.xpath.XPathConstants; |
17 | import javax.xml.xpath.XPathConstants; |
13 | import javax.xml.xpath.XPathExpression; |
18 | import javax.xml.xpath.XPathExpression; |
14 | import javax.xml.xpath.XPathExpressionException; |
19 | import javax.xml.xpath.XPathExpressionException; |
15 | import javax.xml.xpath.XPathFactory; |
20 | import javax.xml.xpath.XPathFactory; |
16 | 21 | ||
17 | import org.w3c.dom.Document; |
22 | import org.w3c.dom.Document; |
18 | import org.w3c.dom.NamedNodeMap; |
- | |
19 | import org.w3c.dom.Node; |
23 | import org.w3c.dom.Element; |
20 | import org.w3c.dom.NodeList; |
24 | import org.w3c.dom.NodeList; |
21 | import org.xml.sax.SAXException; |
25 | import org.xml.sax.SAXException; |
22 | 26 | ||
23 | import android.app.Notification; |
- | |
24 | import android.app.NotificationManager; |
- | |
25 | import android.app.PendingIntent; |
- | |
26 | import android.content.Context; |
- | |
27 | import android.content.Intent; |
27 | import android.content.Intent; |
- | 28 | import android.util.Log; |
|
- | 29 | import android.view.View; |
|
- | 30 | import android.widget.TextView; |
|
28 | import de.pointedears.converter.R; |
31 | import de.pointedears.converter.R; |
- | 32 | import de.pointedears.converter.app.CurrenciesActivity; |
|
- | 33 | import de.pointedears.converter.db.ConversionData; |
|
29 | import de.pointedears.converter.helpers.ConverterThread; |
34 | import de.pointedears.converter.helpers.ConverterThread; |
- | 35 | import de.pointedears.converter.helpers.UpdateService; |
|
30 | 36 | ||
31 | /**
|
37 | /**
|
32 | * @author pelinux
|
38 | * @author pelinux
|
33 | *
|
39 | *
|
34 | */
|
40 | */
|
35 | public class RatesUpdater implements Runnable |
41 | public class RatesUpdater implements Runnable |
36 | {
|
42 | {
|
37 | private static final String URL_ECB = |
43 | private static final String URL_ECB = |
38 | "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
44 | "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
39 | 45 | ||
40 | private final Context activityContext; |
46 | private final CurrenciesActivity activityContext; |
41 | private ConverterThread updateThread = null; |
47 | private ConverterThread updateThread = null; |
42 | 48 | ||
- | 49 | private final UpdateService service; |
|
- | 50 | ||
43 | /**
|
51 | /**
|
- | 52 | * @param updateService
|
|
44 | *
|
53 | *
|
45 | */
|
54 | */
|
46 | public RatesUpdater(Context activityContext) |
55 | public RatesUpdater(CurrenciesActivity activityContext, |
- | 56 | UpdateService updateService)
|
|
47 | {
|
57 | {
|
48 | this.activityContext = activityContext; |
58 | this.activityContext = activityContext; |
- | 59 | this.service = updateService; |
|
49 | }
|
60 | }
|
50 | 61 | ||
51 | /**
|
62 | /**
|
52 | * @return the updateThread
|
63 | * @return the updateThread
|
53 | */
|
64 | */
|
54 | public ConverterThread getUpdateThread() |
65 | public ConverterThread getUpdateThread() |
55 | {
|
66 | {
|
56 | return this.updateThread; |
67 | return this.updateThread; |
57 | }
|
68 | }
|
58 | 69 | ||
59 | /**
|
70 | /**
|
60 | * @param updateThread
|
71 | * @param updateThread
|
61 | */
|
72 | */
|
62 | public void setUpdateThread(ConverterThread updateThread) |
73 | public void setUpdateThread(ConverterThread updateThread) |
63 | {
|
74 | {
|
64 | this.updateThread = updateThread; |
75 | this.updateThread = updateThread; |
65 | }
|
76 | }
|
66 | 77 | ||
67 | /*
|
78 | /*
|
68 | * (non-Javadoc)
|
79 | * (non-Javadoc)
|
69 | *
|
80 | *
|
70 | * @see java.lang.Runnable#run()
|
81 | * @see java.lang.Runnable#run()
|
71 | */
|
82 | */
|
72 | @Override |
83 | @Override |
73 | public void run() |
84 | public void run() |
74 | {
|
85 | {
|
- | 86 | int len = 0; |
|
- | 87 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); //$NON-NLS-1$ |
|
- | 88 | Date updated = new Date(); |
|
- | 89 | ||
75 | if (this.getUpdateThread() != null) |
90 | if (this.getUpdateThread() != null) |
76 | {
|
91 | {
|
- | 92 | TextView textUpdating = |
|
- | 93 | (TextView) this.activityContext |
|
77 | // CurrenciesActivity.this.editValue1.setText("42");
|
94 | .findViewById(R.id.currencies_text_updating); |
- | 95 | textUpdating.setVisibility(View.VISIBLE); |
|
78 | 96 | ||
79 | DocumentBuilderFactory documentBuilderFactory = |
97 | DocumentBuilderFactory documentBuilderFactory = |
80 | DocumentBuilderFactory.newInstance(); |
98 | DocumentBuilderFactory.newInstance(); |
81 | documentBuilderFactory.setNamespaceAware(true); |
99 | documentBuilderFactory.setNamespaceAware(true); |
- | 100 | DocumentBuilder builder; |
|
82 | try
|
101 | try
|
83 | {
|
102 | {
|
84 | DocumentBuilder builder = |
103 | builder = documentBuilderFactory.newDocumentBuilder(); |
85 | documentBuilderFactory.newDocumentBuilder(); |
104 | Document doc; |
86 | try
|
105 | try
|
87 | {
|
106 | {
|
88 | Document doc = builder.parse(RatesUpdater.URL_ECB); |
107 | doc = builder.parse(RatesUpdater.URL_ECB); |
89 | XPathFactory xpathFactory = XPathFactory.newInstance(); |
108 | XPathFactory xpathFactory = XPathFactory.newInstance(); |
90 | XPath xpath = xpathFactory.newXPath(); |
109 | XPath xpath = xpathFactory.newXPath(); |
91 | // NamespaceContextHelper namespaceContext =
|
110 | // NamespaceContextHelper namespaceContext =
|
92 | // new NamespaceContextHelper();
|
111 | // new NamespaceContextHelper();
|
93 | // namespaceContext.add("gesmes",
|
112 | // namespaceContext.add("gesmes",
|
94 | // "http://www.gesmes.org/xml/2002-08-01");
|
113 | // "http://www.gesmes.org/xml/2002-08-01");
|
95 | // xpath.setNamespaceContext(namespaceContext);
|
114 | // xpath.setNamespaceContext(namespaceContext);
|
96 | 115 | ||
97 | try
|
116 | try
|
98 | {
|
117 | {
|
99 | /*
|
118 | /*
|
100 | * FIXME: Why doesn't a simple "./Cube/Cube/Cube" work even with a
|
119 | * FIXME: Why doesn't a simple "./Cube/Cube/Cube" work even with a
|
101 | * namespace resolver?
|
120 | * namespace resolver?
|
102 | */
|
121 | */
|
- | 122 | @SuppressWarnings("nls") |
|
103 | XPathExpression expr = |
123 | XPathExpression expr = |
104 | xpath |
124 | xpath |
105 | .compile("./*[local-name() = 'Cube']/*[local-name() = 'Cube']/*[local-name() = 'Cube']"); //$NON-NLS-1$ |
125 | .compile("./*[local-name() = 'Cube']/*[local-name() = 'Cube']"); |
106 | Object result = |
126 | NodeList nodes = (NodeList) |
107 | expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
127 | expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
108 | NodeList nodes = (NodeList) result; |
128 | Element parentCube = (Element) nodes.item(0); |
109 | - | ||
110 | int len = nodes.getLength(); |
129 | if (parentCube == null) |
- | 130 | {
|
|
- | 131 | return; |
|
- | 132 | }
|
|
111 | 133 | ||
112 | String ns = Context.NOTIFICATION_SERVICE; |
- | |
113 | NotificationManager mNotificationManager = |
- | |
114 | (NotificationManager) this.activityContext |
- | |
115 | .getSystemService(ns); |
134 | try
|
116 | - | ||
117 | int icon = R.drawable.icon; |
135 | {
|
118 | CharSequence tickerText = "Found " + len + " nodes!"; |
- | |
119 | long when = System.currentTimeMillis(); |
136 | updated = df.parse(parentCube.getAttribute("time")); |
120 | - | ||
121 | Notification notification = |
137 | }
|
122 | new Notification(icon, tickerText, when); |
- | |
123 | - | ||
124 | Context applicationContext = |
138 | catch (ParseException e) |
125 | this.activityContext.getApplicationContext(); |
- | |
126 | CharSequence contentTitle = "Converter"; |
- | |
127 | CharSequence contentText = "Found " + len + " nodes!"; |
- | |
128 | Intent notificationIntent = |
139 | {
|
129 | new Intent(this.activityContext, this.activityContext.getClass()); |
- | |
130 | PendingIntent contentIntent = |
- | |
131 | PendingIntent.getActivity(this.activityContext, 0, |
- | |
132 | notificationIntent, 0); |
140 | Log.e(this.getClass().toString(), |
133 | - | ||
134 | notification.setLatestEventInfo(applicationContext, contentTitle, |
141 | "Could not parse the `time' attribute into a Date", e); |
135 | contentText, |
142 | }
|
136 | contentIntent); |
- | |
137 | 143 | ||
- | 144 | expr = |
|
- | 145 | xpath |
|
- | 146 | .compile("./*[local-name()='Cube' and (@currency='CHF' or @currency='USD')]"); //$NON-NLS-1$ |
|
- | 147 | nodes = |
|
- | 148 | (NodeList) expr.evaluate(parentCube, XPathConstants.NODESET); |
|
138 | // private static final int HELLO_ID = 1;
|
149 | NodeList childCubes = nodes; |
139 | 150 | ||
140 | mNotificationManager.notify(1, notification); |
151 | len = childCubes.getLength(); |
141 | 152 | ||
- | 153 | HashMap<String, ConversionData> conversionRates = |
|
- | 154 | this.activityContext.getConversionRates(); |
|
142 | for (int i = 0; i < len; ++i) |
155 | for (int i = 0; i < len; ++i) |
143 | {
|
156 | {
|
144 | Node item = nodes.item(i); |
157 | Element item = (Element) childCubes.item(i); |
145 | NamedNodeMap attributes = item.getAttributes(); |
158 | String currency = item.getAttribute("currency"); |
- | 159 | ||
146 | String currency = |
160 | try
|
- | 161 | {
|
|
147 | attributes |
162 | Double rate = |
148 | .getNamedItem("currency").getNodeValue(); //$NON-NLS-1$ |
163 | Double.parseDouble(item.getAttribute("rate")); //$NON-NLS-1$ |
- | 164 | conversionRates |
|
149 | String rate = attributes.getNamedItem("rate").getNodeValue(); //$NON-NLS-1$ |
165 | .put(currency, new ConversionData(rate, updated)); |
- | 166 | }
|
|
- | 167 | catch (NumberFormatException e) |
|
- | 168 | {
|
|
150 | 169 | ||
151 | /* TODO: Update UI */
|
- | |
152 | System.out.println(currency + ": " + rate); //$NON-NLS-1$ |
- | |
153 | }
|
170 | }
|
154 | }
|
171 | }
|
- | 172 | ||
- | 173 | this.activityContext.getDatabase().writeConversionsToDatabase(null); |
|
- | 174 | this.activityContext.fillTableRates(); |
|
- | 175 | }
|
|
155 | catch (XPathExpressionException e) |
176 | catch (XPathExpressionException e) |
156 | {
|
177 | {
|
157 | // TODO Auto-generated catch block
|
178 | Log.e(this.getClass().toString(), "Error in XPath expression", e); |
158 | e.printStackTrace(); |
- | |
159 | }
|
179 | }
|
160 | }
|
180 | }
|
161 | catch (SAXException e) |
181 | catch (SAXException e) |
162 | {
|
182 | {
|
163 | // TODO Auto-generated catch block
|
183 | Log.e(this.getClass().toString(), |
164 | e.printStackTrace(); |
184 | "Exception while parsing external XML resource", e); |
165 | }
|
185 | }
|
166 | catch (IOException e) |
186 | catch (IOException e) |
167 | {
|
187 | {
|
168 | // TODO Auto-generated catch block
|
188 | Log.e(this.getClass().toString(), |
169 | e.printStackTrace(); |
189 | "I/O exception while parsing external XML resource", e); |
170 | }
|
190 | }
|
171 | }
|
191 | }
|
172 | catch (ParserConfigurationException e) |
192 | catch (ParserConfigurationException e) |
173 | {
|
193 | {
|
174 | // TODO Auto-generated catch block
|
194 | Log.e(this.getClass().toString(), |
175 | e.printStackTrace(); |
195 | "Document builder cannot be created", e); |
176 | }
|
196 | }
|
- | 197 | ||
- | 198 | if (len > 0) |
|
- | 199 | {
|
|
- | 200 | /*
|
|
- | 201 | * Notify the activity that we are done (causes a notification to be
|
|
- | 202 | * shown)
|
|
- | 203 | */
|
|
- | 204 | Intent intent = new Intent(UpdateService.ACTION_UPDATE); |
|
- | 205 | intent.putExtra(UpdateService.EXTRA_NUM_RATES, len); |
|
- | 206 | intent.putExtra(UpdateService.EXTRA_DATE, updated); |
|
- | 207 | this.service.sendBroadcast(intent); |
|
- | 208 | }
|
|
- | 209 | ||
- | 210 | textUpdating.setVisibility(View.GONE); |
|
177 | }
|
211 | }
|
178 | }
|
212 | }
|
179 | }
|
213 | }
|
180 | 214 |