don't shut down MQTT levels we didn't reach

This is a shot-in-the-dark at internal MQTT crashes being reported
through the Play Store.
This commit is contained in:
Eric House 2022-08-06 16:09:08 -07:00
parent 37b5f80e05
commit 97f280a6ea

View file

@ -297,6 +297,7 @@ public class MQTTUtils extends Thread
if ( !stateOk ) { if ( !stateOk ) {
Log.e( TAG, "%H.setState(): bad state for %s: %s", this, newState, mState ); Log.e( TAG, "%H.setState(): bad state for %s: %s", this, newState, mState );
Assert.failDbg();
} }
} }
@ -434,6 +435,7 @@ public class MQTTUtils extends Thread
mShouldExit = true; mShouldExit = true;
State prevState = mState;
setState( State.CLOSING ); setState( State.CLOSING );
MqttAsyncClient client; MqttAsyncClient client;
@ -449,7 +451,7 @@ public class MQTTUtils extends Thread
if ( null == client ) { if ( null == client ) {
Log.e( TAG, "disconnect(): null client" ); Log.e( TAG, "disconnect(): null client" );
} else { } else {
startDisconThread( client ); startDisconThread( client, prevState );
} }
// Make sure we don't need to call clearInstance(this) // Make sure we don't need to call clearInstance(this)
@ -459,7 +461,7 @@ public class MQTTUtils extends Thread
Log.d( TAG, "%H.disconnect() DONE", this ); Log.d( TAG, "%H.disconnect() DONE", this );
} }
private void startDisconThread( final MqttAsyncClient client ) private void startDisconThread( final MqttAsyncClient client, State maxState )
{ {
new Thread( new Runnable() { new Thread( new Runnable() {
@Override @Override
@ -471,12 +473,16 @@ public class MQTTUtils extends Thread
try { try {
switch ( ii ) { switch ( ii ) {
case 0: case 0:
action = "unsubscribe"; if ( State.SUBSCRIBED == maxState ) {
token = client.unsubscribe( mDevID ); action = "unsubscribe";
token = client.unsubscribe( mDevID );
}
break; // not continue, which skips the Log() below break; // not continue, which skips the Log() below
case 1: case 1:
action = "disconnect"; if ( client.isConnected() ) {
token = client.disconnect(); action = "disconnect";
token = client.disconnect();
}
break; break;
case 2: case 2:
action = "close"; action = "close";