add new activity that's popped up on creation of a networked game to

ask only for the room name to make it as easy as possible to
get started.
This commit is contained in:
Andy2 2010-11-04 19:48:46 -07:00
parent c9239fc921
commit 23924ff74e
8 changed files with 212 additions and 50 deletions

View file

@ -108,5 +108,7 @@
</intent-filter>
</activity>
<activity android:name="RelayGameActivity"/>
</application>
</manifest>

View file

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- only one direct child possible -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/explain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<EditText android:id="@+id/room_edit"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollHorizontally="false"
android:autoText="false"
android:capitalize="none"
android:singleLine="true"
android:selectAllOnFocus="true"
android:hint="@string/new_room_hint"
/>
<Button android:id="@+id/play_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/play"
/>
</LinearLayout>
</ScrollView>

View file

@ -4,6 +4,9 @@
<item android:id="@+id/list_item_config"
android:title="@string/list_item_config"
/>
<item android:id="@+id/list_item_netconfig"
android:title="net (remove me)"
/>
<item android:id="@+id/list_item_copy"
android:title="@string/list_item_copy"
/>

View file

@ -472,4 +472,10 @@
game on the relay. You will be notified when the remaining
device[s] have joined your room and play can begin.</string>
<string name="relay_game_explainf">To start a simple two-player
game in %s over the network all you need to do is agree on a room
name with the other person and make sure you both enter it in the
field below. Do that and press the Play game button and you\'ll
connect.</string>
</resources>

View file

@ -864,52 +864,12 @@ public class GameConfig extends XWActivity
private void applyChanges( boolean forceNew )
{
// This should be a separate function, commitChanges() or
// somesuch. But: do we have a way to save changes to a gi
// that don't reset the game, e.g. player name for standalone
// games?
byte[] dictBytes = GameUtils.openDict( this, m_gi.dictName );
int gamePtr = XwJNI.initJNI();
boolean madeGame = false;
if ( !forceNew ) {
byte[] stream = GameUtils.savedGame( this, m_path );
// Will fail if there's nothing in the stream but a gi.
madeGame = XwJNI.game_makeFromStream( gamePtr, stream,
JNIUtilsImpl.get(),
new CurGameInfo(this),
dictBytes, m_gi.dictName,
m_cp );
}
if ( forceNew || !madeGame ) {
m_gi.setInProgress( false );
m_gi.fixup();
XwJNI.game_makeNewGame( gamePtr, m_gi, JNIUtilsImpl.get(),
m_cp, dictBytes, m_gi.dictName );
}
if ( null != m_car ) {
XwJNI.comms_setAddr( gamePtr, m_car );
}
GameUtils.saveGame( this, gamePtr, m_gi, m_path );
GameSummary summary = new GameSummary();
XwJNI.game_summarize( gamePtr, m_gi.nPlayers, summary );
DBUtils.saveSummary( this, m_path, summary );
XwJNI.game_dispose( gamePtr );
GameUtils.applyChanges( this, m_gi, m_car, m_path, forceNew );
}
private void launchGame()
{
File file = new File( m_path );
Uri uri = Uri.fromFile( file );
Intent intent = new Intent( Intent.ACTION_EDIT, uri,
this, BoardActivity.class );
startActivity( intent );
finish();
GameUtils.launchGame( this, m_path );
}
private void refreshNames()

View file

