diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index 90b2eff70..daa93e8f1 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -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 ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/OnBootReceiver.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/OnBootReceiver.java index 5b581b2e1..753192689 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/OnBootReceiver.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/OnBootReceiver.java @@ -43,7 +43,7 @@ public class OnBootReceiver extends BroadcastReceiver { protected static void startTimers( Context context ) { NagTurnReceiver.restartTimer( context ); - RelayReceiver.restartTimer( context ); + RelayReceiver.setTimer( context ); } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/PollListPreference.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/PollListPreference.java index 684e79573..4eb601639 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/PollListPreference.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/PollListPreference.java @@ -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; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayReceiver.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayReceiver.java index abb2bd5fb..1906bb9c8 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayReceiver.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayReceiver.java @@ -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 ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java index 0ae29286f..0176b557a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java @@ -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();