From a06aa2020740348f7f9fe16fd58149c2c537dbf2 Mon Sep 17 00:00:00 2001 From: agk512 Date: Thu, 21 Nov 2013 14:03:23 -0500 Subject: [PATCH] AlertDB being re-commited to Git --- bin/AndroidManifest.xml | 1 + src/com/uncc/gameday/activities/Home.java | 28 +++ src/com/uncc/gameday/alerts/Alert.java | 78 ++----- src/com/uncc/gameday/alerts/AlertDB.java | 178 ++++++++++++---- src/com/uncc/gameday/alerts/AlertFetcher.java | 199 +++++++++--------- src/com/uncc/gameday/alerts/AlertService.java | 52 +++-- src/com/uncc/gameday/alerts/AlertType.java | 29 +++ 7 files changed, 342 insertions(+), 223 deletions(-) create mode 100644 src/com/uncc/gameday/alerts/AlertType.java diff --git a/bin/AndroidManifest.xml b/bin/AndroidManifest.xml index d2f6b13..ec0e8ad 100644 --- a/bin/AndroidManifest.xml +++ b/bin/AndroidManifest.xml @@ -36,6 +36,7 @@ + diff --git a/src/com/uncc/gameday/activities/Home.java b/src/com/uncc/gameday/activities/Home.java index 028794f..a4c7625 100644 --- a/src/com/uncc/gameday/activities/Home.java +++ b/src/com/uncc/gameday/activities/Home.java @@ -1,15 +1,21 @@ package com.uncc.gameday.activities; +import java.util.List; + import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; +import android.widget.ArrayAdapter; +import android.widget.ListView; import com.uncc.gameday.R; +import com.uncc.gameday.alerts.Alert; +import com.uncc.gameday.alerts.AlertDB; import com.uncc.gameday.alerts.AlertService; // TODO: Auto-generated Javadoc @@ -43,6 +49,28 @@ public class Home extends MenuActivity { // Double check if we need to do any first-run code this.onFirstRun(); + + this.displayList(); + } + + + public void displayList() + { + + List alerts = new AlertDB(this).fetchAll(); + + String[] printArray = new String[alerts.size()]; + + //get message from each alert and put in printArray + for(int i = 0; i < alerts.size(); i++) + { + printArray[i] = alerts.get(i).getMessage(); + } + + ListView listView = (ListView)findViewById(R.id.alertsListView); + ArrayAdapter adapter = + new ArrayAdapter(this,android.R.layout.simple_list_item_1, printArray); + listView.setAdapter(adapter); } /** diff --git a/src/com/uncc/gameday/alerts/Alert.java b/src/com/uncc/gameday/alerts/Alert.java index 946d323..8b2d4c6 100644 --- a/src/com/uncc/gameday/alerts/Alert.java +++ b/src/com/uncc/gameday/alerts/Alert.java @@ -12,99 +12,49 @@ import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; -// TODO: Auto-generated Javadoc -/** - * The Class Alert. - */ public class Alert { - /** The alarm date. */ private Date alarmDate; - - /** The message. */ private String message; - - /** The shown. */ - private boolean shown; + private int shown; + private String type; // Default constructor - /** - * Instantiates a new alert. - */ public Alert(){} - /** - * Instantiates a new alert. - * - * @param alarmDate the alarm date - * @param message the message - * @param shown the shown - */ - public Alert(Date alarmDate, String message, boolean shown) { + public Alert(Date alarmDate, String message, int shown, String type) { this.setAlarmDate(alarmDate); this.setMessage(message); this.setShown(shown); + this.setType(type); } - /** - * Gets the alarm date. - * - * @return the alarm date - */ + private void setType(String type) { + this.type = type; + } + + public String getType() { + return type; + } public Date getAlarmDate() { return alarmDate; } - - /** - * Sets the alarm date. - * - * @param alarmDate the new alarm date - */ public void setAlarmDate(Date alarmDate) { this.alarmDate = alarmDate; } - - /** - * Gets the message. - * - * @return the message - */ public String getMessage() { return message; } - - /** - * Sets the message. - * - * @param message the new message - */ public void setMessage(String message) { this.message = message; } - - /** - * Checks if is shown. - * - * @return true, if is shown - */ - public boolean isShown() { + public int isShown() { return shown; } - - /** - * Sets the shown. - * - * @param shown the new shown - */ - public void setShown(boolean shown) { - this.shown = shown; + public void setShown(int i) { + this.shown = i; } - /** - * Display notification. - * - * @param ctx the ctx - */ public void displayNotification(Context ctx) { NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx) .setSmallIcon(R.drawable.ic_launcher) diff --git a/src/com/uncc/gameday/alerts/AlertDB.java b/src/com/uncc/gameday/alerts/AlertDB.java index 81b731a..965f89e 100644 --- a/src/com/uncc/gameday/alerts/AlertDB.java +++ b/src/com/uncc/gameday/alerts/AlertDB.java @@ -1,72 +1,172 @@ package com.uncc.gameday.alerts; +import java.util.ArrayList; import java.util.Date; import java.util.List; -// TODO: Auto-generated Javadoc +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + /* Responsible for handling persistence/fetching of alerts */ -/** - * The Class AlertDB. - */ -public class AlertDB { +public class AlertDB extends SQLiteOpenHelper { + + private static final int DATABASE_VERSION = 1; + private static final String DATABASE_NAME = "AlertDataBase"; + private static final String TABLE_ALERTS = "alerts"; - /** - * Persist. - * - * @param a the a - */ + private static final String KEY_ALARM_DATE = "alarm_date"; + private static final String KEY_MESSAGE = "message"; + private static final String KEY_SHOWN = "shown"; + private static final String KEY_TYPE = "type"; + + + public AlertDB(Context context){ + super(context, DATABASE_NAME, null, DATABASE_VERSION); + + } + @Override + public void onCreate(SQLiteDatabase db) { + String CREATE_ALERTS_TABLE = "CREATE TABLE " + TABLE_ALERTS + "(" + + KEY_MESSAGE + " STRING PRIMARY KEY," + KEY_ALARM_DATE + " LONG," + + KEY_SHOWN + " INT," + KEY_TYPE + " STRING" + ")"; + db.execSQL(CREATE_ALERTS_TABLE); + } + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + //drop older table if it exists + db.execSQL("DROP OLDER TABLE " + TABLE_ALERTS); + + //recreate tables + onCreate(db); + + } + + //persist alert into Database public void persist(Alert a) { + SQLiteDatabase db = this.getWritableDatabase(); + + ContentValues values = new ContentValues(); + values.put(KEY_MESSAGE, a.getMessage()); + + long dateValue = a.getAlarmDate().getTime(); + values.put(KEY_ALARM_DATE, dateValue); + values.put(KEY_SHOWN, a.isShown()); + values.put(KEY_TYPE, a.getType()); + + db.insert(TABLE_ALERTS, null, values); + db.close(); } - /** - * Persist multiple. - * - * @param alerts the alerts - */ + //persist list of alerts by looping through list + //and calling persist public void persistMultiple(List alerts) { + + for(int i = 0; i < alerts.size(); i++) + { + persist(alerts.get(i)); + } } - /** - * Fetch. - * - * @param d the d - * @return the alert - */ public Alert fetch(Date d) { return null; } - /** - * Fetch multiple. - * - * @param dates the dates - * @return the list - */ public List fetchMultiple(List dates) { return null; } - /** - * Fetch all. - * - * @return the list - */ + + //get all alerts from Database + //regardless of type, or if it has been shown + //update each alert in DB as being shown public List fetchAll() { - return null; + List alertList = new ArrayList(); + + String selectQuery = "SELECT * FROM " + TABLE_ALERTS; + + SQLiteDatabase db = this.getWritableDatabase(); + Cursor cursor = db.rawQuery(selectQuery, null); + + //for each alert in database + //add it to alert list to be shown + //and modify it in DB to be classified as shown + if (cursor.moveToFirst()) + { + do + { + Alert alert = new Alert(); + alert.setMessage(cursor.getString(0)); + alert.setAlarmDate(new Date(cursor.getLong(1))); + alert.setShown(cursor.getInt(2)); + updateAlert(alert); + + alertList.add(alert); + } while (cursor.moveToNext()); + + } + db.close(); + return alertList; } - /** - * Fetch unread. - * - * @return the list - */ + //fetch just unread alerts from database public List fetchUnread() { - return null; + + List alertList = new ArrayList(); + String selectQuery = "SELECT * FROM " + TABLE_ALERTS; + + SQLiteDatabase db = this.getWritableDatabase(); + Cursor cursor = db.rawQuery(selectQuery, null); + + //for each alert in database + //given it is classified as unread + //add it to alert list to be shown + //and modify it in DB to be classified as shown + if (cursor.moveToFirst()) + { + do + { + Alert alert = new Alert(); + if(cursor.getInt(2) == 0) + { + alert.setMessage(cursor.getString(0)); + alert.setAlarmDate(new Date(cursor.getLong(1))); + alert.setShown(cursor.getInt(2)); + + updateAlert(alert); + alertList.add(alert); + } + } while (cursor.moveToNext()); + + } + db.close(); + return alertList; } + + //update alert in DB to be classified as shown + private int updateAlert(Alert alert) { + + SQLiteDatabase db = this.getWritableDatabase(); + + ContentValues values = new ContentValues(); + + long dateValue = alert.getAlarmDate().getTime(); + values.put(KEY_ALARM_DATE, dateValue); + values.put(KEY_SHOWN, 1); + + return db.update(TABLE_ALERTS, values, KEY_MESSAGE + " = ?", + new String[] { String.valueOf(alert.getMessage()) }); + + } + } + + diff --git a/src/com/uncc/gameday/alerts/AlertFetcher.java b/src/com/uncc/gameday/alerts/AlertFetcher.java index 7d383d6..0ab3139 100644 --- a/src/com/uncc/gameday/alerts/AlertFetcher.java +++ b/src/com/uncc/gameday/alerts/AlertFetcher.java @@ -19,109 +19,98 @@ import com.uncc.gameday.R; * The Class AlertFetcher. */ public class AlertFetcher { - // Class responsible for fetching all alerts as necessary. - - /** The max tweets. */ - private int maxTweets = 5; - - /** - * Fetch alerts. - * - * @param ctx the ctx - */ - public void fetchAlerts(Context ctx) { - // Fetch all alerts. Responsible for discovering what sources need to be fetched. - - try { - // Note we have to use the SharedPreferences so that we have preferences no matter what activity - // sent us this context - SharedPreferences settings = ctx.getSharedPreferences(ctx.getString(R.string.preferences_file), 0); // MODE_PRIVATE - - if (settings.getBoolean("ALERT_ORGANIZATION", false)) - // Fetch organization alerts - this.fetchOrganizationAlerts(ctx); - if (settings.getBoolean("ALERT_UNIVERSITY", false)) - // Fetch university alerts - this.fetchUniversityAlerts(ctx); - - // And always fetch alerts made by us. Period. - this.fetchGamedayAlerts(ctx); - } catch (TwitterException e) { - Log.w("AlertFetcher", "Unable to fetch alerts from Twitter...", e); - } - } - - /** - * Fetch organization alerts. - * - * @param ctx the ctx - * @throws TwitterException the twitter exception - */ - private void fetchOrganizationAlerts(Context ctx) throws TwitterException { - // Process fetching organization alerts (alerts retweeted by us) - // Will not necessarily fetch `maxTweets` tweets. - String handle = ctx.getString(R.string.gameday_handle); - Twitter twitter = TwitterFactory.getSingleton(); - List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); - - // Filter for anything created by us (retweet) - for (Iterator it = statuses.iterator(); it.hasNext();){ - // May need to switch to isRetweetByMe (documentation is awful) - if (!it.next().isRetweet()) - it.remove(); - } - - // List contains all valid alerts now - } - - /** - * Fetch university alerts. - * - * @param ctx the ctx - * @throws TwitterException the twitter exception - */ - private void fetchUniversityAlerts(Context ctx) throws TwitterException { - // Process fetching university alerts - // Guaranteed to get `maxTweets` tweets - String handle = ctx.getString(R.string.university_handle); - Twitter twitter = TwitterFactory.getSingleton(); - List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); - - // List contains all valid alerts now - } - - /** - * Fetch gameday alerts. - * - * @param ctx the ctx - * @throws TwitterException the twitter exception - */ - private void fetchGamedayAlerts(Context ctx) throws TwitterException { - // Process fetching alerts generated by staff of UNCCGameDay - // Not guaranteed to get `maxTweets` tweets - String handle = ctx.getString(R.string.gameday_handle); - Twitter twitter = TwitterFactory.getSingleton(); - List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); - - // Filter out anything not from us - for (Iterator it = statuses.iterator(); it.hasNext();){ - // May need to switch to isRetweetByMe (documentation is awful) - if (it.next().isRetweet()) - it.remove(); - } - - // List contains all valid alerts now. + // Class responsible for fetching all alerts as necessary. + + /** The max tweets. */ + private int maxTweets = 5; + + /** + * Fetch alerts. + * + * @param ctx the ctx + */ + public void fetchAlerts(Context ctx) { + // Fetch all alerts. Responsible for discovering what sources need to be fetched. + + try { + // Note we have to use the SharedPreferences so that we have preferences no matter what activity + // sent us this context + SharedPreferences settings = ctx.getSharedPreferences(ctx.getString(R.string.preferences_file), 0); // MODE_PRIVATE + + if (settings.getBoolean("ALERT_ORGANIZATION", false)) + // Fetch organization alerts + this.fetchOrganizationAlerts(ctx); + if (settings.getBoolean("ALERT_UNIVERSITY", false)) + // Fetch university alerts + this.fetchUniversityAlerts(ctx); + + // And always fetch alerts made by us. Period. + this.fetchGamedayAlerts(ctx); + } catch (TwitterException e) { + Log.w("AlertFetcher", "Unable to fetch alerts from Twitter...", e); + } + } + + /** + * Fetch organization alerts. + * + * @param ctx the ctx + * @throws TwitterException the twitter exception + */ + private void fetchOrganizationAlerts(Context ctx) throws TwitterException { + // Process fetching organization alerts (alerts retweeted by us) + // Will not necessarily fetch `maxTweets` tweets. + String handle = ctx.getString(R.string.gameday_handle); + Twitter twitter = TwitterFactory.getSingleton(); + List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); + + // Filter for anything created by us (retweet) + for (Iterator it = statuses.iterator(); it.hasNext();){ + // May need to switch to isRetweetByMe (documentation is awful) + if (!it.next().isRetweet()) + it.remove(); + } + + // List contains all valid alerts now + } + + /** + * Fetch university alerts. + * + * @param ctx the ctx + * @throws TwitterException the twitter exception + */ + private void fetchUniversityAlerts(Context ctx) throws TwitterException { + // Process fetching university alerts + // Guaranteed to get `maxTweets` tweets + String handle = ctx.getString(R.string.university_handle); + Twitter twitter = TwitterFactory.getSingleton(); + List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); + + // List contains all valid alerts now + } + + /** + * Fetch gameday alerts. + * + * @param ctx the ctx + * @throws TwitterException the twitter exception + */ + private void fetchGamedayAlerts(Context ctx) throws TwitterException { + // Process fetching alerts generated by staff of UNCCGameDay + // Not guaranteed to get `maxTweets` tweets + String handle = ctx.getString(R.string.gameday_handle); + Twitter twitter = TwitterFactory.getSingleton(); + List statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets)); + + // Filter out anything not from us + for (Iterator it = statuses.iterator(); it.hasNext();){ + // May need to switch to isRetweetByMe (documentation is awful) + if (it.next().isRetweet()) + it.remove(); + } + + // List contains all valid alerts now. - } - - /** - * Creates the timed alerts. - * - * @param ctx the ctx - * @return the list - */ - public static List createTimedAlerts(Context ctx) { - // Create the timed alerts so we can add or remove them from the DB - return null; - } -} + } +} \ No newline at end of file diff --git a/src/com/uncc/gameday/alerts/AlertService.java b/src/com/uncc/gameday/alerts/AlertService.java index 22a6b2a..6a66272 100644 --- a/src/com/uncc/gameday/alerts/AlertService.java +++ b/src/com/uncc/gameday/alerts/AlertService.java @@ -1,39 +1,61 @@ package com.uncc.gameday.alerts; +import java.util.Date; import java.util.List; - import android.app.IntentService; import android.content.Intent; +import android.content.SharedPreferences; -// TODO: Auto-generated Javadoc -/** - * The Class AlertService. - */ public class AlertService extends IntentService { - - /** The Constant name. */ private static final String name = "AlertService"; + + SharedPreferences prefs = null; - /** - * Instantiates a new alert service. - */ public AlertService() { super(name); } - /* (non-Javadoc) - * @see android.app.IntentService#onHandleIntent(android.content.Intent) - */ @Override protected void onHandleIntent(Intent intent) { // Go fetch all the alerts! - new AlertFetcher().fetchAlerts(this); - List alerts = new AlertDB().fetchUnread(); + //new AlertFetcher().fetchAlerts(this); + + //if first application run, create and store + //timed alerts into database + prefs = getSharedPreferences("com.uncc.gameday", MODE_PRIVATE); + if(prefs.getBoolean("firstrun", true)){ + onFirstRun(); + prefs.edit().putBoolean("firstrun", false).commit(); + } + + List alerts = new AlertDB(this).fetchAll(); // And then display all of them! for (Alert a: alerts) { a.displayNotification(this); } + + } + + //Creates timed alerts and adds them to AlertDB + //Only runs on first application startup + protected void onFirstRun() + { + @SuppressWarnings("deprecation") + Alert a1 = new Alert(new Date(2003, 10, 10), "This is a test1", 0, AlertType.getValue(AlertType.ORGANIZATION)); + @SuppressWarnings("deprecation") + Alert b = new Alert(new Date(2003, 10, 10), "This is a test2", 0, AlertType.getValue(AlertType.GAMEDAY)); + @SuppressWarnings("deprecation") + Alert c = new Alert(new Date(2003, 10, 10), "This is a test3", 0, AlertType.getValue(AlertType.TIMED)); + + AlertDB db = new AlertDB(this); + + db.persist(a1); + db.persist(b); + db.persist(c); + } + + } diff --git a/src/com/uncc/gameday/alerts/AlertType.java b/src/com/uncc/gameday/alerts/AlertType.java new file mode 100644 index 0000000..d290a39 --- /dev/null +++ b/src/com/uncc/gameday/alerts/AlertType.java @@ -0,0 +1,29 @@ +package com.uncc.gameday.alerts; + +public enum AlertType { + + TIMED ("TIMED"), + ORGANIZATION ("ORGANIZATION"), + UNIVERSITY ("UNIVERSITY"), + GAMEDAY ("GAMEDAY"); + + String type; + AlertType(String type) {this.type = type; } + public String getValue() { return type; } + + + public static String getValue(AlertType type) + { + switch(type) + { + case TIMED: + return "TIMED"; + case ORGANIZATION: + return "ORGANIZATION"; + case UNIVERSITY: + return "UNIVERSITY"; + default: + return "GAMEDAY"; + } + } +} \ No newline at end of file