wrap a bit more chat code in test so java can leave it out

This commit is contained in:
Eric House 2013-09-09 19:31:17 -07:00
parent bd81eb4bbe
commit 1669687f19
2 changed files with 64 additions and 50 deletions

View file

@ -40,32 +40,39 @@ public class ChatActivity extends XWActivity implements View.OnClickListener {
@Override @Override
public void onCreate( Bundle savedInstanceState ) public void onCreate( Bundle savedInstanceState )
{ {
super.onCreate( savedInstanceState ); if ( XWApp.CHAT_SUPPORTED ) {
super.onCreate( savedInstanceState );
setContentView( R.layout.chat ); setContentView( R.layout.chat );
m_rowid = getIntent().getLongExtra( GameUtils.INTENT_KEY_ROWID, -1 ); m_rowid = getIntent().getLongExtra( GameUtils.INTENT_KEY_ROWID, -1 );
DBUtils.HistoryPair[] pairs = DBUtils.getChatHistory( this, m_rowid ); DBUtils.HistoryPair[] pairs = DBUtils.getChatHistory( this, m_rowid );
if ( null != pairs ) { if ( null != pairs ) {
LinearLayout layout = (LinearLayout)findViewById( R.id.chat_history ); LinearLayout layout = (LinearLayout)
LayoutInflater factory = LayoutInflater.from( this ); findViewById( R.id.chat_history );
LayoutInflater factory = LayoutInflater.from( this );
for ( DBUtils.HistoryPair pair : pairs ) { for ( DBUtils.HistoryPair pair : pairs ) {
TextView view = TextView view = (TextView)factory
(TextView)factory.inflate( pair.sourceLocal .inflate( pair.sourceLocal
? R.layout.chat_history_local ? R.layout.chat_history_local
: R.layout.chat_history_remote, : R.layout.chat_history_remote,
null ); null );
view.setText( pair.msg ); view.setText( pair.msg );
layout.addView( view ); layout.addView( view );
}
} }
((Button)findViewById( R.id.send_button ))
.setOnClickListener( this );
setTitle( getString( R.string.chat_titlef,
GameUtils.getName( this, m_rowid ) ) );
} else {
// Should really assert....
finish();
} }
((Button)findViewById( R.id.send_button )).setOnClickListener( this );
setTitle( getString( R.string.chat_titlef,
GameUtils.getName( this, m_rowid ) ) );
} }
@Override @Override

View file

@ -847,15 +847,17 @@ public class DBUtils {
public static HistoryPair[] getChatHistory( Context context, long rowid ) public static HistoryPair[] getChatHistory( Context context, long rowid )
{ {
HistoryPair[] result = null; HistoryPair[] result = null;
final String localPrefix = context.getString( R.string.chat_local_id ); if ( XWApp.CHAT_SUPPORTED ) {
String history = getChatHistoryStr( context, rowid ); final String localPrefix = context.getString( R.string.chat_local_id );
if ( null != history ) { String history = getChatHistoryStr( context, rowid );
String[] msgs = history.split( "\n" ); if ( null != history ) {
result = new HistoryPair[msgs.length]; String[] msgs = history.split( "\n" );
for ( int ii = 0; ii < result.length; ++ii ) { result = new HistoryPair[msgs.length];
String msg = msgs[ii]; for ( int ii = 0; ii < result.length; ++ii ) {
boolean isLocal = msg.startsWith( localPrefix ); String msg = msgs[ii];
result[ii] = new HistoryPair( msg, isLocal ); boolean isLocal = msg.startsWith( localPrefix );
result[ii] = new HistoryPair( msg, isLocal );
}
} }
} }
return result; return result;
@ -1107,21 +1109,24 @@ public class DBUtils {
private static String getChatHistoryStr( Context context, long rowid ) private static String getChatHistoryStr( Context context, long rowid )
{ {
String result = null; String result = null;
initDB( context ); if ( XWApp.CHAT_SUPPORTED ) {
synchronized( s_dbHelper ) { initDB( context );
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.CHAT_HISTORY }; String[] columns = { DBHelper.CHAT_HISTORY };
String selection = String.format( ROW_ID_FMT, rowid ); String selection = String.format( ROW_ID_FMT, rowid );
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
result = result =
cursor.getString( cursor cursor.getString( cursor
.getColumnIndex(DBHelper.CHAT_HISTORY)); .getColumnIndex(DBHelper
.CHAT_HISTORY));
}
cursor.close();
db.close();
} }
cursor.close();
db.close();
} }
return result; return result;
} }
@ -1129,16 +1134,18 @@ public class DBUtils {
public static void appendChatHistory( Context context, long rowid, public static void appendChatHistory( Context context, long rowid,
String msg, boolean local ) String msg, boolean local )
{ {
Assert.assertNotNull( msg ); if ( XWApp.CHAT_SUPPORTED ) {
int id = local ? R.string.chat_local_id : R.string.chat_other_id; Assert.assertNotNull( msg );
msg = context.getString( id ) + msg; int id = local ? R.string.chat_local_id : R.string.chat_other_id;
msg = context.getString( id ) + msg;
String cur = getChatHistoryStr( context, rowid ); String cur = getChatHistoryStr( context, rowid );
if ( null != cur ) { if ( null != cur ) {
msg = cur + "\n" + msg; msg = cur + "\n" + msg;
}
saveChatHistory( context, rowid, msg );
} }
saveChatHistory( context, rowid, msg );
} // appendChatHistory } // appendChatHistory
public static void clearChatHistory( Context context, long rowid ) public static void clearChatHistory( Context context, long rowid )