Search in sources :

Example 1 with StringPreference

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

the class OsmandSettings method registerCustomRenderProperty.

public CommonPreference<String> registerCustomRenderProperty(@NonNull String attrName, @Nullable String defaultValue) {
    CommonPreference<String> preference = new StringPreference(this, RENDERER_PREFERENCE_PREFIX + attrName, defaultValue).makeProfile();
    customRendersProps.put(attrName, preference);
    return preference;
}
Also used : ListStringPreference(net.osmand.plus.settings.backend.preferences.ListStringPreference) BooleanStringPreference(net.osmand.plus.settings.backend.preferences.BooleanStringPreference) StringPreference(net.osmand.plus.settings.backend.preferences.StringPreference) EnumStringPreference(net.osmand.plus.settings.backend.preferences.EnumStringPreference)

Example 2 with StringPreference

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

the class OsmandSettings method setPreference.

// TODO doesn't look correct
@SuppressWarnings("unchecked")
public boolean setPreference(String key, Object value, ApplicationMode mode) {
    OsmandPreference<?> preference = registeredPreferences.get(key);
    if (preference != null) {
        if (preference == APPLICATION_MODE) {
            if (value instanceof String) {
                String appModeKey = (String) value;
                ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null);
                if (appMode != null) {
                    setApplicationMode(appMode);
                    return true;
                }
            }
        } else if (preference == DEFAULT_APPLICATION_MODE) {
            if (value instanceof String) {
                String appModeKey = (String) value;
                ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null);
                if (appMode != null) {
                    DEFAULT_APPLICATION_MODE.set(appMode);
                    return true;
                }
            }
        } else if (preference == METRIC_SYSTEM) {
            MetricsConstants metricSystem = null;
            if (value instanceof String) {
                String metricSystemName = (String) value;
                try {
                    metricSystem = MetricsConstants.valueOf(metricSystemName);
                } catch (IllegalArgumentException e) {
                    return false;
                }
            } else if (value instanceof Integer) {
                int index = (Integer) value;
                if (index >= 0 && index < MetricsConstants.values().length) {
                    metricSystem = MetricsConstants.values()[index];
                }
            }
            if (metricSystem != null) {
                METRIC_SYSTEM.setModeValue(mode, metricSystem);
                return true;
            }
        } else if (preference == SPEED_SYSTEM) {
            SpeedConstants speedSystem = null;
            if (value instanceof String) {
                String speedSystemName = (String) value;
                try {
                    speedSystem = SpeedConstants.valueOf(speedSystemName);
                } catch (IllegalArgumentException e) {
                    return false;
                }
            } else if (value instanceof Integer) {
                int index = (Integer) value;
                if (index >= 0 && index < SpeedConstants.values().length) {
                    speedSystem = SpeedConstants.values()[index];
                }
            }
            if (speedSystem != null) {
                SPEED_SYSTEM.setModeValue(mode, speedSystem);
                return true;
            }
        } else if (preference instanceof BooleanPreference) {
            if (value instanceof Boolean) {
                ((BooleanPreference) preference).setModeValue(mode, (Boolean) value);
                return true;
            }
        } else if (preference instanceof StringPreference) {
            if (value instanceof String) {
                ((StringPreference) preference).setModeValue(mode, (String) value);
                return true;
            }
        } else if (preference instanceof FloatPreference) {
            if (value instanceof Float) {
                ((FloatPreference) preference).setModeValue(mode, (Float) value);
                return true;
            }
        } else if (preference instanceof IntPreference) {
            if (value instanceof Integer) {
                ((IntPreference) preference).setModeValue(mode, (Integer) value);
                return true;
            }
        } else if (preference instanceof LongPreference) {
            if (value instanceof Long) {
                ((LongPreference) preference).setModeValue(mode, (Long) value);
                return true;
            }
        } else if (preference instanceof EnumStringPreference) {
            EnumStringPreference enumPref = (EnumStringPreference) preference;
            if (value instanceof String) {
                Enum<?> enumValue = enumPref.parseString((String) value);
                if (enumValue != null) {
                    return enumPref.setModeValue(mode, enumValue);
                }
                return false;
            } else if (value instanceof Enum) {
                return enumPref.setModeValue(mode, value);
            } else if (value instanceof Integer) {
                int newVal = (Integer) value;
                if (enumPref.getValues().length > newVal) {
                    Enum<?> enumValue = enumPref.getValues()[newVal];
                    return enumPref.setModeValue(mode, enumValue);
                }
                return false;
            }
        } else if (preference instanceof ContextMenuItemsPreference) {
            if (value instanceof ContextMenuItemsSettings) {
                ((ContextMenuItemsPreference) preference).setModeValue(mode, (ContextMenuItemsSettings) value);
            }
        }
    }
    return false;
}
Also used : FloatPreference(net.osmand.plus.settings.backend.preferences.FloatPreference) SpeedConstants(net.osmand.plus.settings.enums.SpeedConstants) ContextMenuItemsPreference(net.osmand.plus.settings.backend.preferences.ContextMenuItemsPreference) IntPreference(net.osmand.plus.settings.backend.preferences.IntPreference) ListStringPreference(net.osmand.plus.settings.backend.preferences.ListStringPreference) BooleanStringPreference(net.osmand.plus.settings.backend.preferences.BooleanStringPreference) StringPreference(net.osmand.plus.settings.backend.preferences.StringPreference) EnumStringPreference(net.osmand.plus.settings.backend.preferences.EnumStringPreference) SuppressLint(android.annotation.SuppressLint) BooleanPreference(net.osmand.plus.settings.backend.preferences.BooleanPreference) ContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings) MainContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings) MetricsConstants(net.osmand.plus.settings.enums.MetricsConstants) LongPreference(net.osmand.plus.settings.backend.preferences.LongPreference) EnumStringPreference(net.osmand.plus.settings.backend.preferences.EnumStringPreference)