@ -20,10 +20,14 @@
package org.eehouse.android.xw4;
import android.app.Activity;
import android.content.Context;
import java.io.InputStream;
import android.content.Intent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.net.Uri;
import java.util.ArrayList;
import android.content.res.AssetManager;
@ -357,6 +361,58 @@ public class GameUtils {
return path.substring( 0, path.lastIndexOf( XWConstants.GAME_EXTN ) );
}
public static void launchGame( Activity activity, String path )
{
File file = new File( path );
Uri uri = Uri.fromFile( file );
Intent intent = new Intent( Intent.ACTION_EDIT, uri,
activity, BoardActivity.class );
activity.startActivity( intent );
activity.finish();
}
public static void applyChanges( Context context, CurGameInfo gi,
CommsAddrRec car, String path,
boolean forceNew )
{
// This should be a separate function, commitChanges() or
// somesuch. But: do we have a way to save changes to a gi
// that don't reset the game, e.g. player name for standalone
// games?
byte[] dictBytes = GameUtils.openDict( context, gi.dictName );
int gamePtr = XwJNI.initJNI();
boolean madeGame = false;
CommonPrefs cp = CommonPrefs.get( context );
if ( !forceNew ) {
byte[] stream = GameUtils.savedGame( context, path );
// Will fail if there's nothing in the stream but a gi.
madeGame = XwJNI.game_makeFromStream( gamePtr, stream,
JNIUtilsImpl.get(),
new CurGameInfo(context),
dictBytes, gi.dictName, cp );
}
if ( forceNew || !madeGame ) {
gi.setInProgress( false );
gi.fixup();
XwJNI.game_makeNewGame( gamePtr, gi, JNIUtilsImpl.get(),
cp, dictBytes, gi.dictName );
}
if ( null != car ) {
XwJNI.comms_setAddr( gamePtr, car );
}
GameUtils.saveGame( context, gamePtr, gi, path );
GameSummary summary = new GameSummary();
XwJNI.game_summarize( gamePtr, gi.nPlayers, summary );
DBUtils.saveSummary( context, path, summary );
XwJNI.game_dispose( gamePtr );
}
private static String removeExtn( String str )
{
if ( str.endsWith( XWConstants.DICT_EXTN ) ) {

View file

@ -85,9 +85,11 @@ public class GamesList extends XWListActivity
newGameB.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick( View v ) {
addGame( true );
String path = addGame( true );
showNotAgainDlgThen( R.string.not_again_newgamenet,
R.string.key_notagain_newgamenet, null );
doConfig( path, RelayGameActivity.class );
}
});
@ -309,7 +311,13 @@ public class GamesList extends XWListActivity
} else {
switch ( menuID ) {
case R.id.list_item_config:
doConfig( path );
doConfig( path, GameConfig.class );
m_invalPath = path;
break;
// For development only; don't ship!!!
case R.id.list_item_netconfig:
doConfig( path, RelayGameActivity.class );
m_invalPath = path;
break;
@ -355,12 +363,10 @@ public class GamesList extends XWListActivity
return handled;
} // handleMenuItem
private void doConfig( String path )
private void doConfig( String path, Class clazz )
{
Uri uri = Uri.fromFile( new File(path) );
Intent intent = new Intent( Intent.ACTION_EDIT, uri,
this, GameConfig.class );
Intent intent = new Intent( Intent.ACTION_EDIT, uri, this, clazz );
startActivity( intent );
}
@ -374,11 +380,12 @@ public class GamesList extends XWListActivity
return path;
}
private void addGame( boolean networked )
private String addGame( boolean networked )
{
String path = saveNew( new CurGameInfo( this, networked ) );
GameUtils.resetGame( this, path, path );
onContentChanged();
return path;
}
}

View file

@ -0,0 +1,89 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
/*
* Copyright 2009-2010 by Eric House (xwords@eehouse.org). All
* rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// This activity is for newbies. Bring it up when network game
// created. It explains they need only a room name -- that everything
// else is derived from defaults and configurable via the main config
// dialog (which offer to launch)
package org.eehouse.android.xw4;
import android.app.Activity;
import android.os.Bundle;
import android.net.Uri;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import junit.framework.Assert;
import org.eehouse.android.xw4.jni.*;
public class RelayGameActivity extends XWActivity
implements View.OnClickListener {
private String m_path;
private CurGameInfo m_gi;
private CommsAddrRec m_car;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.relay_game_config );
Uri uri = getIntent().getData();
m_path = uri.getPath();
if ( m_path.charAt(0) == '/' ) {
m_path = m_path.substring( 1 );
}
int gamePtr = XwJNI.initJNI();
m_gi = new CurGameInfo( this );
GameUtils.loadMakeGame( this, gamePtr, m_gi, m_path );
m_car = new CommsAddrRec( this );
if ( XwJNI.game_hasComms( gamePtr ) ) {
XwJNI.comms_getAddr( gamePtr, m_car );
} else {
Assert.fail();
// String relayName = CommonPrefs.getDefaultRelayHost( this );
// int relayPort = CommonPrefs.getDefaultRelayPort( this );
// XwJNI.comms_getInitialAddr( m_carOrig, relayName, relayPort );
}
XwJNI.game_dispose( gamePtr );
String lang = DictLangCache.getLangName( this, m_gi.dictName );
String fmt = getString( R.string.relay_game_explainf );
TextView text = (TextView)findViewById( R.id.explain );
text.setText( String.format( fmt, lang ) );
Button play_button = (Button)findViewById( R.id.play_button );
play_button.setOnClickListener( this );
} // onCreate
@Override
public void onClick( View view )
{
m_car.ip_relay_invite = Utils.getText( this, R.id.room_edit ).trim();
GameUtils.applyChanges( this, m_gi, m_car, m_path, false );
GameUtils.launchGame( this, m_path );
}
} // class RelayGameActivity