mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-18 22:26:30 +01:00
move byte[]<->NetLaunchInfo convert into the class
This commit is contained in:
parent
2f10712379
commit
a23777bade
2 changed files with 44 additions and 36 deletions
|
@ -27,8 +27,13 @@ import android.net.Uri;
|
|||
import android.os.Bundle;
|
||||
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.Serializable;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
@ -139,6 +144,20 @@ public class NetLaunchInfo implements Serializable {
|
|||
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 )
|
||||
{
|
||||
this();
|
||||
|
@ -595,6 +614,20 @@ public class NetLaunchInfo implements Serializable {
|
|||
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 )
|
||||
{
|
||||
Assert.fail();
|
||||
|
|
|
@ -77,7 +77,7 @@ public class SMSService extends XWService {
|
|||
private static final String BUFFER = "BUFFER";
|
||||
private static final String BINBUFFER = "BINBUFFER";
|
||||
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 =
|
||||
SMSService.class.getName() + "_PHONES";
|
||||
|
@ -193,9 +193,9 @@ public class SMSService extends XWService {
|
|||
{
|
||||
Intent intent = getIntentTo( context, SMSAction.INVITE );
|
||||
intent.putExtra( PHONE, phone );
|
||||
String asString = nli.toString();
|
||||
Log.w( TAG, "inviteRemote(%s, '%s')", phone, asString );
|
||||
intent.putExtra( GAMEDATA_STR, asString );
|
||||
Log.w( TAG, "inviteRemote(%s, '%s')", phone, nli );
|
||||
byte[] data = nli.asByteArray();
|
||||
intent.putExtra( GAMEDATA_BA, data );
|
||||
context.startService( intent );
|
||||
}
|
||||
|
||||
|
@ -337,7 +337,8 @@ public class SMSService extends XWService {
|
|||
break;
|
||||
case INVITE:
|
||||
phone = intent.getStringExtra( PHONE );
|
||||
inviteRemote( phone, intent.getStringExtra( GAMEDATA_STR ) );
|
||||
buffer = intent.getByteArrayExtra( GAMEDATA_BA );
|
||||
inviteRemote( phone, buffer );
|
||||
break;
|
||||
case ADDED_MISSING:
|
||||
NetLaunchInfo nli
|
||||
|
@ -373,19 +374,9 @@ public class SMSService extends XWService {
|
|||
return result;
|
||||
} // onStartCommand
|
||||
|
||||
private void inviteRemote( String phone, String nliData )
|
||||
private void inviteRemote( String phone, byte[] asBytes )
|
||||
{
|
||||
try {
|
||||
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 );
|
||||
}
|
||||
resendFor( phone, SMS_CMD.INVITE, 0, asBytes, true );
|
||||
}
|
||||
|
||||
private void ackInvite( String phone, int gameID )
|
||||
|
@ -464,7 +455,7 @@ public class SMSService extends XWService {
|
|||
Log.i( TAG, "receive(cmd=%s)", msg.cmd );
|
||||
switch( msg.cmd ) {
|
||||
case INVITE:
|
||||
makeForInvite( phone, msg.data );
|
||||
makeForInvite( phone, NetLaunchInfo.makeFrom( this, msg.data ) );
|
||||
break;
|
||||
case DATA:
|
||||
if ( feedMessage( msg.gameID, msg.data, new CommsAddrRec( phone ) ) ) {
|
||||
|
@ -508,26 +499,10 @@ public class SMSService extends XWService {
|
|||
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 )
|
||||
{
|
||||
if ( handleInvitation( nli, phone, DictFetchOwner.OWNER_SMS ) ) {
|
||||
if ( nli != null &&
|
||||
handleInvitation( nli, phone, DictFetchOwner.OWNER_SMS ) ) {
|
||||
ackInvite( phone, nli.gameID() );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue