add a clear-history menu to chat

This commit is contained in:
Andy2 2011-02-17 07:14:58 -08:00
parent b3ffb4895b
commit c271cb2fdd
4 changed files with 51 additions and 2 deletions

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/chat_menu_clear"
android:title="@string/chat_menu_clear"
/>
</menu>

View file

@ -402,6 +402,7 @@
<string name="chat_local_id">Me: </string>
<string name="chat_other_id">Not me: </string>
<string name="chat_send">Send</string>
<string name="chat_menu_clear">Clear history</string>
<string name="notify_title">Moves made</string>
<string name="notify_body">New game moves have arrived</string>

View file

@ -28,6 +28,9 @@ import android.widget.EditText;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.LinearLayout;
public class ChatActivity extends XWActivity implements View.OnClickListener {
@ -64,6 +67,29 @@ public class ChatActivity extends XWActivity implements View.OnClickListener {
((Button)findViewById( R.id.send_button )).setOnClickListener( this );
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.chat_menu, menu );
return true;
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
boolean handled = R.id.chat_menu_clear == item.getItemId();
if ( handled ) {
DBUtils.clearChatHistory( this, m_path );
LinearLayout layout =
(LinearLayout)findViewById( R.id.chat_history );
layout.removeAllViews();
} else {
handled = super.onOptionsItemSelected( item );
}
return handled;
}
@Override
public void onClick( View view )
{

View file

@ -533,13 +533,28 @@ public class DBUtils {
msg = cur + "\n" + msg;
}
saveChatHistory( context, path, msg );
} // appendChatHistory
public static void clearChatHistory( Context context, String path )
{
saveChatHistory( context, path, null );
}
private static void saveChatHistory( Context context, String path,
String history )
{
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = DBHelper.FILE_NAME + "=\"" + path + "\"";
ContentValues values = new ContentValues();
values.put( DBHelper.CHAT_HISTORY, msg );
if ( null != history ) {
values.put( DBHelper.CHAT_HISTORY, history );
} else {
values.putNull( DBHelper.CHAT_HISTORY );
}
long timestamp = new Date().getTime();
values.put( DBHelper.LASTPLAY_TIME, timestamp );
@ -548,7 +563,7 @@ public class DBUtils {
values, selection, null );
db.close();
}
} // appendChatHistory
}
private static String[] parsePlayers( final String players, int nPlayers ){
String[] result = null;