Search in sources :

Example 1 with SpeedConstants

use of net.osmand.plus.settings.enums.SpeedConstants 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 2 with SpeedConstants

use of net.osmand.plus.settings.enums.SpeedConstants in project Osmand by osmandapp.

the class VehicleParametersFragment method showSeekbarSettingsDialog.

private void showSeekbarSettingsDialog(@NonNull Activity activity, final boolean defaultSpeedOnly) {
    final ApplicationMode mode = getSelectedAppMode();
    SpeedConstants units = app.getSettings().SPEED_SYSTEM.getModeValue(mode);
    String speedUnits = units.toShortString(activity);
    final float[] ratio = new float[1];
    switch(units) {
        case MILES_PER_HOUR:
            ratio[0] = 3600 / OsmAndFormatter.METERS_IN_ONE_MILE;
            break;
        case KILOMETERS_PER_HOUR:
            ratio[0] = 3600 / OsmAndFormatter.METERS_IN_KILOMETER;
            break;
        case MINUTES_PER_KILOMETER:
            ratio[0] = 3600 / OsmAndFormatter.METERS_IN_KILOMETER;
            speedUnits = activity.getString(R.string.km_h);
            break;
        case NAUTICALMILES_PER_HOUR:
            ratio[0] = 3600 / OsmAndFormatter.METERS_IN_ONE_NAUTICALMILE;
            break;
        case MINUTES_PER_MILE:
            ratio[0] = 3600 / OsmAndFormatter.METERS_IN_ONE_MILE;
            speedUnits = activity.getString(R.string.mile_per_hour);
            break;
        case METERS_PER_SECOND:
            ratio[0] = 1;
            break;
    }
    float settingsMinSpeed = mode.getMinSpeed();
    float settingsMaxSpeed = mode.getMaxSpeed();
    float settingsDefaultSpeed = mode.getDefaultSpeed();
    final int[] defaultValue = { Math.round(settingsDefaultSpeed * ratio[0]) };
    final int[] minValue = new int[1];
    final int[] maxValue = new int[1];
    final int min;
    final int max;
    GeneralRouter router = app.getRouter(mode);
    if (defaultSpeedOnly || router == null) {
        minValue[0] = Math.round(Math.min(1, settingsDefaultSpeed) * ratio[0]);
        maxValue[0] = Math.round(Math.max(300, settingsDefaultSpeed) * ratio[0]);
        min = minValue[0];
        max = maxValue[0];
    } else {
        float minSpeedValue = settingsMinSpeed > 0 ? settingsMinSpeed : router.getMinSpeed();
        float maxSpeedValue = settingsMaxSpeed > 0 ? settingsMaxSpeed : router.getMaxSpeed();
        minValue[0] = Math.round(Math.min(minSpeedValue, settingsDefaultSpeed) * ratio[0]);
        maxValue[0] = Math.round(Math.max(maxSpeedValue, settingsDefaultSpeed) * ratio[0]);
        min = Math.round(Math.min(minValue[0], router.getMinSpeed() * ratio[0] / 2f));
        max = Math.round(Math.max(maxValue[0], router.getMaxSpeed() * ratio[0] * 1.5f));
    }
    boolean nightMode = !app.getSettings().isLightContentForMode(mode);
    Context themedContext = UiUtilities.getThemedContext(activity, nightMode);
    AlertDialog.Builder builder = new AlertDialog.Builder(themedContext);
    View seekbarView = LayoutInflater.from(themedContext).inflate(R.layout.default_speed_dialog, null, false);
    builder.setView(seekbarView);
    builder.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mode.setDefaultSpeed(defaultValue[0] / ratio[0]);
            if (!defaultSpeedOnly) {
                mode.setMinSpeed(minValue[0] / ratio[0]);
                mode.setMaxSpeed(maxValue[0] / ratio[0]);
            }
            app.getRoutingHelper().onSettingsChanged(mode);
        }
    });
    builder.setNegativeButton(R.string.shared_string_cancel, null);
    builder.setNeutralButton(R.string.shared_string_revert, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mode.resetDefaultSpeed();
            if (!defaultSpeedOnly) {
                mode.setMinSpeed(0f);
                mode.setMaxSpeed(0f);
            }
            app.getRoutingHelper().onSettingsChanged(mode);
        }
    });
    int selectedModeColor = mode.getProfileColor(nightMode);
    if (!defaultSpeedOnly) {
        setupSpeedSlider(SpeedSliderType.DEFAULT_SPEED, speedUnits, defaultValue, minValue, maxValue, min, max, seekbarView, selectedModeColor);
        setupSpeedSlider(SpeedSliderType.MIN_SPEED, speedUnits, defaultValue, minValue, maxValue, min, max, seekbarView, selectedModeColor);
        setupSpeedSlider(SpeedSliderType.MAX_SPEED, speedUnits, defaultValue, minValue, maxValue, min, max, seekbarView, selectedModeColor);
    } else {
        setupSpeedSlider(SpeedSliderType.DEFAULT_SPEED_ONLY, speedUnits, defaultValue, minValue, maxValue, min, max, seekbarView, selectedModeColor);
        seekbarView.findViewById(R.id.default_speed_div).setVisibility(View.GONE);
        seekbarView.findViewById(R.id.default_speed_container).setVisibility(View.GONE);
        seekbarView.findViewById(R.id.max_speed_div).setVisibility(View.GONE);
        seekbarView.findViewById(R.id.max_speed_container).setVisibility(View.GONE);
    }
    builder.show();
}
Also used : Context(android.content.Context) AlertDialog(androidx.appcompat.app.AlertDialog) DialogInterface(android.content.DialogInterface) GeneralRouter(net.osmand.router.GeneralRouter) SpeedConstants(net.osmand.plus.settings.enums.SpeedConstants) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView)

