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,13 +218,18 @@ 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 {
cmd = MsgCmds.values()[intent.getIntExtra( CMD_STR, -1 )];
} catch (Exception ex) { // OOB most likely
cmd = null;
}
if ( null != cmd ) {
DbgUtils.logf( "RelayService::onStartCommand: cmd=%s",
cmd.toString() );
switch( cmd ) { switch( cmd ) {
case -1:
break;
case PROCESS_MSGS: case PROCESS_MSGS:
String[] relayIDs = new String[1]; String[] relayIDs = new String[1];
relayIDs[0] = intent.getStringExtra( RELAY_ID ); relayIDs[0] = intent.getStringExtra( RELAY_ID );
@ -250,7 +257,7 @@ public class RelayService extends XWService
startUDPThreadsIfNot(); startUDPThreadsIfNot();
long rowid = intent.getLongExtra( ROWID, -1 ); long rowid = intent.getLongExtra( ROWID, -1 );
byte[] msg = intent.getByteArrayExtra( BINBUFFER ); byte[] msg = intent.getByteArrayExtra( BINBUFFER );
if ( SEND == cmd ) { if ( MsgCmds.SEND.equals( cmd ) ) {
sendMessage( rowid, msg ); sendMessage( rowid, msg );
} else { } else {
feedMessage( rowid, msg ); feedMessage( rowid, msg );
@ -266,7 +273,10 @@ public class RelayService extends XWService
} }
result = Service.START_STICKY; result = Service.START_STICKY;
} else { }
}
if ( null == result ) {
result = Service.START_STICKY_COMPATIBILITY; result = Service.START_STICKY_COMPATIBILITY;
} }