~robinj/android-omgubuntu/trunk

« back to all changes in this revision

Viewing changes to src/com/robinj/omgubuntu/MainActivity.java

  • Committer: Robin Jacobs
  • Date: 2012-09-07 18:03:30 UTC
  • Revision ID: broederjacobs@gmail.com-20120907180330-b6vk75ks5t0odu4f
Initial Launchpad release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.robinj.omgubuntu;
 
2
 
 
3
import java.net.URL;
 
4
import java.util.ArrayList;
 
5
import java.util.List;
 
6
 
 
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;
 
22
 
 
23
import javax.xml.parsers.DocumentBuilder;
 
24
import javax.xml.parsers.DocumentBuilderFactory;
 
25
import javax.xml.parsers.SAXParser;
 
26
import javax.xml.parsers.SAXParserFactory;
 
27
 
 
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;
 
34
 
 
35
public class MainActivity extends Activity
 
36
{
 
37
        String newLine = System.getProperty ("line.separator");
 
38
        List<String[]> lastData;
 
39
        
 
40
        @Override
 
41
        public void onCreate(Bundle savedInstanceState)
 
42
        {
 
43
                super.onCreate (savedInstanceState);
 
44
                setContentView (R.layout.activity_main);
 
45
        }
 
46
        
 
47
        @Override
 
48
        public boolean onCreateOptionsMenu(Menu menu)
 
49
        {
 
50
                getMenuInflater ().inflate (R.menu.activity_main, menu);
 
51
                return true;
 
52
        }
 
53
        
 
54
        /*public String GetFromUrl (String address)
 
55
        {
 
56
                String output = null;
 
57
                
 
58
                URL url = new URL (address);
 
59
                InputStream stream = url.openStream ();
 
60
                DataInputStream dstream = new DataInputStream (new BufferedInputStream (stream));
 
61
                String line = null;
 
62
                
 
63
                while ((line = dstream.readLine ()) != null)
 
64
                {
 
65
                        output += line;
 
66
                }
 
67
                
 
68
                return output;
 
69
        }*/
 
70
        
 
71
        public void showDialog(String message)
 
72
        {
 
73
                AlertDialog dlg = new AlertDialog.Builder (this).create ();
 
74
                dlg.setMessage (message);
 
75
                dlg.setButton ("OK", new DialogInterface.OnClickListener ()
 
76
                {
 
77
                        public void onClick(DialogInterface dialog, int which)
 
78
                        {
 
79
                                dialog.dismiss ();
 
80
                        }
 
81
                });
 
82
                dlg.show ();
 
83
        }
 
84
        
 
85
        public void logo_clicked(View view)
 
86
        {
 
87
                Intent browserIntent = new Intent (Intent.ACTION_VIEW, Uri.parse ("http://www.omgubuntu.co.uk/"));
 
88
                startActivity (browserIntent);
 
89
        }
 
90
        
 
91
        public void btnRefresh_clicked(View view)
 
92
        {
 
93
                try
 
94
                {
 
95
                        WebView web = (WebView) findViewById (R.id.web);
 
96
                        List<String[]> data = getArticles ();
 
97
                        
 
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; }";
 
104
                        
 
105
                        String html = "<html>" +
 
106
                                        "<head>" +
 
107
                                        "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" +
 
108
                                        "<style type=\"text/css\">" + css + "</style>" +
 
109
                                        "</head>" +
 
110
                                        "<body>";
 
111
                        
 
112
                        for (int i = 0; i < data.size (); i++)
 
113
                        {
 
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 />";
 
118
                        }
 
119
                        
 
120
                        html += "</body>" +
 
121
                                        "</html>";
 
122
                        
 
123
                        web.clearView ();
 
124
                        web.loadData (html, "text/html", null);
 
125
                        
 
126
                        if (data.get (0)[0].equals (lastData.get (0)[0])) //FIXME//
 
127
                        {
 
128
                                String notificationService = Context.NOTIFICATION_SERVICE;
 
129
                                NotificationManager notificationManager = (NotificationManager) getSystemService (notificationService);
 
130
                                
 
131
                                int icon = R.drawable.ic_launcher;
 
132
                                CharSequence ticker = "OMG! New articles!";
 
133
                                long time = System.currentTimeMillis ();
 
134
                                
 
135
                                Notification notification = new Notification (icon, ticker, time);
 
136
                                
 
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);
 
142
                                
 
143
                                notification.setLatestEventInfo (context, title, message, contentIntent);
 
144
                                
 
145
                                final int HELLO_ID = 1;
 
146
                                notificationManager.notify (HELLO_ID, notification);
 
147
                        }
 
148
                        else
 
149
                        {
 
150
                                lastData = data;
 
151
                        }
 
152
                } 
 
153
                catch (Exception ex)
 
154
                {
 
155
                        showDialog ("Error!" + newLine + newLine + ex.getMessage ());
 
156
                }
 
157
        }
 
158
        
 
159
        public List<String[]> getArticles ()
 
160
        {
 
161
                List<String[]> data = new ArrayList<String[]>();
 
162
                
 
163
                try
 
164
                {
 
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 ();
 
169
                        
 
170
                        NodeList items = doc.getElementsByTagName ("item");
 
171
                        
 
172
                        for (int i = 0; i < items.getLength (); i++)
 
173
                        {
 
174
                                Node item = items.item (i);
 
175
                                
 
176
                                if (item.getNodeType () == Node.ELEMENT_NODE)
 
177
                                {
 
178
                                        NodeList tags = item.getChildNodes ();
 
179
                                        
 
180
                                        String title = null;
 
181
                                        String link = null;
 
182
                                        String date = null;
 
183
                                        String content = null;
 
184
                                        
 
185
                                        for (int j = 0; j < tags.getLength (); j++)
 
186
                                        {
 
187
                                                Node tag = tags.item (j);
 
188
                                                
 
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 ();
 
197
                                        }
 
198
                                        
 
199
                                        String[] article = {title, link, date, content};
 
200
                                        data.add (article);
 
201
                                }
 
202
                        }
 
203
                }
 
204
                catch (Exception ex)
 
205
                {
 
206
                        showDialog ("Aie caramba!" + newLine + newLine + ex.getMessage ());
 
207
                }
 
208
                
 
209
                return data;
 
210
        }
 
211
}