class SensorSanity {

    public synchronized static boolean checkLibreSensorChange(final String currentSerial) {
        Log.i(TAG, "checkLibreSensorChange called currentSerial = " + currentSerial);
        if ((currentSerial == null) || currentSerial.length() < 4) return false;
        final Sensor this_sensor = Sensor.currentSensor();
        if(this_sensor == null || this_sensor.uuid == null|| this_sensor.uuid.length() < 4) {
            Log.i(TAG, "no senosr open, deleting everything");
            PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, "");
            PersistentStore.setString(PREF_LIBRE_SN, "");
            return false;
        }
        final String lastSn = PersistentStore.getString(PREF_LIBRE_SN);
        final String last_uuid = PersistentStore.getString(PREF_LIBRE_SENSOR_UUID);
        Log.i(TAG, "checkLibreSensorChange Initial values: lastSn = " + lastSn + " last_uuid = " + last_uuid);
        if(lastSn.length() < 4 || last_uuid.length() < 4) {
            Log.i(TAG, "lastSn or last_uuid not valid, writing current values.");
            PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, this_sensor.uuid);
            PersistentStore.setString(PREF_LIBRE_SN, currentSerial);
            return false;
        }
        // Here we have the data that we need to start verifying.
        if(lastSn.equals(currentSerial)) {
            if(this_sensor.uuid.equals(last_uuid)) {
                // all well
                Log.i(TAG, "checkLibreSensorChange returning false 1");
                return false;
            } else {
                // This is the case that the user had a sensor, but he stopped it and started a new one in xDrip.
                // This is probably ok, since physical sensor has not changed.
                // we learn the new uuid and continue.
                Log.e(TAG, "A new xdrip sensor was found, updating state.");
                PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, this_sensor.uuid);
                return false;
            }
        } else {
            // We have a new sensorid. So physical sensors have changed.
            // verify uuid have also changed.
            if(this_sensor.uuid.equals(last_uuid)) {
                // We need to stop the sensor.
                Log.e(TAG, String.format("Different sensor serial number for same sensor uuid: %s :: %s vs %s", last_uuid, lastSn, currentSerial));
                Sensor.stopSensor();
                JoH.static_toast_long("Stopping sensor due to serial number change");
                // There is no open sensor now.
                PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, "");
                PersistentStore.setString(PREF_LIBRE_SN, "");
                return true;

            } else {
                // This is the first time we see this sensor, update our uuid and current serial.
                Log.i(TAG, "This is the first time we see this sensor uuid = " +  this_sensor.uuid);
                PersistentStore.setString(PREF_LIBRE_SENSOR_UUID, this_sensor.uuid);
                PersistentStore.setString(PREF_LIBRE_SN, currentSerial);
                return false;
            }
        }
    }

}