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.
This commit is contained in:
Eric House 2017-06-28 08:16:10 -07:00
parent bdc1f5ff51
commit d6bbccb773
3 changed files with 52 additions and 16 deletions

View file

@ -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<Long,CommsConnTypeSet> 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<Void, Void, Void> {
private Context m_context;
private HashMap<Long,CommsConnTypeSet> m_games;
private boolean m_showUI;
private ResendDoneProc m_doneProc;
private CommsConnType m_filter;
private MultiMsgSink m_sink;
public ResendTask( Context context, HashMap<Long,CommsConnTypeSet> 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 );
}
}
}

View file

@ -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;
}
}

View file

@ -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;