Example 3 with SpeedConstants

use of net.osmand.plus.settings.enums.SpeedConstants in project Osmand by osmandapp.

the class GeneralProfileSettingsFragment method setupSpeedSystemPref.

private void setupSpeedSystemPref() {
    SpeedConstants[] speedConstants = SpeedConstants.values();
    String[] entries = new String[speedConstants.length];
    Integer[] entryValues = new Integer[speedConstants.length];
    for (int i = 0; i < entries.length; i++) {
        entries[i] = speedConstants[i].toHumanString(app);
        entryValues[i] = speedConstants[i].ordinal();
    }
    ListPreferenceEx speedSystem = (ListPreferenceEx) findPreference(settings.SPEED_SYSTEM.getId());
    speedSystem.setEntries(entries);
    speedSystem.setEntryValues(entryValues);
    speedSystem.setDescription(R.string.default_speed_system_descr);
    speedSystem.setIcon(getActiveIcon(R.drawable.ic_action_speed));
}
Also used : ListPreferenceEx(net.osmand.plus.settings.preferences.ListPreferenceEx) SpeedConstants(net.osmand.plus.settings.enums.SpeedConstants)

Example 4 with SpeedConstants

use of net.osmand.plus.settings.enums.SpeedConstants in project Osmand by osmandapp.

the class GpxUiHelper method createGPXSpeedDataSet.

