@Test public void whenKeysAlreadyExistInPrefs_overridesWithDefaults() throws Exception { initPrefs(generalPrefs, "key1", "existing", "key2", false ); initPrefs(adminPrefs, "key1", 0 ); assertThat(importer.fromJSON(emptySettings()), is(true)); assertPrefs(generalPrefs, "key1", "default", "key2", true ); assertPrefs(adminPrefs, "key1", 5 ); }
public boolean fromJSON(@NonNull String json) { if (!settingsValidator.isValid(json)) { return false; } generalSharedPrefs.edit().clear().apply(); adminSharedPrefs.edit().clear().apply(); try { JSONObject jsonObject = new JSONObject(json); JSONObject general = jsonObject.getJSONObject("general"); importToPrefs(general, generalSharedPrefs); JSONObject admin = jsonObject.getJSONObject("admin"); importToPrefs(admin, adminSharedPrefs); } catch (JSONException ignored) { } preferenceMigrator.migrate(generalSharedPrefs, adminSharedPrefs); clearUnknownKeys(generalSharedPrefs, generalDefaults); clearUnknownKeys(adminSharedPrefs, adminDefaults); loadDefaults(generalSharedPrefs, generalDefaults); loadDefaults(adminSharedPrefs, adminDefaults); for (Map.Entry<String, ?> entry: generalSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } for (Map.Entry<String, ?> entry: adminSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } return true; }
SettingsImporter { public boolean fromJSON(@NonNull String json) { if (!settingsValidator.isValid(json)) { return false; } generalSharedPrefs.edit().clear().apply(); adminSharedPrefs.edit().clear().apply(); try { JSONObject jsonObject = new JSONObject(json); JSONObject general = jsonObject.getJSONObject("general"); importToPrefs(general, generalSharedPrefs); JSONObject admin = jsonObject.getJSONObject("admin"); importToPrefs(admin, adminSharedPrefs); } catch (JSONException ignored) { } preferenceMigrator.migrate(generalSharedPrefs, adminSharedPrefs); clearUnknownKeys(generalSharedPrefs, generalDefaults); clearUnknownKeys(adminSharedPrefs, adminDefaults); loadDefaults(generalSharedPrefs, generalDefaults); loadDefaults(adminSharedPrefs, adminDefaults); for (Map.Entry<String, ?> entry: generalSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } for (Map.Entry<String, ?> entry: adminSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } return true; } }
SettingsImporter { public boolean fromJSON(@NonNull String json) { if (!settingsValidator.isValid(json)) { return false; } generalSharedPrefs.edit().clear().apply(); adminSharedPrefs.edit().clear().apply(); try { JSONObject jsonObject = new JSONObject(json); JSONObject general = jsonObject.getJSONObject("general"); importToPrefs(general, generalSharedPrefs); JSONObject admin = jsonObject.getJSONObject("admin"); importToPrefs(admin, adminSharedPrefs); } catch (JSONException ignored) { } preferenceMigrator.migrate(generalSharedPrefs, adminSharedPrefs); clearUnknownKeys(generalSharedPrefs, generalDefaults); clearUnknownKeys(adminSharedPrefs, adminDefaults); loadDefaults(generalSharedPrefs, generalDefaults); loadDefaults(adminSharedPrefs, adminDefaults); for (Map.Entry<String, ?> entry: generalSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } for (Map.Entry<String, ?> entry: adminSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } return true; } SettingsImporter(SharedPreferences generalSharedPrefs, SharedPreferences adminSharedPrefs, SettingsPreferenceMigrator preferenceMigrator, SettingsValidator settingsValidator, Map<String, Object> generalDefaults, Map<String, Object> adminDefaults, SettingsChangeHandler settingsChangedHandler); }
SettingsImporter { public boolean fromJSON(@NonNull String json) { if (!settingsValidator.isValid(json)) { return false; } generalSharedPrefs.edit().clear().apply(); adminSharedPrefs.edit().clear().apply(); try { JSONObject jsonObject = new JSONObject(json); JSONObject general = jsonObject.getJSONObject("general"); importToPrefs(general, generalSharedPrefs); JSONObject admin = jsonObject.getJSONObject("admin"); importToPrefs(admin, adminSharedPrefs); } catch (JSONException ignored) { } preferenceMigrator.migrate(generalSharedPrefs, adminSharedPrefs); clearUnknownKeys(generalSharedPrefs, generalDefaults); clearUnknownKeys(adminSharedPrefs, adminDefaults); loadDefaults(generalSharedPrefs, generalDefaults); loadDefaults(adminSharedPrefs, adminDefaults); for (Map.Entry<String, ?> entry: generalSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } for (Map.Entry<String, ?> entry: adminSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } return true; } SettingsImporter(SharedPreferences generalSharedPrefs, SharedPreferences adminSharedPrefs, SettingsPreferenceMigrator preferenceMigrator, SettingsValidator settingsValidator, Map<String, Object> generalDefaults, Map<String, Object> adminDefaults, SettingsChangeHandler settingsChangedHandler); boolean fromJSON(@NonNull String json); }
SettingsImporter { public boolean fromJSON(@NonNull String json) { if (!settingsValidator.isValid(json)) { return false; } generalSharedPrefs.edit().clear().apply(); adminSharedPrefs.edit().clear().apply(); try { JSONObject jsonObject = new JSONObject(json); JSONObject general = jsonObject.getJSONObject("general"); importToPrefs(general, generalSharedPrefs); JSONObject admin = jsonObject.getJSONObject("admin"); importToPrefs(admin, adminSharedPrefs); } catch (JSONException ignored) { } preferenceMigrator.migrate(generalSharedPrefs, adminSharedPrefs); clearUnknownKeys(generalSharedPrefs, generalDefaults); clearUnknownKeys(adminSharedPrefs, adminDefaults); loadDefaults(generalSharedPrefs, generalDefaults); loadDefaults(adminSharedPrefs, adminDefaults); for (Map.Entry<String, ?> entry: generalSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } for (Map.Entry<String, ?> entry: adminSharedPrefs.getAll().entrySet()) { settingsChangedHandler.onSettingChanged(entry.getKey(), entry.getValue()); } return true; } SettingsImporter(SharedPreferences generalSharedPrefs, SharedPreferences adminSharedPrefs, SettingsPreferenceMigrator preferenceMigrator, SettingsValidator settingsValidator, Map<String, Object> generalDefaults, Map<String, Object> adminDefaults, SettingsChangeHandler settingsChangedHandler); boolean fromJSON(@NonNull String json);  }