Skip to content

Commit

Permalink
Fixes and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Chumva committed Jul 17, 2023
1 parent 01c3e16 commit 444409f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 231 deletions.
6 changes: 2 additions & 4 deletions OsmAnd/res/layout/preference_description.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
android:layout_height="wrap_content"
android:background="?attr/list_background_color"
android:minHeight="@dimen/setting_list_item_large_height"
android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding"
android:paddingStart="@dimen/content_padding"
android:paddingEnd="@dimen/content_padding">
android:paddingHorizontal="@dimen/content_padding"
android:paddingVertical="@dimen/content_padding_small">

<net.osmand.plus.widgets.TextViewEx
android:id="@android:id/title"
Expand Down
17 changes: 5 additions & 12 deletions OsmAnd/res/layout/preference_item_description.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/list_background_color"
android:minHeight="64dp"
android:minHeight="@dimen/bottom_sheet_large_list_item_height"
android:orientation="vertical">

<TextView
<net.osmand.plus.widgets.TextViewEx
android:id="@android:id/title"
style="@style/DescStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginHorizontal="@dimen/content_padding"
android:layout_marginVertical="@dimen/content_padding_small"
android:letterSpacing="@dimen/text_button_letter_spacing"
android:lineSpacingExtra="5sp"
android:text="@string/dangerous_goods_description"
android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_desc_text_size"
osmand:typeface="@string/font_roboto_regular" />
tools:text="@string/dangerous_goods_description" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/divider_color_basic" />
</LinearLayout>
58 changes: 0 additions & 58 deletions OsmAnd/res/layout/preference_switch_divider.xml

This file was deleted.

11 changes: 10 additions & 1 deletion OsmAnd/res/xml/dangerous_goods_parameters.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/dangerous_goods">

<Preference
android:key="dangerous_goods_description"
android:layout="@layout/preference_item_description"
android:persistent="false"
android:selectable="false"
android:title="@string/dangerous_goods_description" />

<Preference
android:key="description_divider"
android:layout="@layout/simple_divider_item"
android:selectable="false" />

</PreferenceScreen>
Original file line number Diff line number Diff line change
@@ -1,170 +1,151 @@
package net.osmand.plus.settings.fragments;

import static net.osmand.plus.settings.backend.OsmandSettings.ROUTING_PREFERENCE_PREFIX;
import static net.osmand.plus.settings.fragments.RouteParametersFragment.HAZMAT_CATEGORY_USA_PREFIX;
import static net.osmand.plus.utils.AndroidUtils.getRoutingStringPropertyName;
import static net.osmand.router.GeneralRouter.*;
import static net.osmand.router.RoutingConfiguration.parseSilentInt;
import static net.osmand.router.GeneralRouter.RoutingParameter;
import static net.osmand.router.GeneralRouter.RoutingParameterType;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreferenceCompat;

