Search in sources :

Example 1 with BooleanPreference

use of net.osmand.plus.settings.backend.preferences.BooleanPreference 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 2 with BooleanPreference

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

the class OsmandSettings method registerBooleanPreference.

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

Example 3 with BooleanPreference

use of net.osmand.plus.settings.backend.preferences.BooleanPreference 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 4 with BooleanPreference

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

the class OsmandSettings method registerCustomRenderBooleanProperty.

public CommonPreference<Boolean> registerCustomRenderBooleanProperty(@NonNull String attrName, boolean defaultValue) {
    CommonPreference<Boolean> preference = new BooleanPreference(this, RENDERER_PREFERENCE_PREFIX + attrName, defaultValue).makeProfile();
    customBooleanRendersProps.put(attrName, preference);
    return preference;
}
Also used : BooleanPreference(net.osmand.plus.settings.backend.preferences.BooleanPreference)

Example 5 with BooleanPreference

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

the class OsmandSettings method registerBooleanAccessibilityPreference.

@SuppressWarnings("unchecked")
public CommonPreference<Boolean> registerBooleanAccessibilityPreference(String id, boolean defValue) {
    if (registeredPreferences.containsKey(id)) {
        return (CommonPreference<Boolean>) registeredPreferences.get(id);
    }
    BooleanPreference p = new BooleanAccessibilityPreference(this, id, defValue);
    registeredPreferences.put(id, p);
    return p;
}
Also used : BooleanPreference(net.osmand.plus.settings.backend.preferences.BooleanPreference) CommonPreference(net.osmand.plus.settings.backend.preferences.CommonPreference) BooleanAccessibilityPreference(net.osmand.plus.settings.backend.preferences.BooleanAccessibilityPreference)

Aggregations

BooleanPreference (net.osmand.plus.settings.backend.preferences.BooleanPreference)6 CommonPreference (net.osmand.plus.settings.backend.preferences.CommonPreference)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 View (android.view.View)1 Fragment (androidx.fragment.app.Fragment)1 HashSet (java.util.HashSet)1 OsmandApplication (net.osmand.plus.OsmandApplication)1 BaseBottomSheetItem (net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem)1 BottomSheetItemWithCompoundButton (net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton)1 BottomSheetItemWithDescription (net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription)1 TitleItem (net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem)1 ApplicationMode (net.osmand.plus.settings.backend.ApplicationMode)1 ContextMenuItemsSettings (net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings)1 MainContextMenuItemsSettings (net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings)1 BooleanAccessibilityPreference (net.osmand.plus.settings.backend.preferences.BooleanAccessibilityPreference)1 BooleanStringPreference (net.osmand.plus.settings.backend.preferences.BooleanStringPreference)1 ContextMenuItemsPreference (net.osmand.plus.settings.backend.preferences.ContextMenuItemsPreference)1 EnumStringPreference (net.osmand.plus.settings.backend.preferences.EnumStringPreference)1 FloatPreference (net.osmand.plus.settings.backend.preferences.FloatPreference)1