Search in sources :

Example 1 with OsmandPreference

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

the class TestVoiceActivity method addButton.

public void addButton(ViewGroup layout, final String description, final CommandBuilder builder) {
    final Button button = new Button(this);
    button.setGravity(Gravity.LEFT);
    // or else button text is all upper case
    button.setTransformationMethod(null);
    button.setText(description);
    button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    if (!description.startsWith("\u25BA (")) {
        // Section headline buttons
        button.setPadding(10, 20, 10, 5);
    } else {
        button.setPadding(40, 5, 10, 5);
    }
    if (description.startsWith("\u25BA (11.1)")) {
        // Buttons with refreshable caption
        buttonInfo = button;
    } else if (description.startsWith("\u25BA (11.2)")) {
        buttonDelay = button;
    } else if (description.startsWith("\u25BA (11.3)")) {
        buttonDisplay = button;
    }
    layout.addView(button);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            builder.play();
            if (description.startsWith("\u25BA (11.1)")) {
                buttonInfo.setText("\u25BA (11.1) Voice system info:\n" + getVoiceSystemInfo());
            // Toast.makeText(TestVoiceActivity.this, "Info refreshed.", Toast.LENGTH_LONG).show();
            }
            if (description.startsWith("\u25BA (11.2)")) {
                int ams = ((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get();
                OsmandPreference<Integer> pref = ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[ams];
                if (pref != null) {
                    if (pref.get() >= 3000) {
                        pref.set(0);
                    } else {
                        pref.set(pref.get() + 500);
                    }
                // Toast.makeText(TestVoiceActivity.this, "Voice prompt delay changed to " + pref.get() + "\u00A0ms.", Toast.LENGTH_LONG).show();
                }
                buttonDelay.setText("\u25BA (11.2) (TAP TO CHANGE)\n \u25CF Voice prompt delay for selected output: " + ((OsmandApplication) getApplication()).getSettings().VOICE_PROMPT_DELAY[((OsmandApplication) getApplication()).getSettings().AUDIO_MANAGER_STREAM.get()].get() + "\u00A0ms\n (Avoids car stereo cutting off prompts. Default is 1500\u00A0ms for Phone call audio, or else 0\u00A0ms.)");
            }
            if (description.startsWith("\u25BA (11.3)")) {
                if (((OsmandApplication) getApplication()).getSettings().DISPLAY_TTS_UTTERANCE.get() == false) {
                    ((OsmandApplication) getApplication()).getSettings().DISPLAY_TTS_UTTERANCE.set(true);
                } else {
                    ((OsmandApplication) getApplication()).getSettings().DISPLAY_TTS_UTTERANCE.set(false);
                }
                buttonDisplay.setText("\u25BA (11.3) (TAP TO TOGGLE)\n \u25CF Display each TTS utterance on screen: " + ((OsmandApplication) getApplication()).getSettings().DISPLAY_TTS_UTTERANCE.get().toString());
            }
        }
    });
}
Also used : LayoutParams(android.widget.LinearLayout.LayoutParams) OsmandApplication(net.osmand.plus.OsmandApplication) Button(android.widget.Button) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView)

Example 2 with OsmandPreference

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

the class BooleanPreferenceBottomSheet method createMenuItems.

