Search in sources :

Example 1 with ContextMenuItemsSettings

use of net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings 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 ContextMenuItemsSettings

use of net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings in project Osmand by osmandapp.

the class ConfigureMenuItemsFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
    Context ctx = requireContext();
    View root = mInflater.inflate(R.layout.edit_arrangement_list_fragment, container, false);
    AppBarLayout appbar = root.findViewById(R.id.appbar);
    View toolbar = mInflater.inflate(R.layout.global_preference_toolbar, container, false);
    TextView toolbarTitle = toolbar.findViewById(R.id.toolbar_title);
    ImageButton toolbarButton = toolbar.findViewById(R.id.close_button);
    toolbar.setBackgroundColor(ColorUtilities.getListBgColor(ctx, nightMode));
    toolbarTitle.setTextColor(nightMode ? getResources().getColor(R.color.text_color_primary_dark) : getResources().getColor(R.color.list_background_color_dark));
    toolbarButton.setImageDrawable(getPaintedContentIcon(AndroidUtils.getNavigationIconResId(app), getResources().getColor(R.color.text_color_secondary_light)));
    toolbarTitle.setText(screenType.titleRes);
    toolbarButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            exitFragment();
        }
    });
    appbar.addView(toolbar);
    recyclerView = root.findViewById(R.id.profiles_list);
    View cancelButton = root.findViewById(R.id.dismiss_button);
    root.findViewById(R.id.buttons_divider).setVisibility(View.VISIBLE);
    UiUtilities.setupDialogButton(nightMode, cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel);
    View applyButton = root.findViewById(R.id.right_bottom_button);
    cancelButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentActivity fragmentActivity = getActivity();
            if (fragmentActivity != null) {
                fragmentActivity.onBackPressed();
            }
        }
    });
    UiUtilities.setupDialogButton(nightMode, applyButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_apply);
    applyButton.setVisibility(View.VISIBLE);
    applyButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            List<ContextMenuItem> defItems = getCustomizableDefaultItems(contextMenuAdapter.getDefaultItems());
            List<String> ids = new ArrayList<>();
            if (!menuItemsOrder.isEmpty()) {
                sortByCustomOrder(defItems, menuItemsOrder);
                for (ContextMenuItem item : defItems) {
                    ids.add(item.getId());
                }
            }
            FragmentManager fm = getFragmentManager();
            final ContextMenuItemsSettings prefToSave;
            if (screenType == ScreenType.DRAWER) {
                prefToSave = new DrawerMenuItemsSettings(hiddenMenuItems, ids);
            } else if (screenType == ScreenType.CONTEXT_MENU_ACTIONS) {
                prefToSave = new MainContextMenuItemsSettings(mainActionItems, hiddenMenuItems, ids);
            } else {
                prefToSave = new ContextMenuItemsSettings(hiddenMenuItems, ids);
            }
            if (fm != null) {
                ChangeGeneralProfilesPrefBottomSheet.showInstance(fm, getSettingForScreen().getId(), prefToSave, getTargetFragment(), false, R.string.back_to_editing, appMode, new ChangeGeneralProfilesPrefBottomSheet.OnChangeSettingListener() {

                    @Override
                    public void onApplied(boolean profileOnly) {
                        dismissFragment();
                    }

                    @Override
                    public void onDiscard() {
                    }
                });
            }
        }
    });
    if (Build.VERSION.SDK_INT >= 21) {
        AndroidUtils.addStatusBarPadding21v(app, root);
    }
    return root;
}
Also used : Context(android.content.Context) ContextMenuItem(net.osmand.plus.ContextMenuItem) DrawerMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.DrawerMenuItemsSettings) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) FragmentManager(androidx.fragment.app.FragmentManager) FragmentActivity(androidx.fragment.app.FragmentActivity) ImageButton(android.widget.ImageButton) MainContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings) ContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings) MainContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings) TextView(android.widget.TextView) List(java.util.List) ArrayList(java.util.ArrayList) AppBarLayout(com.google.android.material.appbar.AppBarLayout) Nullable(androidx.annotation.Nullable)

Example 3 with ContextMenuItemsSettings

use of net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings in project Osmand by osmandapp.

the class ConfigureMenuItemsFragment method initSavedIds.

private void initSavedIds(ApplicationMode appMode, boolean useDefaultValue) {
    ContextMenuItemsSettings settings = getMenuItemsSettings(appMode, useDefaultValue);
    hiddenMenuItems = new ArrayList<>(settings.getHiddenIds());
    menuItemsOrder = new HashMap<>();
    List<String> orderIds = settings.getOrderIds();
    for (int i = 0; i < orderIds.size(); i++) {
        menuItemsOrder.put(orderIds.get(i), i);
    }
}
Also used : MainContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings) ContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings)

Example 4 with ContextMenuItemsSettings

use of net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings in project Osmand by osmandapp.

the class ConfigureMenuItemsFragment method initMainActionsIds.

private void initMainActionsIds(ApplicationMode appMode, boolean useDefaultValue) {
    List<ContextMenuItem> defItems = getCustomizableDefaultItems(contextMenuAdapter.getDefaultItems());
    ContextMenuItemsSettings pref = getMenuItemsSettings(appMode, useDefaultValue);
    if (pref instanceof MainContextMenuItemsSettings) {
        mainActionItems = new ArrayList<>(((MainContextMenuItemsSettings) pref).getMainIds());
        if (mainActionItems.isEmpty()) {
            for (int i = 0; i < MAIN_BUTTONS_QUANTITY && i < defItems.size(); i++) {
                mainActionItems.add(defItems.get(i).getId());
            }
        }
    }
}
Also used : ContextMenuItem(net.osmand.plus.ContextMenuItem) MainContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings) ContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings) MainContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings)

Example 5 with ContextMenuItemsSettings

use of net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings in project Osmand by osmandapp.

the class ContextMenuItemsPreference method readValue.

private ContextMenuItemsSettings readValue(String s) {
    ContextMenuItemsSettings value = getDefaultValue().newInstance();
    value.readFromJsonString(s, idScheme);
    return value;
}
Also used : ContextMenuItemsSettings(net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings)

Aggregations

ContextMenuItemsSettings (net.osmand.plus.settings.backend.menuitems.ContextMenuItemsSettings)5 MainContextMenuItemsSettings (net.osmand.plus.settings.backend.menuitems.MainContextMenuItemsSettings)4 ContextMenuItem (net.osmand.plus.ContextMenuItem)2 SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 View (android.view.View)1 ImageButton (android.widget.ImageButton)1 TextView (android.widget.TextView)1 Nullable (androidx.annotation.Nullable)1 FragmentActivity (androidx.fragment.app.FragmentActivity)1 FragmentManager (androidx.fragment.app.FragmentManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 AppBarLayout (com.google.android.material.appbar.AppBarLayout)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DrawerMenuItemsSettings (net.osmand.plus.settings.backend.menuitems.DrawerMenuItemsSettings)1 BooleanPreference (net.osmand.plus.settings.backend.preferences.BooleanPreference)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