move byte[]<->NetLaunchInfo convert into the class

This commit is contained in:
Eric House 2018-07-27 07:44:02 -07:00
parent 2f10712379
commit a23777bade
2 changed files with 44 additions and 36 deletions

View file

@ -27,8 +27,13 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Iterator; import java.util.Iterator;
import org.json.JSONException; import org.json.JSONException;
@ -139,6 +144,20 @@ public class NetLaunchInfo implements Serializable {
return nli; return nli;
} }
public static NetLaunchInfo makeFrom( Context context, byte[] data )
{
NetLaunchInfo nli = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream( data );
DataInputStream dis = new DataInputStream( bais );
String nliData = dis.readUTF();
nli = NetLaunchInfo.makeFrom( context, nliData );
} catch ( java.io.IOException ex ) {
Assert.assertFalse( BuildConfig.DEBUG );
}
return nli;
}
public NetLaunchInfo( Context context, Uri data ) public NetLaunchInfo( Context context, Uri data )
{ {
this(); this();
@ -595,6 +614,20 @@ public class NetLaunchInfo implements Serializable {
return makeLaunchJSON(); return makeLaunchJSON();
} }
public byte[] asByteArray()
{
byte[] result = null;
try {
ByteArrayOutputStream bas = new ByteArrayOutputStream();
DataOutputStream das = new DataOutputStream( bas );
das.writeUTF( makeLaunchJSON() );
result = bas.toByteArray();
} catch ( java.io.IOException ex ) {
Assert.assertFalse( BuildConfig.DEBUG );
}
return result;
}
public static void putExtras( Intent intent, int gameID, String btAddr ) public static void putExtras( Intent intent, int gameID, String btAddr )
{ {
Assert.fail(); Assert.fail();

View file

@ -77,7 +77,7 @@ public class SMSService extends XWService {
private static final String BUFFER = "BUFFER"; private static final String BUFFER = "BUFFER";
private static final String BINBUFFER = "BINBUFFER"; private static final String BINBUFFER = "BINBUFFER";
private static final String PHONE = "PHONE"; private static final String PHONE = "PHONE";
private static final String GAMEDATA_STR = "GD"; private static final String GAMEDATA_BA = "GD";
private static final String PHONE_RECS_KEY = private static final String PHONE_RECS_KEY =
SMSService.class.getName() + "_PHONES"; SMSService.class.getName() + "_PHONES";
@ -193,9 +193,9 @@ public class SMSService extends XWService {
{ {
Intent intent = getIntentTo( context, SMSAction.INVITE ); Intent intent = getIntentTo( context, SMSAction.INVITE );
intent.putExtra( PHONE, phone ); intent.putExtra( PHONE, phone );
String asString = nli.toString(); Log.w( TAG, "inviteRemote(%s, '%s')", phone, nli );
Log.w( TAG, "inviteRemote(%s, '%s')", phone, asString ); byte[] data = nli.asByteArray();
intent.putExtra( GAMEDATA_STR, asString ); intent.putExtra( GAMEDATA_BA, data );
context.startService( intent ); context.startService( intent );
} }
@ -337,7 +337,8 @@ public class SMSService extends XWService {
break; break;
case INVITE: case INVITE:
phone = intent.getStringExtra( PHONE ); phone = intent.getStringExtra( PHONE );
inviteRemote( phone, intent.getStringExtra( GAMEDATA_STR ) ); buffer = intent.getByteArrayExtra( GAMEDATA_BA );
inviteRemote( phone, buffer );
break; break;
case ADDED_MISSING: case ADDED_MISSING:
NetLaunchInfo nli NetLaunchInfo nli
@ -373,19 +374,9 @@ public class SMSService extends XWService {
return result; return result;
} // onStartCommand } // onStartCommand
private void inviteRemote( String phone, String nliData ) private void inviteRemote( String phone, byte[] asBytes )
{ {
try { resendFor( phone, SMS_CMD.INVITE, 0, asBytes, true );
ByteArrayOutputStream bas = new ByteArrayOutputStream();
DataOutputStream das = new DataOutputStream( bas );
das.writeUTF( nliData );
byte[] asBytes = bas.toByteArray();
resendFor( phone, SMS_CMD.INVITE, 0, asBytes, true );
} catch ( java.io.UnsupportedEncodingException uee ) {
Log.ex( TAG, uee );
} catch ( java.io.IOException ioe ) {
Log.ex( TAG, ioe );
}
} }
private void ackInvite( String phone, int gameID ) private void ackInvite( String phone, int gameID )
@ -464,7 +455,7 @@ public class SMSService extends XWService {
Log.i( TAG, "receive(cmd=%s)", msg.cmd ); Log.i( TAG, "receive(cmd=%s)", msg.cmd );
switch( msg.cmd ) { switch( msg.cmd ) {
case INVITE: case INVITE:
makeForInvite( phone, msg.data ); makeForInvite( phone, NetLaunchInfo.makeFrom( this, msg.data ) );
break; break;
case DATA: case DATA:
if ( feedMessage( msg.gameID, msg.data, new CommsAddrRec( phone ) ) ) { if ( feedMessage( msg.gameID, msg.data, new CommsAddrRec( phone ) ) ) {
@ -508,26 +499,10 @@ public class SMSService extends XWService {
GameUtils.postInvitedNotification( this, gameID, body, rowid ); GameUtils.postInvitedNotification( this, gameID, body, rowid );
} }
private void makeForInvite( String phone, byte[] data )
{
try {
ByteArrayInputStream bais = new ByteArrayInputStream( data );
DataInputStream dis = new DataInputStream( bais );
String nliData = dis.readUTF();
NetLaunchInfo nli = NetLaunchInfo.makeFrom( this, nliData );
if ( null != nli ) {
makeForInvite( phone, nli );
} else {
Log.e( TAG, "null nli; bad data?" );
}
} catch ( java.io.IOException ioe ) {
Log.ex( TAG, ioe );
}
}
private void makeForInvite( String phone, NetLaunchInfo nli ) private void makeForInvite( String phone, NetLaunchInfo nli )
{ {
if ( handleInvitation( nli, phone, DictFetchOwner.OWNER_SMS ) ) { if ( nli != null &&
handleInvitation( nli, phone, DictFetchOwner.OWNER_SMS ) ) {
ackInvite( phone, nli.gameID() ); ackInvite( phone, nli.gameID() );
} }
} }