replace static final ints with enum

This commit is contained in:
Eric House 2013-08-27 07:03:25 -07:00
parent 084abb57e3
commit a7d5d261d3

View file

@ -63,12 +63,14 @@ public class RelayService extends XWService
private static final String CMD_STR = "CMD"; private static final String CMD_STR = "CMD";
// These should be enums // These should be enums
private static final int PROCESS_MSGS = 1; private static enum MsgCmds { INVALID
private static final int UDP_CHANGED = 2; , PROCESS_MSGS
private static final int SEND = 3; , UDP_CHANGED
private static final int RECEIVE = 4; , SEND
private static final int TIMER_FIRED = 5; , RECEIVE
private static final int RESET = 6; , TIMER_FIRED
, RESET
}
private static final String MSGS = "MSGS"; private static final String MSGS = "MSGS";
private static final String RELAY_ID = "RELAY_ID"; private static final String RELAY_ID = "RELAY_ID";
@ -122,20 +124,20 @@ public class RelayService extends XWService
public static void startService( Context context ) public static void startService( Context context )
{ {
Intent intent = getIntentTo( context, UDP_CHANGED ); Intent intent = getIntentTo( context, MsgCmds.UDP_CHANGED );
context.startService( intent ); context.startService( intent );
} }
public static void reset( Context context ) public static void reset( Context context )
{ {
Intent intent = getIntentTo( context, RESET ); Intent intent = getIntentTo( context, MsgCmds.RESET );
context.startService( intent ); context.startService( intent );
} }
public static void timerFired( Context context ) public static void timerFired( Context context )
{ {
Intent intent = getIntentTo( context, TIMER_FIRED ); Intent intent = getIntentTo( context, MsgCmds.TIMER_FIRED );
context.startService( intent ); context.startService( intent );
} }
@ -143,7 +145,7 @@ public class RelayService extends XWService
{ {
int result = -1; int result = -1;
if ( NetStateCache.netAvail( context ) ) { if ( NetStateCache.netAvail( context ) ) {
Intent intent = getIntentTo( context, SEND ) Intent intent = getIntentTo( context, MsgCmds.SEND )
.putExtra( ROWID, rowid ) .putExtra( ROWID, rowid )
.putExtra( BINBUFFER, msg ); .putExtra( BINBUFFER, msg );
context.startService( intent ); context.startService( intent );
@ -160,7 +162,7 @@ public class RelayService extends XWService
DbgUtils.logf( "RelayService::postData: packet of length %d for token %d", DbgUtils.logf( "RelayService::postData: packet of length %d for token %d",
msg.length, rowid ); msg.length, rowid );
if ( DBUtils.haveGame( context, rowid ) ) { if ( DBUtils.haveGame( context, rowid ) ) {
Intent intent = getIntentTo( context, RECEIVE ) Intent intent = getIntentTo( context, MsgCmds.RECEIVE )
.putExtra( ROWID, rowid ) .putExtra( ROWID, rowid )
.putExtra( BINBUFFER, msg ); .putExtra( BINBUFFER, msg );
context.startService( intent ); context.startService( intent );
@ -178,16 +180,16 @@ public class RelayService extends XWService
public static void processMsgs( Context context, String relayId, public static void processMsgs( Context context, String relayId,
String[] msgs64 ) String[] msgs64 )
{ {
Intent intent = getIntentTo( context, PROCESS_MSGS ) Intent intent = getIntentTo( context, MsgCmds.PROCESS_MSGS )
.putExtra( MSGS, msgs64 ) .putExtra( MSGS, msgs64 )
.putExtra( RELAY_ID, relayId ); .putExtra( RELAY_ID, relayId );
context.startService( intent ); context.startService( intent );
} }
private static Intent getIntentTo( Context context, int cmd ) private static Intent getIntentTo( Context context, MsgCmds cmd )
{ {
Intent intent = new Intent( context, RelayService.class ); Intent intent = new Intent( context, RelayService.class );
intent.putExtra( CMD_STR, cmd ); intent.putExtra( CMD_STR, cmd.ordinal() );
return intent; return intent;
} }
@ -216,57 +218,65 @@ public class RelayService extends XWService
@Override @Override
public int onStartCommand( Intent intent, int flags, int startId ) public int onStartCommand( Intent intent, int flags, int startId )
{ {
int result; Integer result = null;
if ( null != intent ) { if ( null != intent ) {
int cmd = intent.getIntExtra( CMD_STR, -1 ); MsgCmds cmd;
DbgUtils.logf( "RelayService::onStartCommand: cmd=%d", cmd ); try {
switch( cmd ) { cmd = MsgCmds.values()[intent.getIntExtra( CMD_STR, -1 )];
case -1: } catch (Exception ex) { // OOB most likely
break; cmd = null;
case PROCESS_MSGS:
String[] relayIDs = new String[1];
relayIDs[0] = intent.getStringExtra( RELAY_ID );
long[] rowIDs = DBUtils.getRowIDsFor( this, relayIDs[0] );
if ( 0 < rowIDs.length ) {
String[] msgs64 = intent.getStringArrayExtra( MSGS );
int count = msgs64.length;
byte[][][] msgs = new byte[1][count][];
for ( int ii = 0; ii < count; ++ii ) {
msgs[0][ii] = XwJNI.base64Decode( msgs64[ii] );
}
process( msgs, rowIDs, relayIDs );
}
break;
case UDP_CHANGED:
startThreads();
break;
case RESET:
stopThreads();
startThreads();
break;
case SEND:
case RECEIVE:
startUDPThreadsIfNot();
long rowid = intent.getLongExtra( ROWID, -1 );
byte[] msg = intent.getByteArrayExtra( BINBUFFER );
if ( SEND == cmd ) {
sendMessage( rowid, msg );
} else {
feedMessage( rowid, msg );
}
break;
case TIMER_FIRED:
if ( !startFetchThreadIf() ) {
sendKeepAlive();
}
break;
default:
Assert.fail();
} }
if ( null != cmd ) {
DbgUtils.logf( "RelayService::onStartCommand: cmd=%s",
cmd.toString() );
switch( cmd ) {
case PROCESS_MSGS:
String[] relayIDs = new String[1];
relayIDs[0] = intent.getStringExtra( RELAY_ID );
long[] rowIDs = DBUtils.getRowIDsFor( this, relayIDs[0] );
if ( 0 < rowIDs.length ) {
String[] msgs64 = intent.getStringArrayExtra( MSGS );
int count = msgs64.length;
result = Service.START_STICKY; byte[][][] msgs = new byte[1][count][];
} else { for ( int ii = 0; ii < count; ++ii ) {
msgs[0][ii] = XwJNI.base64Decode( msgs64[ii] );
}
process( msgs, rowIDs, relayIDs );
}
break;
case UDP_CHANGED:
startThreads();
break;
case RESET:
stopThreads();
startThreads();
break;
case SEND:
case RECEIVE:
startUDPThreadsIfNot();
long rowid = intent.getLongExtra( ROWID, -1 );
byte[] msg = intent.getByteArrayExtra( BINBUFFER );
if ( MsgCmds.SEND.equals( cmd ) ) {
sendMessage( rowid, msg );
} else {
feedMessage( rowid, msg );
}
break;
case TIMER_FIRED:
if ( !startFetchThreadIf() ) {
sendKeepAlive();
}
break;
default:
Assert.fail();
}
result = Service.START_STICKY;
}
}
if ( null == result ) {
result = Service.START_STICKY_COMPATIBILITY; result = Service.START_STICKY_COMPATIBILITY;
} }