1
package com.robinj.omgubuntu;
4
import java.util.ArrayList;
7
import android.net.Uri;
8
import android.os.Bundle;
9
import android.app.Activity;
10
import android.app.AlertDialog;
11
import android.app.Notification;
12
import android.app.NotificationManager;
13
import android.app.PendingIntent;
14
import android.content.Context;
15
import android.content.DialogInterface;
16
import android.content.Intent;
17
import android.util.Log;
18
import android.view.Menu;
19
import android.view.View;
20
import android.webkit.WebView;
21
import android.widget.ListView;
23
import javax.xml.parsers.DocumentBuilder;
24
import javax.xml.parsers.DocumentBuilderFactory;
25
import javax.xml.parsers.SAXParser;
26
import javax.xml.parsers.SAXParserFactory;
28
import org.w3c.dom.Document;
29
import org.w3c.dom.NodeList;
30
import org.w3c.dom.Node;
31
import org.w3c.dom.Element;
32
import org.xml.sax.InputSource;
33
import org.xml.sax.XMLReader;
35
public class MainActivity extends Activity
37
String newLine = System.getProperty ("line.separator");
38
List<String[]> lastData;
41
public void onCreate(Bundle savedInstanceState)
43
super.onCreate (savedInstanceState);
44
setContentView (R.layout.activity_main);
48
public boolean onCreateOptionsMenu(Menu menu)
50
getMenuInflater ().inflate (R.menu.activity_main, menu);
54
/*public String GetFromUrl (String address)
58
URL url = new URL (address);
59
InputStream stream = url.openStream ();
60
DataInputStream dstream = new DataInputStream (new BufferedInputStream (stream));
63
while ((line = dstream.readLine ()) != null)
71
public void showDialog(String message)
73
AlertDialog dlg = new AlertDialog.Builder (this).create ();
74
dlg.setMessage (message);
75
dlg.setButton ("OK", new DialogInterface.OnClickListener ()
77
public void onClick(DialogInterface dialog, int which)
85
public void logo_clicked(View view)
87
Intent browserIntent = new Intent (Intent.ACTION_VIEW, Uri.parse ("http://www.omgubuntu.co.uk/"));
88
startActivity (browserIntent);
91
public void btnRefresh_clicked(View view)
95
WebView web = (WebView) findViewById (R.id.web);
96
List<String[]> data = getArticles ();
98
String css = "html, body { background: white; color: black; font-family: \"Ubuntu\", \"Droid Sans\"; margin: 0px; padding: 0px 2px 0px 2px; }" +
99
"div.item { margin: 2px 0px 2px 0px; }" +
100
"a { color: black; text-decoration: none; font-weight: bold; }" +
101
"span.title { font-size: 16px; }" +
102
"span.date { color: #888888; font-size: 10px; font-weight: normal; }" +
103
"hr { background-color: #ABABAB; height: 0px; }";
105
String html = "<html>" +
107
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" +
108
"<style type=\"text/css\">" + css + "</style>" +
112
for (int i = 0; i < data.size (); i++)
114
html += (i < 1 ? "" : "<hr />") +
115
"<div class=\"item\">" +
116
"<a href=\"" + data.get (i)[1] + "\"><span class=\"title\">" + data.get (i)[0] + "</span></a><br />" +
117
"<a href=\"" + data.get (i)[1] + "\"><span class=\"date\">" + data.get (i)[2] + "</span><a /><br />";
124
web.loadData (html, "text/html", null);
126
if (data.get (0)[0].equals (lastData.get (0)[0])) //FIXME//
128
String notificationService = Context.NOTIFICATION_SERVICE;
129
NotificationManager notificationManager = (NotificationManager) getSystemService (notificationService);
131
int icon = R.drawable.ic_launcher;
132
CharSequence ticker = "OMG! New articles!";
133
long time = System.currentTimeMillis ();
135
Notification notification = new Notification (icon, ticker, time);
137
Context context = getApplicationContext ();
138
CharSequence title = "OMG! Ubuntu!";
139
CharSequence message = "OMG! New articles!";
140
Intent notificationIntent = new Intent (this, MainActivity.class);
141
PendingIntent contentIntent = PendingIntent.getActivity (this, 0, notificationIntent, 0);
143
notification.setLatestEventInfo (context, title, message, contentIntent);
145
final int HELLO_ID = 1;
146
notificationManager.notify (HELLO_ID, notification);
155
showDialog ("Error!" + newLine + newLine + ex.getMessage ());
159
public List<String[]> getArticles ()
161
List<String[]> data = new ArrayList<String[]>();
165
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance ();
166
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder ();
167
Document doc = dBuilder.parse ("http://feeds.feedburner.com/d0od?format=xml");
168
doc.getDocumentElement ().normalize ();
170
NodeList items = doc.getElementsByTagName ("item");
172
for (int i = 0; i < items.getLength (); i++)
174
Node item = items.item (i);
176
if (item.getNodeType () == Node.ELEMENT_NODE)
178
NodeList tags = item.getChildNodes ();
183
String content = null;
185
for (int j = 0; j < tags.getLength (); j++)
187
Node tag = tags.item (j);
189
if (tag.getNodeName ().equals ("title"))
190
title = tag.getTextContent ();
191
else if (tag.getNodeName ().equals ("link"))
192
link = tag.getTextContent ();
193
else if (tag.getNodeName ().equals ("pubDate"))
194
date = tag.getTextContent ();
195
else if (tag.getNodeName ().equals ("content"))
196
content = tag.getTextContent ();
199
String[] article = {title, link, date, content};
206
showDialog ("Aie caramba!" + newLine + newLine + ex.getMessage ());