Created GameListActivity to display a list of games.

Set GameListActivity as the main Activity.

Fixed ExaltedDice save instance state methods to restore the picker
values.

Sorted all classes
Change-Id: I6a8a724d831a977aeaff8816c5506246398617b7
This commit is contained in:
2012-02-04 02:34:10 -05:00
parent 157fd5f22d
commit 03d5bc1a77
6 changed files with 319 additions and 110 deletions

View File

@@ -9,7 +9,7 @@
android:icon="@drawable/icon" android:icon="@drawable/icon"
android:label="@string/app_name" > android:label="@string/app_name" >
<activity <activity
android:name=".ExaltedDice" android:name="GameListActivity"
android:label="@string/app_name" > android:label="@string/app_name" >
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@@ -24,6 +24,8 @@
<activity android:name="com.TwentyCodes.android.exception.ExceptionReportActivity" > <activity android:name="com.TwentyCodes.android.exception.ExceptionReportActivity" >
</activity> </activity>
<activity android:name="ExaltedDice" >
</activity>
</application> </application>
<uses-sdk android:minSdkVersion="3" /> <uses-sdk android:minSdkVersion="3" />
@@ -32,4 +34,4 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.READ_LOGS" />
</manifest><!-- android:screenOrientation="portrait" --> </manifest> <!-- android:screenOrientation="portrait" -->

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/new_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:drawable/ic_menu_add" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/new_game_button" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/empty_msg"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

View File

@@ -5,5 +5,6 @@
<string name="rolled">Rolled: </string> <string name="rolled">Rolled: </string>
<string name="email">twentycodes@gmail.com</string> <string name="email">twentycodes@gmail.com</string>
<string name="deleteing">Deleting…</string> <string name="deleteing">Deleting…</string>
<string name="empty_msg">Click the plus button to start a new game</string>
</resources> </resources>

View File

