From 542a8e891e6dda0af0883621e418b9314dd6c3ea Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Tue, 17 Jan 2012 21:03:04 +0100 Subject: [PATCH 1/8] MAJOR code refucktoring. --- .../studentersamfundet/app/DataHandler.java | 14 +- src/com/studentersamfundet/app/Event.java | 18 ++- .../studentersamfundet/app/FeedFetcher.java | 120 ++++++++------- .../studentersamfundet/app/IRSSParser.java | 7 + src/com/studentersamfundet/app/News.java | 11 +- .../studentersamfundet/app/RSSParserNews.java | 2 +- .../app/RSSParserProgram.java | 2 +- .../app/ui/BaseDnsActivity.java | 26 ++-- .../app/ui/BaseListActivity.java | 138 ++++++++++++++++++ .../app/ui/DnsActivity.java | 41 +++--- .../app/ui/EventListActivity.java | 112 +++----------- .../app/ui/EventListTicketsActivity.java | 58 +++----- 12 files changed, 320 insertions(+), 229 deletions(-) create mode 100644 src/com/studentersamfundet/app/IRSSParser.java create mode 100644 src/com/studentersamfundet/app/ui/BaseListActivity.java diff --git a/src/com/studentersamfundet/app/DataHandler.java b/src/com/studentersamfundet/app/DataHandler.java index baa57de..3e48c8f 100644 --- a/src/com/studentersamfundet/app/DataHandler.java +++ b/src/com/studentersamfundet/app/DataHandler.java @@ -1,13 +1,17 @@ package com.studentersamfundet.app; +import java.io.Serializable; +import java.util.Collection; +import java.util.LinkedHashSet; import java.util.LinkedList; -import java.util.List; import java.util.Set; import java.util.TreeSet; -public class DataHandler { - private List events = new LinkedList(); - private List news = new LinkedList(); +public class DataHandler implements Serializable { + private static final long serialVersionUID = 6787541405689146002L; + + private Collection events = new LinkedHashSet(); + private Collection news = new LinkedList(); private Set eventCategories = new TreeSet(); public void insertEvent(String id, @@ -22,8 +26,6 @@ public class DataHandler { String priceMem, String ticketUriStr) { int intId = Integer.parseInt(id); - if (getEvent(intId) != null) - return; Event event = new Event(intId, title, description, date, location, text, category, imageUri); diff --git a/src/com/studentersamfundet/app/Event.java b/src/com/studentersamfundet/app/Event.java index 505f0b9..c379843 100644 --- a/src/com/studentersamfundet/app/Event.java +++ b/src/com/studentersamfundet/app/Event.java @@ -8,11 +8,11 @@ import java.util.Date; import android.net.Uri; public class Event implements Serializable { - private static final long serialVersionUID = 1L; - public static final String ALL = "Alle"; - + private static final long serialVersionUID = -5222901012434000257L; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + public static final String ALL = "Alle"; + public final int id; public final String title; private String description; @@ -118,4 +118,16 @@ public class Event implements Serializable { this.memberPrice = memberPrice; this.ticketUriStr = ticketUri; } + + public int hashCode() { + return this.id; + } + + public boolean equals(Object o2) { + if (o2 instanceof Event) { + Event e2 = (Event)o2; + return this.id == e2.id; + } + return false; + } } diff --git a/src/com/studentersamfundet/app/FeedFetcher.java b/src/com/studentersamfundet/app/FeedFetcher.java index e83b84c..3a38127 100644 --- a/src/com/studentersamfundet/app/FeedFetcher.java +++ b/src/com/studentersamfundet/app/FeedFetcher.java @@ -5,6 +5,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.Serializable; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; @@ -19,14 +20,12 @@ import org.xml.sax.SAXException; import android.content.Context; -public class FeedFetcher { - private final String LOCAL_FILENAME = "dns_events"; - - private Context context; +public class FeedFetcher implements Serializable { + private static final long serialVersionUID = 3408406671996232103L; + private static final String LOCAL_FILENAME = "dns_events"; private String url; - public FeedFetcher(Context context, String url) { - this.context = context; + public FeedFetcher(String url) { this.url = url; } @@ -61,54 +60,67 @@ public class FeedFetcher { * @throws IOException Is thrown if the app cannot download the file from * the internet and there is no local copy. */ - public NodeList fetch(boolean forcedUpdate) throws IOException { - InputStream in = null; - NodeList itemNodes = null; - - /* Update the local file if possible. */ - try { - if (forcedUpdate || doesFileNeedUpdate(context)) { - in = openHttpConnection(); - saveFile(in, context); - } - - } catch(IOException e) { - /* OK, so we failed to update it. It doesn't matter yet, because we can try to... */ - } finally { - if (in != null) - in.close(); - } - - /* ...load local file: */ - in = loadFile(context); // if we fail here, we fail ultimately. Throw the Exception! - - Document doc = null; - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder db; - - try { - db = dbf.newDocumentBuilder(); - doc = db.parse(in); - } catch (ParserConfigurationException e) { - /* This should never happen. */ - } catch (SAXException e) { - /* This shouldn't happen unless the xml returned by the server - * is somehow invalid. If it happens, the best course of action - * is to pretend we failed to download the event list (it's - * close enough and it simplifies exception-handling). */ - throw new IOException("Bad response from server."); - } - - doc.getDocumentElement().normalize(); - - // Retrieve all the nodes. - itemNodes = doc.getElementsByTagName("item"); - in.close(); - - return itemNodes; + public NodeList fetch(Context context, boolean forcedUpdate) throws IOException { + NodeList itemNodes = null; + InputStream in = null; + + /* Update the local file if possible. */ + try { + if (forcedUpdate || doesFileNeedUpdate(context)) { + in = openHttpConnection(); + saveFile(in, context); + } + + } catch(IOException e) { + /* OK, so we failed to update it. It doesn't matter yet, because we can try to... */ + } finally { + if (in != null) + in.close(); + } + + /* ...load local file: */ + in = loadFile(context); // if we fail here, we fail ultimately. Throw the Exception! + itemNodes = parseInputStream(in); + in.close(); + + return itemNodes; } - private boolean doesFileNeedUpdate(Context c) throws IOException { + public static NodeList loadLocalData(Context c) throws IOException { + NodeList nodes; + + InputStream is = loadFile(c); + nodes = parseInputStream(is); + is.close(); + + return nodes; + } + + private static NodeList parseInputStream(InputStream iStream) throws IOException { + Document doc = null; + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db; + + try { + db = dbf.newDocumentBuilder(); + doc = db.parse(iStream); + } catch (ParserConfigurationException e) { + /* This should never happen. */ + } catch (SAXException e) { + /* This shouldn't happen unless the xml returned by the server + * is somehow invalid. If it happens, the best course of action + * is to pretend we failed to download the event list (it's + * close enough and it simplifies exception-handling). */ + throw new IOException("Bad response from server."); + } + + doc.getDocumentElement().normalize(); + + // Retrieve all the nodes. + return doc.getElementsByTagName("item"); + } + + private static boolean doesFileNeedUpdate(Context c) throws IOException { /** Maximum amount of time before the file needs updating, in ms: */ final long maxInterval = 1000 * 60 * 60; // one hour @@ -121,7 +133,7 @@ public class FeedFetcher { return now - lastMod > maxInterval; } - private void saveFile(InputStream in, Context c) throws IOException, FileNotFoundException { + private static void saveFile(InputStream in, Context c) throws IOException, FileNotFoundException { final int BUFFER_SIZE = 128; OutputStream os = c.openFileOutput(LOCAL_FILENAME, Context.MODE_PRIVATE); @@ -135,7 +147,7 @@ public class FeedFetcher { os.close(); } - private InputStream loadFile(Context c) throws FileNotFoundException { + private static InputStream loadFile(Context c) throws FileNotFoundException { return c.openFileInput(LOCAL_FILENAME); } } diff --git a/src/com/studentersamfundet/app/IRSSParser.java b/src/com/studentersamfundet/app/IRSSParser.java new file mode 100644 index 0000000..72415d8 --- /dev/null +++ b/src/com/studentersamfundet/app/IRSSParser.java @@ -0,0 +1,7 @@ +package com.studentersamfundet.app; + +import org.w3c.dom.NodeList; + +public interface IRSSParser { + public DataHandler parse(NodeList nodes); +} diff --git a/src/com/studentersamfundet/app/News.java b/src/com/studentersamfundet/app/News.java index 68f16b6..ee32772 100644 --- a/src/com/studentersamfundet/app/News.java +++ b/src/com/studentersamfundet/app/News.java @@ -1,16 +1,17 @@ package com.studentersamfundet.app; +import java.io.Serializable; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; -import android.net.Uri; - -public class News { +public class News implements Serializable { + private static final long serialVersionUID = 4015441794784305793L; + public String title; public String description; public String content; - public Uri link; + public String link; public Date pubDate; private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -19,7 +20,7 @@ public class News { this.title = title.trim(); this.description = description.trim(); this.content = content.trim(); - this.link = Uri.parse(link.trim()); + this.link = link.trim(); try { this.pubDate = sdf.parse(pubDate.trim()); diff --git a/src/com/studentersamfundet/app/RSSParserNews.java b/src/com/studentersamfundet/app/RSSParserNews.java index 562d98e..f56a418 100644 --- a/src/com/studentersamfundet/app/RSSParserNews.java +++ b/src/com/studentersamfundet/app/RSSParserNews.java @@ -4,7 +4,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -public class RSSParserNews { +public class RSSParserNews implements IRSSParser { private final DataHandler dh; public RSSParserNews() { diff --git a/src/com/studentersamfundet/app/RSSParserProgram.java b/src/com/studentersamfundet/app/RSSParserProgram.java index 7accb81..deee2fa 100644 --- a/src/com/studentersamfundet/app/RSSParserProgram.java +++ b/src/com/studentersamfundet/app/RSSParserProgram.java @@ -4,7 +4,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -public class RSSParserProgram { +public class RSSParserProgram implements IRSSParser { private final DataHandler dh; public RSSParserProgram() { diff --git a/src/com/studentersamfundet/app/ui/BaseDnsActivity.java b/src/com/studentersamfundet/app/ui/BaseDnsActivity.java index 2b6ac5a..0b1ef25 100644 --- a/src/com/studentersamfundet/app/ui/BaseDnsActivity.java +++ b/src/com/studentersamfundet/app/ui/BaseDnsActivity.java @@ -11,30 +11,21 @@ public abstract class BaseDnsActivity extends Activity { protected Handler handler = new Handler(); public void clickHandler(View v) { - Intent intent = new Intent(); + Intent intent = null; /* Program button was pushed: */ if (v.getId() == R.id.main_menu_program) { - intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT - | Intent.FLAG_ACTIVITY_SINGLE_TOP); - - intent.setClass(this, EventListActivity.class); + intent = setupNewIntent(EventListActivity.class); startActivity(intent); /* Join button was pushed: */ } else if (v.getId() == R.id.main_menu_join) { - intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY - | Intent.FLAG_ACTIVITY_SINGLE_TOP); - - intent.setClass(this, JoinUsActivity.class); + intent = setupNewIntent(JoinUsActivity.class); startActivity(intent); /* The header was pushed: */ } else if (v.getId() == R.id.header) { - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP - | Intent.FLAG_ACTIVITY_SINGLE_TOP); - - intent.setClass(this, DnsActivity.class); + intent = setupNewIntent(DnsActivity.class); startActivity(intent); } } @@ -42,4 +33,13 @@ public abstract class BaseDnsActivity extends Activity { public void doNothing(View v) { /* As promised. */ } + + protected Intent setupNewIntent(Class cl) { + Intent intent = new Intent(); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP + | Intent.FLAG_ACTIVITY_SINGLE_TOP); + intent.setClass(this, cl); + + return intent; + } } diff --git a/src/com/studentersamfundet/app/ui/BaseListActivity.java b/src/com/studentersamfundet/app/ui/BaseListActivity.java new file mode 100644 index 0000000..0096b4c --- /dev/null +++ b/src/com/studentersamfundet/app/ui/BaseListActivity.java @@ -0,0 +1,138 @@ +package com.studentersamfundet.app.ui; + +import java.io.IOException; + +import org.w3c.dom.NodeList; + +import com.studentersamfundet.app.DataHandler; +import com.studentersamfundet.app.Event; +import com.studentersamfundet.app.FeedFetcher; +import com.studentersamfundet.app.IRSSParser; +import com.studentersamfundet.app.R; +import com.studentersamfundet.app.RSSParserProgram; + +import android.content.Context; +import android.content.Intent; +import android.os.AsyncTask; +import android.os.Bundle; +import android.view.View; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.ProgressBar; +import android.widget.Toast; + +public abstract class BaseListActivity extends BaseDnsActivity { + private DataHandler dataHandler; + private FeedFetcher feed; + private IRSSParser parser; + + private boolean isUpdated = false; + + + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + /* Fetch the feed fetcher: */ + Intent i = getIntent(); + this.feed = (FeedFetcher)i.getExtras().get("feed"); + if (this.feed == null) + throw new RuntimeException("This activity must be fed a feed feeder."); + + /* Create the parser: */ + this.parser = new RSSParserProgram(); + + /* DataHandler will be set later. */ + } + + protected final DataHandler getDataHandler() { + if (!isUpdated && dataHandler == null) + throw new RuntimeException("You have to create and run list creator first!"); + + return this.dataHandler; + } + + protected abstract ListAdapter createAdapter(T[] objects); + protected abstract ListCreator getListCreator(Context c, int idList, int idProgressBar); + + protected abstract class ListCreator extends AsyncTask { + private String category = Event.ALL; + private boolean forcedUpdate = false; + + private final Context context; + private final int idList; + private final int idProgressBar; + + public ListCreator(Context context, int idList, int idProgressBar) { + this.context = context; + this.idList = idList; + this.idProgressBar = idProgressBar; + } + + protected String getCategory() { + return this.category; + } + + protected void setCategory(String category) { + this.category = category; + } + + protected boolean getForcedUpdate() { + return this.forcedUpdate; + } + + protected void setForcedUpdate(boolean forcedUpdate) { + this.forcedUpdate = forcedUpdate; + } + + @Override + protected void onPreExecute() { + if (forcedUpdate) { + dataHandler = null; + } + + ListView list = (ListView)findViewById(idList); + list.setVisibility(View.GONE); + + ProgressBar pb = (ProgressBar)findViewById(idProgressBar); + pb.setVisibility(View.VISIBLE); + } + + @Override + protected Boolean doInBackground(Void ... v) { + // Do we have intarwebs? + // YAY = Fetch the feed. + // NAY = Inform about no connection. + try { + if (dataHandler == null) { + NodeList itemNodes = feed.fetch(BaseListActivity.this, this.forcedUpdate); + dataHandler = parser.parse(itemNodes); + } + return true; + + } catch (IOException e) { + return false; + } + } + + @Override + protected void onPostExecute(Boolean result) { + isUpdated = true; + + if (result) { + ListView list = (ListView)findViewById(idList); + T[] objects = getObjects(); + ListAdapter adapter = createAdapter(objects); + list.setAdapter(adapter); + list.setVisibility(View.VISIBLE); + + ProgressBar pb = (ProgressBar)findViewById(idProgressBar); + pb.setVisibility(View.GONE); + } else { + Toast toast = Toast.makeText(context, R.string.error_noconnection_noupdate, Toast.LENGTH_LONG); + toast.show(); + } + } + + abstract protected T[] getObjects(); + } +} diff --git a/src/com/studentersamfundet/app/ui/DnsActivity.java b/src/com/studentersamfundet/app/ui/DnsActivity.java index 1c038ce..b2a89a4 100644 --- a/src/com/studentersamfundet/app/ui/DnsActivity.java +++ b/src/com/studentersamfundet/app/ui/DnsActivity.java @@ -12,10 +12,15 @@ import android.view.animation.Animation; import android.widget.TextView; import android.widget.Toast; +import com.studentersamfundet.app.FeedFetcher; import com.studentersamfundet.app.R; public class DnsActivity extends BaseDnsActivity { - String[] openingHoursHouse = { + public static final String feedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; + + private FeedFetcher feedFetcher; + + private String[] openingHoursHouse = { "10.00 - 01.00", "10.00 - 01.00", "10.00 - 01.00", @@ -24,7 +29,7 @@ public class DnsActivity extends BaseDnsActivity { "12.00 - 03.00", "12.00 - 20.00"}; - String[] openingHoursGB = { + private String[] openingHoursGB = { "13.00 - 01.00", "13.00 - 01.00", "13.00 - 01.00", @@ -33,7 +38,7 @@ public class DnsActivity extends BaseDnsActivity { "16.00 - 03.00", "Stengt"}; - String[] openingHoursBC = { + private String[] openingHoursBC = { "19.00 - 00.00", "19.00 - 00.00", "19.00 - 00.00", @@ -48,6 +53,9 @@ public class DnsActivity extends BaseDnsActivity { super.onCreate(savedInstanceState); setContentView(R.layout.main); + /* Initialize necessary variables: */ + feedFetcher = new FeedFetcher(feedURL); + /* Set opening hours: */ TextView tvNeuf = (TextView)findViewById(R.id.main_menu_hours_neuf); TextView tvBC = (TextView)findViewById(R.id.main_menu_hours_bc); @@ -138,22 +146,12 @@ public class DnsActivity extends BaseDnsActivity { } public void programButton(View v) { - Intent intent = new Intent(); - - intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT - | Intent.FLAG_ACTIVITY_SINGLE_TOP); - intent.setClass(this, EventListActivity.class); - + Intent intent = setupNewIntent(EventListActivity.class); startActivity(intent); } public void ticketButton(View v) { - Intent intent = new Intent(); - - intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT - | Intent.FLAG_ACTIVITY_SINGLE_TOP); - intent.setClass(this, EventListTicketsActivity.class); - + Intent intent = setupNewIntent(EventListTicketsActivity.class); startActivity(intent); } @@ -162,12 +160,15 @@ public class DnsActivity extends BaseDnsActivity { } public void joinButton(View v) { - Intent intent = new Intent(); - - intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); - intent.setClass(this, JoinUsActivity.class); - + Intent intent = setupNewIntent(JoinUsActivity.class); startActivity(intent); } + + protected Intent setupNewIntent(Class cl) { + Intent intent = super.setupNewIntent(cl); + intent.putExtra("feed", feedFetcher); + + return intent; + } } \ No newline at end of file diff --git a/src/com/studentersamfundet/app/ui/EventListActivity.java b/src/com/studentersamfundet/app/ui/EventListActivity.java index 561eb24..7ab8ad5 100644 --- a/src/com/studentersamfundet/app/ui/EventListActivity.java +++ b/src/com/studentersamfundet/app/ui/EventListActivity.java @@ -7,13 +7,11 @@ import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; -import org.w3c.dom.NodeList; - import android.app.Dialog; +import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.os.AsyncTask; import android.os.Bundle; import android.text.Html; import android.view.LayoutInflater; @@ -26,26 +24,16 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListAdapter; -import android.widget.ListView; -import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; -import com.studentersamfundet.app.DataHandler; import com.studentersamfundet.app.Event; -import com.studentersamfundet.app.FeedFetcher; import com.studentersamfundet.app.R; -import com.studentersamfundet.app.RSSParserProgram; import com.studentersamfundet.app.ui.ChooseCategoryDialog.Callback; -public class EventListActivity extends BaseDnsActivity { - public static final String feedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; +public class EventListActivity extends BaseListActivity { public static final int imageSize = 80; - protected FeedFetcher feed; - protected RSSParserProgram parser; - protected DataHandler dataHandler; - private String currentCategory = Event.ALL; @Override @@ -53,10 +41,7 @@ public class EventListActivity extends BaseDnsActivity { super.onCreate(savedInstanceState); setContentView(R.layout.event_list); - this.feed = new FeedFetcher(this, feedURL); - this.parser = new RSSParserProgram(); - - ListCreator lc = getListCreator(Event.ALL, false); + ListCreator lc = getListCreator(this, R.id.event_list, R.id.event_list_progress_bar); lc.execute(); } @@ -72,7 +57,8 @@ public class EventListActivity extends BaseDnsActivity { switch (item.getItemId()) { case R.id.menu_refresh: - ListCreator lc = getListCreator(Event.ALL, true); + ListCreator lc = getListCreator(this, R.id.event_list, R.id.event_list_progress_bar); + lc.setForcedUpdate(true); lc.execute(); return true; @@ -90,13 +76,13 @@ public class EventListActivity extends BaseDnsActivity { if (currentCategory.equals(Event.ALL)) { super.onBackPressed(); } else { - ListCreator lc = getListCreator(Event.ALL, false); + ListCreator lc = getListCreator(this, R.id.event_list, R.id.event_list_progress_bar); lc.execute(); } } - - - protected ListAdapter createAdapter(Event[] events) { + + protected ListAdapter createAdapter(T[] objects) { + Event[] events = (Event[])objects; ListAdapter adapter = new ArrayAdapter(this, R.layout.event_list_row, R.id.event_list_row_text, events) { @Override @@ -167,84 +153,30 @@ public class EventListActivity extends BaseDnsActivity { if (message.length < 1) message = new String[] { Event.ALL }; - ListCreator lc = getListCreator(message[0], false); + ListCreator lc = getListCreator(EventListActivity.this, R.id.event_list, R.id.event_list_progress_bar); + lc.setCategory(message[0]); lc.execute(); } }; - if (dataHandler != null) { - Dialog dialog = new ChooseCategoryDialog(this, callback, dataHandler.getEventCategories()); + if (getDataHandler() != null) { + Dialog dialog = new ChooseCategoryDialog(this, callback, getDataHandler().getEventCategories()); dialog.show(); } else { Toast.makeText(this, R.string.error_loading, Toast.LENGTH_SHORT); } } - protected class ListCreator extends AsyncTask { - protected String category; - protected boolean forcedUpdate; - - public ListCreator(String category, boolean forcedUpdate) { - this.category = category; - this.forcedUpdate = forcedUpdate; - } - - @Override - protected void onPreExecute() { - if (forcedUpdate) { - dataHandler = null; - } - - ListView list = (ListView)findViewById(R.id.event_list); - list.setVisibility(View.GONE); - - ProgressBar pb = (ProgressBar)findViewById(R.id.event_list_progress_bar); - pb.setVisibility(View.VISIBLE); - } - - @Override - protected Boolean doInBackground(Void ... v) { - // Do we have intarwebs? - // YAY = Fetch the feed. - // NAY = Inform about no connection. - try { - if (dataHandler == null) { - NodeList itemNodes = feed.fetch(this.forcedUpdate); - dataHandler = parser.parse(itemNodes); - } - return true; - - } catch (IOException e) { - return false; - } - } - - @Override - protected void onPostExecute(Boolean result) { - if (result) { - ListView list = (ListView)findViewById(R.id.event_list); - Event[] events = getEvents(); - ListAdapter adapter = createAdapter(events); - list.setAdapter(adapter); - list.setVisibility(View.VISIBLE); - - ProgressBar pb = (ProgressBar)findViewById(R.id.event_list_progress_bar); - pb.setVisibility(View.GONE); - - currentCategory = category; - } else { - Toast toast = Toast.makeText(EventListActivity.this, R.string.error_noconnection_noupdate, Toast.LENGTH_LONG); - toast.show(); - } - } - - protected Event[] getEvents() { - return dataHandler.getEvents(this.category); - } - } + - protected ListCreator getListCreator(String category, boolean forcedUpdate) { - return new ListCreator(category, forcedUpdate); + protected ListCreator getListCreator(Context context, int idList, int idProgressBar) { + return new ListCreator(context, idList, idProgressBar) { + + @Override + protected Event[] getObjects() { + return getDataHandler().getEvents(Event.ALL); + } + }; } protected OnClickListener getShortClickListener (final Event e) { diff --git a/src/com/studentersamfundet/app/ui/EventListTicketsActivity.java b/src/com/studentersamfundet/app/ui/EventListTicketsActivity.java index fd8f268..c727b3a 100644 --- a/src/com/studentersamfundet/app/ui/EventListTicketsActivity.java +++ b/src/com/studentersamfundet/app/ui/EventListTicketsActivity.java @@ -1,48 +1,34 @@ package com.studentersamfundet.app.ui; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.view.View; import android.view.View.OnClickListener; -import android.view.View.OnLongClickListener; import com.studentersamfundet.app.Event; public class EventListTicketsActivity extends EventListActivity { - - class TicketListCreator extends ListCreator { - public TicketListCreator(String category, boolean forcedUpdate) { - super(category, forcedUpdate); - } - - @Override - protected Event[] getEvents() { - return dataHandler.getEventsWithTickets(); - } - } - @Override - protected ListCreator getListCreator(String category, boolean forcedUpdate) { - return new TicketListCreator(category, forcedUpdate); - } - - protected OnClickListener getShortClickListener (final Event e) { - return new OnClickListener() { - public void onClick(View v) { - Uri webDestination = e.getTicketUri(); - - Intent webIntent = new Intent(Intent.ACTION_VIEW, webDestination); - startActivity(webIntent); - - } - }; - } - - protected OnLongClickListener getLongClickListener (final Event e) { - return new OnLongClickListener() { - public boolean onLongClick(View v) { - return false; - } - }; - } + protected ListCreator getListCreator(Context context, int idList, int idProgressBar) { + return new ListCreator(context, idList, idProgressBar) { + @Override + protected Event[] getObjects() { + return getDataHandler().getEventsWithTickets(); + } + }; + } + + @Override + protected OnClickListener getShortClickListener (final Event e) { + return new OnClickListener() { + public void onClick(View v) { + Uri webDestination = e.getTicketUri(); + + Intent webIntent = new Intent(Intent.ACTION_VIEW, webDestination); + startActivity(webIntent); + + } + }; + } } From e13c92ecf718e45cf03b3aff0d7cf3238dd955ad Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Tue, 24 Jan 2012 18:04:18 +0100 Subject: [PATCH 2/8] Added news. --- AndroidManifest.xml | 5 +- res/layout/news_list_row.xml | 21 +++++++ .../app/ui/BaseDnsActivity.java | 1 + .../app/ui/DnsActivity.java | 7 ++- .../app/ui/{ => lists}/BaseListActivity.java | 30 ++++++---- .../app/ui/{ => lists}/EventListActivity.java | 6 +- .../{ => lists}/EventListTicketsActivity.java | 4 +- .../app/ui/lists/NewsListActivity.java | 60 +++++++++++++++++++ 8 files changed, 116 insertions(+), 18 deletions(-) create mode 100644 res/layout/news_list_row.xml rename src/com/studentersamfundet/app/ui/{ => lists}/BaseListActivity.java (84%) rename src/com/studentersamfundet/app/ui/{ => lists}/EventListActivity.java (96%) rename src/com/studentersamfundet/app/ui/{ => lists}/EventListTicketsActivity.java (84%) create mode 100644 src/com/studentersamfundet/app/ui/lists/NewsListActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index f547fb9..96c1c9b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -19,8 +19,9 @@ - - + + + \ No newline at end of file diff --git a/res/layout/news_list_row.xml b/res/layout/news_list_row.xml new file mode 100644 index 0000000..6c1567b --- /dev/null +++ b/res/layout/news_list_row.xml @@ -0,0 +1,21 @@ + + + + + + + + diff --git a/src/com/studentersamfundet/app/ui/BaseDnsActivity.java b/src/com/studentersamfundet/app/ui/BaseDnsActivity.java index 0b1ef25..8dbe4e8 100644 --- a/src/com/studentersamfundet/app/ui/BaseDnsActivity.java +++ b/src/com/studentersamfundet/app/ui/BaseDnsActivity.java @@ -1,6 +1,7 @@ package com.studentersamfundet.app.ui; import com.studentersamfundet.app.R; +import com.studentersamfundet.app.ui.lists.EventListActivity; import android.app.Activity; import android.content.Intent; diff --git a/src/com/studentersamfundet/app/ui/DnsActivity.java b/src/com/studentersamfundet/app/ui/DnsActivity.java index b2a89a4..77b4bf9 100644 --- a/src/com/studentersamfundet/app/ui/DnsActivity.java +++ b/src/com/studentersamfundet/app/ui/DnsActivity.java @@ -10,10 +10,12 @@ import android.view.View.OnTouchListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.TextView; -import android.widget.Toast; import com.studentersamfundet.app.FeedFetcher; import com.studentersamfundet.app.R; +import com.studentersamfundet.app.ui.lists.EventListActivity; +import com.studentersamfundet.app.ui.lists.EventListTicketsActivity; +import com.studentersamfundet.app.ui.lists.NewsListActivity; public class DnsActivity extends BaseDnsActivity { public static final String feedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; @@ -156,7 +158,8 @@ public class DnsActivity extends BaseDnsActivity { } public void newsButton(View v) { - Toast.makeText(this, "Not implemented (yet!)", Toast.LENGTH_SHORT).show(); + Intent intent = setupNewIntent(NewsListActivity.class); + startActivity(intent); } public void joinButton(View v) { diff --git a/src/com/studentersamfundet/app/ui/BaseListActivity.java b/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java similarity index 84% rename from src/com/studentersamfundet/app/ui/BaseListActivity.java rename to src/com/studentersamfundet/app/ui/lists/BaseListActivity.java index 0096b4c..8930d19 100644 --- a/src/com/studentersamfundet/app/ui/BaseListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java @@ -1,4 +1,4 @@ -package com.studentersamfundet.app.ui; +package com.studentersamfundet.app.ui.lists; import java.io.IOException; @@ -10,11 +10,13 @@ import com.studentersamfundet.app.FeedFetcher; import com.studentersamfundet.app.IRSSParser; import com.studentersamfundet.app.R; import com.studentersamfundet.app.RSSParserProgram; +import com.studentersamfundet.app.ui.BaseDnsActivity; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; @@ -22,7 +24,7 @@ import android.widget.ProgressBar; import android.widget.Toast; public abstract class BaseListActivity extends BaseDnsActivity { - private DataHandler dataHandler; + private DataHandler datahandler; private FeedFetcher feed; private IRSSParser parser; @@ -45,14 +47,18 @@ public abstract class BaseListActivity extends BaseDnsActivity { } protected final DataHandler getDataHandler() { - if (!isUpdated && dataHandler == null) + if (!isUpdated && datahandler == null) throw new RuntimeException("You have to create and run list creator first!"); - return this.dataHandler; + return this.datahandler; } + /** + * Creates list adapter used by listview. + * @param objects + * @return + */ protected abstract ListAdapter createAdapter(T[] objects); - protected abstract ListCreator getListCreator(Context c, int idList, int idProgressBar); protected abstract class ListCreator extends AsyncTask { private String category = Event.ALL; @@ -87,14 +93,15 @@ public abstract class BaseListActivity extends BaseDnsActivity { @Override protected void onPreExecute() { if (forcedUpdate) { - dataHandler = null; + datahandler = null; } ListView list = (ListView)findViewById(idList); list.setVisibility(View.GONE); ProgressBar pb = (ProgressBar)findViewById(idProgressBar); - pb.setVisibility(View.VISIBLE); + if (pb != null) + pb.setVisibility(View.VISIBLE); } @Override @@ -103,9 +110,9 @@ public abstract class BaseListActivity extends BaseDnsActivity { // YAY = Fetch the feed. // NAY = Inform about no connection. try { - if (dataHandler == null) { + if (datahandler == null) { NodeList itemNodes = feed.fetch(BaseListActivity.this, this.forcedUpdate); - dataHandler = parser.parse(itemNodes); + datahandler = parser.parse(itemNodes); } return true; @@ -121,12 +128,15 @@ public abstract class BaseListActivity extends BaseDnsActivity { if (result) { ListView list = (ListView)findViewById(idList); T[] objects = getObjects(); + + Log.i("DNSapp", "Showing " +objects.length +" objects."); ListAdapter adapter = createAdapter(objects); list.setAdapter(adapter); list.setVisibility(View.VISIBLE); ProgressBar pb = (ProgressBar)findViewById(idProgressBar); - pb.setVisibility(View.GONE); + if (pb != null) + pb.setVisibility(View.GONE); } else { Toast toast = Toast.makeText(context, R.string.error_noconnection_noupdate, Toast.LENGTH_LONG); toast.show(); diff --git a/src/com/studentersamfundet/app/ui/EventListActivity.java b/src/com/studentersamfundet/app/ui/lists/EventListActivity.java similarity index 96% rename from src/com/studentersamfundet/app/ui/EventListActivity.java rename to src/com/studentersamfundet/app/ui/lists/EventListActivity.java index 7ab8ad5..44e6bf8 100644 --- a/src/com/studentersamfundet/app/ui/EventListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/EventListActivity.java @@ -1,4 +1,4 @@ -package com.studentersamfundet.app.ui; +package com.studentersamfundet.app.ui.lists; import java.io.IOException; import java.io.InputStream; @@ -29,6 +29,8 @@ import android.widget.Toast; import com.studentersamfundet.app.Event; import com.studentersamfundet.app.R; +import com.studentersamfundet.app.ui.ChooseCategoryDialog; +import com.studentersamfundet.app.ui.EventViewActivity; import com.studentersamfundet.app.ui.ChooseCategoryDialog.Callback; public class EventListActivity extends BaseListActivity { @@ -169,7 +171,7 @@ public class EventListActivity extends BaseListActivity { - protected ListCreator getListCreator(Context context, int idList, int idProgressBar) { + protected ListCreator getListCreator(Context context, int idList, int idProgressBar) { return new ListCreator(context, idList, idProgressBar) { @Override diff --git a/src/com/studentersamfundet/app/ui/EventListTicketsActivity.java b/src/com/studentersamfundet/app/ui/lists/EventListTicketsActivity.java similarity index 84% rename from src/com/studentersamfundet/app/ui/EventListTicketsActivity.java rename to src/com/studentersamfundet/app/ui/lists/EventListTicketsActivity.java index c727b3a..51d1fc1 100644 --- a/src/com/studentersamfundet/app/ui/EventListTicketsActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/EventListTicketsActivity.java @@ -1,4 +1,4 @@ -package com.studentersamfundet.app.ui; +package com.studentersamfundet.app.ui.lists; import android.content.Context; import android.content.Intent; @@ -10,7 +10,7 @@ import com.studentersamfundet.app.Event; public class EventListTicketsActivity extends EventListActivity { @Override - protected ListCreator getListCreator(Context context, int idList, int idProgressBar) { + protected ListCreator getListCreator(Context context, int idList, int idProgressBar) { return new ListCreator(context, idList, idProgressBar) { @Override protected Event[] getObjects() { diff --git a/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java new file mode 100644 index 0000000..adf59b4 --- /dev/null +++ b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java @@ -0,0 +1,60 @@ +package com.studentersamfundet.app.ui.lists; + +import com.studentersamfundet.app.News; +import com.studentersamfundet.app.R; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.TextView; + +public class NewsListActivity extends BaseListActivity { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.event_list); + + ListCreator lc = new ListCreator(this, R.id.event_list, R.id.event_list_progress_bar) { + + @Override + protected News[] getObjects() { + return getDataHandler().getNews(); + } + }; + lc.execute(); + } + + + @Override + protected ListAdapter createAdapter(T[] objects) { + final News[] news = (News[])objects; + ListAdapter adapter = new ArrayAdapter(this, 0, 0, news) { + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewGroup row; + + if (null == convertView) { + LayoutInflater inflater = getLayoutInflater(); + row = (ViewGroup)inflater.inflate(0, null); + } else { + row = (ViewGroup)convertView; + } + + News article = news[position]; + + TextView title = (TextView)row.findViewById(R.id.news_list_row_title); + TextView description = (TextView)row.findViewById(R.id.news_list_row_desc); + + title.setText(article.title); + description.setText(article.description); + + return row; + } + }; + + return adapter; + } +} From 855ae6c0551599c330b762f5bb90470e01dd14b7 Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Wed, 8 Feb 2012 20:06:13 +0100 Subject: [PATCH 3/8] Not sure what I did here. I've done it like a month ago and I just don't remember, but I don't want to lose the code in case it does something important or fancy. --- .../studentersamfundet/app/FeedFetcher.java | 19 ++++++++++--------- .../app/ui/DnsActivity.java | 2 +- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/com/studentersamfundet/app/FeedFetcher.java b/src/com/studentersamfundet/app/FeedFetcher.java index 3a38127..ddf1cbf 100644 --- a/src/com/studentersamfundet/app/FeedFetcher.java +++ b/src/com/studentersamfundet/app/FeedFetcher.java @@ -22,11 +22,12 @@ import android.content.Context; public class FeedFetcher implements Serializable { private static final long serialVersionUID = 3408406671996232103L; - private static final String LOCAL_FILENAME = "dns_events"; + private final String localFilename; private String url; - public FeedFetcher(String url) { + public FeedFetcher(String url, String localFilename) { this.url = url; + this.localFilename = localFilename; } private InputStream openHttpConnection() throws IOException { @@ -86,7 +87,7 @@ public class FeedFetcher implements Serializable { return itemNodes; } - public static NodeList loadLocalData(Context c) throws IOException { + public NodeList loadLocalData(Context c) throws IOException { NodeList nodes; InputStream is = loadFile(c); @@ -120,11 +121,11 @@ public class FeedFetcher implements Serializable { return doc.getElementsByTagName("item"); } - private static boolean doesFileNeedUpdate(Context c) throws IOException { + private boolean doesFileNeedUpdate(Context c) throws IOException { /** Maximum amount of time before the file needs updating, in ms: */ final long maxInterval = 1000 * 60 * 60; // one hour - File file = c.getFileStreamPath(LOCAL_FILENAME); + File file = c.getFileStreamPath(localFilename); if (! file.exists()) return true; long now = System.currentTimeMillis(); @@ -133,10 +134,10 @@ public class FeedFetcher implements Serializable { return now - lastMod > maxInterval; } - private static void saveFile(InputStream in, Context c) throws IOException, FileNotFoundException { + private void saveFile(InputStream in, Context c) throws IOException, FileNotFoundException { final int BUFFER_SIZE = 128; - OutputStream os = c.openFileOutput(LOCAL_FILENAME, Context.MODE_PRIVATE); + OutputStream os = c.openFileOutput(localFilename, Context.MODE_PRIVATE); byte[] inputBuffer = new byte[BUFFER_SIZE]; int bytes = 0; @@ -147,7 +148,7 @@ public class FeedFetcher implements Serializable { os.close(); } - private static InputStream loadFile(Context c) throws FileNotFoundException { - return c.openFileInput(LOCAL_FILENAME); + private InputStream loadFile(Context c) throws FileNotFoundException { + return c.openFileInput(localFilename); } } diff --git a/src/com/studentersamfundet/app/ui/DnsActivity.java b/src/com/studentersamfundet/app/ui/DnsActivity.java index 77b4bf9..124a017 100644 --- a/src/com/studentersamfundet/app/ui/DnsActivity.java +++ b/src/com/studentersamfundet/app/ui/DnsActivity.java @@ -56,7 +56,7 @@ public class DnsActivity extends BaseDnsActivity { setContentView(R.layout.main); /* Initialize necessary variables: */ - feedFetcher = new FeedFetcher(feedURL); + feedFetcher = new FeedFetcher(feedURL, "local_events"); /* Set opening hours: */ TextView tvNeuf = (TextView)findViewById(R.id.main_menu_hours_neuf); From 63549b1f053ea1abecf35c38b7e665609ef932a3 Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Thu, 1 Mar 2012 19:13:03 +0100 Subject: [PATCH 4/8] News are shown. --- .../studentersamfundet/app/IRSSParser.java | 4 ++- .../studentersamfundet/app/RSSParserNews.java | 3 ++- .../app/RSSParserProgram.java | 1 + .../app/ui/DnsActivity.java | 25 +++++++++++-------- .../app/ui/lists/BaseListActivity.java | 5 ++-- .../app/ui/lists/NewsListActivity.java | 4 +-- 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/com/studentersamfundet/app/IRSSParser.java b/src/com/studentersamfundet/app/IRSSParser.java index 72415d8..60223be 100644 --- a/src/com/studentersamfundet/app/IRSSParser.java +++ b/src/com/studentersamfundet/app/IRSSParser.java @@ -1,7 +1,9 @@ package com.studentersamfundet.app; +import java.io.Serializable; + import org.w3c.dom.NodeList; -public interface IRSSParser { +public interface IRSSParser extends Serializable { public DataHandler parse(NodeList nodes); } diff --git a/src/com/studentersamfundet/app/RSSParserNews.java b/src/com/studentersamfundet/app/RSSParserNews.java index f56a418..52b3cd6 100644 --- a/src/com/studentersamfundet/app/RSSParserNews.java +++ b/src/com/studentersamfundet/app/RSSParserNews.java @@ -5,6 +5,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class RSSParserNews implements IRSSParser { + private static final long serialVersionUID = -2654591623794449527L; private final DataHandler dh; public RSSParserNews() { @@ -37,7 +38,7 @@ public class RSSParserNews implements IRSSParser { NodeList titleNodes = (itemElement).getElementsByTagName("title"); NodeList descriptionNodes = (itemElement).getElementsByTagName("description"); NodeList linkNodes = (itemElement).getElementsByTagName("link"); - NodeList textNodes = (itemElement).getElementsByTagName("content"); + NodeList textNodes = (itemElement).getElementsByTagName("content:encoded"); NodeList dateNodes = (itemElement).getElementsByTagName("pubDate"); // Convert a Node into an Element. diff --git a/src/com/studentersamfundet/app/RSSParserProgram.java b/src/com/studentersamfundet/app/RSSParserProgram.java index deee2fa..eaff858 100644 --- a/src/com/studentersamfundet/app/RSSParserProgram.java +++ b/src/com/studentersamfundet/app/RSSParserProgram.java @@ -5,6 +5,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class RSSParserProgram implements IRSSParser { + private static final long serialVersionUID = 3364287016121595334L; private final DataHandler dh; public RSSParserProgram() { diff --git a/src/com/studentersamfundet/app/ui/DnsActivity.java b/src/com/studentersamfundet/app/ui/DnsActivity.java index 124a017..6eb7fbc 100644 --- a/src/com/studentersamfundet/app/ui/DnsActivity.java +++ b/src/com/studentersamfundet/app/ui/DnsActivity.java @@ -13,14 +13,18 @@ import android.widget.TextView; import com.studentersamfundet.app.FeedFetcher; import com.studentersamfundet.app.R; +import com.studentersamfundet.app.RSSParserNews; +import com.studentersamfundet.app.RSSParserProgram; import com.studentersamfundet.app.ui.lists.EventListActivity; import com.studentersamfundet.app.ui.lists.EventListTicketsActivity; import com.studentersamfundet.app.ui.lists.NewsListActivity; public class DnsActivity extends BaseDnsActivity { - public static final String feedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; + public static final String eventFeedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; + public static final String newsFeedURL = "http://studentersamfundet.no/rss/nyheter_feed.php"; - private FeedFetcher feedFetcher; + private FeedFetcher eventFeedFetcher; + private FeedFetcher newsFeedFetcher; private String[] openingHoursHouse = { "10.00 - 01.00", @@ -56,7 +60,8 @@ public class DnsActivity extends BaseDnsActivity { setContentView(R.layout.main); /* Initialize necessary variables: */ - feedFetcher = new FeedFetcher(feedURL, "local_events"); + eventFeedFetcher = new FeedFetcher(eventFeedURL, "local_events"); + newsFeedFetcher = new FeedFetcher(newsFeedURL, "local_news"); /* Set opening hours: */ TextView tvNeuf = (TextView)findViewById(R.id.main_menu_hours_neuf); @@ -149,29 +154,27 @@ public class DnsActivity extends BaseDnsActivity { public void programButton(View v) { Intent intent = setupNewIntent(EventListActivity.class); + intent.putExtra("feed", eventFeedFetcher); + intent.putExtra("parser", new RSSParserProgram()); startActivity(intent); } public void ticketButton(View v) { Intent intent = setupNewIntent(EventListTicketsActivity.class); + intent.putExtra("feed", eventFeedFetcher); + intent.putExtra("parser", new RSSParserProgram()); startActivity(intent); } public void newsButton(View v) { Intent intent = setupNewIntent(NewsListActivity.class); + intent.putExtra("feed", newsFeedFetcher); + intent.putExtra("parser", new RSSParserNews()); startActivity(intent); } public void joinButton(View v) { Intent intent = setupNewIntent(JoinUsActivity.class); startActivity(intent); - - } - - protected Intent setupNewIntent(Class cl) { - Intent intent = super.setupNewIntent(cl); - intent.putExtra("feed", feedFetcher); - - return intent; } } \ No newline at end of file diff --git a/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java b/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java index 8930d19..7c6824f 100644 --- a/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/BaseListActivity.java @@ -9,7 +9,6 @@ import com.studentersamfundet.app.Event; import com.studentersamfundet.app.FeedFetcher; import com.studentersamfundet.app.IRSSParser; import com.studentersamfundet.app.R; -import com.studentersamfundet.app.RSSParserProgram; import com.studentersamfundet.app.ui.BaseDnsActivity; import android.content.Context; @@ -41,7 +40,9 @@ public abstract class BaseListActivity extends BaseDnsActivity { throw new RuntimeException("This activity must be fed a feed feeder."); /* Create the parser: */ - this.parser = new RSSParserProgram(); + this.parser = (IRSSParser)i.getExtras().get("parser"); + if (this.parser == null) + throw new RuntimeException("This activity requires feed parser"); /* DataHandler will be set later. */ } diff --git a/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java index adf59b4..01a7bb8 100644 --- a/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java @@ -31,14 +31,14 @@ public class NewsListActivity extends BaseListActivity { @Override protected ListAdapter createAdapter(T[] objects) { final News[] news = (News[])objects; - ListAdapter adapter = new ArrayAdapter(this, 0, 0, news) { + ListAdapter adapter = new ArrayAdapter(this, R.layout.news_list_row, R.id.news_list_row_desc, news) { @Override public View getView(int position, View convertView, ViewGroup parent) { ViewGroup row; if (null == convertView) { LayoutInflater inflater = getLayoutInflater(); - row = (ViewGroup)inflater.inflate(0, null); + row = (ViewGroup)inflater.inflate(R.layout.news_list_row, null); } else { row = (ViewGroup)convertView; } From 170c00f85c706a340f60432de661f99f7cc7e076 Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Thu, 1 Mar 2012 19:28:53 +0100 Subject: [PATCH 5/8] Changed feed source. --- src/com/studentersamfundet/app/ui/DnsActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/studentersamfundet/app/ui/DnsActivity.java b/src/com/studentersamfundet/app/ui/DnsActivity.java index 6eb7fbc..cd86274 100644 --- a/src/com/studentersamfundet/app/ui/DnsActivity.java +++ b/src/com/studentersamfundet/app/ui/DnsActivity.java @@ -20,7 +20,7 @@ import com.studentersamfundet.app.ui.lists.EventListTicketsActivity; import com.studentersamfundet.app.ui.lists.NewsListActivity; public class DnsActivity extends BaseDnsActivity { - public static final String eventFeedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; + public static final String eventFeedURL = "http://studentersamfundet.no/rss/robert_program_feed.php"; public static final String newsFeedURL = "http://studentersamfundet.no/rss/nyheter_feed.php"; private FeedFetcher eventFeedFetcher; From c149ba038d1e57ef207fc3b9d1ea2982b2a18a5a Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Thu, 1 Mar 2012 19:29:27 +0100 Subject: [PATCH 6/8] Adjusted fonts a little in news list. --- res/layout/news_list_row.xml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/res/layout/news_list_row.xml b/res/layout/news_list_row.xml index 6c1567b..e002a00 100644 --- a/res/layout/news_list_row.xml +++ b/res/layout/news_list_row.xml @@ -10,12 +10,14 @@ android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_alignParentLeft="true" - android:layout_alignParentTop="true" /> + android:layout_alignParentTop="true" + android:textSize="16sp" /> + android:layout_below="@id/news_list_row_title" + android:textSize="12sp" /> From 7526b27d18ba3465bcfd0b1a9d324983bc67e56b Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Mon, 26 Mar 2012 13:50:32 +0200 Subject: [PATCH 7/8] Enhanced news list. --- res/layout/news_list_row.xml | 12 ++++++++++-- .../app/ui/lists/NewsListActivity.java | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/res/layout/news_list_row.xml b/res/layout/news_list_row.xml index e002a00..a6fc8e9 100644 --- a/res/layout/news_list_row.xml +++ b/res/layout/news_list_row.xml @@ -8,16 +8,24 @@ + + + diff --git a/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java index 01a7bb8..65e316c 100644 --- a/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java @@ -1,5 +1,7 @@ package com.studentersamfundet.app.ui.lists; +import java.text.SimpleDateFormat; + import com.studentersamfundet.app.News; import com.studentersamfundet.app.R; @@ -47,10 +49,14 @@ public class NewsListActivity extends BaseListActivity { TextView title = (TextView)row.findViewById(R.id.news_list_row_title); TextView description = (TextView)row.findViewById(R.id.news_list_row_desc); + TextView footer = (TextView)row.findViewById(R.id.news_list_row_byline); title.setText(article.title); description.setText(article.description); + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm, d. MMMM y"); + footer.setText(sdf.format(article.pubDate)); + return row; } }; From 3e0bbb2d9730e6154a20cd1e86306c3a98ff191f Mon Sep 17 00:00:00 2001 From: Robert Kolner Date: Mon, 26 Mar 2012 14:15:32 +0200 Subject: [PATCH 8/8] Added an activity for showing news. --- AndroidManifest.xml | 1 + res/layout/news_view.xml | 37 +++++++++++++ src/com/studentersamfundet/app/News.java | 30 +++++++++-- .../app/ui/NewsViewActivity.java | 53 +++++++++++++++++++ .../app/ui/lists/NewsListActivity.java | 20 +++++-- 5 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 res/layout/news_view.xml create mode 100644 src/com/studentersamfundet/app/ui/NewsViewActivity.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 96c1c9b..de239e8 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -18,6 +18,7 @@ + diff --git a/res/layout/news_view.xml b/res/layout/news_view.xml new file mode 100644 index 0000000..dfaa979 --- /dev/null +++ b/res/layout/news_view.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + diff --git a/src/com/studentersamfundet/app/News.java b/src/com/studentersamfundet/app/News.java index ee32772..a78aef6 100644 --- a/src/com/studentersamfundet/app/News.java +++ b/src/com/studentersamfundet/app/News.java @@ -8,11 +8,11 @@ import java.util.Date; public class News implements Serializable { private static final long serialVersionUID = 4015441794784305793L; - public String title; - public String description; - public String content; - public String link; - public Date pubDate; + private String title; + private String description; + private String content; + private String link; + private Date pubDate; private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -28,4 +28,24 @@ public class News implements Serializable { this.pubDate = null; } } + + public String getTitle() { + return this.title; + } + + public String getDescription() { + return this.description; + } + + public String getContent() { + return this.content; + } + + public String getLink() { + return this.link; + } + + public Date getPubDate() { + return this.pubDate; + } } diff --git a/src/com/studentersamfundet/app/ui/NewsViewActivity.java b/src/com/studentersamfundet/app/ui/NewsViewActivity.java new file mode 100644 index 0000000..9e5a474 --- /dev/null +++ b/src/com/studentersamfundet/app/ui/NewsViewActivity.java @@ -0,0 +1,53 @@ +package com.studentersamfundet.app.ui; + +import java.text.SimpleDateFormat; + +import android.content.Intent; +import android.os.Bundle; +import android.text.Html; +import android.text.util.Linkify; +import android.widget.TextView; +import com.studentersamfundet.app.News; +import com.studentersamfundet.app.R; + +public class NewsViewActivity extends BaseDnsActivity { + News article; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.news_view); + + Intent intent = getIntent(); + this.article = (News)intent.getSerializableExtra("news"); + final News a = this.article; + + if (a == null) { + throw new Error("This should NEVER happen"); + } + + TextView title = (TextView) findViewById(R.id.news_view_title); + TextView description = (TextView) findViewById(R.id.news_view_description); + TextView datetime = (TextView) findViewById(R.id.news_view_datetime); + + title.setText(Html.fromHtml(a.getTitle())); + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + datetime.setText(a.getPubDate() != null ? dateFormat.format(a.getPubDate()) : ""); + + StringBuilder sb = new StringBuilder(); + + if (a.getDescription().length() > 0) { + sb.append(""); + sb.append(a.getDescription()); + sb.append("

"); + } + + if (a.getContent().length() > 0) { + sb.append(a.getContent()); + } + + description.setText(Html.fromHtml(sb.toString())); + Linkify.addLinks(description, Linkify.ALL); + } +} diff --git a/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java index 65e316c..79bfbd1 100644 --- a/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java +++ b/src/com/studentersamfundet/app/ui/lists/NewsListActivity.java @@ -4,10 +4,13 @@ import java.text.SimpleDateFormat; import com.studentersamfundet.app.News; import com.studentersamfundet.app.R; +import com.studentersamfundet.app.ui.NewsViewActivity; +import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; +import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListAdapter; @@ -45,17 +48,26 @@ public class NewsListActivity extends BaseListActivity { row = (ViewGroup)convertView; } - News article = news[position]; + final News article = news[position]; TextView title = (TextView)row.findViewById(R.id.news_list_row_title); TextView description = (TextView)row.findViewById(R.id.news_list_row_desc); TextView footer = (TextView)row.findViewById(R.id.news_list_row_byline); - title.setText(article.title); - description.setText(article.description); + title.setText(article.getTitle()); + description.setText(article.getDescription()); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm, d. MMMM y"); - footer.setText(sdf.format(article.pubDate)); + footer.setText(sdf.format(article.getPubDate())); + + row.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + Intent intent = new Intent(NewsListActivity.this, NewsViewActivity.class); + intent.putExtra("news", article); + + startActivity(intent); + } + }); return row; }