@SuppressWarnings("unused")
public class PreferredJourney {

    private PreferredStation station1;
    private PreferredStation station2;
    private long             timestamp;
    private int              priority;
    private String           category;
    private String           version;

    public PreferredJourney(PreferredStation station1, PreferredStation station2) {
        this.station1 = station1;
        this.station2 = station2;
        timestamp = DateTime.now().toInstant().getMillis();
        priority = -1;
        category = "";
        version = "";
    }

    public PreferredJourney swapStations() {
        PreferredStation temp = station1;
        station1 = station2;
        station2 = temp;
        return this;
    }

    public PreferredStation getStation1() {
        return station1;
    }

    public void setStation1(PreferredStation station1) {
        this.station1 = station1;
    }

    public PreferredStation getStation2() {
        return station2;
    }

    public void setStation2(PreferredStation station2) {
        this.station2 = station2;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    @Override
    public String toString() {
        return "PreferredJourney{" +
                "station1=" + station1 +
                ", station2=" + station2 +
                ", timestamp=" + timestamp +
                ", priority=" + priority +
                ", category='" + category + '\'' +
                ", version='" + version + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        return timestamp == ((PreferredJourney) o).timestamp;
    }
} @SuppressWarnings("unused")
public class PreferredStation {

    private String stationShortId;
    private String stationLongId;
    private String nameShort;
    private String nameLong;

    private Map<String, Journey.Solution> preferredSolutions;

    public PreferredStation() {
        preferredSolutions = new HashMap<>();
    }

    public PreferredStation(Station station) {
        this.stationShortId = station.getStationShortId();
        this.stationLongId = station.getStationLongId();
        this.nameShort = station.getNameShort();
        this.nameLong = station.getNameLong();
        preferredSolutions = new HashMap<>();
    }

    public PreferredStation(String stationShortId, String stationLongId, String nameShort, String nameLong) {
        this.stationShortId = stationShortId;
        this.stationLongId = stationLongId;
        this.nameShort = nameShort;
        this.nameLong = nameLong;
        preferredSolutions = new HashMap<>();
    }

    public String getStationShortId() {
        return stationShortId;
    }

    public void setStationShortId(String stationShortId) {
        this.stationShortId = stationShortId;
    }

    public String getStationLongId() {
        return stationLongId;
    }

    public void setStationLongId(String stationLongId) {
        this.stationLongId = stationLongId;
    }

    public String getNameShort() {
        return nameShort;
    }

    public void setNameShort(String nameShort) {
        this.nameShort = nameShort;
    }

    public String getNameLong() {
        return nameLong;
    }

    public void setNameLong(String nameLong) {
        this.nameLong = nameLong;
    }

    public Map<String, Journey.Solution> getPreferredSolutions() {
        return preferredSolutions;
    }

    public void addPreferredSolutions(String id, Journey.Solution solution) {
        this.preferredSolutions.put(id, solution);
    }

    public void removePreferredSolutions(String id) {
        this.preferredSolutions.remove(id);
    }

    @Override
    public String toString() {
        return "PreferredStation{" +
                "stationShortId='" + stationShortId + '\'' +
                ", stationLongId='" + stationLongId + '\'' +
                ", nameShort='" + nameShort + '\'' +
                ", nameLong='" + nameLong + '\'' +
                ", preferredSolutions=" + preferredSolutions +
                '}';
    }
} public static final String SP0_FIRST_START               = "firstStart"; public static final String SP0_NOTIFICATION_PREF_JOURNEY = "notificationPrefJourney"; public static final String SP0_NOTIFICATION_SOLUTION     = "notificationSolution"; public static final String SP0_PREF_LIGHTNING            = "prefLigthining"; public static final String SP0_SAVED_VERSION             = "savedVersion"; public static final String SP0_SERVER_CONFIG             = "serverConfig"; public static final String SP_NOTIFICATION_PREF_JOURNEY = "notification>notifiedPrefJourney"; public static final String SP_NOTIFICATION_SOLUTION     = "notification>notifiedSolution"; public static final String SP_SETT_LIGHTNING            = "settings>isLightningEnabled"; public static final String SP_SP_FIRST_START            = "settings>isFirstStart"; public static final String SP_SP_SAVED_VERSION          = "prefs>prevLaunchVersionCode"; public static final String SP_SP_SERVER_CONFIG          = "prefs>serverConfig"; // should not be existing anymore public static final String SP_SP_VERSION                = "prefs>prevLaunchSPVersion"; public static final int SP_VERSION = 0; public static Map<String, ?> getAll(Context context) {
    SharedPreferences settings = context.getSharedPreferences(PREF_FILE, 0);
    return settings.getAll();
}
import com.google.gson.Gson;
import android.content.Context;
import com.jaus.albertogiunta.justintrain_oraritreni.BuildConfig;
import com.jaus.albertogiunta.justintrain_oraritreni.data.PreferredJourney;
import com.jaus.albertogiunta.justintrain_oraritreni.data.PreferredStation;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import trikita.log.Log;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_INITIAL.SP0_FIRST_START;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_INITIAL.SP0_NOTIFICATION_PREF_JOURNEY;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_INITIAL.SP0_NOTIFICATION_SOLUTION;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_INITIAL.SP0_PREF_LIGHTNING;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_INITIAL.SP0_SAVED_VERSION;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_INITIAL.SP0_SERVER_CONFIG;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_NOTIFICATION_PREF_JOURNEY;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_NOTIFICATION_SOLUTION;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_SETT_LIGHTNING;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_SP_FIRST_START;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_SP_SAVED_VERSION;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_SP_SERVER_CONFIG;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_SP_VERSION;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.constants.CONST_SP_V0.SP_VERSION;
import static com.jaus.albertogiunta.justintrain_oraritreni.utils.sharedPreferences.SharedPreferencesHelper.getAll;
package com.jaus.albertogiunta.justintrain_oraritreni.utils.sharedPreferences;







public class MigrationHelper {

    /*
    nuova installaziione => fai default e sala sp_version a SP_VERSION
                            se il bomber elimina i settings ricomincia da zero e quindi risalva i settings

    vecchia installazione => o sp_version esiste ed è minore rispetto a quella dell'app, allora devi fare la migration
                             oppure sp_version non esiste e allora è il caso da -1 a 0 (perchè in tutti gli altri casi è alla prima installazione)
     */

    public static void migrateIfDue(Context context) {
//        SharedPreferencesHelper.setSharedPreferenceInt(context, SP_SP_VERSION, -1); // use for testing purposes
        int oldSPVersion = SharedPreferencesHelper.getSharedPreferenceInt(context, SP_SP_VERSION, -1);
        if (oldSPVersion == -1 && SP_VERSION == 0) {
            SharedPreferencesHelper.setSharedPreferenceInt(context, SP_SP_VERSION, SP_VERSION);
            migrationFromNothingToVersionZero(context);
        }
    }

    private static void migrationFromNothingToVersionZero(Context context) {

        // migro il firstStart
        SharedPreferencesHelper.setSharedPreferenceBoolean(context, SP_SP_FIRST_START, SharedPreferencesHelper.getSharedPreferenceBoolean(context, SP0_FIRST_START, true));
        SharedPreferencesHelper.removeSharedPreferenceObject(context, SP0_FIRST_START);

        // migro il savedVersion
        SharedPreferencesHelper.setSharedPreferenceInt(context, SP_SP_SAVED_VERSION, SharedPreferencesHelper.getSharedPreferenceInt(context, SP0_SAVED_VERSION, BuildConfig.VERSION_CODE));
        SharedPreferencesHelper.removeSharedPreferenceObject(context, SP0_SAVED_VERSION);

        // rimuovo e basta il server config perchè dovrebbe bastare firebase
        SharedPreferencesHelper.removeSharedPreferenceObject(context, SP0_SERVER_CONFIG);

        // migro il lightning
        SharedPreferencesHelper.setSharedPreferenceBoolean(context, SP_SETT_LIGHTNING, SharedPreferencesHelper.getSharedPreferenceBoolean(context, SP0_PREF_LIGHTNING, false));
        SharedPreferencesHelper.removeSharedPreferenceObject(context, SP0_PREF_LIGHTNING);

        // migro il notificationsolution
        String notificationSolution = SharedPreferencesHelper.getSharedPreferenceString(context, SP0_NOTIFICATION_SOLUTION, "");
        if (!notificationSolution.equalsIgnoreCase("")) {
            SharedPreferencesHelper.setSharedPreferenceString(context, SP_NOTIFICATION_SOLUTION, SharedPreferencesHelper.getSharedPreferenceString(context, SP0_NOTIFICATION_SOLUTION, ""));
            SharedPreferencesHelper.removeSharedPreferenceObject(context, SP0_NOTIFICATION_SOLUTION);
        }

        // migro il notificationprefjourney
        String notificationPrefJourney = SharedPreferencesHelper.getSharedPreferenceString(context, SP0_NOTIFICATION_PREF_JOURNEY, "");
        if (!notificationPrefJourney.equalsIgnoreCase("")) {
            SharedPreferencesHelper.setSharedPreferenceString(context, SP_NOTIFICATION_PREF_JOURNEY, SharedPreferencesHelper.getSharedPreferenceString(context, SP0_NOTIFICATION_PREF_JOURNEY, ""));
            SharedPreferencesHelper.removeSharedPreferenceObject(context, SP0_NOTIFICATION_PREF_JOURNEY);
        }

        // migro tutti le preferredJourneys
        List<PreferredJourney> preferredJourneysList = getAllOLDPreferredJourneys(context);
        for (PreferredJourney preferredJourney : preferredJourneysList) {
            PreferredStationsPreferences.setPreferredJourney(context, preferredJourney);
            SharedPreferencesHelper.removeSharedPreferenceObject(context, buildOLDPreferredJourneyId(preferredJourney));
        }
    }

    public static String buildOLDPreferredJourneyId(PreferredJourney preferredJourney) {
        return buildOLDPreferredJourneyId(preferredJourney.getStation1().getStationShortId(), preferredJourney.getStation2().getStationShortId());
    }


    public static String buildOLDPreferredJourneyId(PreferredStation departureStation, PreferredStation arrivalStation) {