diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java index 884b57f2a..8ad4d6533 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/MQTTUtils.java @@ -74,6 +74,7 @@ public class MQTTUtils extends Thread private LinkedBlockingQueue mOutboundQueue = new LinkedBlockingQueue<>(); private boolean mShouldExit = false; private State mState = State.NONE; + private boolean mNeedsResend; private static TimerReceiver.TimerCallback sTimerCallbacks = new TimerReceiver.TimerCallback() { @@ -106,11 +107,16 @@ public class MQTTUtils extends Thread Log.d( TAG, "onNetAvail(avail=%b)", nowAvailable ); DbgUtils.assertOnUIThread(); if ( nowAvailable ) { - GameUtils.resendAllIf( context, CommsConnType.COMMS_CONN_MQTT ); + resendAllIf( context ); } } }; + private static void resendAllIf( Context context ) + { + GameUtils.resendAllIf( context, CommsConnType.COMMS_CONN_MQTT ); + } + public static void init( Context context ) { Log.d( TAG, "init()" ); @@ -155,16 +161,20 @@ public class MQTTUtils extends Thread static void onConfigChanged( Context context ) { - MQTTUtils instance; synchronized ( sInstance ) { - instance = sInstance[0]; - } - if ( null != instance ) { - clearInstance( instance ); + if ( null != sInstance[0] ) { + clearInstance( sInstance[0] ); + } } + getOrStart( context, true ); } private static MQTTUtils getOrStart( Context context ) + { + return getOrStart( context, false ); + } + + private static MQTTUtils getOrStart( Context context, boolean resendOnConnect ) { MQTTUtils result = null; if ( XWPrefs.getMQTTEnabled( context ) ) { @@ -172,7 +182,7 @@ public class MQTTUtils extends Thread result = sInstance[0]; if ( null == result ) { try { - result = new MQTTUtils( context ); + result = new MQTTUtils( context, resendOnConnect ); setInstance( result ); result.start(); } catch ( MqttException me ) { @@ -285,10 +295,11 @@ public class MQTTUtils extends Thread curInstance.disconnect(); } - private MQTTUtils( Context context ) throws MqttException + private MQTTUtils( Context context, boolean resendOnConnect ) throws MqttException { Log.d( TAG, "%H.()", this ); mContext = context; + mNeedsResend = resendOnConnect; mDevID = XwJNI.dvc_getMQTTDevID(); mSubTopics = XwJNI.dvc_getMQTTSubTopics(); Assert.assertTrueNR( 16 == mDevID.length() ); @@ -620,10 +631,14 @@ public class MQTTUtils extends Thread // MqttCallbackExtended @Override - public void connectComplete(boolean reconnect, String serverURI) + public void connectComplete( boolean reconnect, String serverURI ) { Log.d( TAG, "%H.connectComplete(reconnect=%b, serverURI=%s)", this, reconnect, serverURI ); + if ( mNeedsResend ) { + mNeedsResend = false; + resendAllIf( mContext ); + } } @Override