@@ -34,48 +34,6 @@ import android.util.Log;
*/ */
public class Database { public class Database {
private static final String TAG = "Database";
private Context mContext;
private SQLiteDatabase mDb;
public boolean isUpgrading = false;
private DatabaseListener mListener;
/**
* database version. If this is increased, the database will be upgraded the next time it connects
*/
private final int DATABASE_VERSION = 1;
/**
* database file name
*/
private final String DATABASE_NAME = "history.db";
/**
* database table for games
*/
private final String GAME_NAME_TABLE = "game_name";
/**
* Database table of history
*/
private final String GAME_HISTORY_TABLE = "game_history";
/*
* Database keys
*/
private static final String KEY = "key";
private static final String KEY_VALUE = "value";
/*
* database value keys
*/
public final static String KEY_NAME = "name";
public final static String KEY_D_TYPE = "d_type";
public final static String KEY_NUMBER = "number";
public final static String KEY_LOG = "log";
public final static String KEY_ROLL_ID = "log_number";
/** /**
* A helper class to manage database creation and version management. * A helper class to manage database creation and version management.
* @author ricky barrette * @author ricky barrette
@@ -161,7 +119,7 @@ public class Database {
}).start(); }).start();
} }
} }
private static final String TAG = "Database";
/** /**
* Parses a string boolean from the database * Parses a string boolean from the database
* @param bool * @param bool
@@ -175,6 +133,48 @@ public class Database {
return false; return false;
} }
} }
private Context mContext;
private SQLiteDatabase mDb;
public boolean isUpgrading = false;
private DatabaseListener mListener;
/**
* database version. If this is increased, the database will be upgraded the next time it connects
*/
private final int DATABASE_VERSION = 1;
/**
* database file name
*/
private final String DATABASE_NAME = "history.db";
/**
* database table for games
*/
private final String GAME_NAME_TABLE = "game_name";
/**
* Database table of history
*/
private final String GAME_HISTORY_TABLE = "game_history";
/*
* Database keys
*/
private static final String KEY = "key";
private static final String KEY_VALUE = "value";
/*
* database value keys
*/
public final static String KEY_NAME = "name";
public final static String KEY_D_TYPE = "d_type";
public final static String KEY_NUMBER = "number";
public final static String KEY_LOG = "log";
public final static String KEY_ROLL_ID = "log_number";
/** /**
* Creates a new Database * Creates a new Database
@@ -240,6 +240,14 @@ public class Database {
return ringerName; return ringerName;
} }
/**
* Closes the database
* @author ricky barrette
*/
public void close() {
mDb.close();
}
/** /**
* Copies a file * Copies a file
* @param src file * @param src file
@@ -298,7 +306,7 @@ public class Database {
} }
}).start(); }).start();
} }
/** /**
* @return a cursor containing all game names * @return a cursor containing all game names
* @author ricky barrette * @author ricky barrette
@@ -325,7 +333,7 @@ public class Database {
} }
return list; return list;
} }
/** /**
* gets a game name from a row id; * gets a game name from a row id;
* @param id * @param id
@@ -335,7 +343,7 @@ public class Database {
public Cursor getGameFromId(long id) { public Cursor getGameFromId(long id) {
return this.mDb.query(GAME_NAME_TABLE, new String[]{ KEY_NAME }, "id = "+id, null, null, null, null); return this.mDb.query(GAME_NAME_TABLE, new String[]{ KEY_NAME }, "id = "+id, null, null, null, null);
} }
/** /**
* gets a games's histrory info from the supplied ringer name * gets a games's histrory info from the supplied ringer name
* @param gameName * @param gameName
@@ -412,7 +420,7 @@ public class Database {
mDb.insert(GAME_HISTORY_TABLE, null, values); mDb.insert(GAME_HISTORY_TABLE, null, values);
} }
} }
/** /**
* Restores the database from external storage * Restores the database from external storage
* @return true if successful * @return true if successful
@@ -489,31 +497,6 @@ public class Database {
*/ */
mDb.update(GAME_NAME_TABLE, game, "id" + "= "+ id, null); mDb.update(GAME_NAME_TABLE, game, "id" + "= "+ id, null);
} }
/**
* Updates the row ids after a row is deleted
* @param id of the row to start with
* @author ricky barrette
*/
private void updateRowIds(long id) {
long currentRow;
ContentValues values = new ContentValues();
Cursor cursor = this.mDb.query(GAME_NAME_TABLE, new String[] { "id" },null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
currentRow = cursor.getLong(0);
if(currentRow == id){
id++;
values.clear();
values.put("id", currentRow -1);
mDb.update(GAME_NAME_TABLE, values, "id" + "= "+ currentRow, null);
}
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
/** /**
* Updates all the roll ids after a row is deleted * Updates all the roll ids after a row is deleted
@@ -540,4 +523,29 @@ public class Database {
cursor.close(); cursor.close();
} }
} }
/**
* Updates the row ids after a row is deleted
* @param id of the row to start with
* @author ricky barrette
*/
private void updateRowIds(long id) {
long currentRow;
ContentValues values = new ContentValues();
Cursor cursor = this.mDb.query(GAME_NAME_TABLE, new String[] { "id" },null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
currentRow = cursor.getLong(0);
if(currentRow == id){
id++;
values.clear();
values.put("id", currentRow -1);
mDb.update(GAME_NAME_TABLE, values, "id" + "= "+ currentRow, null);
}
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
} }

View File

@@ -10,6 +10,8 @@ import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.os.Vibrator; import android.os.Vibrator;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@@ -24,7 +26,7 @@ import android.widget.Toast;
import com.TwentyCodes.android.exception.ExceptionHandler; import com.TwentyCodes.android.exception.ExceptionHandler;
public class ExaltedDice extends Activity implements OnClickListener, OnItemClickListener, OnValueChangeListener { public class ExaltedDice extends Activity implements OnClickListener, OnItemClickListener, OnValueChangeListener, DatabaseListener {
private ListView listview; private ListView listview;
private ArrayList<String> rollHistory = new ArrayList<String>(); private ArrayList<String> rollHistory = new ArrayList<String>();
@@ -34,6 +36,8 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
private int mD = 2; private int mD = 2;
private NumberPicker mNumberPicker; private NumberPicker mNumberPicker;
private int mCurrentDie; private int mCurrentDie;
private NumberPicker mDPicker;
private Database mDb;
private static final int MENU_QUIT = Menu.FIRST; private static final int MENU_QUIT = Menu.FIRST;
private static final int MENU_CLEAR = Menu.FIRST + 1; private static final int MENU_CLEAR = Menu.FIRST + 1;
private static final String TAG = "ExaltedDice"; private static final String TAG = "ExaltedDice";
@@ -49,7 +53,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
rolled.clear(); rolled.clear();
listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row, rollHistory)); listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row, rollHistory));
} }
/** /**
* also implemented OnClickListener * also implemented OnClickListener
* *
@@ -66,6 +70,16 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
} }
/**
* (non-Javadoc)
* @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onContextItemSelected(item);
}
/** /**
* Called when the activity is first created. starts gui and sets up buttons * Called when the activity is first created. starts gui and sets up buttons
* *
@@ -82,16 +96,18 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
listview = (ListView) findViewById(R.id.list); listview = (ListView) findViewById(R.id.list);
listview.setOnItemClickListener(this); listview.setOnItemClickListener(this);
NumberPicker mDPicker = (NumberPicker) findViewById(R.id.d_Picker); mDPicker = (NumberPicker) findViewById(R.id.d_Picker);
mDPicker.setMinValue(0); mDPicker.setMinValue(0);
mDPicker.setMaxValue(DICE_VALUES.length -1); mDPicker.setMaxValue(DICE_VALUES.length -1);
mDPicker.setDisplayedValues(DICE_VALUES); mDPicker.setDisplayedValues(DICE_VALUES);
mDPicker.setOnValueChangedListener(this); mDPicker.setOnValueChangedListener(this);
mDPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
mNumberPicker = (NumberPicker) findViewById(R.id.number_Picker); mNumberPicker = (NumberPicker) findViewById(R.id.number_Picker);
mNumberPicker.setMaxValue(999); mNumberPicker.setMaxValue(999);
mNumberPicker.setMinValue(1); mNumberPicker.setMinValue(1);
mNumberPicker.setOnValueChangedListener(this); mNumberPicker.setOnValueChangedListener(this);
mNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
findViewById(R.id.roll_button).setOnClickListener(this); findViewById(R.id.roll_button).setOnClickListener(this);
@@ -104,6 +120,16 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
} }
/**
* (non-Javadoc)
* @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
}
/** /**
* creates a menu with a quit option * creates a menu with a quit option
* *
@@ -115,7 +141,25 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return true; return true;
} }
/** @Override
public void onDatabaseUpgrade() {
// TODO Auto-generated method stub
}
@Override
public void onDatabaseUpgradeComplete() {
// TODO Auto-generated method stub
}
@Override
public void onDeletionComplete() {
// TODO Auto-generated method stub
}
/**
* rolls same amount of dice as previous roll * rolls same amount of dice as previous roll
* @author ricky barrette * @author ricky barrette
*/ */
@@ -127,6 +171,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
} }
} }
/** /**
* handles menu selection * handles menu selection
* *
@@ -146,6 +191,22 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return false; return false;
} }
/**
* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
mDb.close();
super.onPause();
}
@Override
public void onRestoreComplete() {
// TODO Auto-generated method stub
}
/** /**
* resorts application state after rotation * resorts application state after rotation
* @author ricky barrette * @author ricky barrette
@@ -153,29 +214,45 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
@Override @Override
public void onRestoreInstanceState(Bundle savedInstanceState) { public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState. mDPicker.setValue(savedInstanceState.getInt("d"));
// This bundle has also been passed to onCreate. mNumberPicker.setValue(savedInstanceState.getInt("number"));
rollHistory = savedInstanceState.getStringArrayList("roll_history");
rolled = savedInstanceState.getIntegerArrayList("rolled");
listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row, rollHistory));
} }
/** /**
* saves application state before rotatoin * (non-Javadoc)
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
mDb = new Database(this, this);
super.onResume();
}
/**
* saves application state before rotation
* @author ricky barrette * @author ricky barrette
*/ */
@Override @Override
public void onSaveInstanceState(Bundle savedInstanceState) { public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState. savedInstanceState.putInt("d", mDPicker.getValue());
// This bundle will be passed to onCreate if the process is savedInstanceState.putInt("number", mNumberPicker.getValue());
// killed and restarted.
savedInstanceState.putStringArrayList("roll_history", rollHistory);
savedInstanceState.putIntegerArrayList("rolled", rolled);
super.onSaveInstanceState(savedInstanceState); super.onSaveInstanceState(savedInstanceState);
} }
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
switch(picker.getId()){
case R.id.d_Picker:
mD = Integer.parseInt(DICE_VALUES[newVal].substring(1));
mCurrentDie = newVal;
break;
case R.id.number_Picker:
mRolls = newVal;
break;
}
}
/** /**
* displays a quit dialog * displays a quit dialog
* *
* @author ricky barrette 3-28-2010 * @author ricky barrette 3-28-2010
@@ -198,7 +275,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
builder.show(); builder.show();
} }
/** /**
* returns a custom string containing dice rolls and number of successes * returns a custom string containing dice rolls and number of successes
* *
* @param int times * @param int times
@@ -231,7 +308,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return resultsString.toString(); return resultsString.toString();
} }
/** /**
* Performs a dice roll * Performs a dice roll
* *
* @author ricky barrette * @author ricky barrette
@@ -246,7 +323,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row, rollHistory)); listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row, rollHistory));
} }
/** /**
* generates an array containing 10 sided dice rolls * generates an array containing 10 sided dice rolls
* *
* @param int times * @param int times
@@ -263,7 +340,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return roll; return roll;
} }
/** /**
* counts each dice roll that is greater than or equal to 7 as a success. 10 * counts each dice roll that is greater than or equal to 7 as a success. 10
* gets another success (for a total of 2) * gets another success (for a total of 2)
* *
@@ -283,7 +360,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return intSuccesses; return intSuccesses;
} }
/** /**
* displays toast message with a long duration * displays toast message with a long duration
* *
* @param msg * @param msg
@@ -295,7 +372,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
} }
/** /**
* starts Vibrator service and then vibrates for x milliseconds * starts Vibrator service and then vibrates for x milliseconds
* *
* @param Long * @param Long
@@ -314,17 +391,4 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
*/ */
vib.vibrate(milliseconds); vib.vibrate(milliseconds);
} }
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
switch(picker.getId()){
case R.id.d_Picker:
mD = Integer.parseInt(DICE_VALUES[newVal].substring(1));
mCurrentDie = newVal;
break;
case R.id.number_Picker:
mRolls = newVal;
break;
}
}
} }

