Skip to content
Browse files

Changes to download games from the internet, temporarily removed rost…

…er tab

currently something is broken.
  • Loading branch information...
1 parent 3262c4f commit 1936066c732542312c084ed1efaa916adeee2875 @lsiegert committed Oct 21, 2011
View
4 AndroidManifest.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lsiegert"
- android:versionCode="1"
- android:versionName="1">
+ android:versionCode="2"
+ android:versionName="2">
<application android:icon="@drawable/icon_terrible" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".NUHockey"
android:label="@string/app_name"
View
4 src/com/lsiegert/DatabaseHelper.java
@@ -44,7 +44,7 @@ public void createDatabase() throws IOException{
if(dbExists){
//do nothing - database already exists
}else{
- db_Read = this.getReadableDatabase();
+ db_Read = this.getReadableDatabase();
db_Read.close();
try {
@@ -189,7 +189,7 @@ public int getNumOfShutouts() {
return myDb.rawQuery(query, null).getCount();
}
- public int getNumOfWinsAtAgganis() {
+ public int getNumOfWinsAtagganis() {
String query ="select _id from Games where attended=1 and " +
"nuscore>oppscore and opponent='boston university' and location='away'";
return myDb.rawQuery(query, null).getCount();
View
135 src/com/lsiegert/GamesDbAdapter.java
@@ -0,0 +1,135 @@
+package com.lsiegert;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.text.format.DateFormat;
+
+public class GamesDbAdapter {
+ public static final String ID = "_id";
+ public static final String LOCATION = "location";
+ public static final String OPPSCORE = "oppscore";
+ public static final String OPPONENT = "opponent";
+ public static final String SEASON = "season";
+ public static final String NUSCORE = "nuscore";
+ public static final String DATE = "date";
+ public static final String UPDATED ="updated";
+
+ public static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+ private final Context mCtx;
+ private SQLiteOpenHelper mDbHelper;
+ private SQLiteDatabase mDb;
+
+ public GamesDbAdapter(Context ctx) {
+ mCtx = ctx;
+ }
+
+ public GamesDbAdapter open() throws SQLException {
+ mDbHelper = new DatabaseHelper(mCtx);
+ mDb = mDbHelper.getWritableDatabase();
+ return this;
+ }
+
+ public void close() {
+ mDbHelper.close();
+ }
+
+ public long createGame(int id, String date, String season, String opponent, int nuscore, int oppscore, String location) {
+ ContentValues initialValues = new ContentValues();
+ initialValues.put(ID, id);
+ initialValues.put(DATE, date);
+ initialValues.put(SEASON, season);
+ initialValues.put(OPPONENT, opponent);
+ initialValues.put(NUSCORE, nuscore);
+ initialValues.put(OPPSCORE, oppscore);
+ initialValues.put(LOCATION, location);
+
+ return mDb.insert("games", null, initialValues);
+ }
+
+ public boolean updateGame(int id, String date, String season, String opponent, int nuscore, int oppscore, String location) {
+ ContentValues values = new ContentValues();
+ values.put(ID, id);
+ values.put(DATE, date);
+ values.put(SEASON, season);
+ values.put(OPPONENT, opponent);
+ values.put(NUSCORE, nuscore);
+ values.put(OPPSCORE, oppscore);
+ values.put(LOCATION, location);
+
+ return mDb.update("games", values, ID + "=" + id, null) > 0;
+ }
+
+ public boolean deleteGame(int id) {
+ return mDb.delete("games", ID + "=" + id, null) > 0;
+ }
+
+ public Date getLastUpdated() throws ParseException{
+ Cursor c = mDb.query("updated", new String[] { UPDATED }, null, null, null, null, null);
+ if (c != null) {
+ c.moveToFirst();
+ String s = c.getString(0);
+ return df.parse(s);
+ } else {
+ return new Date(0);
+ }
+ }
+
+ public boolean saveLastUpdated(Date date){
+ ContentValues values = new ContentValues();
+ values.put(ID, 1);
+ values.put(UPDATED, df.format(date));
+
+ return mDb.replace("updates", UPDATED, values) > 0;
+ }
+
+ private static class DatabaseHelper extends SQLiteOpenHelper {
+ private static final String DATABASE_NAME = "gamedata";
+ private static final int DATABASE_VERSION = 1;
+
+ private static final String GAMES_TABLE_CREATE =
+ "create table games(" +
+ ID + " integer primary key, " +
+ DATE + " text not null," +
+ SEASON + " text not null" +
+ OPPONENT + " text not null" +
+ NUSCORE + " integer" +
+ OPPSCORE + " integer" +
+ LOCATION + " text not null" +
+ ");";
+
+ private static final String UPDATED_TABLE_CREATE =
+ "create table updated(" +
+ ID + " integer primary key autoincrement, " +
+ UPDATED + " text not null," +
+ ");";
+
+
+
+ public DatabaseHelper(Context context) {
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ }
+
+ @Override
+ public void onCreate(SQLiteDatabase db) {
+ db.execSQL(GAMES_TABLE_CREATE);
+ db.execSQL(UPDATED_TABLE_CREATE);
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
+ db.execSQL("DROP TABLE IF EXISTS games");
+ db.execSQL("DROP TABLE IF EXISTS updated");
+ onCreate(db);
+ }
+
+ }
+}
View
234 src/com/lsiegert/NUHockey.java
@@ -1,61 +1,195 @@
package com.lsiegert;
-import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URI;
+import java.util.Date;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
public class NUHockey extends TabActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- DatabaseHelper myDbHelper = new DatabaseHelper(this);
-
- try {
- myDbHelper.createDatabase();
- } catch (IOException ioe) {
- throw new Error("Unable to create database");
- }
-
- Resources res = getResources();
- TabHost tabHost = getTabHost();
- TabHost.TabSpec spec;
- Intent intent;
-
- intent = new Intent().setClass(this, RosterActivity.class);
-
- spec = tabHost.newTabSpec("roster").setIndicator("Roster",
- res.getDrawable(R.drawable.ic_tab_roster))
- .setContent(intent);
- tabHost.addTab(spec);
-
- intent = new Intent().setClass(this, RSSActivity.class);
-
- spec = tabHost.newTabSpec("rss").setIndicator("RSS",
- res.getDrawable(R.drawable.ic_tab_rss))
- .setContent(intent);
- tabHost.addTab(spec);
-
- intent = new Intent().setClass(this, ScheduleActivity.class);
-
- spec = tabHost.newTabSpec("schedule").setIndicator("Schedule",
- res.getDrawable(R.drawable.ic_tab_schedule))
- .setContent(intent);
- tabHost.addTab(spec);
-
- intent = new Intent().setClass(this, StatsActivity.class);
-
- spec = tabHost.newTabSpec("stats").setIndicator("Your Stats",
- res.getDrawable(R.drawable.ic_tab_stats))
- .setContent(intent);
- tabHost.addTab(spec);
-
- tabHost.setCurrentTab(1);
- }
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+
+ // Check here for any updates
+ try {
+ checkForUpdates();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ Resources res = getResources();
+ TabHost tabHost = getTabHost();
+ TabHost.TabSpec spec;
+ Intent intent;
+
+// intent = new Intent().setClass(this, RosterActivity.class);
+//
+// spec = tabHost.newTabSpec("roster").setIndicator("Roster",
+// res.getDrawable(R.drawable.ic_tab_roster))
+// .setContent(intent);
+// tabHost.addTab(spec);
+
+ intent = new Intent().setClass(this, RSSActivity.class);
+
+ spec = tabHost.newTabSpec("rss").setIndicator("RSS",
+ res.getDrawable(R.drawable.ic_tab_rss))
+ .setContent(intent);
+ tabHost.addTab(spec);
+
+ intent = new Intent().setClass(this, ScheduleActivity.class);
+
+ spec = tabHost.newTabSpec("schedule").setIndicator("Schedule",
+ res.getDrawable(R.drawable.ic_tab_schedule))
+ .setContent(intent);
+ tabHost.addTab(spec);
+
+ intent = new Intent().setClass(this, StatsActivity.class);
+
+ spec = tabHost.newTabSpec("stats").setIndicator("Your Stats",
+ res.getDrawable(R.drawable.ic_tab_stats))
+ .setContent(intent);
+ tabHost.addTab(spec);
+
+ tabHost.setCurrentTab(1);
+ }
+
+ private void checkForUpdates() throws Exception {
+
+ // First we check for any game updates
+ checkForGameUpdates();
+
+ // Then check for roster updates
+ // checkForRosterUpdates();
+ }
+
+ private void checkForGameUpdates() throws Exception {
+
+ GamesDbAdapter gamesDba = new GamesDbAdapter(this);
+ gamesDba.open();
+
+ // First, let's look and see how long ago we last checked
+ // If it was more than 3 days ago, let's fetch updates. Otherwise, finish.
+
+
+ // If fetching, let's tell the user we are doing something. Show a waiting thing.
+ ProgressDialog dialog = ProgressDialog.show(NUHockey.this, "",
+ "Fetching game updates...", true);
+
+ // Let's get the list of updates from our server
+ List<Map<String,String>> updates = fetchGameUpdates(gamesDba.getLastUpdated());;
+
+ // Once we have them, remember the exact time.
+ Date now = new Date();
+
+ // For each update, determine what the hell it wants us to do and apply it to the gamesDba
+ for (int i = 0; i < updates.size(); i++) {
+ Map<String,String> game = updates.get(i);
+ String op = game.get("operation");
+ int id = Integer.parseInt(game.get("id"));
+ String date = game.get("date");
+ String opponent = game.get("opponent");
+ String season = game.get("season");
+ int nuscore = Integer.parseInt(game.get("nuscore"));
+ int oppscore = Integer.parseInt(game.get("oppscore"));
+ String location = game.get("location");
+
+ if (op=="new") {
+ gamesDba.createGame(id, date, season, opponent, nuscore, oppscore, location);
+ } else if (op=="update") {
+ // update game
+ gamesDba.updateGame(id, date, season, opponent, nuscore, oppscore, location);
+ } else if (op=="deleted") {
+ gamesDba.deleteGame(id);
+ }
+ }
+
+ // When finished, this was the last time we updated. Save.
+ gamesDba.saveLastUpdated(now);
+ gamesDba.close();
+ dialog.dismiss();
+ }
+
+ private List<Map<String,String>> fetchGameUpdates(Date lastChecked) throws Exception {
+ BufferedReader in = null;
+ SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
+ List<Map<String,String>> updates = new ArrayList<Map<String,String>>();
+ String date = df.format(lastChecked);
+
+ // Make some sort of http request
+ HttpClient client = new DefaultHttpClient();
+ HttpGet request = new HttpGet();
+ URI uri = new URI("http://morning-planet-4215.heroku.com/gameupdates/" + date + ".txt");
+ request.setURI(uri);
+ HttpResponse response = client.execute(request);
+
+ InputStream content = response.getEntity().getContent();
+ in = new BufferedReader(new InputStreamReader(content));
+
+ String line = "";
+
+ // We are going to get some sort of lines. For each line, create a Map.
+ while ((line = in.readLine()) != null) {
+ Map<String,String> m = new HashMap<String,String>();
+ String[] parts = line.split(",");
+ m.put("operation", parts[0]);
+ m.put("id", parts[1]);
+ m.put("date", parts[2]);
+ m.put("opponent", parts[3]);
+ m.put("season", parts[4]);
+ m.put("nuscore", parts[5]);
+ m.put("oppscore", parts[6]);
+ m.put("location", parts[7]);
+ updates.add(m);
+ }
+ // Return a list of Maps that can be used by the methods of gamesDba
+ return updates;
+ }
+
+ private void checkForRosterUpdates() {
+
+ // RosterDbAdapter rosterDba = new RosterDbAdapter(this);
+ // rosterDba.open();
+
+ // When was the last time we checked? If more than 3 days, fetch updates. Otherwise finish.
+
+ // Tell the user we are doing something with a wait thing.
+
+ // Get the listof updates from somewhere
+
+ // Remove all data from the roster database. For each update, put it into the database.
+
+ // When finished, save last updated time.
+
+ // rosterDba.close();
+ }
+
+ private List<Map<String,String>> fetchRosterUpdates() {
+
+ // Make a http request for the html page
+
+ // Parse the html and pull out the roster info
+
+ // For each roster entry, return a Map.
+ return null;
+ }
}
View
2 src/com/lsiegert/StatsActivity.java
@@ -38,7 +38,7 @@ public void onResume() {
String homeRecord = dbHelper.getRecordByLocation("home");
String awayRecord = dbHelper.getRecordByLocation("away");
String neutralRecord = dbHelper.getRecordByLocation("neutral");
- int winsAtAgganis = dbHelper.getNumOfWinsAtAgganis();
+ int winsAtAgganis = dbHelper.getNumOfWinsAtagganis();
int numTeams = dbHelper.getNumOfTeams();
int goals = dbHelper.getNumOfGoals();
int shutouts = dbHelper.getNumOfShutouts();

0 comments on commit 1936066

Please sign in to comment.
Something went wrong with that request. Please try again.