mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2024-12-28 09:58:30 +01:00
synchronize saveSummary and getSummary. They can be called from
different threads and that may be the cause of a few crashes I've seen and had reported.
This commit is contained in:
parent
ca2aa982a7
commit
391982f16f
1 changed files with 71 additions and 67 deletions
|
@ -38,91 +38,95 @@ public class DBUtils {
|
|||
public static GameSummary getSummary( Context context, String file )
|
||||
{
|
||||
initDB( context );
|
||||
|
||||
GameSummary summary = new GameSummary();
|
||||
SQLiteDatabase db = m_dbHelper.getReadableDatabase();
|
||||
|
||||
String[] columns = { DBHelper.NUM_MOVES, DBHelper.GAME_OVER,
|
||||
DBHelper.CONTYPE, DBHelper.ROOMNAME,
|
||||
DBHelper.SMSPHONE, DBHelper.SCORES
|
||||
};
|
||||
String selection = DBHelper.FILE_NAME + "=\"" + file + "\"";
|
||||
synchronized( m_dbHelper ) {
|
||||
SQLiteDatabase db = m_dbHelper.getReadableDatabase();
|
||||
String[] columns = { DBHelper.NUM_MOVES, DBHelper.GAME_OVER,
|
||||
DBHelper.CONTYPE, DBHelper.ROOMNAME,
|
||||
DBHelper.SMSPHONE, DBHelper.SCORES
|
||||
};
|
||||
String selection = DBHelper.FILE_NAME + "=\"" + file + "\"";
|
||||
|
||||
Cursor cursor = db.query( DBHelper.TABLE_NAME, columns, selection,
|
||||
null, null, null, null );
|
||||
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
|
||||
summary = new GameSummary();
|
||||
summary.nMoves = cursor.getInt(cursor.
|
||||
getColumnIndex(DBHelper.NUM_MOVES));
|
||||
int tmp = cursor.getInt(cursor.
|
||||
getColumnIndex(DBHelper.GAME_OVER));
|
||||
summary.gameOver = tmp == 0 ? false : true;
|
||||
Cursor cursor = db.query( DBHelper.TABLE_NAME, columns, selection,
|
||||
null, null, null, null );
|
||||
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
|
||||
summary = new GameSummary();
|
||||
summary.nMoves = cursor.getInt(cursor.
|
||||
getColumnIndex(DBHelper.NUM_MOVES));
|
||||
int tmp = cursor.getInt(cursor.
|
||||
getColumnIndex(DBHelper.GAME_OVER));
|
||||
summary.gameOver = tmp == 0 ? false : true;
|
||||
|
||||
String scoresStr = cursor.getString( cursor.
|
||||
getColumnIndex(DBHelper.SCORES));
|
||||
StringTokenizer st = new StringTokenizer( scoresStr );
|
||||
int[] scores = new int[st.countTokens()];
|
||||
for ( int ii = 0; ii < scores.length; ++ii ) {
|
||||
Assert.assertTrue( st.hasMoreTokens() );
|
||||
String token = st.nextToken();
|
||||
scores[ii] = Integer.parseInt( token );
|
||||
}
|
||||
summary.scores = scores;
|
||||
|
||||
int col = cursor.getColumnIndex( DBHelper.CONTYPE );
|
||||
if ( col >= 0 ) {
|
||||
tmp = cursor.getInt( col );
|
||||
summary.conType = CommsAddrRec.CommsConnType.values()[tmp];
|
||||
col = cursor.getColumnIndex( DBHelper.ROOMNAME );
|
||||
if ( col >= 0 ) {
|
||||
summary.roomName = cursor.getString( col );
|
||||
String scoresStr =
|
||||
cursor.getString( cursor.getColumnIndex(DBHelper.SCORES));
|
||||
StringTokenizer st = new StringTokenizer( scoresStr );
|
||||
int[] scores = new int[st.countTokens()];
|
||||
for ( int ii = 0; ii < scores.length; ++ii ) {
|
||||
Assert.assertTrue( st.hasMoreTokens() );
|
||||
String token = st.nextToken();
|
||||
scores[ii] = Integer.parseInt( token );
|
||||
}
|
||||
col = cursor.getColumnIndex( DBHelper.SMSPHONE );
|
||||
summary.scores = scores;
|
||||
|
||||
int col = cursor.getColumnIndex( DBHelper.CONTYPE );
|
||||
if ( col >= 0 ) {
|
||||
summary.smsPhone = cursor.getString( col );
|
||||
tmp = cursor.getInt( col );
|
||||
summary.conType = CommsAddrRec.CommsConnType.values()[tmp];
|
||||
col = cursor.getColumnIndex( DBHelper.ROOMNAME );
|
||||
if ( col >= 0 ) {
|
||||
summary.roomName = cursor.getString( col );
|
||||
}
|
||||
col = cursor.getColumnIndex( DBHelper.SMSPHONE );
|
||||
if ( col >= 0 ) {
|
||||
summary.smsPhone = cursor.getString( col );
|
||||
}
|
||||
}
|
||||
}
|
||||
cursor.close();
|
||||
db.close();
|
||||
}
|
||||
cursor.close();
|
||||
db.close();
|
||||
return summary;
|
||||
}
|
||||
|
||||
public static void saveSummary( String path, GameSummary summary )
|
||||
{
|
||||
SQLiteDatabase db = m_dbHelper.getWritableDatabase();
|
||||
synchronized( m_dbHelper ) {
|
||||
SQLiteDatabase db = m_dbHelper.getWritableDatabase();
|
||||
|
||||
if ( null == summary ) {
|
||||
String selection = DBHelper.FILE_NAME + "=\"" + path + "\"";
|
||||
db.delete( DBHelper.TABLE_NAME, selection, null );
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put( DBHelper.FILE_NAME, path );
|
||||
values.put( DBHelper.NUM_MOVES, summary.nMoves );
|
||||
values.put( DBHelper.GAME_OVER, summary.gameOver );
|
||||
if ( null == summary ) {
|
||||
String selection = DBHelper.FILE_NAME + "=\"" + path + "\"";
|
||||
db.delete( DBHelper.TABLE_NAME, selection, null );
|
||||
} else {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put( DBHelper.FILE_NAME, path );
|
||||
values.put( DBHelper.NUM_MOVES, summary.nMoves );
|
||||
values.put( DBHelper.GAME_OVER, summary.gameOver );
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for ( int score : summary.scores ) {
|
||||
sb.append( String.format( "%d ", score ) );
|
||||
}
|
||||
values.put( DBHelper.SCORES, sb.toString() );
|
||||
|
||||
if ( null != summary.conType ) {
|
||||
values.put( DBHelper.CONTYPE, summary.conType.ordinal() );
|
||||
Utils.logf( "wrote CONTYPE" );
|
||||
values.put( DBHelper.ROOMNAME, summary.roomName );
|
||||
values.put( DBHelper.SMSPHONE, summary.smsPhone );
|
||||
}
|
||||
|
||||
Utils.logf( "saveSummary: nMoves=%d", summary.nMoves );
|
||||
|
||||
try {
|
||||
long result = db.replaceOrThrow( DBHelper.TABLE_NAME, "", values );
|
||||
} catch ( Exception ex ) {
|
||||
Utils.logf( "ex: %s", ex.toString() );
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for ( int score : summary.scores ) {
|
||||
sb.append( String.format( "%d ", score ) );
|
||||
}
|
||||
values.put( DBHelper.SCORES, sb.toString() );
|
||||
|
||||
if ( null != summary.conType ) {
|
||||
values.put( DBHelper.CONTYPE, summary.conType.ordinal() );
|
||||
Utils.logf( "wrote CONTYPE" );
|
||||
values.put( DBHelper.ROOMNAME, summary.roomName );
|
||||
values.put( DBHelper.SMSPHONE, summary.smsPhone );
|
||||
}
|
||||
|
||||
Utils.logf( "saveSummary: nMoves=%d", summary.nMoves );
|
||||
|
||||
try {
|
||||
long result = db.replaceOrThrow( DBHelper.TABLE_NAME, "",
|
||||
values );
|
||||
} catch ( Exception ex ) {
|
||||
Utils.logf( "ex: %s", ex.toString() );
|
||||
}
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
|
||||
private static void initDB( Context context )
|
||||
|
|
Loading…
Reference in a new issue