public static OrderedLineDataSet createGPXSpeedDataSet(@NonNull OsmandApplication ctx, @NonNull LineChart mChart, @NonNull GPXTrackAnalysis analysis, @NonNull GPXDataSetAxisType axisType, boolean useRightAxis, boolean drawFilled, boolean calcWithoutGaps) {
    OsmandSettings settings = ctx.getSettings();
    boolean light = settings.isLightContent();
    float divX;
    XAxis xAxis = mChart.getXAxis();
    if (axisType == GPXDataSetAxisType.TIME && analysis.isTimeSpecified()) {
        divX = setupXAxisTime(xAxis, calcWithoutGaps ? analysis.timeSpanWithoutGaps : analysis.timeSpan);
    } else if (axisType == GPXDataSetAxisType.TIMEOFDAY && analysis.isTimeSpecified()) {
        divX = setupXAxisTimeOfDay(xAxis, analysis.startTime);
    } else {
        divX = setupAxisDistance(ctx, xAxis, calcWithoutGaps ? analysis.totalDistanceWithoutGaps : analysis.totalDistance);
    }
    SpeedConstants sps = settings.SPEED_SYSTEM.get();
    float mulSpeed = Float.NaN;
    float divSpeed = Float.NaN;
    final String mainUnitY = sps.toShortString(ctx);
    if (sps == SpeedConstants.KILOMETERS_PER_HOUR) {
        mulSpeed = 3.6f;
    } else if (sps == SpeedConstants.MILES_PER_HOUR) {
        mulSpeed = 3.6f * METERS_IN_KILOMETER / METERS_IN_ONE_MILE;
    } else if (sps == SpeedConstants.NAUTICALMILES_PER_HOUR) {
        mulSpeed = 3.6f * METERS_IN_KILOMETER / METERS_IN_ONE_NAUTICALMILE;
    } else if (sps == SpeedConstants.MINUTES_PER_KILOMETER) {
        divSpeed = METERS_IN_KILOMETER / 60;
    } else if (sps == SpeedConstants.MINUTES_PER_MILE) {
        divSpeed = METERS_IN_ONE_MILE / 60;
    } else {
        mulSpeed = 1f;
    }
    YAxis yAxis;
    if (useRightAxis) {
        yAxis = mChart.getAxisRight();
        yAxis.setEnabled(true);
    } else {
        yAxis = mChart.getAxisLeft();
    }
    if (analysis.hasSpeedInTrack()) {
        yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange_label));
    } else {
        yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_red_label));
    }
    yAxis.setAxisMinimum(0f);
    ArrayList<Entry> values = new ArrayList<>();
    List<Speed> speedData = analysis.speedData;
    float nextX = 0;
    float nextY;
    float x;
    for (Speed s : speedData) {
        switch(axisType) {
            case TIMEOFDAY:
            case TIME:
                x = s.time;
                break;
            default:
                x = s.distance;
                break;
        }
        if (x > 0) {
            if (axisType == GPXDataSetAxisType.TIME && x > 60 || axisType == GPXDataSetAxisType.TIMEOFDAY && x > 60) {
                values.add(new Entry(nextX + 1, 0));
                values.add(new Entry(nextX + x - 1, 0));
            }
            if (!(calcWithoutGaps && s.firstPoint)) {
                nextX += x / divX;
            }
            if (Float.isNaN(divSpeed)) {
                nextY = s.speed * mulSpeed;
            } else {
                nextY = divSpeed / s.speed;
            }
            if (nextY < 0 || Float.isInfinite(nextY)) {
                nextY = 0;
            }
            if (s.firstPoint) {
                values.add(new Entry(nextX, 0));
            }
            values.add(new Entry(nextX, nextY));
            if (s.lastPoint) {
                values.add(new Entry(nextX, 0));
            }
        }
    }
    OrderedLineDataSet dataSet = new OrderedLineDataSet(values, "", GPXDataSetType.SPEED, axisType, !useRightAxis);
    String format = null;
    if (dataSet.getYMax() < 3) {
        format = "{0,number,0.#} ";
    }
    final String formatY = format;
    yAxis.setValueFormatter((value, axis) -> {
        if (!Algorithms.isEmpty(formatY)) {
            return MessageFormat.format(formatY + mainUnitY, value);
        } else {
            return OsmAndFormatter.formatInteger((int) value, mainUnitY, ctx);
        }
    });
    if (Float.isNaN(divSpeed)) {
        dataSet.priority = analysis.avgSpeed * mulSpeed;
    } else {
        dataSet.priority = divSpeed / analysis.avgSpeed;
    }
    dataSet.divX = divX;
    if (Float.isNaN(divSpeed)) {
        dataSet.mulY = mulSpeed;
        dataSet.divY = Float.NaN;
    } else {
        dataSet.divY = divSpeed;
        dataSet.mulY = Float.NaN;
    }
    dataSet.units = mainUnitY;
    if (analysis.hasSpeedInTrack()) {
        dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange));
    } else {
        dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_red));
    }
    dataSet.setLineWidth(1f);
    if (drawFilled) {
        dataSet.setFillAlpha(128);
        if (analysis.hasSpeedInTrack()) {
            dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange));
        } else {
            dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_red));
        }
        dataSet.setDrawFilled(true);
    } else {
        dataSet.setDrawFilled(false);
    }
    dataSet.setDrawValues(false);
    dataSet.setValueTextSize(9f);
    dataSet.setFormLineWidth(1f);
    dataSet.setFormSize(15.f);
    dataSet.setDrawCircles(false);
    dataSet.setDrawCircleHole(false);
    dataSet.setHighlightEnabled(true);
    dataSet.setDrawVerticalHighlightIndicator(true);
    dataSet.setDrawHorizontalHighlightIndicator(false);
    dataSet.setHighLightColor(ColorUtilities.getSecondaryTextColor(mChart.getContext(), !light));
    if (useRightAxis) {
        dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
    }
    return dataSet;
}
Also used : Speed(net.osmand.GPXUtilities.Speed) ArrayList(java.util.ArrayList) SpeedConstants(net.osmand.plus.settings.enums.SpeedConstants) SpannableString(android.text.SpannableString) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) XAxis(com.github.mikephil.charting.components.XAxis) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) YAxis(com.github.mikephil.charting.components.YAxis)

Example 5 with SpeedConstants

use of net.osmand.plus.settings.enums.SpeedConstants in project Osmand by osmandapp.

the class OsmAndFormatter method getFormattedSpeedValue.

