mirror of
				https://github.com/bspeice/UNCCGameDay
				synced 2025-11-04 02:10:35 -05:00 
			
		
		
		
	AlertDB being re-commited to Git
This commit is contained in:
		@ -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<Alert> 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<String> adapter =
 | 
			
		||||
	            new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, printArray);
 | 
			
		||||
	    listView.setAdapter(adapter);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	/**
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
 | 
			
		||||
@ -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<Alert> 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<Alert> fetchMultiple(List<Date> 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<Alert> fetchAll() {
 | 
			
		||||
		
 | 
			
		||||
		return null;
 | 
			
		||||
		List<Alert> alertList = new ArrayList<Alert>();
 | 
			
		||||
		
 | 
			
		||||
		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<Alert> fetchUnread() {
 | 
			
		||||
		return null;
 | 
			
		||||
		
 | 
			
		||||
		List<Alert> alertList = new ArrayList<Alert>();
 | 
			
		||||
		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()) });
 | 
			
		||||
		
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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<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();
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		// 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<Status> 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<Status> statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets));
 | 
			
		||||
		
 | 
			
		||||
		// Filter out anything not from us
 | 
			
		||||
		for (Iterator<Status> 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<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();
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                // 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<Status> 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<Status> statuses = twitter.getUserTimeline(handle, new Paging(1, maxTweets));
 | 
			
		||||
                
 | 
			
		||||
                // Filter out anything not from us
 | 
			
		||||
                for (Iterator<Status> 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<Alert> createTimedAlerts(Context ctx) {
 | 
			
		||||
		// Create the timed alerts so we can add or remove them from the DB
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
        }
 | 
			
		||||
}
 | 
			
		||||
@ -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<Alert> 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<Alert> 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);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										29
									
								
								src/com/uncc/gameday/alerts/AlertType.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/com/uncc/gameday/alerts/AlertType.java
									
									
									
									
									
										Normal file
									
								
							@ -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";
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user