Rev 17 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
16 | PointedEar | 1 | /** |
2 | * |
||
3 | */ |
||
4 | package de.pointedears.converter.net; |
||
5 | |||
6 | import java.io.IOException; |
||
7 | |||
8 | import javax.xml.parsers.DocumentBuilder; |
||
9 | import javax.xml.parsers.DocumentBuilderFactory; |
||
10 | import javax.xml.parsers.ParserConfigurationException; |
||
11 | import javax.xml.xpath.XPath; |
||
12 | import javax.xml.xpath.XPathConstants; |
||
13 | import javax.xml.xpath.XPathExpression; |
||
14 | import javax.xml.xpath.XPathExpressionException; |
||
15 | import javax.xml.xpath.XPathFactory; |
||
16 | |||
17 | import org.w3c.dom.Document; |
||
18 | import org.w3c.dom.NamedNodeMap; |
||
19 | import org.w3c.dom.Node; |
||
20 | import org.w3c.dom.NodeList; |
||
21 | import org.xml.sax.SAXException; |
||
22 | |||
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; |
||
28 | import de.pointedears.converter.R; |
||
29 | import de.pointedears.converter.helpers.ConverterThread; |
||
30 | |||
31 | /** |
||
32 | * @author pelinux |
||
33 | * |
||
34 | */ |
||
35 | public class RatesUpdater implements Runnable |
||
36 | { |
||
37 | private static final String URL_ECB = |
||
38 | "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"; //$NON-NLS-1$ |
||
39 | |||
40 | private final Context activityContext; |
||
41 | private ConverterThread updateThread = null; |
||
42 | |||
43 | /** |
||
44 | * |
||
45 | */ |
||
46 | public RatesUpdater(Context activityContext) |
||
47 | { |
||
48 | this.activityContext = activityContext; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return the updateThread |
||
53 | */ |
||
54 | public ConverterThread getUpdateThread() |
||
55 | { |
||
56 | return this.updateThread; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param updateThread |
||
61 | */ |
||
62 | public void setUpdateThread(ConverterThread updateThread) |
||
63 | { |
||
64 | this.updateThread = updateThread; |
||
65 | } |
||
66 | |||
67 | /* |
||
68 | * (non-Javadoc) |
||
69 | * |
||
70 | * @see java.lang.Runnable#run() |
||
71 | */ |
||
72 | @Override |
||
73 | public void run() |
||
74 | { |
||
75 | if (this.getUpdateThread() != null) |
||
76 | { |
||
77 | // CurrenciesActivity.this.editValue1.setText("42"); |
||
78 | |||
79 | DocumentBuilderFactory documentBuilderFactory = |
||
80 | DocumentBuilderFactory.newInstance(); |
||
81 | documentBuilderFactory.setNamespaceAware(true); |
||
82 | try |
||
83 | { |
||
84 | DocumentBuilder builder = |
||
85 | documentBuilderFactory.newDocumentBuilder(); |
||
86 | try |
||
87 | { |
||
88 | Document doc = builder.parse(RatesUpdater.URL_ECB); |
||
89 | XPathFactory xpathFactory = XPathFactory.newInstance(); |
||
90 | XPath xpath = xpathFactory.newXPath(); |
||
91 | // NamespaceContextHelper namespaceContext = |
||
92 | // new NamespaceContextHelper(); |
||
93 | // namespaceContext.add("gesmes", |
||
94 | // "http://www.gesmes.org/xml/2002-08-01"); |
||
95 | // xpath.setNamespaceContext(namespaceContext); |
||
96 | |||
97 | try |
||
98 | { |
||
99 | /* |
||
100 | * FIXME: Why doesn't a simple "./Cube/Cube/Cube" work even with a |
||
101 | * namespace resolver? |
||
102 | */ |
||
103 | XPathExpression expr = |
||
104 | xpath |
||
105 | .compile("./*[local-name() = 'Cube']/*[local-name() = 'Cube']/*[local-name() = 'Cube']"); //$NON-NLS-1$ |
||
106 | Object result = |
||
107 | expr.evaluate(doc.getDocumentElement(), XPathConstants.NODESET); |
||
108 | NodeList nodes = (NodeList) result; |
||
109 | |||
110 | int len = nodes.getLength(); |
||
111 | |||
112 | String ns = Context.NOTIFICATION_SERVICE; |
||
113 | NotificationManager mNotificationManager = |
||
114 | (NotificationManager) this.activityContext |
||
115 | .getSystemService(ns); |
||
116 | |||
117 | int icon = R.drawable.icon; |
||
118 | CharSequence tickerText = "Found " + len + " nodes!"; |
||
119 | long when = System.currentTimeMillis(); |
||
120 | |||
121 | Notification notification = |
||
122 | new Notification(icon, tickerText, when); |
||
123 | |||
124 | Context applicationContext = |
||
125 | this.activityContext.getApplicationContext(); |
||
126 | CharSequence contentTitle = "Converter"; |
||
127 | CharSequence contentText = "Found " + len + " nodes!"; |
||
128 | Intent notificationIntent = |
||
129 | new Intent(this.activityContext, this.activityContext.getClass()); |
||
130 | PendingIntent contentIntent = |
||
131 | PendingIntent.getActivity(this.activityContext, 0, |
||
132 | notificationIntent, 0); |
||
133 | |||
134 | notification.setLatestEventInfo(applicationContext, contentTitle, |
||
135 | contentText, |
||
136 | contentIntent); |
||
137 | |||
138 | // private static final int HELLO_ID = 1; |
||
139 | |||
140 | mNotificationManager.notify(1, notification); |
||
141 | |||
142 | for (int i = 0; i < len; ++i) |
||
143 | { |
||
144 | Node item = nodes.item(i); |
||
145 | NamedNodeMap attributes = item.getAttributes(); |
||
146 | String currency = |
||
147 | attributes |
||
148 | .getNamedItem("currency").getNodeValue(); //$NON-NLS-1$ |
||
149 | String rate = attributes.getNamedItem("rate").getNodeValue(); //$NON-NLS-1$ |
||
150 | |||
151 | /* TODO: Update UI */ |
||
152 | System.out.println(currency + ": " + rate); //$NON-NLS-1$ |
||
153 | } |
||
154 | } |
||
155 | catch (XPathExpressionException e) |
||
156 | { |
||
157 | // TODO Auto-generated catch block |
||
158 | e.printStackTrace(); |
||
159 | } |
||
160 | } |
||
161 | catch (SAXException e) |
||
162 | { |
||
163 | // TODO Auto-generated catch block |
||
164 | e.printStackTrace(); |
||
165 | } |
||
166 | catch (IOException e) |
||
167 | { |
||
168 | // TODO Auto-generated catch block |
||
169 | e.printStackTrace(); |
||
170 | } |
||
171 | } |
||
172 | catch (ParserConfigurationException e) |
||
173 | { |
||
174 | // TODO Auto-generated catch block |
||
175 | e.printStackTrace(); |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | } |