I have an issue, i'm refactoring my App written years a go, i've changed the whole Settings activity which first was a simple activity with custom checkboxes and edittexts which was saving data in a custom SharedPrefs
"settings".
Today i've rewritted the whole Settings by using root_preferences
, so the old and the new datas are saved in different SharedPrefs
, old data are in SharedPrefs
"settings" and the new one in DefaultSharedPreferences
, another point is that in the old Settings page all the switches values was saved as a string like if the switch was on i was saving "ON" else "OFF" and i would refactor even this by simply settings SwitchPreferenceCompat
saved value to "ON" / "OFF" to make it simplier.
Which would be the way i should take to "migrate" the SharedPrefs
from settings to DefaultSharedPrefs
?
As if i'd update the App now to someone it will still read values from SharedPrefs settings and the data set in new Settings page will be useless...
EDIT: After following the @snachmsm raccomandations i've ended up by doing a version check of the application (to check if it's the first run or if it's an old version with old settings activity) by reusing the code from this answer and them i've just made a function which make the whole preferences migration from old values and old shared prefs to new one.
Here is how my code looks like:
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
checkFirstRun(sharedPreferences);
}
private void checkFirstRun(SharedPreferences sharedPreferences) {
final String PREFS_NAME = "FirstRun";
final String PREF_VERSION_CODE_KEY = "version_code";
final int DOESNT_EXIST = -1;
int currentVersionCode = BuildConfig.VERSION_CODE;
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);
// Check for first run or upgrade
if (currentVersionCode == savedVersionCode) {
return;
} else if (savedVersionCode == DOESNT_EXIST) {
migratePreferences(sharedPreferences);
} else if (currentVersionCode > savedVersionCode) {
// UPGRADE
}
// Update the shared preferences with the current version code
prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}
private void migratePreferences(SharedPreferences sharedPreferences) {
SharedPreferences oldPrefs = getSharedPreferences("settings",MODE_PRIVATE);
if (oldPrefs.getAll().size() == 0) {
return;
}
SharedPreferences.Editor preferencesEditor = sharedPreferences.edit();
preferencesEditor.putString("ip", oldPrefs.getString("ip", "10.0.0.51"));
preferencesEditor.putString("txtArt", oldPrefs.getString("txtArt", "10.0.0.51"));
preferencesEditor.putString("txtTasti", oldPrefs.getString("txtTasti", "10.0.0.51"));
preferencesEditor.putString("txtTerm", oldPrefs.getString("txtTerm", "1"));
preferencesEditor.putString("txtColonne", oldPrefs.getString("txtColonne", "4"));
preferencesEditor.putString("varianti", String.valueOf(oldPrefs.getInt("varianti", 5)));
preferencesEditor.putString("turni", String.valueOf(oldPrefs.getInt("turni", 0)));
preferencesEditor.putBoolean("sottoTavoli", oldPrefs.getString("sottoTavoli", "OFF").equals("ON"));
preferencesEditor.putBoolean("prezzoTasto", oldPrefs.getString("prezzoTasto", "OFF").equals("ON"));
preferencesEditor.putBoolean("chiediConto", oldPrefs.getString("chiediConto", "OFF").equals("ON"));
preferencesEditor.putBoolean("prossimoTurno", oldPrefs.getString("prossimoTurno", "OFF").equals("ON"));
preferencesEditor.putBoolean("prezz", oldPrefs.getString("prezz", "OFF").equals("ON"));
preferencesEditor.putBoolean("switchMsg", oldPrefs.getString("switchMsg", "OFF").equals("ON"));
preferencesEditor.putBoolean("soloSotto", oldPrefs.getString("soloSotto", "OFF").equals("ON"));
preferencesEditor.putBoolean("night", oldPrefs.getString("night", "OFF").equals("ON"));
preferencesEditor.putBoolean("tastiLaterali", oldPrefs.getString("tastiLaterali", "OFF").equals("ON"));
preferencesEditor.putBoolean("famSwitch", oldPrefs.getString("famSwitch", "OFF").equals("ON"));
preferencesEditor.putBoolean("riproponiTurno", oldPrefs.getString("riproponiTurno", "OFF").equals("ON"));
preferencesEditor.putBoolean("ricaricatasti", oldPrefs.getString("famSwitch", "OFF").equals("ON"));
preferencesEditor.putBoolean("order", oldPrefs.getBoolean("order", false));
preferencesEditor.putBoolean("fidelity", oldPrefs.getBoolean("order", false));
preferencesEditor.apply();
// Deleting all old prefs.
oldPrefs.edit().clear().apply();
}