From d6bbccb77354fec735ecf1443c0df17480c802ca Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 28 Jun 2017 08:16:10 -0700 Subject: [PATCH] let caller deal with result of message resend Use a new interface to let caller of resendAllIf() know how many messages were resent. If none, a new timer might not be called for. --- .../org/eehouse/android/xw4/GameUtils.java | 40 ++++++++++++++----- .../android/xw4/SMSResendReceiver.java | 25 +++++++++--- .../org/eehouse/android/xw4/SMSService.java | 3 +- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java index c84787594..fb9dc9cca 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java @@ -65,6 +65,10 @@ public class GameUtils { public static final String INVITED = "invited"; public static final String INTENT_KEY_ROWID = "rowid"; + interface ResendDoneProc { + void onResendDone( Context context, int numSent ); + } + private static Integer s_minScreen; // Used to determine whether to resend all messages on networking coming // back up. The length of the array determines the number of times in the @@ -435,6 +439,26 @@ public class GameUtils { public static void resendAllIf( Context context, CommsConnType filter, boolean force, boolean showUI ) + { + ResendDoneProc proc = null; + if ( showUI ) { + proc = new ResendDoneProc() { + @Override + public void onResendDone( Context context, int nSent ) + { + String msg = LocUtils + .getQuantityString( context, + R.plurals.resent_msgs_fmt, + nSent, nSent ); + DbgUtils.showf( context, msg ); + } + }; + } + resendAllIf( context, filter, force, proc ); + } + + public static void resendAllIf( Context context, CommsConnType filter, + boolean force, ResendDoneProc proc ) { long now = Utils.getCurSeconds(); @@ -450,7 +474,7 @@ public class GameUtils { HashMap games = DBUtils.getGamesWithSendsPending( context ); if ( 0 < games.size() ) { - new ResendTask( context, games, filter, showUI ).execute(); + new ResendTask( context, games, filter, proc ).execute(); System.arraycopy( s_sendTimes, 0, /* src */ s_sendTimes, 1, /* dest */ @@ -1216,17 +1240,17 @@ public class GameUtils { private static class ResendTask extends AsyncTask { private Context m_context; private HashMap m_games; - private boolean m_showUI; + private ResendDoneProc m_doneProc; private CommsConnType m_filter; private MultiMsgSink m_sink; public ResendTask( Context context, HashMap games, - CommsConnType filter, boolean showUI ) + CommsConnType filter, ResendDoneProc proc ) { m_context = context; m_games = games; m_filter = filter; - m_showUI = showUI; + m_doneProc = proc; } @Override @@ -1278,13 +1302,9 @@ public class GameUtils { @Override protected void onPostExecute( Void unused ) { - if ( m_showUI ) { + if ( null != m_doneProc ) { int nSent = null == m_sink ? 0 : m_sink.numSent(); - String msg = - LocUtils.getQuantityString( m_context, - R.plurals.resent_msgs_fmt, - nSent, nSent ); - DbgUtils.showf( m_context, msg ); + m_doneProc.onResendDone( m_context, nSent ); } } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSResendReceiver.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSResendReceiver.java index 4260a0d1d..4f0535f27 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSResendReceiver.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSResendReceiver.java @@ -48,8 +48,22 @@ public class SMSResendReceiver extends BroadcastReceiver { public void onReceive( Context context, Intent intent ) { GameUtils.resendAllIf( context, CommsConnType.COMMS_CONN_SMS, true, - BuildConfig.DEBUG ); - setTimer( context, true ); + new GameUtils.ResendDoneProc() { + @Override + public void onResendDone( Context context, + int nSent ) { + int backoff = -1; + if ( 0 < nSent ) { + backoff = setTimer( context, true ); + } + if ( BuildConfig.DEBUG ) { + DbgUtils.showf( context, + "%d SMS msgs resent;" + + " backoff: %d", + nSent, backoff); + } + } + } ); } static void resetTimer( Context context ) @@ -58,12 +72,12 @@ public class SMSResendReceiver extends BroadcastReceiver { setTimer( context ); } - static void setTimer( Context context ) + static int setTimer( Context context ) { - setTimer( context, false ); + return setTimer( context, false ); } - private static void setTimer( Context context, boolean advance ) + private static int setTimer( Context context, boolean advance ) { AlarmManager am = (AlarmManager)context.getSystemService( Context.ALARM_SERVICE ); @@ -82,5 +96,6 @@ public class SMSResendReceiver extends BroadcastReceiver { Log.d( TAG, "set for %d seconds from now", millis / 1000 ); millis += SystemClock.elapsedRealtime(); am.set( AlarmManager.ELAPSED_REALTIME, millis, pi ); + return backoff; } } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java index 84e7afd19..fb731e214 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/SMSService.java @@ -625,7 +625,8 @@ public class SMSService extends XWService { } catch ( java.io.IOException ioe ) { Log.ex( TAG, ioe ); } catch ( ArrayIndexOutOfBoundsException oob ) { - // enum this older code doesn't know about; drop it + // enum this older code doesn't know about, or just another app's + // message; drop it Log.w( TAG, "disAssemble: dropping message with too-new enum" ); } return success;