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;
}
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,
int seed )
{

View file

@ -43,7 +43,7 @@ public class OnBootReceiver extends BroadcastReceiver {
protected static void startTimers( Context 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;
int val = Integer.parseInt(valstr);
RelayReceiver.restartTimer( m_context, val * 1000 );
RelayReceiver.setTimer( m_context, val * 1000 );
setSummaryToMatch( valstr );
return true;

View file

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

View file

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