@NonNull
public static FormattedValue getFormattedSpeedValue(float metersPerSeconds, @NonNull OsmandApplication ctx) {
    OsmandSettings settings = ctx.getSettings();
    SpeedConstants mc = settings.SPEED_SYSTEM.get();
    String unit = mc.toShortString(ctx);
    ApplicationMode am = settings.getApplicationMode();
    float kmh = metersPerSeconds * 3.6f;
    if (mc == SpeedConstants.KILOMETERS_PER_HOUR) {
        // e.g. car case and for high-speeds: Display rounded to 1 km/h (5% precision at 20 km/h)
        if (kmh >= 20 || am.hasFastSpeed()) {
            return getFormattedSpeed(Math.round(kmh), unit, ctx);
        }
        // for smaller values display 1 decimal digit x.y km/h, (0.5% precision at 20 km/h)
        int kmh10 = Math.round(kmh * 10f);
        return getFormattedSpeed(kmh10 / 10f, unit, ctx);
    } else if (mc == SpeedConstants.MILES_PER_HOUR) {
        float mph = kmh * METERS_IN_KILOMETER / METERS_IN_ONE_MILE;
        if (mph >= 20 || am.hasFastSpeed()) {
            return getFormattedSpeed(Math.round(mph), unit, ctx);
        } else {
            int mph10 = Math.round(mph * 10f);
            return getFormattedSpeed(mph10 / 10f, unit, ctx);
        }
    } else if (mc == SpeedConstants.NAUTICALMILES_PER_HOUR) {
        float mph = kmh * METERS_IN_KILOMETER / METERS_IN_ONE_NAUTICALMILE;
        if (mph >= 20 || am.hasFastSpeed()) {
            return getFormattedSpeed(Math.round(mph), unit, ctx);
        } else {
            int mph10 = Math.round(mph * 10f);
            return getFormattedSpeed(mph10 / 10f, unit, ctx);
        }
    } else if (mc == SpeedConstants.MINUTES_PER_KILOMETER) {
        if (metersPerSeconds < 0.111111111) {
            return new FormattedValue("-", unit, false);
        }
        float minPerKm = METERS_IN_KILOMETER / (metersPerSeconds * 60);
        if (minPerKm >= 10) {
            return getFormattedSpeed(Math.round(minPerKm), unit, ctx);
        } else {
            int seconds = Math.round(minPerKm * 60);
            return new FormattedValue(Algorithms.formatDuration(seconds, false), unit);
        }
    } else if (mc == SpeedConstants.MINUTES_PER_MILE) {
        if (metersPerSeconds < 0.111111111) {
            return new FormattedValue("-", unit, false);
        }
        float minPerM = (METERS_IN_ONE_MILE) / (metersPerSeconds * 60);
        if (minPerM >= 10) {
            return getFormattedSpeed(Math.round(minPerM), unit, ctx);
        } else {
            int mph10 = Math.round(minPerM * 10f);
            return getFormattedSpeed(mph10 / 10f, unit, ctx);
        }
    } else {
        String metersPerSecond = SpeedConstants.METERS_PER_SECOND.toShortString(ctx);
        if (metersPerSeconds >= 10) {
            return getFormattedSpeed(Math.round(metersPerSeconds), metersPerSecond, ctx);
        }
        // for smaller values display 1 decimal digit x.y km/h, (0.5% precision at 20 km/h)
        int kmh10 = Math.round(metersPerSeconds * 10f);
        return getFormattedSpeed(kmh10 / 10f, metersPerSecond, ctx);
    }
}
Also used : SpeedConstants(net.osmand.plus.settings.enums.SpeedConstants) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) OsmandSettings(net.osmand.plus.settings.backend.OsmandSettings) ZonedUTMPoint(com.jwetherell.openmap.common.ZonedUTMPoint) MGRSPoint(com.jwetherell.openmap.common.MGRSPoint) LatLonPoint(com.jwetherell.openmap.common.LatLonPoint) NonNull(androidx.annotation.NonNull)

Aggregations

SpeedConstants (net.osmand.plus.settings.enums.SpeedConstants)5 ApplicationMode (net.osmand.plus.settings.backend.ApplicationMode)2 OsmandSettings (net.osmand.plus.settings.backend.OsmandSettings)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 DialogInterface (android.content.DialogInterface)1 SpannableString (android.text.SpannableString)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 AlertDialog (androidx.appcompat.app.AlertDialog)1 XAxis (com.github.mikephil.charting.components.XAxis)1 YAxis (com.github.mikephil.charting.components.YAxis)1 BarEntry (com.github.mikephil.charting.data.BarEntry)1 Entry (com.github.mikephil.charting.data.Entry)1 LatLonPoint (com.jwetherell.openmap.common.LatLonPoint)1 MGRSPoint (com.jwetherell.openmap.common.MGRSPoint)1 ZonedUTMPoint (com.jwetherell.openmap.common.ZonedUTMPoint)1 ArrayList (java.util.ArrayList)1