Make timer to check relay for messages non-repeating, and don't

[re]set it when there are no networked games on the device. I didn't
add a change to explicitly set it when creating the first networked
game, but that doesn't seem to be necessary.
This commit is contained in:
Eric House 2015-05-05 07:47:10 -07:00
parent 5bba268ede
commit 38b958751c
5 changed files with 22 additions and 11 deletions

View file

@ -751,6 +751,15 @@ public class DBUtils {
return result; return result;
} }
public static boolean haveRelayGames( Context context )
{
long[][] rowIDss = new long[1][];
String[] relayIDs = getRelayIDs( context, rowIDss );
boolean result = null != relayIDs && 0 < relayIDs.length;
DbgUtils.logf( "haveRelayGames() => %b", result );
return result;
}
public static void addDeceased( Context context, String relayID, public static void addDeceased( Context context, String relayID,
int seed ) int seed )
{ {

View file

@ -43,7 +43,7 @@ public class OnBootReceiver extends BroadcastReceiver {
protected static void startTimers( Context context ) protected static void startTimers( Context context )
{ {
NagTurnReceiver.restartTimer( context ); NagTurnReceiver.restartTimer( context );
RelayReceiver.restartTimer( context ); RelayReceiver.setTimer( context );
} }
} }

View file

@ -51,7 +51,7 @@ public class PollListPreference extends ListPreference
{ {
String valstr = (String)newValue; String valstr = (String)newValue;
int val = Integer.parseInt(valstr); int val = Integer.parseInt(valstr);
RelayReceiver.restartTimer( m_context, val * 1000 ); RelayReceiver.setTimer( m_context, val * 1000 );
setSummaryToMatch( valstr ); setSummaryToMatch( valstr );
return true; return true;

View file

@ -36,25 +36,26 @@ public class RelayReceiver extends BroadcastReceiver {
RelayService.timerFired( context ); RelayService.timerFired( context );
} }
public static void restartTimer( Context context ) public static void setTimer( Context context )
{ {
restartTimer( context, 1000 * XWPrefs.getProxyInterval( context ) ); setTimer( context, 1000 * XWPrefs.getProxyInterval( context ) );
} }
public static void restartTimer( Context context, long interval_millis ) public static void setTimer( Context context, long interval_millis )
{ {
DbgUtils.logf( "RelayReceiver.restartTimer(%d)", interval_millis );
AlarmManager am = AlarmManager am =
(AlarmManager)context.getSystemService( Context.ALARM_SERVICE ); (AlarmManager)context.getSystemService( Context.ALARM_SERVICE );
Intent intent = new Intent( context, RelayReceiver.class ); Intent intent = new Intent( context, RelayReceiver.class );
PendingIntent pi = PendingIntent.getBroadcast( context, 0, intent, 0 ); PendingIntent pi = PendingIntent.getBroadcast( context, 0, intent, 0 );
if ( interval_millis > 0 ) { // Check if we have any relay games
long first_millis = SystemClock.elapsedRealtime() + interval_millis; if ( interval_millis > 0 && DBUtils.haveRelayGames( context ) ) {
am.setInexactRepeating( AlarmManager.ELAPSED_REALTIME_WAKEUP, long fire_millis = SystemClock.elapsedRealtime() + interval_millis;
first_millis, // first firing am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, fire_millis, pi );
interval_millis, pi );
} else { } else {
DbgUtils.logf( "RelayReceiver.restartTimer(): cancelling" );
// will happen if user's set getProxyInterval to return 0 // will happen if user's set getProxyInterval to return 0
am.cancel( pi ); am.cancel( pi );
} }

View file

@ -348,6 +348,7 @@ public class RelayService extends XWService
} else if ( registerWithRelayIfNot() ) { } else if ( registerWithRelayIfNot() ) {
requestMessages(); requestMessages();
} }
RelayReceiver.setTimer( this );
break; break;
default: default:
Assert.fail(); Assert.fail();
@ -371,7 +372,7 @@ public class RelayService extends XWService
{ {
if ( shouldMaintainConnection() ) { if ( shouldMaintainConnection() ) {
long interval_millis = getMaxIntervalSeconds() * 1000; long interval_millis = getMaxIntervalSeconds() * 1000;
RelayReceiver.restartTimer( this, interval_millis ); RelayReceiver.setTimer( this, interval_millis );
} }
stopThreads(); stopThreads();
super.onDestroy(); super.onDestroy();