package game-start data into a json and send it via nfc, then open it

on the other end. Works to start a game!
This commit is contained in:
Eric House 2013-11-11 22:06:28 -08:00
parent a88835a4a1
commit d0313d3c4b
3 changed files with 56 additions and 10 deletions

View file

@ -548,19 +548,27 @@ public class GameUtils {
if ( null == inviteID ) { if ( null == inviteID ) {
inviteID = makeRandomID(); inviteID = makeRandomID();
} }
Uri gameUri = NetLaunchInfo.makeLaunchUri( activity, room, inviteID, String msgString;
lang, dict, nPlayers ); if ( DlgDelegate.NFC_BTN == chosen ) {
if ( null != gameUri ) { msgString = NetLaunchInfo.makeLaunchJSON( activity, room, inviteID,
lang, dict, nPlayers );
} else {
Uri gameUri = NetLaunchInfo.makeLaunchUri( activity, room, inviteID,
lang, dict, nPlayers );
msgString = null == gameUri ? null : gameUri.toString();
}
if ( null != msgString ) {
if ( DlgDelegate.NFC_BTN == chosen ) { if ( DlgDelegate.NFC_BTN == chosen ) {
DbgUtils.logf( "wants to launch NFC" );
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter( activity ); NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter( activity );
NdefMessage msg = new NdefMessage( new NdefRecord[] { NdefMessage msg = new NdefMessage( new NdefRecord[] {
new NdefRecord(NdefRecord.TNF_MIME_MEDIA, new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
"application/org.eehouse.android.xw4" "application/org.eehouse.android.xw4"
.getBytes(Charset.forName("US-ASCII")), .getBytes(Charset.forName("US-ASCII")),
new byte[0], "Beam me up, Android!" new byte[0], msgString.
.getBytes(Charset.forName("US-ASCII"))) getBytes(Charset.forName("US-ASCII")))
,NdefRecord.createApplicationRecord("org.eehouse.android.xw4") ,NdefRecord.
createApplicationRecord("org.eehouse.android.xw4")
}); });
nfcAdapter.setNdefPushMessage( msg, activity ); nfcAdapter.setNdefPushMessage( msg, activity );
Utils.showToast( activity, "Tap the receiving device now" ); Utils.showToast( activity, "Tap the receiving device now" );
@ -569,7 +577,7 @@ public class GameUtils {
int fmtId = choseEmail? R.string.invite_htmf : R.string.invite_txtf; int fmtId = choseEmail? R.string.invite_htmf : R.string.invite_txtf;
int choiceID; int choiceID;
String message = activity.getString( fmtId, gameUri.toString() ); String message = activity.getString( fmtId, msgString );
Intent intent = new Intent(); Intent intent = new Intent();
if ( choseEmail ) { if ( choseEmail ) {

View file

@ -1135,8 +1135,12 @@ public class GamesList extends XWExpandableListActivity
// only one message sent during the beam // only one message sent during the beam
NdefMessage msg = (NdefMessage) rawMsgs[0]; NdefMessage msg = (NdefMessage) rawMsgs[0];
// record 0 contains the MIME type, record 1 is the AAR, if present // record 0 contains the MIME type, record 1 is the AAR, if present
String txt = new String( msg.getRecords()[0].getPayload() ); String data = new String( msg.getRecords()[0].getPayload() );
DbgUtils.logf( "got message: %s", txt );
NetLaunchInfo nli = new NetLaunchInfo( data );
if ( nli.isValid() ) {
startNewNetGame( nli );
}
} }
} }

View file

@ -57,6 +57,21 @@ public class NetLaunchInfo {
bundle.putBoolean( VALID, m_valid ); bundle.putBoolean( VALID, m_valid );
} }
public NetLaunchInfo( String data )
{
try {
JSONObject json = new JSONObject( data );
room = json.getString( MultiService.ROOM );
inviteID = json.getString( MultiService.INVITEID );
lang = json.getInt( MultiService.LANG );
dict = json.getString( MultiService.DICT );
nPlayersT = json.getInt( MultiService.NPLAYERST );
m_valid = true;
} catch ( org.json.JSONException jse ) {
m_valid = false;
}
}
public NetLaunchInfo( Bundle bundle ) public NetLaunchInfo( Bundle bundle )
{ {
lang = bundle.getInt( LANG ); lang = bundle.getInt( LANG );
@ -134,6 +149,25 @@ public class NetLaunchInfo {
return ub.build(); return ub.build();
} }
public static String makeLaunchJSON( Context context, String room,
String inviteID, int lang,
String dict, int nPlayersT )
{
String result = null;
try {
result = new JSONObject()
.put( MultiService.ROOM, room )
.put( MultiService.INVITEID, inviteID )
.put( MultiService.LANG, lang )
.put( MultiService.DICT, dict )
.put( MultiService.NPLAYERST, nPlayersT )
.toString();
} catch ( org.json.JSONException jse ) {
DbgUtils.loge( jse );
}
return result;
}
public boolean isValid() public boolean isValid()
{ {
return m_valid; return m_valid;