@Override
public void createMenuItems(Bundle savedInstanceState) {
    final OsmandApplication app = getMyApplication();
    if (app == null) {
        return;
    }
    final SwitchPreferenceEx switchPreference = getSwitchPreferenceEx();
    if (switchPreference == null) {
        return;
    }
    OsmandPreference preference = app.getSettings().getPreference(switchPreference.getKey());
    if (!(preference instanceof BooleanPreference)) {
        return;
    }
    Context themedCtx = UiUtilities.getThemedContext(app, nightMode);
    String title = switchPreference.getTitle().toString();
    items.add(new TitleItem(title));
    final BooleanPreference pref = (BooleanPreference) preference;
    CharSequence summaryOn = switchPreference.getSummaryOn();
    CharSequence summaryOff = switchPreference.getSummaryOff();
    final String on = summaryOn == null || summaryOn.toString().isEmpty() ? getString(R.string.shared_string_enabled) : summaryOn.toString();
    final String off = summaryOff == null || summaryOff.toString().isEmpty() ? getString(R.string.shared_string_disabled) : summaryOff.toString();
    final int activeColor = AndroidUtils.resolveAttribute(themedCtx, R.attr.active_color_basic);
    final int disabledColor = AndroidUtils.resolveAttribute(themedCtx, android.R.attr.textColorSecondary);
    boolean checked = switchPreference.isChecked();
    final BottomSheetItemWithCompoundButton[] preferenceBtn = new BottomSheetItemWithCompoundButton[1];
    preferenceBtn[0] = (BottomSheetItemWithCompoundButton) new BottomSheetItemWithCompoundButton.Builder().setChecked(checked).setTitle(checked ? on : off).setTitleColorId(checked ? activeColor : disabledColor).setCustomView(getCustomButtonView(app, getAppMode(), checked, nightMode)).setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            boolean newValue = !switchPreference.isChecked();
            Fragment targetFragment = getTargetFragment();
            if (targetFragment instanceof OnConfirmPreferenceChange) {
                ApplyQueryType applyQueryType = getApplyQueryType();
                if (applyQueryType == ApplyQueryType.SNACK_BAR) {
                    applyQueryType = ApplyQueryType.NONE;
                }
                OnConfirmPreferenceChange confirmationInterface = (OnConfirmPreferenceChange) targetFragment;
                if (confirmationInterface.onConfirmPreferenceChange(switchPreference.getKey(), newValue, applyQueryType)) {
                    switchPreference.setChecked(newValue);
                    preferenceBtn[0].setTitle(newValue ? on : off);
                    preferenceBtn[0].setChecked(newValue);
                    preferenceBtn[0].setTitleColorId(newValue ? activeColor : disabledColor);
                    updateCustomButtonView(app, getAppMode(), v, newValue, nightMode);
                    if (targetFragment instanceof OnPreferenceChanged) {
                        ((OnPreferenceChanged) targetFragment).onPreferenceChanged(switchPreference.getKey());
                    }
                }
            }
        }
    }).create();
    if (isProfileDependent()) {
        preferenceBtn[0].setCompoundButtonColor(getAppMode().getProfileColor(nightMode));
    }
    items.add(preferenceBtn[0]);
    String description = switchPreference.getDescription();
    if (description != null) {
        BaseBottomSheetItem preferenceDescription = new BottomSheetItemWithDescription.Builder().setDescription(description).setLayoutId(R.layout.bottom_sheet_item_descr).create();
        items.add(preferenceDescription);
    }
}
Also used : Context(android.content.Context) BaseBottomSheetItem(net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem) OsmandApplication(net.osmand.plus.OsmandApplication) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) TitleItem(net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem) SwitchPreferenceEx(net.osmand.plus.settings.preferences.SwitchPreferenceEx) OnConfirmPreferenceChange(net.osmand.plus.settings.fragments.OnConfirmPreferenceChange) View(android.view.View) Fragment(androidx.fragment.app.Fragment) BottomSheetItemWithDescription(net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription) BooleanPreference(net.osmand.plus.settings.backend.preferences.BooleanPreference) ApplyQueryType(net.osmand.plus.settings.fragments.ApplyQueryType) BottomSheetItemWithCompoundButton(net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton) OnPreferenceChanged(net.osmand.plus.settings.fragments.OnPreferenceChanged)

Example 3 with OsmandPreference

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

the class OsmandSettings method copyProfilePreferences.

public void copyProfilePreferences(ApplicationMode modeFrom, ApplicationMode modeTo, List<OsmandPreference> profilePreferences) {
    for (OsmandPreference pref : profilePreferences) {
        if (prefCanBeCopiedOrReset(pref) && !USER_PROFILE_NAME.getId().equals(pref.getId())) {
            CommonPreference profilePref = (CommonPreference) pref;
            if (PARENT_APP_MODE.getId().equals(pref.getId())) {
                if (modeTo.isCustomProfile()) {
                    modeTo.setParentAppMode(modeFrom.isCustomProfile() ? modeFrom.getParent() : modeFrom);
                }
            } else {
                Object copiedValue = profilePref.getModeValue(modeFrom);
                profilePref.setModeValue(modeTo, copiedValue);
            }
        }
    }
}
Also used : CommonPreference(net.osmand.plus.settings.backend.preferences.CommonPreference) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) JSONObject(org.json.JSONObject)

Example 4 with OsmandPreference

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

the class RouteParametersFragment method setupRoutingPrefs.

