UNCCGameDay/src/com/uncc/gameday/activities/Home.java

114 lines
3.6 KiB
Java
Raw Normal View History

package com.uncc.gameday.activities;
2013-11-21 14:03:23 -05:00
import java.util.List;
2013-10-09 20:20:04 -04:00
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
2013-10-09 19:39:20 -04:00
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
2013-11-21 14:55:43 -05:00
import android.view.View;
2013-11-21 15:52:23 -05:00
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
2013-11-21 14:03:23 -05:00
import android.widget.ArrayAdapter;
2013-11-21 14:55:43 -05:00
import android.widget.Button;
2013-11-21 14:03:23 -05:00
import android.widget.ListView;
import com.uncc.gameday.R;
2013-11-21 14:03:23 -05:00
import com.uncc.gameday.alerts.Alert;
import com.uncc.gameday.alerts.AlertDB;
2013-10-09 20:20:04 -04:00
import com.uncc.gameday.alerts.AlertService;
// TODO: Auto-generated Javadoc
/**
* The Class Home.
*/
public class Home extends MenuActivity {
/** The alarm rate. */
2013-10-09 20:20:04 -04:00
private final long alarmRate = 300000; // 5 Minutes
/* (non-Javadoc)
* @see com.uncc.gameday.activities.MenuActivity#onCreate(android.os.Bundle)
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
2013-11-21 14:55:43 -05:00
//ClearAllAlerts button
2013-12-02 03:34:15 -05:00
//Clears any text in ListView
2013-11-21 14:55:43 -05:00
final Button button = (Button) findViewById(R.id.clearAlertsButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
2013-12-02 03:34:15 -05:00
ListView listView = (ListView)findViewById(R.id.alertsListView);
listView.setAdapter(null);
}
});
//Refresh Alerts button
//fetches most recent alerts
//displays them on listview
final Button button2 = (Button) findViewById(R.id.refreshAlertsButton);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
displayList();
2013-11-21 14:55:43 -05:00
}
});
2013-10-30 14:15:38 -04:00
2013-10-09 20:20:04 -04:00
// Start up the AlarmManager to fetch alerts in the background
AlarmManager am = (AlarmManager) this.getSystemService(ALARM_SERVICE);
Intent alertFetcher = new Intent(this, AlertService.class);
PendingIntent pendingAlertFetcher = PendingIntent.getService(this, 0, alertFetcher, 0);
2013-10-09 20:20:04 -04:00
// Cancel any previous alarm managers, and start the new one
am.cancel(pendingAlertFetcher);
am.setRepeating(0, this.alarmRate, this.alarmRate, pendingAlertFetcher);
// Double check if we need to do any first-run code
this.onFirstRun();
2013-11-21 14:03:23 -05:00
this.displayList();
}
2013-11-21 15:52:23 -05:00
//displays all alerts in database, regardless of being shown or not
2013-11-21 14:03:23 -05:00
public void displayList()
{
2013-12-02 03:34:15 -05:00
final List<Alert> alerts = new AlertDB(this).fetchAll();
2013-11-21 15:52:23 -05:00
final ListView listView = (ListView)findViewById(R.id.alertsListView);
2013-12-02 03:34:15 -05:00
2013-11-21 15:52:23 -05:00
final ArrayAdapter<Alert> adapter =
new ArrayAdapter<Alert>(this,android.R.layout.simple_list_item_1, alerts);
2013-11-21 14:03:23 -05:00
listView.setAdapter(adapter);
2013-11-21 15:52:23 -05:00
//tap to delete alert from list and database
2013-11-21 15:52:23 -05:00
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
deleteAlert(alerts.get(position));
2013-12-02 03:34:15 -05:00
alerts.remove(alerts.get(position));
listView.setAdapter(adapter);
2013-11-21 15:52:23 -05:00
}
});
2013-10-09 19:39:20 -04:00
}
2013-11-21 15:52:23 -05:00
//function to delete alert from database
public void deleteAlert(Alert alert)
{
new AlertDB(this).deleteAlert(alert);
}
/**
* On first run.
*/
protected void onFirstRun() {
2013-10-09 19:39:20 -04:00
SharedPreferences settings = this.getPreferences(MODE_PRIVATE);
if (settings.getBoolean("FIRST_RUN", true)) {
// First run code
Editor editor = settings.edit();
editor.putBoolean("FIRST_RUN", false);
editor.commit();
}
}
}