mirror of
				https://github.com/bspeice/UNCCGameDay
				synced 2025-11-04 02:10:35 -05:00 
			
		
		
		
	Add some more skeleton to how Alerts should be processed
Create a way to statically get the app context
This commit is contained in:
		
							
								
								
									
										17
									
								
								src/com/uncc/gameday/GameDay.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/com/uncc/gameday/GameDay.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,17 @@
 | 
			
		||||
package com.uncc.gameday;
 | 
			
		||||
 | 
			
		||||
import android.app.Application;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
 | 
			
		||||
public class GameDay extends Application{
 | 
			
		||||
	private static Context context;
 | 
			
		||||
 | 
			
		||||
    public void onCreate(){
 | 
			
		||||
        super.onCreate();
 | 
			
		||||
        GameDay.context = getApplicationContext();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static Context getAppContext() {
 | 
			
		||||
    	return GameDay.context;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,26 +1,16 @@
 | 
			
		||||
package com.uncc.gameday.activities;
 | 
			
		||||
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import twitter4j.Paging;
 | 
			
		||||
import twitter4j.Status;
 | 
			
		||||
import twitter4j.Twitter;
 | 
			
		||||
import twitter4j.TwitterException;
 | 
			
		||||
import twitter4j.TwitterFactory;
 | 
			
		||||
 | 
			
		||||
import com.uncc.gameday.R;
 | 
			
		||||
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
import android.widget.CheckBox;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
import com.uncc.gameday.R;
 | 
			
		||||
 | 
			
		||||
public class Alerts extends MenuActivity {
 | 
			
		||||
	
 | 
			
		||||
	Context ctx;
 | 
			
		||||
	int maxTweets = 5; // Most tweets at a time from any source
 | 
			
		||||
 | 
			
		||||
	protected void onCreate(Bundle savedInstanceState) {
 | 
			
		||||
		super.onCreate(savedInstanceState);
 | 
			
		||||
@ -28,92 +18,6 @@ public class Alerts extends MenuActivity {
 | 
			
		||||
		setContentView(R.layout.activity_alerts);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void fetchAlerts() {
 | 
			
		||||
		// Fetch all alerts. Responsible for discovering what sources need to be fetched.
 | 
			
		||||
		
 | 
			
		||||
		if (((CheckBox)findViewById(R.id.alerts_check_timed)).isChecked())
 | 
			
		||||
			// Fetch timed alerts
 | 
			
		||||
			this.fetchTimedAlerts();
 | 
			
		||||
		else if (((CheckBox)findViewById(R.id.alerts_check_organizations)).isChecked())
 | 
			
		||||
			// Fetch organization alerts
 | 
			
		||||
			this.fetchOrganizationAlerts();
 | 
			
		||||
		else if (((CheckBox)findViewById(R.id.alerts_check_university)).isChecked())
 | 
			
		||||
			// Fetch university alerts
 | 
			
		||||
			this.fetchUniversityAlerts();
 | 
			
		||||
		
 | 
			
		||||
		// And always fetch alerts made by us. Period.
 | 
			
		||||
		this.fetchGamedayAlerts();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchTimedAlerts() {
 | 
			
		||||
		// Process the rules for all timed alerts.
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchOrganizationAlerts() {
 | 
			
		||||
		// Process fetching organization alerts (alerts retweeted by us)
 | 
			
		||||
		int duration = Toast.LENGTH_SHORT;
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			String handle = 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
 | 
			
		||||
			
 | 
			
		||||
		} catch (TwitterException e) {
 | 
			
		||||
			Toast.makeText(this.ctx, "Unable to fetch alerts for organizations!\nAre you connected to the internet?", duration).show();
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchUniversityAlerts() {
 | 
			
		||||
		// Process fetching university alerts
 | 
			
		||||
		int duration = Toast.LENGTH_SHORT;
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			String handle = 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
 | 
			
		||||
			
 | 
			
		||||
		} catch (TwitterException e) {
 | 
			
		||||
			Toast.makeText(this.ctx, "Unable to fetch alerts for the University!\nAre you connected to the internet?", duration).show();
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchGamedayAlerts() {
 | 
			
		||||
		// Process fetching alerts generated by staff of UNCCGameDay
 | 
			
		||||
		int duration = Toast.LENGTH_SHORT;
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			String handle = 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.
 | 
			
		||||
			
 | 
			
		||||
		} catch (TwitterException e) {
 | 
			
		||||
			Toast.makeText(this.ctx, "Unable to fetch alerts from Gameday!\nAre you connected to the internet?", duration).show();
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void onClickTimedAlerts(View view) {
 | 
			
		||||
		int toastDuration = Toast.LENGTH_SHORT;
 | 
			
		||||
		if (((CheckBox) view).isChecked())
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,7 @@ import android.content.Context;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
 | 
			
		||||
import com.uncc.gameday.R;
 | 
			
		||||
import com.uncc.gameday.alerts.AlertFetcher;
 | 
			
		||||
 | 
			
		||||
public class Home extends MenuActivity {
 | 
			
		||||
	
 | 
			
		||||
@ -25,6 +26,6 @@ public class Home extends MenuActivity {
 | 
			
		||||
	private void getRecentAlerts() {
 | 
			
		||||
		// Responsible for discovering what the most recent alerts are and showing them.
 | 
			
		||||
		// Likely should be implemented by querying a local DB of alerts, grabbing recent 20.
 | 
			
		||||
		new Alerts().fetchAlerts();
 | 
			
		||||
		new AlertFetcher().fetchAlerts();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										24
									
								
								src/com/uncc/gameday/alerts/Alert.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/com/uncc/gameday/alerts/Alert.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
package com.uncc.gameday.alerts;
 | 
			
		||||
 | 
			
		||||
import java.util.Date;
 | 
			
		||||
 | 
			
		||||
public class Alert {
 | 
			
		||||
	
 | 
			
		||||
	private Date alarmDate;
 | 
			
		||||
	private String message;
 | 
			
		||||
	
 | 
			
		||||
	public Date getAlarmDate() {
 | 
			
		||||
		return alarmDate;
 | 
			
		||||
	}
 | 
			
		||||
	public void setAlarmDate(Date alarmDate) {
 | 
			
		||||
		this.alarmDate = alarmDate;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public String getMessage() {
 | 
			
		||||
		return message;
 | 
			
		||||
	}
 | 
			
		||||
	public void setMessage(String message) {
 | 
			
		||||
		this.message = message;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								src/com/uncc/gameday/alerts/AlertDB.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/com/uncc/gameday/alerts/AlertDB.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,43 @@
 | 
			
		||||
package com.uncc.gameday.alerts;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import android.database.sqlite.SQLiteDatabase;
 | 
			
		||||
 | 
			
		||||
import com.uncc.gameday.GameDay;
 | 
			
		||||
import com.uncc.gameday.R;
 | 
			
		||||
 | 
			
		||||
public class AlertDB {
 | 
			
		||||
	
 | 
			
		||||
	private SQLiteDatabase dbHandle;
 | 
			
		||||
	private SQLiteDatabase.CursorFactory factory;
 | 
			
		||||
	
 | 
			
		||||
	public AlertDB() {
 | 
			
		||||
		this.dbHandle = SQLiteDatabase.openOrCreateDatabase(GameDay.getAppContext().getString(R.string.db_path), factory);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void insertAlert(Alert alert) {
 | 
			
		||||
		// Add a new date to the database
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void insertAlertRead(Alert alert) {
 | 
			
		||||
		// Add a new date to the database that should not be alerted.
 | 
			
		||||
		// This way, you can display it in the recent alerts list, but
 | 
			
		||||
		// not show it to the user.
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void insertAlerts(List<Alert> alarmDates) {
 | 
			
		||||
		// Add multiple new dates to the database
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void insertAlertsRead(List<Alert> alarmDates) {
 | 
			
		||||
		// Add multiple new dates to the database, and mark them as read.
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public List<Alert> fetchUnreadAlerts() {
 | 
			
		||||
		// Get a list of all currently unread alerts
 | 
			
		||||
		
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										116
									
								
								src/com/uncc/gameday/alerts/AlertFetcher.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								src/com/uncc/gameday/alerts/AlertFetcher.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,116 @@
 | 
			
		||||
package com.uncc.gameday.alerts;
 | 
			
		||||
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import twitter4j.Paging;
 | 
			
		||||
import twitter4j.Status;
 | 
			
		||||
import twitter4j.Twitter;
 | 
			
		||||
import twitter4j.TwitterException;
 | 
			
		||||
import twitter4j.TwitterFactory;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
import com.uncc.gameday.GameDay;
 | 
			
		||||
import com.uncc.gameday.R;
 | 
			
		||||
 | 
			
		||||
public class AlertFetcher {
 | 
			
		||||
	// Class responsible for fetching all alerts as necessary.
 | 
			
		||||
	
 | 
			
		||||
	private int maxTweets = 5;
 | 
			
		||||
	
 | 
			
		||||
	public void fetchAlerts() {
 | 
			
		||||
		/* This method needs to be re-written to use better logic. */
 | 
			
		||||
		
 | 
			
		||||
		/*
 | 
			
		||||
		// Fetch all alerts. Responsible for discovering what sources need to be fetched.
 | 
			
		||||
		
 | 
			
		||||
		if (((CheckBox)findViewById(R.id.alerts_check_timed)).isChecked())
 | 
			
		||||
			// Fetch timed alerts
 | 
			
		||||
			this.fetchTimedAlerts();
 | 
			
		||||
		else if (((CheckBox)findViewById(R.id.alerts_check_organizations)).isChecked())
 | 
			
		||||
			// Fetch organization alerts
 | 
			
		||||
			this.fetchOrganizationAlerts();
 | 
			
		||||
		else if (((CheckBox)findViewById(R.id.alerts_check_university)).isChecked())
 | 
			
		||||
			// Fetch university alerts
 | 
			
		||||
			this.fetchUniversityAlerts();
 | 
			
		||||
		
 | 
			
		||||
		// And always fetch alerts made by us. Period.
 | 
			
		||||
		this.fetchGamedayAlerts();
 | 
			
		||||
		*/
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchTimedAlerts() {
 | 
			
		||||
		// Process the rules for all timed alerts.
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchOrganizationAlerts() {
 | 
			
		||||
		// Process fetching organization alerts (alerts retweeted by us)
 | 
			
		||||
		int duration = Toast.LENGTH_SHORT;
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			String handle = GameDay.getAppContext().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
 | 
			
		||||
			
 | 
			
		||||
		} catch (TwitterException e) {
 | 
			
		||||
			Toast.makeText(GameDay.getAppContext(),
 | 
			
		||||
					"Unable to fetch alerts for organizations!\nAre you connected to the internet?",
 | 
			
		||||
					duration).show();
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchUniversityAlerts() {
 | 
			
		||||
		// Process fetching university alerts
 | 
			
		||||
		int duration = Toast.LENGTH_SHORT;
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			String handle = GameDay.getAppContext().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
 | 
			
		||||
			
 | 
			
		||||
		} catch (TwitterException e) {
 | 
			
		||||
			Toast.makeText(GameDay.getAppContext(),
 | 
			
		||||
					"Unable to fetch alerts for the University!\nAre you connected to the internet?",
 | 
			
		||||
					duration).show();
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private void fetchGamedayAlerts() {
 | 
			
		||||
		// Process fetching alerts generated by staff of UNCCGameDay
 | 
			
		||||
		int duration = Toast.LENGTH_SHORT;
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			String handle = GameDay.getAppContext().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.
 | 
			
		||||
			
 | 
			
		||||
		} catch (TwitterException e) {
 | 
			
		||||
			Toast.makeText(GameDay.getAppContext(),
 | 
			
		||||
					"Unable to fetch alerts from Gameday!\nAre you connected to the internet?",
 | 
			
		||||
					duration).show();
 | 
			
		||||
			e.printStackTrace();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user