Work done so far.

This commit is contained in:
Lars Jansøn Engvik committed 2011-09-14 16:37:07 +02:00
1 parent fe9a6aed20
commit 5fdcbfac04
38 files changed
+539

No files matched your search

@@ -0,0 +1,29 @@
package com.studentersamfundet.app;
public class DataHandler {
Event events;
void insert(String id, String title, String description, String date, String location, String category) {
int intId = Integer.parseInt(id);
if (get(intId) != null)
return;
Event e = new Event(intId, title, description, date, location, category);
if (events == null)
events = e;
else {
Event tmp = events;
while (tmp.next != null)
tmp = tmp.next;
tmp.next = e;
}
}
Event get(int id) {
Event e = events;
while (e != null && e.id != id)
e = e.next;
return e;
}
}
@@ -0,0 +1,103 @@
package com.studentersamfundet.app;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class DnsActivity extends Activity {
public static final String PREFS_NAME = "DnsPrefs";
public static final String feedURL = "http://folk.uio.no/larsjeng/test.xml";
public static final String localURL = "/data/com.studentersamfundet.app/files/dns_events";
public static FeedFetcher feed = new FeedFetcher();
public static XmlParser parser = new XmlParser();
public DataHandler dh;
Context context = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View tmp = this.findViewById(R.id.join_us_textview);
tmp.setVisibility(View.INVISIBLE);
context = getApplicationContext();
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
boolean firstStart = prefs.getBoolean("firstStart", false);
// Is it the first time the app is started?
if (!firstStart) {
// Do we have intarwebs?
// YAY = Fetch the feed.
// NAY = Inform about no connection.
if (checkConnection()) {
NodeList itemNodes = feed.fetch(feedURL, this, true);
dh = parser.parse(itemNodes);
} else {
Toast toast = Toast.makeText(context, R.string.error_noconnection_firstart, Toast.LENGTH_LONG);
toast.show();
}
} else {
if (checkConnection()) {
NodeList itemNodes = feed.fetch(feedURL, this, true);
dh = parser.parse(itemNodes);
} else {
NodeList itemNodes = feed.fetch(localURL, this, false);
dh = parser.parse(itemNodes);
Toast toast = Toast.makeText(context, R.string.error_noconnection_noupdate, Toast.LENGTH_LONG);
toast.show();
}
}
}
public void clickHandler(View v) {
if (v.getId() == R.id.program_button) {
startActivity(new Intent(context, ProgramList.class));
} else if (v.getId() == R.id.bli_med_button) {
View j = this.findViewById(R.id.bli_med_button);
ImageView p = (ImageView)this.findViewById(R.id.paragraph);
View t = this.findViewById(R.id.join_us_textview);
j.setClickable(false);
p.setVisibility(View.INVISIBLE);
t.setVisibility(View.VISIBLE);
// FIX ON BACK PRESSED!
}
}
@Override
public void onBackPressed () {
View t = this.findViewById(R.id.join_us_textview);
if (t.getVisibility() == View.VISIBLE) {
View j = this.findViewById(R.id.bli_med_button);
ImageView p = (ImageView)this.findViewById(R.id.paragraph);
j.setClickable(true);
t.setVisibility(View.INVISIBLE);
p.setVisibility(View.VISIBLE);
} else {
finish();
}
}
/* Check for Internet connection. */
boolean checkConnection() {
ConnectivityManager conMan
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
return netInfo != null;
}
}
+20
View File
@@ -0,0 +1,20 @@
package com.studentersamfundet.app;
public class Event {
int id;
String title;
String description;
String date;
String location;
String category;
Event next;
Event(int id, String title, String description, String date, String location, String category) {
this.id = id;
this.title = title;
this.description = description;
this.date = date;
this.location = location;
this.category = category;
}
}
@@ -0,0 +1,109 @@
package com.studentersamfundet.app;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.content.Context;
public class FeedFetcher {
String curLastBuild = "";
private InputStream OpenHttpConnection(String URL) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(URL);
URLConnection con = url.openConnection();
if (!(con instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection!");
try {
HttpURLConnection httpCon = (HttpURLConnection) con;
httpCon.setAllowUserInteraction(false);
httpCon.setInstanceFollowRedirects(true);
httpCon.setRequestMethod("GET");
httpCon.connect();
response = httpCon.getResponseCode();
if (response == HttpURLConnection.HTTP_OK)
in = httpCon.getInputStream();
} catch (Exception e) {
throw new IOException("Error connecting.");
}
return in;
}
public NodeList fetch(String URL, Context c, boolean con) {
InputStream in = null;
NodeList itemNodes = null;
try {
if (con) {
in = OpenHttpConnection(URL);
saveFile(in, c);
in = OpenHttpConnection(URL);
} else
in = new FileInputStream(URL);
Document doc = null;
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(in);
} catch (ParserConfigurationException e) {
} catch (SAXException e) {}
doc.getDocumentElement().normalize();
// Retrieve all the <item> nodes.
itemNodes = doc.getElementsByTagName("item");
in.close();
} catch (IOException e) {}
return itemNodes;
}
void saveFile(InputStream in, Context c) {
InputStreamReader isr = new InputStreamReader(in);
int charRead;
int BUFFER_SIZE = 2000;
String FILENAME = "dns_events";
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0) {
String readString = String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {}
try {
FileOutputStream fos = c.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(str.getBytes());
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e){}
}
}
@@ -0,0 +1,51 @@
package com.studentersamfundet.app;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ProgramList extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.event_list);
// View v = this.findViewById(R.id.program_button);
// v.setClickable(false);
View header = getLayoutInflater().inflate(R.layout.header, null);
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);
ListAdapter adapter = createAdapter();
setListAdapter(adapter);
}
protected ListAdapter createAdapter() {
String[] vals = populateList();
ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, vals);
return adapter;
}
public String[] populateList() {
String[] val = new String[10];
val[0] = "test!";
val[1] = "test2";
val[2] = "test3";
val[3] = "test3";
val[4] = "test3";
val[5] = "test3";
val[6] = "test3";
val[7] = "test3";
val[8] = "test3";
val[9] = "test3";
return val;
}
}
@@ -0,0 +1,66 @@
package com.studentersamfundet.app;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlParser {
public static DataHandler dh;
public XmlParser() {
dh = new DataHandler();
}
DataHandler parse(NodeList itemNodes) {
String id = "";
String title = "";
String description = "";
String date = "";
String location = "";
String category = "";
// String pictureUrl = ""; Necessary?
for (int i = 0; i < itemNodes.getLength(); i++) {
Node itemNode = itemNodes.item(i);
if (itemNode.getNodeType() == Node.ELEMENT_NODE) {
// Convert the Node into an Element.
Element itemElement = (Element) itemNode;
// Get all elements under the <item> element.
NodeList idNodes = (itemElement).getElementsByTagName("id");
NodeList titleNodes = (itemElement).getElementsByTagName("title");
NodeList descriptionNodes = (itemElement).getElementsByTagName("description");
NodeList dateNodes = (itemElement).getElementsByTagName("date");
NodeList locationNodes = (itemElement).getElementsByTagName("location");
NodeList categoryNodes = (itemElement).getElementsByTagName("category");
// Convert a Node into an Element.
Element idElement = (Element) idNodes.item(0);
Element titleElement = (Element) titleNodes.item(0);
Element descriptionElement = (Element) descriptionNodes.item(0);
Element dateElement = (Element) dateNodes.item(0);
Element locationElement = (Element) locationNodes.item(0);
Element categoryElement = (Element) categoryNodes.item(0);
// Get all the child nodes.
NodeList idTextNodes = ((Node) idElement).getChildNodes();
NodeList titleTextNodes = ((Node) titleElement).getChildNodes();
NodeList descriptionTextNodes = ((Node) descriptionElement).getChildNodes();
NodeList dateTextNodes = ((Node) dateElement).getChildNodes();
NodeList locationTextNodes = ((Node) locationElement).getChildNodes();
NodeList categoryTextNodes = ((Node) categoryElement).getChildNodes();
// Retrieve the text.
id = ((Node) idTextNodes.item(0)).getNodeValue();
title = ((Node) titleTextNodes.item(0)).getNodeValue();
description = ((Node) descriptionTextNodes.item(0)).getNodeValue();
date = ((Node) dateTextNodes.item(0)).getNodeValue();
location = ((Node) locationTextNodes.item(0)).getNodeValue();
category = ((Node) categoryTextNodes.item(0)).getNodeValue();
dh.insert(id, title, description, date, location, category);
}
}
return dh;
}
}