This commit is contained in:
ehouse 2010-01-20 06:52:37 +00:00
parent dd05c5b58e
commit f4535f3408

View file

@ -19,6 +19,7 @@
package org.eehouse.android.xw4;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
@ -34,147 +35,282 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
import java.io.PrintStream;
import java.io.FileOutputStream;
import android.text.Editable;
import android.database.DataSetObserver;
import android.app.Dialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import org.eehouse.android.xw4.jni.*;
/**
* A generic activity for editing a note in a database. This can be used
* either to simply view a note {@link Intent#ACTION_VIEW}, view and edit a note
* {@link Intent#ACTION_EDIT}, or create a new note {@link Intent#ACTION_INSERT}.
*/
public class GameConfig extends Activity implements View.OnClickListener {
private static final String TAG = "Games";
public class GameConfig extends ListActivity implements View.OnClickListener {
/** The index of the note column */
private static final int COLUMN_INDEX_NOTE = 1;
// This is our state data that is stored when freezing.
private static final String ORIGINAL_CONTENT = "origContent";
// Identifiers for our menu items.
private static final int REVERT_ID = Menu.FIRST;
private static final int DISCARD_ID = Menu.FIRST + 1;
private static final int DELETE_ID = Menu.FIRST + 2;
// The different distinct states the activity can be run in.
private static final int STATE_EDIT = 0;
private static final int STATE_INSERT = 1;
private int mState;
private boolean mNoteOnly = false;
private Uri mUri;
private String mOriginalContent;
private static final int PLAYER_EDIT = 1;
private Button mDoneB;
private Button mOpenB;
private Button m_addPlayerButton;
private String m_path;
private CurGameInfo m_gi;
private int m_whichPlayer;
private Dialog m_curDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
// Do some setup based on the action being performed.
final String action = intent.getAction();
Utils.logf( "action: " + action );
if (Intent.ACTION_EDIT.equals(action)) {
// Requested to edit: set that state, and the data being edited.
mState = STATE_EDIT;
mUri = intent.getData();
} else if (Intent.ACTION_INSERT.equals(action)) {
Utils.logf( "matches insert" );
// Requested to insert: set that state, and create a new entry
// in the container.
mState = STATE_INSERT;
// mUri = getContentResolver().insert(intent.getData(), null);
// Utils.logf( mUri.toString() );
// If we were unable to create a new note, then just finish
// this activity. A RESULT_CANCELED will be sent back to the
// original activity if they requested a result.
// if (mUri == null) {
// Log.e(TAG, "Failed to insert new note into " + getIntent().getData());
// finish();
// return;
// }
// The new entry was created, so assume all will end well and
// set the result to be returned.
// setResult(RESULT_OK, (new Intent()).setAction(mUri.toString()));
} else {
// Whoops, unknown action! Bail.
Utils.logf("Unknown action, exiting");
finish();
return;
private class PlayerListAdapter implements ListAdapter {
public boolean areAllItemsEnabled() {
return true;
}
setContentView(R.layout.game_config);
public boolean isEnabled( int position ) {
return position < m_gi.nPlayers;
}
public int getCount() {
return m_gi.nPlayers;
}
public Object getItem( int position ) {
TextView view = new TextView( GameConfig.this );
LocalPlayer lp = m_gi.players[position];
StringBuffer sb = new StringBuffer()
.append( lp.name )
.append( lp.isRobot? " (ROBOT)":" (HUMAN)")
.append( lp.isLocal? " (LOCAL)":" (REMOTE)" )
;
view.setText( sb.toString() );
return view;
}
public long getItemId( int position ) {
return position;
}
public int getItemViewType( int position ) {
return 0;
}
public View getView( int position, View convertView, ViewGroup parent ) {
return (View)getItem( position );
}
public int getViewTypeCount() {
return 1;
}
public boolean hasStableIds() {
return true;
}
public boolean isEmpty() {
return false;
}
public void registerDataSetObserver(DataSetObserver observer) {}
public void unregisterDataSetObserver(DataSetObserver observer) {}
} // class PlayerListAdapter
@Override
protected Dialog onCreateDialog( int id )
{
switch (id) {
case PLAYER_EDIT:
LayoutInflater factory = LayoutInflater.from(this);
final View playerEditView
= factory.inflate( R.layout.player_edit, null );
return new AlertDialog.Builder( this )
// .setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.player_edit_title)
.setView(playerEditView)
.setPositiveButton(R.string.button_ok,
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int whichButton ) {
getPlayerSettings();
onContentChanged();
}
})
.setNegativeButton(R.string.button_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
})
.create();
}
return null;
}
@Override
protected void onPrepareDialog( int id, Dialog dialog )
{
m_curDialog = dialog;
setPlayerSettings();
super.onPrepareDialog( id, dialog );
}
private void setPlayerSettings()
{
LocalPlayer lp = m_gi.players[m_whichPlayer];
EditText player = (EditText)m_curDialog.findViewById( R.id.player_edit );
player.setText( lp.name );
CheckBox isRobot = (CheckBox)m_curDialog.findViewById( R.id.robot_check );
isRobot.setChecked( lp.isRobot );
}
private void getPlayerSettings()
{
LocalPlayer lp = m_gi.players[m_whichPlayer];
EditText player = (EditText)m_curDialog.findViewById( R.id.player_edit );
lp.name = player.getText().toString();
CheckBox isRobot = (CheckBox)m_curDialog.findViewById( R.id.robot_check );
lp.isRobot = isRobot.isChecked();
}
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game_config);
registerForContextMenu( getListView() );
// mOpenB = (Button)findViewById(R.id.game_config_open);
// mOpenB.setOnClickListener( this );
mDoneB = (Button)findViewById(R.id.game_config_done);
mDoneB.setOnClickListener( this );
// If an instance of this activity had previously stopped, we can
// get the original text it started with.
// if (savedInstanceState != null) {
// mOriginalContent = savedInstanceState.getString(ORIGINAL_CONTENT);
// }
m_addPlayerButton = (Button)findViewById(R.id.add_player);
m_addPlayerButton.setOnClickListener( this );
// Do some setup based on the action being performed.
Intent intent = getIntent();
Uri uri = intent.getData();
m_path = uri.getPath();
if ( m_path.charAt(0) == '/' ) {
m_path = m_path.substring( 1 );
}
byte[] stream = Utils.savedGame( this, m_path );
m_gi = new CurGameInfo();
XwJNI.gi_from_stream( m_gi, stream );
setListAdapter( new PlayerListAdapter() );
} // onCreate
public void onClick( View view ) {
@Override
public void onCreateContextMenu( ContextMenu menu, View view,
ContextMenuInfo menuInfo ) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.players_list_item_menu, menu );
}
@Override
public boolean onContextItemSelected( MenuItem item )
{
boolean handled = true;
boolean changed = false;
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
Utils.logf( "bad menuInfo:" + e.toString() );
return false;
}
switch (item.getItemId()) {
case R.id.player_list_item_edit:
showDialog( PLAYER_EDIT );
break;
case R.id.player_list_item_up:
changed = m_gi.moveUp( info.position );
break;
case R.id.player_list_item_down:
changed = m_gi.moveDown( info.position );
break;
case R.id.player_list_item_delete:
changed = m_gi.delete( info.position );
break;
default:
handled = false;
}
if ( changed ) {
onContentChanged();
}
return handled;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
m_whichPlayer = position;
showDialog( PLAYER_EDIT );
}
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.game_config_menu, menu );
return true;
}
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
boolean handled = true;
switch (item.getItemId()) {
case R.id.game_config_juggle:
m_gi.juggle();
onContentChanged();
break;
default:
handled = false;
}
return handled;
}
public void onClick( View view )
{
if ( mDoneB == view ) {
EditText et = (EditText)findViewById( R.id.player_1_name );
Editable text = et.getText();
String name1 = text.toString();
et = (EditText)findViewById( R.id.player_2_name );
text = et.getText();
String name2 = text.toString();
Integer num = 0;
int ii;
String[] files = fileList();
String name = null;
while ( name == null ) {
name = "game " + num.toString();
for ( ii = 0; ii < files.length; ++ii ) {
Utils.logf( "comparing " + name + " with " + files[ii] );
if ( files[ii].equals(name) ) {
++num;
name = null;
}
}
byte[] bytes = XwJNI.gi_to_stream( m_gi );
if ( null == bytes ) {
Utils.logf( "gi_to_stream failed" );
} else {
Utils.logf( "got " + bytes.length + " bytes." );
Utils.saveGame( this, bytes, m_path );
}
FileOutputStream out;
try {
out = openFileOutput( name, MODE_PRIVATE );
PrintStream ps = new PrintStream( out );
ps.println( name1 );
ps.println( name2 );
ps.close();
try {
out.close();
} catch ( java.io.IOException ex ) {
Utils.logf( "got IOException: " + ex.toString() );
}
} catch ( java.io.FileNotFoundException ex ) {
Utils.logf( "got FileNotFoundException: " + ex.toString() );
}
finish();
} else if ( mOpenB == view ) {
// finish but after posting an intent that'll cause the
// list view to launch us -- however that's done.
Utils.logf( "got open" );
} else if ( m_addPlayerButton == view ) {
int curIndex = m_gi.nPlayers;
if ( curIndex < CurGameInfo.MAX_NUM_PLAYERS ) {
m_gi.addPlayer();
m_whichPlayer = curIndex;
showDialog( PLAYER_EDIT );
}
} else {
Utils.logf( "unknown v: " + view.toString() );
}