mirror of
https://github.com/bspeice/UNCCGameDay
synced 2024-11-05 07:38:13 -05:00
Add documentation to the alert system, and refactor to use the
TwitterClient
This commit is contained in:
parent
3026441c51
commit
3d50f42f3b
@ -59,6 +59,11 @@ public class Alert {
|
|||||||
return this.message;
|
return this.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to make displaying an alert on the statusbar easy
|
||||||
|
*
|
||||||
|
* @param ctx - The context needed to display the alert.
|
||||||
|
*/
|
||||||
public void displayNotification(Context ctx) {
|
public void displayNotification(Context ctx) {
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx)
|
||||||
.setSmallIcon(R.drawable.ic_launcher)
|
.setSmallIcon(R.drawable.ic_launcher)
|
||||||
|
@ -12,22 +12,45 @@ import android.database.sqlite.SQLiteOpenHelper;
|
|||||||
|
|
||||||
/* Responsible for handling persistence/fetching of alerts */
|
/* Responsible for handling persistence/fetching of alerts */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Class AlertDB.
|
||||||
|
*/
|
||||||
public class AlertDB extends SQLiteOpenHelper {
|
public class AlertDB extends SQLiteOpenHelper {
|
||||||
|
|
||||||
|
/** The Constant DATABASE_VERSION. */
|
||||||
private static final int DATABASE_VERSION = 1;
|
private static final int DATABASE_VERSION = 1;
|
||||||
|
|
||||||
|
/** The Constant DATABASE_NAME. */
|
||||||
private static final String DATABASE_NAME = "AlertDataBase";
|
private static final String DATABASE_NAME = "AlertDataBase";
|
||||||
|
|
||||||
|
/** The Constant TABLE_ALERTS. */
|
||||||
private static final String TABLE_ALERTS = "alerts";
|
private static final String TABLE_ALERTS = "alerts";
|
||||||
|
|
||||||
|
/** The Constant KEY_ALARM_DATE. */
|
||||||
private static final String KEY_ALARM_DATE = "alarm_date";
|
private static final String KEY_ALARM_DATE = "alarm_date";
|
||||||
|
|
||||||
|
/** The Constant KEY_MESSAGE. */
|
||||||
private static final String KEY_MESSAGE = "message";
|
private static final String KEY_MESSAGE = "message";
|
||||||
|
|
||||||
|
/** The Constant KEY_SHOWN. */
|
||||||
private static final String KEY_SHOWN = "shown";
|
private static final String KEY_SHOWN = "shown";
|
||||||
|
|
||||||
|
/** The Constant KEY_TYPE. */
|
||||||
private static final String KEY_TYPE = "type";
|
private static final String KEY_TYPE = "type";
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates our wrapper around the SQLiteOpenHelper
|
||||||
|
*
|
||||||
|
* @param context - The context to open the database in
|
||||||
|
*/
|
||||||
public AlertDB(Context context){
|
public AlertDB(Context context){
|
||||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create the initial Database
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(SQLiteDatabase db) {
|
public void onCreate(SQLiteDatabase db) {
|
||||||
String CREATE_ALERTS_TABLE = "CREATE TABLE " + TABLE_ALERTS + "("
|
String CREATE_ALERTS_TABLE = "CREATE TABLE " + TABLE_ALERTS + "("
|
||||||
@ -35,6 +58,10 @@ public class AlertDB extends SQLiteOpenHelper {
|
|||||||
+ KEY_SHOWN + " INT," + KEY_TYPE + " STRING" + ")";
|
+ KEY_SHOWN + " INT," + KEY_TYPE + " STRING" + ")";
|
||||||
db.execSQL(CREATE_ALERTS_TABLE);
|
db.execSQL(CREATE_ALERTS_TABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upgrade the database on application update
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
//drop older table if it exists
|
//drop older table if it exists
|
||||||
@ -45,7 +72,11 @@ public class AlertDB extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//persist alert into Database
|
/**
|
||||||
|
* Persist an alert into the database
|
||||||
|
*
|
||||||
|
* @param a - The Alert to persist
|
||||||
|
*/
|
||||||
public void persist(Alert a) {
|
public void persist(Alert a) {
|
||||||
|
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
@ -62,8 +93,12 @@ public class AlertDB extends SQLiteOpenHelper {
|
|||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
//persist list of alerts by looping through list
|
/**
|
||||||
//and calling persist
|
* Persist a list of alerts into the database -
|
||||||
|
* helper method for easy iteration
|
||||||
|
*
|
||||||
|
* @param alerts - The alerts to store
|
||||||
|
*/
|
||||||
public void persistMultiple(List<Alert> alerts) {
|
public void persistMultiple(List<Alert> alerts) {
|
||||||
|
|
||||||
for(int i = 0; i < alerts.size(); i++)
|
for(int i = 0; i < alerts.size(); i++)
|
||||||
@ -73,17 +108,35 @@ public class AlertDB extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch an alert by date
|
||||||
|
* TODO: Unused. Remove?
|
||||||
|
*
|
||||||
|
* @param d - The date to fetch an alert from
|
||||||
|
* @return the Alert
|
||||||
|
*/
|
||||||
public Alert fetch(Date d) {
|
public Alert fetch(Date d) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch multiple alerts from the Database
|
||||||
|
* TODO: Unused. Remove?
|
||||||
|
*
|
||||||
|
* @param dates the dates to fetch alerts from
|
||||||
|
* @return the list
|
||||||
|
*/
|
||||||
public List<Alert> fetchMultiple(List<Date> dates) {
|
public List<Alert> fetchMultiple(List<Date> dates) {
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
//delete specified alert from DB
|
/**
|
||||||
|
* Delete an alert from the Database whenever the Alert message matches
|
||||||
|
*
|
||||||
|
* @param alert the alert
|
||||||
|
*/
|
||||||
public void deleteAlert(Alert alert) {
|
public void deleteAlert(Alert alert) {
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
db.delete(TABLE_ALERTS, KEY_MESSAGE + " = ?",
|
db.delete(TABLE_ALERTS, KEY_MESSAGE + " = ?",
|
||||||
@ -94,9 +147,11 @@ public class AlertDB extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
//get all alerts from Database
|
/**
|
||||||
//regardless of type, or if it has been shown
|
* Fetch all alerts from the Database, and mark as read
|
||||||
//update each alert in DB as being shown
|
*
|
||||||
|
* @return the list
|
||||||
|
*/
|
||||||
public List<Alert> fetchAll() {
|
public List<Alert> fetchAll() {
|
||||||
|
|
||||||
List<Alert> alertList = new ArrayList<Alert>();
|
List<Alert> alertList = new ArrayList<Alert>();
|
||||||
@ -127,7 +182,11 @@ public class AlertDB extends SQLiteOpenHelper {
|
|||||||
return alertList;
|
return alertList;
|
||||||
}
|
}
|
||||||
|
|
||||||
//fetch just unread alerts from database
|
/**
|
||||||
|
* Fetch unread alerts from the database, and mark them as read
|
||||||
|
*
|
||||||
|
* @return the list
|
||||||
|
*/
|
||||||
public List<Alert> fetchUnread() {
|
public List<Alert> fetchUnread() {
|
||||||
|
|
||||||
List<Alert> alertList = new ArrayList<Alert>();
|
List<Alert> alertList = new ArrayList<Alert>();
|
||||||
@ -161,7 +220,12 @@ public class AlertDB extends SQLiteOpenHelper {
|
|||||||
return alertList;
|
return alertList;
|
||||||
}
|
}
|
||||||
|
|
||||||
//update alert in DB to be classified as shown
|
/**
|
||||||
|
* Update an alert to be marked as shown
|
||||||
|
*
|
||||||
|
* @param alert the alert
|
||||||
|
* @return the int
|
||||||
|
*/
|
||||||
private int updateAlert(Alert alert) {
|
private int updateAlert(Alert alert) {
|
||||||
|
|
||||||
SQLiteDatabase db = this.getWritableDatabase();
|
SQLiteDatabase db = this.getWritableDatabase();
|
||||||
|
@ -10,20 +10,16 @@ import twitter4j.Twitter;
|
|||||||
import twitter4j.TwitterException;
|
import twitter4j.TwitterException;
|
||||||
import twitter4j.TwitterFactory;
|
import twitter4j.TwitterFactory;
|
||||||
import twitter4j.conf.ConfigurationBuilder;
|
import twitter4j.conf.ConfigurationBuilder;
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.Application;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.uncc.gameday.R;
|
import com.uncc.gameday.R;
|
||||||
|
import com.uncc.gameday.twitter.TwitterClient;
|
||||||
|
|
||||||
// TODO: Auto-generated Javadoc
|
|
||||||
/**
|
/**
|
||||||
* The Class AlertFetcher.
|
* The Class AlertFetcher.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public class AlertFetcher {
|
public class AlertFetcher {
|
||||||
// Class responsible for fetching all alerts as necessary.
|
// Class responsible for fetching all alerts as necessary.
|
||||||
|
|
||||||
@ -31,7 +27,7 @@ public class AlertFetcher {
|
|||||||
private int maxTweets = 5;
|
private int maxTweets = 5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch alerts.
|
* Fetch all alerts - Twitter, timed, etc.
|
||||||
*
|
*
|
||||||
* @param ctx the ctx
|
* @param ctx the ctx
|
||||||
*/
|
*/
|
||||||
@ -57,74 +53,52 @@ public class AlertFetcher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch organization alerts.
|
* Fetch all Organization alerts from Twitter
|
||||||
*
|
*
|
||||||
* @param ctx the ctx
|
* @param ctx - The Context needed to access the Internet
|
||||||
* @throws TwitterException the twitter exception
|
* @throws TwitterException - Error in using the Twitter API
|
||||||
*/
|
*/
|
||||||
private void fetchOrganizationAlerts(Context ctx) throws TwitterException {
|
private void fetchOrganizationAlerts(Context ctx) throws TwitterException {
|
||||||
|
// Process fetching organization alerts (alerts retweeted by us)
|
||||||
//creates configuration for twitter access
|
// Guaranteed to return <= maxTweets
|
||||||
ConfigurationBuilder cb = new ConfigurationBuilder();
|
String handle = ctx.getString(R.string.gameday_handle);
|
||||||
|
TwitterClient tc = new TwitterClient();
|
||||||
cb.setDebugEnabled(true)
|
List<Status> statuses = tc.fetchTweets(handle, maxTweets);
|
||||||
.setOAuthConsumerKey("vfRa3Tr5QYaU8Jr2pKHtiA")
|
|
||||||
.setOAuthConsumerSecret("gGRdIrhPdX2Vrg296xOvTqE4sgOISMphRmPdrGirbU")
|
|
||||||
.setOAuthAccessToken("1912299896-uqrhDiif3oX9Ybkf8rM5pQDWN6mW4Y7vRLK47C7")
|
|
||||||
.setOAuthAccessTokenSecret("kZ11I74dcA00pWgQDZelFQz1ADJJMK0ejr6snnU34jUVT");
|
|
||||||
|
|
||||||
TwitterFactory tf = new TwitterFactory(cb.build());
|
// Filter for anything created by us (retweet)
|
||||||
|
for (Iterator<Status> it = statuses.iterator(); it.hasNext();){
|
||||||
|
// May need to switch to isRetweetByMe, not sure if
|
||||||
|
// We're using the right function (documentation is awful)
|
||||||
|
if (!it.next().isRetweet())
|
||||||
|
it.remove();
|
||||||
|
}
|
||||||
|
|
||||||
// Process fetching organization alerts (alerts retweeted by us)
|
String type = AlertType.getValue(AlertType.ORGANIZATION);
|
||||||
// Will not necessarily fetch `maxTweets` tweets.
|
pushToDatabase(statuses, type, ctx);
|
||||||
String handle = ctx.getString(R.string.gameday_handle);
|
|
||||||
Twitter twitter = tf.getInstance();
|
|
||||||
List<Status> statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets));
|
|
||||||
|
|
||||||
// Filter for anything created by us (retweet)
|
|
||||||
for (Iterator<Status> it = statuses.iterator(); it.hasNext();){
|
|
||||||
// May need to switch to isRetweetByMe (documentation is awful)
|
|
||||||
if (!it.next().isRetweet())
|
|
||||||
it.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
String type = AlertType.getValue(AlertType.ORGANIZATION);
|
|
||||||
pushToDatabase(statuses, type, ctx);
|
|
||||||
|
|
||||||
|
|
||||||
// List contains all valid alerts now
|
// List contains all valid alerts now
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch university alerts.
|
* Fetch alerts from the University Twitter
|
||||||
*
|
*
|
||||||
* @param ctx the ctx
|
* @param ctx - The Context for accessing the Internet
|
||||||
* @throws TwitterException the twitter exception
|
* @throws TwitterException - Throws an exception for misuse of Twitter API
|
||||||
*/
|
*/
|
||||||
private void fetchUniversityAlerts(Context ctx) throws TwitterException {
|
private void fetchUniversityAlerts(Context ctx) throws TwitterException {
|
||||||
// Process fetching university alerts
|
// Process fetching university alerts
|
||||||
// Guaranteed to get `maxTweets` tweets
|
// Guaranteed to get `maxTweets` tweets
|
||||||
|
|
||||||
//creates configuration for twitter access
|
|
||||||
ConfigurationBuilder cb = new ConfigurationBuilder();
|
|
||||||
|
|
||||||
cb.setDebugEnabled(true)
|
|
||||||
.setOAuthConsumerKey("vfRa3Tr5QYaU8Jr2pKHtiA")
|
|
||||||
.setOAuthConsumerSecret("gGRdIrhPdX2Vrg296xOvTqE4sgOISMphRmPdrGirbU")
|
|
||||||
.setOAuthAccessToken("1912299896-uqrhDiif3oX9Ybkf8rM5pQDWN6mW4Y7vRLK47C7")
|
|
||||||
.setOAuthAccessTokenSecret("kZ11I74dcA00pWgQDZelFQz1ADJJMK0ejr6snnU34jUVT");
|
|
||||||
|
|
||||||
TwitterFactory tf = new TwitterFactory(cb.build());
|
String handle = ctx.getString(R.string.university_handle);
|
||||||
|
TwitterClient tc = new TwitterClient();
|
||||||
|
List<Status> statuses = tc.fetchTweets(handle, maxTweets);
|
||||||
|
|
||||||
String handle = ctx.getString(R.string.university_handle);
|
String type = AlertType.getValue(AlertType.UNIVERSITY);
|
||||||
Twitter twitter = tf.getInstance();
|
pushToDatabase(statuses, type, ctx);
|
||||||
List<Status> statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets));
|
|
||||||
|
System.out.println(statuses.get(0).getText());
|
||||||
String type = AlertType.getValue(AlertType.UNIVERSITY);
|
// List contains all valid alerts now
|
||||||
pushToDatabase(statuses, type, ctx);
|
|
||||||
|
|
||||||
System.out.println(statuses.get(0).getText());
|
|
||||||
// List contains all valid alerts now
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -136,21 +110,10 @@ public class AlertFetcher {
|
|||||||
private void fetchGamedayAlerts(Context ctx) throws TwitterException {
|
private void fetchGamedayAlerts(Context ctx) throws TwitterException {
|
||||||
// Process fetching alerts generated by staff of UNCCGameDay
|
// Process fetching alerts generated by staff of UNCCGameDay
|
||||||
// Not guaranteed to get `maxTweets` tweets
|
// Not guaranteed to get `maxTweets` tweets
|
||||||
|
|
||||||
//creates configuration for twitter access
|
|
||||||
ConfigurationBuilder cb = new ConfigurationBuilder();
|
|
||||||
|
|
||||||
cb.setDebugEnabled(true)
|
|
||||||
.setOAuthConsumerKey("vfRa3Tr5QYaU8Jr2pKHtiA")
|
|
||||||
.setOAuthConsumerSecret("gGRdIrhPdX2Vrg296xOvTqE4sgOISMphRmPdrGirbU")
|
|
||||||
.setOAuthAccessToken("1912299896-uqrhDiif3oX9Ybkf8rM5pQDWN6mW4Y7vRLK47C7")
|
|
||||||
.setOAuthAccessTokenSecret("kZ11I74dcA00pWgQDZelFQz1ADJJMK0ejr6snnU34jUVT");
|
|
||||||
|
|
||||||
TwitterFactory tf = new TwitterFactory(cb.build());
|
|
||||||
|
|
||||||
String handle = ctx.getString(R.string.gameday_handle);
|
String handle = ctx.getString(R.string.gameday_handle);
|
||||||
Twitter twitter = tf.getInstance();
|
TwitterClient tc = new TwitterClient();
|
||||||
List<Status> statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets));
|
List<Status> statuses = tc.fetchTweets(handle, maxTweets);
|
||||||
|
|
||||||
// Filter out anything not from us
|
// Filter out anything not from us
|
||||||
for (Iterator<Status> it = statuses.iterator(); it.hasNext();){
|
for (Iterator<Status> it = statuses.iterator(); it.hasNext();){
|
||||||
|
@ -6,15 +6,30 @@ import android.app.IntentService;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
|
// TODO: Auto-generated Javadoc
|
||||||
|
/**
|
||||||
|
* The Class AlertService.
|
||||||
|
*/
|
||||||
public class AlertService extends IntentService {
|
public class AlertService extends IntentService {
|
||||||
|
|
||||||
|
/** The Constant name. */
|
||||||
private static final String name = "AlertService";
|
private static final String name = "AlertService";
|
||||||
|
|
||||||
|
/** The prefs. */
|
||||||
SharedPreferences prefs = null;
|
SharedPreferences prefs = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new alert service.
|
||||||
|
*/
|
||||||
public AlertService() {
|
public AlertService() {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start the actual alert service
|
||||||
|
*
|
||||||
|
* @param intent - The incoming intent that started us
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void onHandleIntent(Intent intent) {
|
protected void onHandleIntent(Intent intent) {
|
||||||
// Go fetch all the alerts!
|
// Go fetch all the alerts!
|
||||||
@ -38,8 +53,10 @@ public class AlertService extends IntentService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Creates timed alerts and adds them to AlertDB
|
/**
|
||||||
//Only runs on first application startup
|
* Creates timed alerts and adds them to AlertDB
|
||||||
|
* Only runs on first application startup
|
||||||
|
*/
|
||||||
protected void onFirstRun()
|
protected void onFirstRun()
|
||||||
{
|
{
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
@ -55,7 +72,4 @@ public class AlertService extends IntentService {
|
|||||||
db.persist(b);
|
db.persist(b);
|
||||||
db.persist(c);
|
db.persist(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ import twitter4j.TwitterFactory;
|
|||||||
import twitter4j.conf.Configuration;
|
import twitter4j.conf.Configuration;
|
||||||
import twitter4j.conf.ConfigurationBuilder;
|
import twitter4j.conf.ConfigurationBuilder;
|
||||||
|
|
||||||
// TODO: Auto-generated Javadoc
|
|
||||||
/**
|
/**
|
||||||
* The Class TwitterClient.
|
* The Class TwitterClient.
|
||||||
*/
|
*/
|
||||||
@ -63,7 +62,7 @@ public class TwitterClient {
|
|||||||
* Fetch tweets.
|
* Fetch tweets.
|
||||||
*
|
*
|
||||||
* @param handle the handle
|
* @param handle the handle
|
||||||
* @param count the count
|
* @param count The maximum number of tweets to fetch
|
||||||
* @return the list
|
* @return the list
|
||||||
*/
|
*/
|
||||||
public List<Status> fetchTweets(String handle, int count) {
|
public List<Status> fetchTweets(String handle, int count) {
|
||||||
|
Loading…
Reference in New Issue
Block a user