resendAll after mqtt host config changes

This commit is contained in:
Eric House 2023-02-03 17:15:02 -08:00
parent 4e029a0fda
commit 6584be9ec6

View file

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