private void setupRoutingPrefs() {
    OsmandApplication app = getMyApplication();
    if (app == null) {
        return;
    }
    PreferenceScreen screen = getPreferenceScreen();
    ApplicationMode am = getSelectedAppMode();
    SwitchPreferenceEx fastRoute = createSwitchPreferenceEx(app.getSettings().FAST_ROUTE_MODE.getId(), R.string.fast_route_mode, R.layout.preference_with_descr_dialog_and_switch);
    fastRoute.setIcon(getRoutingPrefIcon(app.getSettings().FAST_ROUTE_MODE.getId()));
    fastRoute.setDescription(getString(R.string.fast_route_mode_descr));
    fastRoute.setSummaryOn(R.string.shared_string_on);
    fastRoute.setSummaryOff(R.string.shared_string_off);
    if (am.getRouteService() == RouteService.OSMAND) {
        GeneralRouter router = app.getRouter(am);
        clearParameters();
        if (router != null) {
            Map<String, RoutingParameter> parameters = router.getParameters();
            if (!am.isDerivedRoutingFrom(ApplicationMode.CAR)) {
                screen.addPreference(fastRoute);
            }
            for (Map.Entry<String, RoutingParameter> e : parameters.entrySet()) {
                String param = e.getKey();
                RoutingParameter routingParameter = e.getValue();
                if (param.startsWith(AVOID_ROUTING_PARAMETER_PREFIX)) {
                    avoidParameters.add(routingParameter);
                } else if (param.startsWith(PREFER_ROUTING_PARAMETER_PREFIX)) {
                    preferParameters.add(routingParameter);
                } else if (RELIEF_SMOOTHNESS_FACTOR.equals(routingParameter.getGroup())) {
                    reliefFactorParameters.add(routingParameter);
                } else if (DRIVING_STYLE.equals(routingParameter.getGroup())) {
                    drivingStyleParameters.add(routingParameter);
                } else if ((!param.equals(GeneralRouter.USE_SHORTEST_WAY) || am.isDerivedRoutingFrom(ApplicationMode.CAR)) && !param.equals(GeneralRouter.VEHICLE_HEIGHT) && !param.equals(GeneralRouter.VEHICLE_WEIGHT) && !param.equals(GeneralRouter.VEHICLE_WIDTH) && !param.equals(GeneralRouter.VEHICLE_LENGTH)) {
                    otherRoutingParameters.add(routingParameter);
                }
            }
            if (drivingStyleParameters.size() > 0) {
                ListPreferenceEx drivingStyleRouting = createRoutingBooleanListPreference(DRIVING_STYLE, drivingStyleParameters);
                screen.addPreference(drivingStyleRouting);
            }
            if (avoidParameters.size() > 0) {
                String title;
                String description;
                if (am.isDerivedRoutingFrom(ApplicationMode.PUBLIC_TRANSPORT)) {
                    title = getString(R.string.avoid_pt_types);
                    description = getString(R.string.avoid_pt_types_descr);
                } else {
                    title = getString(R.string.impassable_road);
                    description = getString(R.string.avoid_in_routing_descr_);
                }
                MultiSelectBooleanPreference avoidRouting = createRoutingBooleanMultiSelectPref(AVOID_ROUTING_PARAMETER_PREFIX, title, description, avoidParameters);
                screen.addPreference(avoidRouting);
            }
            if (preferParameters.size() > 0) {
                String title = getString(R.string.prefer_in_routing_title);
                MultiSelectBooleanPreference preferRouting = createRoutingBooleanMultiSelectPref(PREFER_ROUTING_PARAMETER_PREFIX, title, "", preferParameters);
                screen.addPreference(preferRouting);
            }
            for (RoutingParameter p : otherRoutingParameters) {
                String title = AndroidUtils.getRoutingStringPropertyName(app, p.getId(), p.getName());
                String description = AndroidUtils.getRoutingStringPropertyDescription(app, p.getId(), p.getDescription());
                if (p.getType() == RoutingParameterType.BOOLEAN) {
                    OsmandPreference pref = settings.getCustomRoutingBooleanProperty(p.getId(), p.getDefaultBoolean());
                    SwitchPreferenceEx switchPreferenceEx = createSwitchPreferenceEx(pref.getId(), title, description, R.layout.preference_with_descr_dialog_and_switch);
                    switchPreferenceEx.setDescription(description);
                    switchPreferenceEx.setIcon(getRoutingPrefIcon(p.getId()));
                    screen.addPreference(switchPreferenceEx);
                    setupOtherBooleanParameterSummary(am, p, switchPreferenceEx);
                } else {
                    Object[] vls = p.getPossibleValues();
                    String[] svlss = new String[vls.length];
                    int i = 0;
                    for (Object o : vls) {
                        svlss[i++] = o.toString();
                    }
                    OsmandPreference pref = settings.getCustomRoutingProperty(p.getId(), p.getType() == RoutingParameterType.NUMERIC ? "0.0" : "-");
                    ListPreferenceEx listPreferenceEx = (ListPreferenceEx) createListPreferenceEx(pref.getId(), p.getPossibleValueDescriptions(), svlss, title, R.layout.preference_with_descr);
                    listPreferenceEx.setDescription(description);
                    listPreferenceEx.setIcon(getRoutingPrefIcon(p.getId()));
                    screen.addPreference(listPreferenceEx);
                }
            }
        }
        setupTimeConditionalRoutingPref();
    } else if (am.getRouteService() == RouteService.BROUTER) {
        screen.addPreference(fastRoute);
        setupTimeConditionalRoutingPref();
    } else if (am.getRouteService() == RouteService.STRAIGHT) {
        Preference straightAngle = new Preference(app.getApplicationContext());
        straightAngle.setPersistent(false);
        straightAngle.setKey(settings.ROUTE_STRAIGHT_ANGLE.getId());
        straightAngle.setTitle(getString(R.string.recalc_angle_dialog_title));
        straightAngle.setSummary(String.format(getString(R.string.shared_string_angle_param), String.valueOf((int) am.getStrAngle())));
        straightAngle.setLayoutResource(R.layout.preference_with_descr);
        // TODO change for appropriate icon when available
        straightAngle.setIcon(getRoutingPrefIcon("routing_recalc_distance"));
        getPreferenceScreen().addPreference(straightAngle);
    }
    addDivider(screen);
    setupRouteRecalcHeader(screen);
    setupSelectRouteRecalcDistance(screen);
    setupReverseDirectionRecalculation(screen);
    if (OsmandPlugin.isActive(OsmandDevelopmentPlugin.class)) {
        setupDevelopmentCategoryPreferences(screen, am);
    }
}
Also used : OsmandApplication(net.osmand.plus.OsmandApplication) PreferenceScreen(androidx.preference.PreferenceScreen) MultiSelectBooleanPreference(net.osmand.plus.settings.preferences.MultiSelectBooleanPreference) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) GeneralRouter(net.osmand.router.GeneralRouter) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) SwitchPreferenceEx(net.osmand.plus.settings.preferences.SwitchPreferenceEx) RoutingParameter(net.osmand.router.GeneralRouter.RoutingParameter) ListPreferenceEx(net.osmand.plus.settings.preferences.ListPreferenceEx) MultiSelectBooleanPreference(net.osmand.plus.settings.preferences.MultiSelectBooleanPreference) BooleanPreference(net.osmand.plus.settings.backend.preferences.BooleanPreference) CommonPreference(net.osmand.plus.settings.backend.preferences.CommonPreference) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) Preference(androidx.preference.Preference) Map(java.util.Map)

Example 5 with OsmandPreference

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

the class OsmandSettingsItemWriter method writeToStream.

@Override
public void writeToStream(@NonNull OutputStream outputStream, @Nullable IProgress progress) throws IOException {
    JSONObject json = new JSONObject();
    List<OsmandPreference<?>> prefs = new ArrayList<>(settings.getRegisteredPreferences().values());
    for (OsmandPreference<?> pref : prefs) {
        try {
            writePreferenceToJson(pref, json);
        } catch (JSONException e) {
            SettingsHelper.LOG.error("Failed to write preference: " + pref.getId(), e);
        }
    }
    if (json.length() > 0) {
        try {
            int bytesDivisor = 1024;
            byte[] bytes = json.toString(2).getBytes("UTF-8");
            if (progress != null) {
                progress.startWork(bytes.length / bytesDivisor);
            }
            Algorithms.streamCopy(new ByteArrayInputStream(bytes), outputStream, progress, bytesDivisor);
        } catch (JSONException e) {
            SettingsHelper.LOG.error("Failed to write json to stream", e);
        }
    }
    if (progress != null) {
        progress.finishTask();
    }
}
Also used : JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) OsmandPreference(net.osmand.plus.settings.backend.preferences.OsmandPreference) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException)

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