View File

@@ -0,0 +1,106 @@
/**
* GameListActivity.java
* @date Feb 4, 2012
* @author ricky barrette
* @author Twenty Codes, LLC
*/
package com.TwentyCode.android.ExaltedDice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* This activity will be used to display a list of games to the user.
*
* @author ricky barrette
*/
public class GameListActivity extends Activity implements OnClickListener, DatabaseListener {
private ListView mList;
private Database mDb;
@Override
public void onClick(View v) {
startActivity(new Intent(this, ExaltedDice.class));
}
/**
* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_list);
findViewById(R.id.new_game_button).setOnClickListener(this);
mList = (ListView) findViewById(android.R.id.list);
registerForContextMenu(mList);
}
/**
* (non-Javadoc)
* @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
/*
* TODO
* delete game
* rename game
*/
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public void onDatabaseUpgrade() {
// TODO Auto-generated method stub
}
@Override
public void onDatabaseUpgradeComplete() {
// TODO Auto-generated method stub
}
@Override
public void onDeletionComplete() {
// TODO Auto-generated method stub
}
/**
* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
mDb.close();
super.onPause();
}
@Override
public void onRestoreComplete() {
// TODO Auto-generated method stub
}
/**
* (non-Javadoc)
* @see android.app.Activity#onResume()
*/
@Override
protected void onResume() {
mDb = new Database(this, this);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDb.getAllGameTitles()));
super.onResume();
}
}