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:label="@string/app_name" >
<activity
android:name=".ExaltedDice"
android:name="GameListActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@@ -24,6 +24,8 @@
<activity android:name="com.TwentyCodes.android.exception.ExceptionReportActivity" >
</activity>
<activity android:name="ExaltedDice" >
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
@@ -32,4 +34,4 @@
<uses-permission android:name="android.permission.INTERNET" />
<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="email">twentycodes@gmail.com</string>
<string name="deleteing">Deleting…</string>
<string name="empty_msg">Click the plus button to start a new game</string>
</resources>

View File

@@ -34,48 +34,6 @@ import android.util.Log;
*/
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.
* @author ricky barrette
@@ -161,7 +119,7 @@ public class Database {
}).start();
}
}
private static final String TAG = "Database";
/**
* Parses a string boolean from the database
* @param bool
@@ -175,6 +133,48 @@ public class Database {
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
@@ -240,6 +240,14 @@ public class Database {
return ringerName;
}
/**
* Closes the database
* @author ricky barrette
*/
public void close() {
mDb.close();
}
/**
* Copies a file
* @param src file
@@ -490,31 +498,6 @@ public class Database {
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
* @param gameName
@@ -540,4 +523,29 @@ public class Database {
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.Vibrator;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@@ -24,7 +26,7 @@ import android.widget.Toast;
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 ArrayList<String> rollHistory = new ArrayList<String>();
@@ -34,6 +36,8 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
private int mD = 2;
private NumberPicker mNumberPicker;
private int mCurrentDie;
private NumberPicker mDPicker;
private Database mDb;
private static final int MENU_QUIT = Menu.FIRST;
private static final int MENU_CLEAR = Menu.FIRST + 1;
private static final String TAG = "ExaltedDice";
@@ -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
*
@@ -82,16 +96,18 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
listview = (ListView) findViewById(R.id.list);
listview.setOnItemClickListener(this);
NumberPicker mDPicker = (NumberPicker) findViewById(R.id.d_Picker);
mDPicker = (NumberPicker) findViewById(R.id.d_Picker);
mDPicker.setMinValue(0);
mDPicker.setMaxValue(DICE_VALUES.length -1);
mDPicker.setDisplayedValues(DICE_VALUES);
mDPicker.setOnValueChangedListener(this);
mDPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
mNumberPicker = (NumberPicker) findViewById(R.id.number_Picker);
mNumberPicker.setMaxValue(999);
mNumberPicker.setMinValue(1);
mNumberPicker.setOnValueChangedListener(this);
mNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
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
*
@@ -115,7 +141,25 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
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
* @author ricky barrette
*/
@@ -127,6 +171,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
}
}
/**
* handles menu selection
*
@@ -146,6 +191,22 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
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
* @author ricky barrette
@@ -153,29 +214,45 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
rollHistory = savedInstanceState.getStringArrayList("roll_history");
rolled = savedInstanceState.getIntegerArrayList("rolled");
listview.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row, rollHistory));
mDPicker.setValue(savedInstanceState.getInt("d"));
mNumberPicker.setValue(savedInstanceState.getInt("number"));
}
/**
* 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
*/
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putStringArrayList("roll_history", rollHistory);
savedInstanceState.putIntegerArrayList("rolled", rolled);
savedInstanceState.putInt("d", mDPicker.getValue());
savedInstanceState.putInt("number", mNumberPicker.getValue());
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
*
* @author ricky barrette 3-28-2010
@@ -198,7 +275,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
builder.show();
}
/**
/**
* returns a custom string containing dice rolls and number of successes
*
* @param int times
@@ -231,7 +308,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return resultsString.toString();
}
/**
/**
* Performs a dice roll
*
* @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));
}
/**
/**
* generates an array containing 10 sided dice rolls
*
* @param int times
@@ -263,7 +340,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return roll;
}
/**
/**
* counts each dice roll that is greater than or equal to 7 as a success. 10
* gets another success (for a total of 2)
*
@@ -283,7 +360,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
return intSuccesses;
}
/**
/**
* displays toast message with a long duration
*
* @param msg
@@ -295,7 +372,7 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
/**
/**
* starts Vibrator service and then vibrates for x milliseconds
*
* @param Long
@@ -314,17 +391,4 @@ public class ExaltedDice extends Activity implements OnClickListener, OnItemClic
*/
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();
}
}