Search in sources :

Example 6 with OsmandPreference

use of net.osmand.plus.settings.backend.preferences.OsmandPreference in project Osmand by osmandapp.

the class ProfileSettingsItem method getReader.

@Nullable
@Override
public SettingsItemReader<? extends SettingsItem> getReader() {
    return new OsmandSettingsItemReader<ProfileSettingsItem>(this, getSettings()) {

        @Override
        protected void readPreferenceFromJson(@NonNull OsmandPreference<?> preference, @NonNull JSONObject json) throws JSONException {
            if (!appModeBeanPrefsIds.contains(preference.getId())) {
                preference.readFromJson(json, appMode);
            }
        }

        @Override
        public void readPreferencesFromJson(final JSONObject json) {
            getSettings().getContext().runInUIThread(() -> {
                OsmandSettings settings = getSettings();
                Map<String, OsmandPreference<?>> prefs = settings.getRegisteredPreferences();
                Iterator<String> iterator = json.keys();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    OsmandPreference<?> p = prefs.get(key);
                    if (p == null) {
                        if (OsmandSettings.isRoutingPreference(key)) {
                            p = settings.registerStringPreference(key, "");
                        }
                    }
                    if (p != null) {
                        try {
                            readPreferenceFromJson(p, json);
                            if (OsmandSettings.isRoutingPreference(p.getId())) {
                                if (p.getId().endsWith(GeneralRouter.USE_SHORTEST_WAY)) {
                                    settings.FAST_ROUTE_MODE.setModeValue(appMode, !settings.getCustomRoutingBooleanProperty(GeneralRouter.USE_SHORTEST_WAY, false).getModeValue(appMode));
                                }
                            }
                        } catch (Exception e) {
                            SettingsHelper.LOG.error("Failed to read preference: " + key, e);
                        }
                    } else {
                        SettingsHelper.LOG.warn("No preference while importing settings: " + key);
                    }
                }
            });
        }
    };
}
Also used : JSONObject(org.json.JSONObject) NonNull(androidx.annotation.NonNull) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) OsmandSettingsItemReader(net.osmand.plus.settings.backend.backup.OsmandSettingsItemReader) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) JSONException(org.json.JSONException) Nullable(androidx.annotation.Nullable)

Aggregations

OsmandPreference (net.osmand.plus.settings.backend.preferences.OsmandPreference)6 OsmandApplication (net.osmand.plus.OsmandApplication)3 JSONObject (org.json.JSONObject)3 View (android.view.View)2 BooleanPreference (net.osmand.plus.settings.backend.preferences.BooleanPreference)2 CommonPreference (net.osmand.plus.settings.backend.preferences.CommonPreference)2 SwitchPreferenceEx (net.osmand.plus.settings.preferences.SwitchPreferenceEx)2 JSONException (org.json.JSONException)2 Context (android.content.Context)1 Button (android.widget.Button)1 LayoutParams (android.widget.LinearLayout.LayoutParams)1 ScrollView (android.widget.ScrollView)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 Fragment (androidx.fragment.app.Fragment)1 Preference (androidx.preference.Preference)1 PreferenceScreen (androidx.preference.PreferenceScreen)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1