import net.osmand.plus.R;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.routing.RoutingHelperUtils;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.preferences.CommonPreference;
import net.osmand.plus.utils.AndroidUtils;
import net.osmand.plus.utils.ColorUtilities;
import net.osmand.router.GeneralRouter;
import net.osmand.util.Algorithms;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class DangerousGoodsFragment extends BaseSettingsFragment {

private static final String DANGEROUS_GOODS_DESCRIPTION_KEY = "dangerous_goods_description";
private static final String HAZMAT_CATEGORY_USA_1 = "hazmat_category_usa_1";
private static final String HAZMAT_CATEGORY_USA_2 = "hazmat_category_usa_2";
private static final String HAZMAT_CATEGORY_USA_3 = "hazmat_category_usa_3";
private static final String HAZMAT_CATEGORY_USA_4 = "hazmat_category_usa_4";
private static final String HAZMAT_CATEGORY_USA_5 = "hazmat_category_usa_5";
private static final String HAZMAT_CATEGORY_USA_6 = "hazmat_category_usa_6";
private static final String HAZMAT_CATEGORY_USA_7 = "hazmat_category_usa_7";
private static final String HAZMAT_CATEGORY_USA_8 = "hazmat_category_usa_8";
private static final String HAZMAT_CATEGORY_USA_9 = "hazmat_category_usa_9";

private String lastPreferenceId;
private final List<RoutingParameter> parameters = new ArrayList<>();

@Override
protected void setupPreferences() {
setupDescription();
setupDangerousGoodsPreferences();
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ApplicationMode mode = getSelectedAppMode();
GeneralRouter router = app.getRouter(mode);
if (router != null) {
Map<String, RoutingParameter> routingParameters = RoutingHelperUtils.getParametersForDerivedProfile(mode, router);
for (Map.Entry<String, RoutingParameter> entry : routingParameters.entrySet()) {
String key = entry.getKey();
RoutingParameter parameter = entry.getValue();
if (key.startsWith(HAZMAT_CATEGORY_USA_PREFIX) && parameter.getType() == RoutingParameterType.BOOLEAN) {
parameters.add(parameter);
}
}
}
}

@Override
protected void createToolbar(@NonNull LayoutInflater inflater, @NonNull View view) {
super.createToolbar(inflater, view);
TextView toolbarTitle = view.findViewById(R.id.toolbar_title);
if (toolbarTitle != null) {
toolbarTitle.setText(R.string.dangerous_goods);
}

boolean nightMode = isNightMode();
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setBackgroundColor(ColorUtilities.getCardAndListBackgroundColor(app, nightMode));

TextView title = view.findViewById(R.id.toolbar_title);
title.setTextColor(ColorUtilities.getPrimaryTextColor(app, nightMode));

ImageView closeButton = view.findViewById(R.id.close_button);
closeButton.setImageDrawable(getIcon(AndroidUtils.getNavigationIconResId(app), ColorUtilities.getPrimaryIconColorId(nightMode)));

ImageView actionButton = toolbar.findViewById(R.id.action_button);
actionButton.setOnClickListener(v -> resetToDefault());
actionButton.setContentDescription(getString(R.string.reset_to_default));
actionButton.setImageDrawable(getContentIcon(R.drawable.ic_action_reset_to_default_dark));
AndroidUiHelper.updateVisibility(actionButton, true);
}

@Override
protected void updateToolbar() {
super.updateToolbar();
View view = getView();
if (view != null) {
ImageView resetIcon = view.findViewById(R.id.profile_icon);
resetIcon.setImageDrawable(getIcon(R.drawable.ic_action_reset_to_default_dark, ColorUtilities.getDefaultIconColorId(isNightMode())));

View resetButton = view.findViewById(R.id.profile_button);
resetButton.setContentDescription(getString(R.string.reset_to_default));
resetButton.setOnClickListener(v -> resetToDefault());
resetButton.setVisibility(View.VISIBLE);
AndroidUtils.setBackground(resetButton, null);
}
protected void setupPreferences() {
setupHazmatPreferences();
}

private void resetToDefault() {
RouteParametersFragment parametersFragment = getRouteParametersFragment();
if (parametersFragment != null) {
for (RoutingParameter parameter : parametersFragment.hazmatCategoryUSAParameters) {
if (parameter.getType() == RoutingParameterType.BOOLEAN) {
CommonPreference<Boolean> pref = settings.getCustomRoutingBooleanProperty(parameter.getId(), parameter.getDefaultBoolean());
pref.resetToDefault();
}
}
updateAllSettings();
}
}
private void setupHazmatPreferences() {
Context context = requireContext();

private void setupDescription() {
Preference preference = findPreference(DANGEROUS_GOODS_DESCRIPTION_KEY);
if (preference != null) {
preference.setTitle(getString(R.string.dangerous_goods_description));
}
}
Iterator<RoutingParameter> iterator = parameters.iterator();
while (iterator.hasNext()) {
RoutingParameter parameter = iterator.next();

private void setupDangerousGoodsPreferences() {
RouteParametersFragment fragment = getRouteParametersFragment();
if (fragment != null) {
PreferenceScreen screen = getPreferenceScreen();
for (RoutingParameter parameter : fragment.hazmatCategoryUSAParameters) {
String title = getRoutingStringPropertyName(app, parameter.getId(), parameter.getName());
if (parameter.getType() == RoutingParameterType.BOOLEAN) {
CommonPreference<Boolean> pref = settings.getCustomRoutingBooleanProperty(parameter.getId(), parameter.getDefaultBoolean());

SwitchPreferenceCompat preference = new SwitchPreferenceCompat(app);
preference.setKey(pref.getId());
preference.setTitle(title);
preference.setLayoutResource(R.layout.preference_switch_divider);
preference.setIcon(getDangerousGoodsPrefIcon(parameter.getId()));
screen.addPreference(preference);

lastPreferenceId = pref.getId();
}
String id = parameter.getId();
String title = getRoutingStringPropertyName(app, id, parameter.getName());
CommonPreference<Boolean> pref = settings.getCustomRoutingBooleanProperty(id, parameter.getDefaultBoolean());

SwitchPreferenceCompat preference = createSwitchPreference(pref, title, null, R.layout.preference_switch);
preference.setLayoutResource(R.layout.preference_switch);
preference.setIcon(getHazmatPrefIcon(id));
addOnPreferencesScreen(preference);

if (iterator.hasNext()) {
Preference divider = new Preference(context);
divider.setLayoutResource(R.layout.divider_half_item_with_background);
divider.setKey(id + "_divider");
divider.setSelectable(false);
addOnPreferencesScreen(divider);
}
}
}

@Override
protected void onBindPreferenceViewHolder(Preference preference, PreferenceViewHolder holder) {
super.onBindPreferenceViewHolder(preference, holder);
holder.itemView.setContentDescription(getString(R.string.shared_string_class) + " " + getDangerousGoodsClass(preference.getKey()) + " " + preference.getTitle());
if (preference.getKey().equals(lastPreferenceId)) {
AndroidUiHelper.updateVisibility(holder.itemView.findViewById(R.id.divider), false);

String key = preference.getKey();
if (key.startsWith(ROUTING_PREFERENCE_PREFIX + HAZMAT_CATEGORY_USA_PREFIX)) {
int hazmatClass = getHazmatUsaClass(key.replace(ROUTING_PREFERENCE_PREFIX, ""));
if (hazmatClass >= 0) {
holder.itemView.setContentDescription(getString(R.string.shared_string_class)
+ " " + hazmatClass + " " + preference.getTitle());
}
}
}

public static int getDangerousGoodsClass(String id) {
Matcher matcher = Pattern.compile("(hazmat_category_usa_)\\d+$").matcher(id);
if (matcher.find()) {
String[] separatedKey = id.split("_");
return parseSilentInt(separatedKey[separatedKey.length - 1], 0);
private void resetToDefault() {
ApplicationMode mode = getSelectedAppMode();
for (RoutingParameter parameter : parameters) {
settings.getCustomRoutingBooleanProperty(parameter.getId(), parameter.getDefaultBoolean()).resetModeToDefault(mode);
}
return 0;
updateAllSettings();
}

@Nullable
private RouteParametersFragment getRouteParametersFragment() {
Fragment fragment = getTargetFragment();
if (fragment instanceof RouteParametersFragment) {
return (RouteParametersFragment) fragment;
private Drawable getHazmatPrefIcon(@NonNull String id) {
int hazmatClass = getHazmatUsaClass(id);
if (hazmatClass >= 0) {
int iconId = AndroidUtils.getDrawableId(app, "ic_action_placard_hazard_" + hazmatClass);
if (iconId > 0) {
return getIcon(iconId);
}
}
return null;
}

private Drawable getDangerousGoodsPrefIcon(String prefId) {
switch (prefId) {
case HAZMAT_CATEGORY_USA_1:
return getIcon(R.drawable.ic_action_placard_hazard_1);
case HAZMAT_CATEGORY_USA_2:
return getIcon(R.drawable.ic_action_placard_hazard_2);
case HAZMAT_CATEGORY_USA_3:
return getIcon(R.drawable.ic_action_placard_hazard_3);
case HAZMAT_CATEGORY_USA_4:
return getIcon(R.drawable.ic_action_placard_hazard_4);
case HAZMAT_CATEGORY_USA_5:
return getIcon(R.drawable.ic_action_placard_hazard_5);
case HAZMAT_CATEGORY_USA_6:
return getIcon(R.drawable.ic_action_placard_hazard_6);
case HAZMAT_CATEGORY_USA_7:
return getIcon(R.drawable.ic_action_placard_hazard_7);
case HAZMAT_CATEGORY_USA_8:
return getIcon(R.drawable.ic_action_placard_hazard_8);
case HAZMAT_CATEGORY_USA_9:
return getIcon(R.drawable.ic_action_placard_hazard_9);
default:
return null;
}
public static int getHazmatUsaClass(@NonNull String id) {
return Algorithms.parseIntSilently(id.replace(HAZMAT_CATEGORY_USA_PREFIX, ""), -1);
}
}
Loading

0 comments on commit 444409f

Please sign in to comment.