Prevent NPE when onQrCodeDecoded() is called after we stop to listen

This commit is contained in:
Torsten Grote
2021-04-12 08:18:54 -03:00
parent 5e84e5b8b6
commit 6ee57315dd

View File

@@ -74,7 +74,6 @@ import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED;
import static android.bluetooth.BluetoothAdapter.EXTRA_SCAN_MODE; import static android.bluetooth.BluetoothAdapter.EXTRA_SCAN_MODE;
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE; import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
import static android.widget.Toast.LENGTH_LONG; import static android.widget.Toast.LENGTH_LONG;
import static java.util.Objects.requireNonNull;
import static java.util.logging.Level.INFO; import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
@@ -417,15 +416,16 @@ class AddNearbyContactViewModel extends AndroidViewModel
@IoExecutor @IoExecutor
public void onQrCodeDecoded(Result result) { public void onQrCodeDecoded(Result result) {
LOG.info("Got result from decoder"); LOG.info("Got result from decoder");
KeyAgreementTask currentTask = task;
// Ignore results until the KeyAgreementTask is ready // Ignore results until the KeyAgreementTask is ready
if (!gotLocalPayload || gotRemotePayload) return; if (!gotLocalPayload || gotRemotePayload || currentTask == null) return;
try { try {
byte[] payloadBytes = result.getText().getBytes(ISO_8859_1); byte[] payloadBytes = result.getText().getBytes(ISO_8859_1);
if (LOG.isLoggable(INFO)) if (LOG.isLoggable(INFO))
LOG.info("Remote payload is " + payloadBytes.length + " bytes"); LOG.info("Remote payload is " + payloadBytes.length + " bytes");
Payload remotePayload = payloadParser.parse(payloadBytes); Payload remotePayload = payloadParser.parse(payloadBytes);
gotRemotePayload = true; gotRemotePayload = true;
requireNonNull(task).connectAndRunProtocol(remotePayload); currentTask.connectAndRunProtocol(remotePayload);
state.postValue(new AddContactState.QrCodeScanned()); state.postValue(new AddContactState.QrCodeScanned());
} catch (UnsupportedVersionException e) { } catch (UnsupportedVersionException e) {
resetPayloadFlags(); resetPayloadFlags();