Example 3 with StringPreference

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

the class OsmandSettings method registerStringPreference.

@SuppressWarnings("unchecked")
public CommonPreference<String> registerStringPreference(String id, String defValue) {
    if (registeredPreferences.containsKey(id)) {
        return (CommonPreference<String>) registeredPreferences.get(id);
    }
    StringPreference p = new StringPreference(this, id, defValue);
    registeredPreferences.put(id, p);
    return p;
}
Also used : CommonPreference(net.osmand.plus.settings.backend.preferences.CommonPreference) ListStringPreference(net.osmand.plus.settings.backend.preferences.ListStringPreference) BooleanStringPreference(net.osmand.plus.settings.backend.preferences.BooleanStringPreference) StringPreference(net.osmand.plus.settings.backend.preferences.StringPreference) EnumStringPreference(net.osmand.plus.settings.backend.preferences.EnumStringPreference)

Example 4 with StringPreference

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

the class VehicleParametersFragment method setupCustomRoutingPropertyPref.

private void setupCustomRoutingPropertyPref(@Nullable RoutingParameter parameter, GeneralRouterProfile routerProfile) {
    if (parameter == null) {
        return;
    }
    String parameterId = parameter.getId();
    String title = AndroidUtils.getRoutingStringPropertyName(app, parameterId, parameter.getName());
    String description = AndroidUtils.getRoutingStringPropertyDescription(app, parameterId, parameter.getDescription());
    String defValue = parameter.getType() == RoutingParameterType.NUMERIC ? ROUTING_PARAMETER_NUMERIC_DEFAULT : ROUTING_PARAMETER_SYMBOLIC_DEFAULT;
    StringPreference pref = (StringPreference) app.getSettings().getCustomRoutingProperty(parameterId, defValue);
    VehicleSizeAssets assets = VehicleSizeAssets.getAssets(parameterId, routerProfile);
    Object[] values = parameter.getPossibleValues();
    String[] valuesStr = new String[values.length];
    for (int i = 0; i < values.length; i++) {
        valuesStr[i] = values[i].toString();
    }
    String[] entriesStr = parameter.getPossibleValueDescriptions().clone();
    entriesStr[0] = app.getString(R.string.shared_string_none);
    for (int i = 1; i < entriesStr.length; i++) {
        int firstCharIndex = Algorithms.findFirstNumberEndIndex(entriesStr[i]);
        entriesStr[i] = String.format(app.getString(R.string.ltr_or_rtl_combine_via_space), entriesStr[i].substring(0, firstCharIndex), getString(assets.getMetricShortRes()));
    }
    Context ctx = getContext();
    if (ctx == null) {
        return;
    }
    SizePreference vehicleSizePref = new SizePreference(ctx);
    vehicleSizePref.setKey(pref.getId());
    vehicleSizePref.setAssets(assets);
    vehicleSizePref.setDefaultValue(defValue);
    vehicleSizePref.setTitle(title);
    vehicleSizePref.setEntries(entriesStr);
    vehicleSizePref.setEntryValues(valuesStr);
    vehicleSizePref.setSummary(description);
    vehicleSizePref.setIcon(getPreferenceIcon(parameterId));
    vehicleSizePref.setLayoutResource(R.layout.preference_with_descr);
    getPreferenceScreen().addPreference(vehicleSizePref);
}
Also used : Context(android.content.Context) VehicleSizeAssets(net.osmand.plus.settings.bottomsheets.VehicleSizeAssets) SizePreference(net.osmand.plus.settings.preferences.SizePreference) StringPreference(net.osmand.plus.settings.backend.preferences.StringPreference)

Aggregations

StringPreference (net.osmand.plus.settings.backend.preferences.StringPreference)4 BooleanStringPreference (net.osmand.plus.settings.backend.preferences.BooleanStringPreference)3 EnumStringPreference (net.osmand.plus.settings.backend.preferences.EnumStringPreference)3 ListStringPreference (net.osmand.plus.settings.backend.preferences.ListStringPreference)3 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 ContextMenuItemsSettings (net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings)1 MainContextMenuItemsSettings (net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings)1 BooleanPreference (net.osmand.plus.settings.backend.preferences.BooleanPreference)1 CommonPreference (net.osmand.plus.settings.backend.preferences.CommonPreference)1 ContextMenuItemsPreference (net.osmand.plus.settings.backend.preferences.ContextMenuItemsPreference)1 FloatPreference (net.osmand.plus.settings.backend.preferences.FloatPreference)1 IntPreference (net.osmand.plus.settings.backend.preferences.IntPreference)1 LongPreference (net.osmand.plus.settings.backend.preferences.LongPreference)1 VehicleSizeAssets (net.osmand.plus.settings.bottomsheets.VehicleSizeAssets)1 MetricsConstants (net.osmand.plus.settings.enums.MetricsConstants)1 SpeedConstants (net.osmand.plus.settings.enums.SpeedConstants)1 SizePreference (net.osmand.plus.settings.preferences.SizePreference)1