From 1dd909b115972032b906cc698125970639f13a76 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Tue, 16 Jun 2020 18:28:35 +0300 Subject: [PATCH] refactoring pr --- .../main/java/net/osmand/osm/MapPoiTypes.java | 5 +- .../java/net/osmand/search/SearchUICore.java | 6 +- .../layout/preference_switch_with_alert.xml | 61 -------------- OsmAnd/res/xml/global_settings.xml | 3 +- OsmAnd/res/xml/screen_alerts.xml | 10 ++- OsmAnd/res/xml/voice_announces.xml | 10 ++- .../access/AccessibilitySettingsFragment.java | 25 +----- .../src/net/osmand/plus/AppInitializer.java | 3 + OsmAnd/src/net/osmand/plus/UiUtilities.java | 55 +++++-------- .../osmand/plus/activities/MapActivity.java | 3 - .../audionotes/MultimediaNotesFragment.java | 19 +---- .../plus/dialogs/SpeedCamerasBottomSheet.java | 40 ++++----- .../mapsource/InputZoomLevelsBottomSheet.java | 20 +---- .../quickaction/QuickActionListFragment.java | 2 +- .../search/QuickSearchDialogFragment.java | 2 +- .../osmand/plus/search/QuickSearchHelper.java | 8 +- .../plus/settings/backend/OsmandSettings.java | 4 +- .../fragments/BaseSettingsFragment.java | 31 ++++++- .../fragments/GlobalSettingsFragment.java | 36 ++++---- .../fragments/ImportCompleteFragment.java | 3 +- .../fragments/ImportDuplicatesFragment.java | 3 +- .../fragments/ImportSettingsFragment.java | 3 +- .../fragments/ScreenAlertsFragment.java | 82 +++++-------------- .../fragments/VoiceAnnouncesFragment.java | 36 +++++--- 24 files changed, 174 insertions(+), 296 deletions(-) delete mode 100644 OsmAnd/res/layout/preference_switch_with_alert.xml diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/MapPoiTypes.java b/OsmAnd-java/src/main/java/net/osmand/osm/MapPoiTypes.java index 7b719ab158..d0c941a898 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/MapPoiTypes.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/MapPoiTypes.java @@ -947,9 +947,8 @@ public class MapPoiTypes { } public void excludeSpeedCameraPoiType() { - Iterator categoryIter = categories.iterator(); - while (categoryIter.hasNext()) { - Iterator poiIter = categoryIter.next().getPoiTypes().iterator(); + for (PoiCategory category : categories) { + Iterator poiIter = category.getPoiTypes().iterator(); while (poiIter.hasNext()) { PoiType poiType = poiIter.next(); if ("speed_camera".equals(poiType.getKeyName())) { diff --git a/OsmAnd-java/src/main/java/net/osmand/search/SearchUICore.java b/OsmAnd-java/src/main/java/net/osmand/search/SearchUICore.java index 8aab2a9103..1143a2965b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/search/SearchUICore.java +++ b/OsmAnd-java/src/main/java/net/osmand/search/SearchUICore.java @@ -657,7 +657,7 @@ public class SearchUICore { private List exportedObjects; private List exportedCities; - private static boolean speedCamerasUninstalled = false; + public static boolean speedCamerasUninstalled = false; public SearchResultMatcher(ResultMatcher matcher, SearchPhrase phrase, int request, AtomicInteger requestNumber, int totalLimit) { @@ -902,10 +902,6 @@ public class SearchUICore { } return "speed_camera".equals(key); } - - public static void setSpeedCamerasUninstalled(boolean speedCamerasUninstalled) { - SearchResultMatcher.speedCamerasUninstalled = speedCamerasUninstalled; - } } private enum ResultCompareStep { diff --git a/OsmAnd/res/layout/preference_switch_with_alert.xml b/OsmAnd/res/layout/preference_switch_with_alert.xml deleted file mode 100644 index cdf10c32a6..0000000000 --- a/OsmAnd/res/layout/preference_switch_with_alert.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/OsmAnd/res/xml/global_settings.xml b/OsmAnd/res/xml/global_settings.xml index b323527a39..4cbc10ef0d 100644 --- a/OsmAnd/res/xml/global_settings.xml +++ b/OsmAnd/res/xml/global_settings.xml @@ -66,7 +66,8 @@ android:title="@string/shared_string_legal" /> diff --git a/OsmAnd/res/xml/screen_alerts.xml b/OsmAnd/res/xml/screen_alerts.xml index c0c0219ab8..ca963a5927 100644 --- a/OsmAnd/res/xml/screen_alerts.xml +++ b/OsmAnd/res/xml/screen_alerts.xml @@ -28,9 +28,17 @@ + + + + Build.VERSION_CODES.LOLLIPOP) { - Drawable bgDrawable = getPaintedIcon(R.drawable.rectangle_rounded, color); - Drawable selectable = getPaintedIcon(R.drawable.ripple_rectangle_rounded, selectedColor); - Drawable[] layers = {bgDrawable, selectable}; - AndroidUtils.setBackground(selectableView, new LayerDrawable(layers)); - } else { - Drawable bgDrawable = getPaintedIcon(R.drawable.rectangle_rounded, color); - AndroidUtils.setBackground(selectableView, bgDrawable); - } - LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) selectableView.getLayoutParams(); - params.setMargins(params.leftMargin, AndroidUtils.dpToPx(app, 6), params.rightMargin, params.bottomMargin); - } + setupPrefRoundedBg(holder); } else if (settings.ACCESSIBILITY_MODE.getId().equals(prefId)) { ImageView imageView = (ImageView) holder.findViewById(android.R.id.icon); if (imageView != null) { diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index c39fe3a9d5..e07399832c 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -406,6 +406,9 @@ public class AppInitializer implements IProgress { return null; } }); + if (app.getSettings().SPEED_CAMERAS_UNINSTALLED.get()) { + app.getPoiTypes().excludeSpeedCameraPoiType(); + } } public void onCreateApplication() { diff --git a/OsmAnd/src/net/osmand/plus/UiUtilities.java b/OsmAnd/src/net/osmand/plus/UiUtilities.java index 0f817aac35..3c24dff20a 100644 --- a/OsmAnd/src/net/osmand/plus/UiUtilities.java +++ b/OsmAnd/src/net/osmand/plus/UiUtilities.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.Intent; import android.content.res.ColorStateList; import android.graphics.Color; +import android.graphics.Typeface; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; @@ -639,44 +640,32 @@ public class UiUtilities { } } - public static SpannableString createSpannableString(@NonNull String text, @NonNull String textToStyle, @NonNull StyleSpan styleSpan) { - SpannableString spannable = new SpannableString(text); - try { - int startIndex = text.indexOf(textToStyle); - spannable.setSpan( - styleSpan, - startIndex, - startIndex + textToStyle.length(), - Spanned.SPAN_INCLUSIVE_INCLUSIVE); - return spannable; - } catch (RuntimeException e) { - LOG.error("Error trying to find index of " + textToStyle + " " + e); - return spannable; - } - } - - public static SpannableString setWordsMediumFont(@NonNull Context ctx, @NonNull String text, @NonNull String... textToStyle) { + public static SpannableString createSpannableString(@NonNull String text, @NonNull StyleSpan styleSpan, @NonNull String... textToStyle) { SpannableString spannable = new SpannableString(text); for (String t : textToStyle) { - try { - int startIndex = text.indexOf(t); - spannable.setSpan( - new CustomTypefaceSpan(FontCache.getRobotoMedium(ctx)), - startIndex, - startIndex + t.length(), - Spanned.SPAN_INCLUSIVE_INCLUSIVE); - } catch (RuntimeException e) { - LOG.error("Error trying to find index of " + t + " " + e); - } + setSpan(spannable, styleSpan, text, t); } return spannable; } - public static GradientDrawable getRoundedBackgroundDrawable(@NonNull Context context, @ColorRes int colorRes, int radius) { - int r = AndroidUtils.dpToPx(context, radius); - GradientDrawable background = new GradientDrawable(); - background.setColor(ContextCompat.getColor(context, colorRes)); - background.setCornerRadius(r); - return background; + private static void setSpan(@NonNull SpannableString spannable, @NonNull Object styleSpan, @NonNull String text, @NonNull String t) { + try { + int startIndex = text.indexOf(t); + spannable.setSpan( + styleSpan, + startIndex, + startIndex + t.length(), + Spanned.SPAN_INCLUSIVE_INCLUSIVE); + } catch (RuntimeException e) { + LOG.error("Error trying to find index of " + t + " " + e); + } + } + + public static SpannableString createCustomFontSpannable(@NonNull Typeface typeface, @NonNull String text, @NonNull String... textToStyle) { + SpannableString spannable = new SpannableString(text); + for (String s : textToStyle) { + setSpan(spannable, new CustomTypefaceSpan(typeface), text, s); + } + return spannable; } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 476768dc6e..9bc9d3e24b 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -389,9 +389,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven lockHelper.setLockUIAdapter(this); mIsDestroyed = false; - if (settings.SPEED_CAMERAS_UNINSTALLED.get()) { - app.getPoiTypes().excludeSpeedCameraPoiType(); - } } public void exitFromFullScreen(View view) { diff --git a/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java b/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java index 1741f768b3..6996f7ee5b 100644 --- a/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java +++ b/OsmAnd/src/net/osmand/plus/audionotes/MultimediaNotesFragment.java @@ -6,7 +6,6 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Typeface; import android.graphics.drawable.Drawable; -import android.graphics.drawable.LayerDrawable; import android.hardware.Camera; import android.media.CamcorderProfile; import android.media.MediaRecorder; @@ -14,7 +13,6 @@ import android.os.Build; import android.os.Bundle; import android.os.StatFs; import android.text.SpannableString; -import android.view.View; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; @@ -28,7 +26,6 @@ import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmAndAppCustomization; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; -import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.FontCache; import net.osmand.plus.profiles.SelectCopyAppModeBottomSheet; @@ -466,21 +463,7 @@ public class MultimediaNotesFragment extends BaseSettingsFragment implements Cop super.onBindPreferenceViewHolder(preference, holder); String prefId = preference.getKey(); if (CAMERA_PERMISSION.equals(prefId)) { - View selectableView = holder.itemView.findViewById(R.id.selectable_list_item); - if (selectableView != null) { - int color = AndroidUtils.getColorFromAttr(app, R.attr.activity_background_color); - int selectedColor = UiUtilities.getColorWithAlpha(getActiveProfileColor(), 0.3f); - - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { - Drawable bgDrawable = getPaintedIcon(R.drawable.rectangle_rounded, color); - Drawable selectable = getPaintedIcon(R.drawable.ripple_rectangle_rounded, selectedColor); - Drawable[] layers = {bgDrawable, selectable}; - AndroidUtils.setBackground(selectableView, new LayerDrawable(layers)); - } else { - Drawable bgDrawable = getPaintedIcon(R.drawable.rectangle_rounded, color); - AndroidUtils.setBackground(selectableView, bgDrawable); - } - } + setupPrefRoundedBg(holder); } else if (OPEN_NOTES_DESCRIPTION.equals(prefId)) { int minHeight = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_list_item_height); holder.itemView.setMinimumHeight(minHeight); diff --git a/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java b/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java index 27b59e97a2..fbf64a8d29 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java @@ -17,12 +17,18 @@ import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; +import net.osmand.plus.helpers.FontCache; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.settings.backend.OsmandSettings.OsmandPreference; +import net.osmand.plus.settings.fragments.OnPreferenceChanged; +import net.osmand.plus.widgets.style.CustomTypefaceSpan; import net.osmand.search.SearchUICore.SearchResultMatcher; public class SpeedCamerasBottomSheet extends MenuBottomSheetDialogFragment { public static final String TAG = SpeedCamerasBottomSheet.class.getName(); private OsmandApplication app; + private OsmandSettings settings; public static void showInstance(@NonNull FragmentManager fm, @Nullable Fragment targetFragment) { if (!fm.isStateSaved()) { @@ -36,22 +42,17 @@ public class SpeedCamerasBottomSheet extends MenuBottomSheetDialogFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = requiredMyApplication(); + settings = app.getSettings(); } @Override public void createMenuItems(Bundle savedInstanceState) { View root = UiUtilities.getInflater(app, nightMode).inflate(R.layout.bottom_sheet_speed_cameras, null); - ((ImageView) root.findViewById(R.id.icon)).setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.img_speed_camera_warning)); + ((ImageView) root.findViewById(R.id.icon)).setImageDrawable(ContextCompat.getDrawable(app, R.drawable.img_speed_camera_warning)); ((TextView) root.findViewById(R.id.description)).setText(getDescriptionText()); items.add(new BaseBottomSheetItem.Builder().setCustomView(root).create()); } - @Override - public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - UiUtilities.setupDialogButton(nightMode, rightButton, getDismissButtonType(), R.string.keep_active); - } - @Override protected void onRightBottomButtonClick() { setDialogShowed(); @@ -60,17 +61,17 @@ public class SpeedCamerasBottomSheet extends MenuBottomSheetDialogFragment { @Override protected void onDismissButtonClickAction() { - app.getSettings().SPEED_CAMERAS_UNINSTALLED.set(true); - app.getSettings().SPEAK_SPEED_CAMERA.set(false); - app.getSettings().SHOW_CAMERAS.set(false); + OsmandPreference speedCamUninstalled = settings.SPEED_CAMERAS_UNINSTALLED; + speedCamUninstalled.set(true); + settings.SPEAK_SPEED_CAMERA.set(false); + settings.SHOW_CAMERAS.set(false); app.getPoiTypes().excludeSpeedCameraPoiType(); - SearchResultMatcher.setSpeedCamerasUninstalled(true); + SearchResultMatcher.speedCamerasUninstalled = true; Fragment targetFragment = getTargetFragment(); - if (targetFragment instanceof OnSpeedCamerasUninstallListener) { - ((OnSpeedCamerasUninstallListener) targetFragment).onSpeedCamerasUninstalled(); + if (targetFragment instanceof OnPreferenceChanged) { + ((OnPreferenceChanged) targetFragment).onPreferenceChanged(speedCamUninstalled.getId()); } setDialogShowed(); - dismiss(); } @Override @@ -83,18 +84,19 @@ public class SpeedCamerasBottomSheet extends MenuBottomSheetDialogFragment { return R.string.keep_active; } + @Override + protected UiUtilities.DialogButtonType getRightBottomButtonType() { + return getDismissButtonType(); + } + private SpannableString getDescriptionText() { String keepActive = getString(R.string.keep_active); String uninstall = getString(R.string.shared_string_uninstall); String text = getString(R.string.speed_cameras_legal_descr, keepActive, uninstall); - return UiUtilities.setWordsMediumFont(app, text, keepActive, uninstall); + return UiUtilities.createCustomFontSpannable(FontCache.getRobotoMedium(app), text, keepActive, uninstall); } private void setDialogShowed() { app.getSettings().SPEED_CAMERAS_ALERT_SHOWED.set(true); } - - public interface OnSpeedCamerasUninstallListener { - void onSpeedCamerasUninstalled(); - } } diff --git a/OsmAnd/src/net/osmand/plus/mapsource/InputZoomLevelsBottomSheet.java b/OsmAnd/src/net/osmand/plus/mapsource/InputZoomLevelsBottomSheet.java index 3f44454f22..8cf8f99444 100644 --- a/OsmAnd/src/net/osmand/plus/mapsource/InputZoomLevelsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/mapsource/InputZoomLevelsBottomSheet.java @@ -3,6 +3,7 @@ package net.osmand.plus.mapsource; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; +import android.text.style.StyleSpan; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; @@ -81,7 +82,7 @@ public class InputZoomLevelsBottomSheet extends MenuBottomSheetDialogFragment { String mapSource = getString(R.string.map_source); String overlayUnderlay = getString(R.string.pref_overlay); String dialogDesr = getString(dialogDescrRes, mapSource, overlayUnderlay); - dialogDescrTv.setText(createSpannableString(dialogDesr, mapSource, overlayUnderlay)); + dialogDescrTv.setText(UiUtilities.createCustomFontSpannable(FontCache.getRobotoMedium(app), dialogDesr, mapSource, overlayUnderlay)); } else { dialogDescrTv.setText(getString(dialogDescrRes)); } @@ -155,23 +156,6 @@ public class InputZoomLevelsBottomSheet extends MenuBottomSheetDialogFragment { return R.string.shared_string_apply; } - private SpannableString createSpannableString(@NonNull String text, @NonNull String... textToStyle) { - SpannableString spannable = new SpannableString(text); - for (String t : textToStyle) { - try { - int startIndex = text.indexOf(t); - spannable.setSpan( - new CustomTypefaceSpan(FontCache.getRobotoMedium(requireContext())), - startIndex, - startIndex + t.length(), - Spanned.SPAN_INCLUSIVE_INCLUSIVE); - } catch (RuntimeException e) { - LOG.error("Error trying to find index of " + t + " " + e); - } - } - return spannable; - } - private void setSliderDescrRes(int sliderDescrRes) { this.sliderDescrRes = sliderDescrRes; } diff --git a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java index 8bb543ef16..b32addf136 100644 --- a/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java +++ b/OsmAnd/src/net/osmand/plus/quickaction/QuickActionListFragment.java @@ -890,7 +890,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment String message = String.format(ctx.getString( R.string.quick_actions_delete_text), actionName); SpannableString styledMessage = UiUtilities.createSpannableString( - message, actionName, new StyleSpan(Typeface.BOLD)); + message, new StyleSpan(Typeface.BOLD), actionName); ConfirmationBottomSheet.showInstance(ctx.getSupportFragmentManager(), target, ctx.getString(R.string.quick_actions_delete), styledMessage, diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 673af0c9e1..022cbb8468 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -1256,7 +1256,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC //show "Apply to all profiles" SnackBar String modeName = appMode.toHumanString(); String text = app.getString(R.string.changes_applied_to_profile, modeName); - SpannableString message = UiUtilities.createSpannableString(text, modeName, new StyleSpan(Typeface.BOLD)); + SpannableString message = UiUtilities.createSpannableString(text, new StyleSpan(Typeface.BOLD), modeName); Snackbar snackbar = Snackbar.make(containerView, message, Snackbar.LENGTH_LONG) .setAction(R.string.apply_to_all_profiles, new View.OnClickListener() { @Override diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java index ad87af9077..0878f0a37c 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchHelper.java @@ -29,6 +29,7 @@ import net.osmand.plus.poi.NominatimPoiFilter; import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.resources.ResourceManager.ResourceListener; +import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType; import net.osmand.search.SearchUICore; @@ -65,10 +66,11 @@ public class QuickSearchHelper implements ResourceListener { public QuickSearchHelper(OsmandApplication app) { this.app = app; - core = new SearchUICore(app.getPoiTypes(), app.getSettings().MAP_PREFERRED_LOCALE.get(), - app.getSettings().MAP_TRANSLITERATE_NAMES.get()); + OsmandSettings settings = app.getSettings(); + core = new SearchUICore(app.getPoiTypes(), settings.MAP_PREFERRED_LOCALE.get(), + settings.MAP_TRANSLITERATE_NAMES.get()); app.getResourceManager().addResourceListener(this); - SearchResultMatcher.setSpeedCamerasUninstalled(app.getSettings().SPEED_CAMERAS_UNINSTALLED.get()); + SearchResultMatcher.speedCamerasUninstalled = settings.SPEED_CAMERAS_UNINSTALLED.get(); } public SearchUICore getCore() { diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index 532dd0a245..ab296c4a3f 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -2108,8 +2108,8 @@ public class OsmandSettings { public final OsmandPreference SPEAK_SPEED_CAMERA = new BooleanPreference("speak_cameras", false).makeProfile().cache(); public final OsmandPreference SPEAK_TUNNELS = new BooleanPreference("speak_tunnels", false).makeProfile().cache(); - public final OsmandPreference SPEED_CAMERAS_UNINSTALLED = new BooleanPreference("speed_cameras_uninstalled", false).makeGlobal().cache(); - public final OsmandPreference SPEED_CAMERAS_ALERT_SHOWED = new BooleanPreference("speed_cameras_alert_showed", false).makeGlobal().cache(); + public final OsmandPreference SPEED_CAMERAS_UNINSTALLED = new BooleanPreference("speed_cameras_uninstalled", false).makeGlobal(); + public final OsmandPreference SPEED_CAMERAS_ALERT_SHOWED = new BooleanPreference("speed_cameras_alert_showed", false).makeGlobal(); public final OsmandPreference ANNOUNCE_WPT = new BooleanPreference("announce_wpt", true) { @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java index d0ed8f7515..f0da066fbe 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/BaseSettingsFragment.java @@ -16,6 +16,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.ColorInt; @@ -923,14 +924,40 @@ public abstract class BaseSettingsFragment extends PreferenceFragmentCompat impl } } - protected void applyPreferenceWithSnackBar(final String prefId, final Serializable newValue) { + public void setupSpeedCamerasAlert() { + Preference speedCamerasAlert = findPreference(settings.SPEED_CAMERAS_UNINSTALLED.getId()); + speedCamerasAlert.setIcon(getContentIcon(R.drawable.ic_action_alert)); + speedCamerasAlert.setVisible(!settings.SPEED_CAMERAS_UNINSTALLED.get()); + } + + public void setupPrefRoundedBg(PreferenceViewHolder holder) { + View selectableView = holder.itemView.findViewById(R.id.selectable_list_item); + if (selectableView != null) { + int color = AndroidUtils.getColorFromAttr(app, R.attr.activity_background_color); + int selectedColor = UiUtilities.getColorWithAlpha(getActiveProfileColor(), 0.3f); + + Drawable bgDrawable = getPaintedIcon(R.drawable.rectangle_rounded, color); + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { + Drawable selectable = getPaintedIcon(R.drawable.ripple_rectangle_rounded, selectedColor); + Drawable[] layers = {bgDrawable, selectable}; + AndroidUtils.setBackground(selectableView, new LayerDrawable(layers)); + } else { + AndroidUtils.setBackground(selectableView, bgDrawable); + } + LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) selectableView.getLayoutParams(); + params.setMargins(params.leftMargin, AndroidUtils.dpToPx(app, 6), params.rightMargin, params.bottomMargin); + } + } + + protected void applyPreferenceWithSnackBar(final String prefId, + final Serializable newValue) { onApplyPreferenceChange(prefId, false, newValue); updateSetting(prefId); View containerView = getView(); if (containerView != null) { String modeName = appMode.toHumanString(); String text = app.getString(R.string.changes_applied_to_profile, modeName); - SpannableString message = UiUtilities.createSpannableString(text, modeName, new StyleSpan(Typeface.BOLD)); + SpannableString message = UiUtilities.createSpannableString(text, new StyleSpan(Typeface.BOLD), modeName); Snackbar snackbar = Snackbar.make(containerView, message, Snackbar.LENGTH_LONG) .setAction(R.string.apply_to_all_profiles, new View.OnClickListener() { @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java index 8896c159b9..9f3a6439aa 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/GlobalSettingsFragment.java @@ -17,7 +17,6 @@ import net.osmand.plus.activities.SettingsGeneralActivity; import net.osmand.plus.dialogs.SendAnalyticsBottomSheetDialogFragment; import net.osmand.plus.dialogs.SendAnalyticsBottomSheetDialogFragment.OnSendAnalyticsPrefsUpdate; import net.osmand.plus.dialogs.SpeedCamerasBottomSheet; -import net.osmand.plus.dialogs.SpeedCamerasBottomSheet.OnSpeedCamerasUninstallListener; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.preferences.ListPreferenceEx; @@ -25,19 +24,14 @@ import net.osmand.plus.settings.preferences.SwitchPreferenceEx; public class GlobalSettingsFragment extends BaseSettingsFragment - implements OnSendAnalyticsPrefsUpdate, OnPreferenceChanged, - OnSpeedCamerasUninstallListener { + implements OnSendAnalyticsPrefsUpdate, OnPreferenceChanged { public static final String TAG = GlobalSettingsFragment.class.getSimpleName(); private static final String SEND_ANONYMOUS_DATA_PREF_ID = "send_anonymous_data"; private static final String DIALOGS_AND_NOTIFICATIONS_PREF_ID = "dialogs_and_notifications"; - private static final String UNINSTALL_SPEED_CAMERAS_PREF_ID = "uninstall_speed_cameras"; private static final String LEGAL_CATEGORY_ID = "legal"; - private Preference uninstallSpeedCameras; - private PreferenceCategory legalCategory; - @Override protected void setupPreferences() { setupDefaultAppModePref(); @@ -47,12 +41,8 @@ public class GlobalSettingsFragment extends BaseSettingsFragment setupSendAnonymousDataPref(); setupDialogsAndNotificationsPref(); setupEnableProxyPref(); - legalCategory = (PreferenceCategory) findPreference(LEGAL_CATEGORY_ID); - uninstallSpeedCameras = (Preference) findPreference(UNINSTALL_SPEED_CAMERAS_PREF_ID); - uninstallSpeedCameras.setIcon(getPersistentPrefIcon(R.drawable.ic_speed_camera_disabled)); - if (settings.SPEED_CAMERAS_UNINSTALLED.get()) { - onSpeedCamerasUninstalled(); - } + setupLegalCategory(); + setupUninstallSpeedCamerasPref(); } @Override @@ -117,6 +107,9 @@ public class GlobalSettingsFragment extends BaseSettingsFragment app.checkPreferredLocale(); app.restartApp(activity); } + } else if (prefId.equals(settings.SPEED_CAMERAS_UNINSTALLED.getId())) { + setupLegalCategory(); + setupUninstallSpeedCamerasPref(); } } @@ -128,7 +121,7 @@ public class GlobalSettingsFragment extends BaseSettingsFragment @Override public boolean onPreferenceClick(Preference preference) { String prefId = preference.getKey(); - if (UNINSTALL_SPEED_CAMERAS_PREF_ID.equals(prefId)) { + if (settings.SPEED_CAMERAS_UNINSTALLED.getId().equals(prefId)) { FragmentManager fm = getFragmentManager(); if (fm != null) { SpeedCamerasBottomSheet.showInstance(fm, this); @@ -221,11 +214,14 @@ public class GlobalSettingsFragment extends BaseSettingsFragment enableProxy.setIcon(getPersistentPrefIcon(R.drawable.ic_action_proxy)); } - @Override - public void onSpeedCamerasUninstalled() { - if (uninstallSpeedCameras != null && legalCategory != null) { - uninstallSpeedCameras.setVisible(false); - legalCategory.setVisible(false); - } + private void setupLegalCategory() { + PreferenceCategory legalCategory = (PreferenceCategory) findPreference(LEGAL_CATEGORY_ID); + legalCategory.setVisible(!settings.SPEED_CAMERAS_UNINSTALLED.get()); + } + + private void setupUninstallSpeedCamerasPref() { + Preference uninstallSpeedCameras = (Preference) findPreference(settings.SPEED_CAMERAS_UNINSTALLED.getId()); + uninstallSpeedCameras.setIcon(getActiveIcon(R.drawable.ic_speed_camera_disabled)); + uninstallSpeedCameras.setVisible(!settings.SPEED_CAMERAS_UNINSTALLED.get()); } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java index 273e0f3de2..b7ab4e13ea 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java @@ -77,8 +77,7 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { recyclerView = root.findViewById(R.id.list); description.setText(UiUtilities.createSpannableString( String.format(getString(R.string.import_complete_description), fileName), - fileName, - new StyleSpan(Typeface.BOLD) + new StyleSpan(Typeface.BOLD), fileName )); btnClose.setOnClickListener(new View.OnClickListener() { @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportDuplicatesFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportDuplicatesFragment.java index 6224105fa9..7ea985e19a 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportDuplicatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportDuplicatesFragment.java @@ -257,8 +257,7 @@ public class ImportDuplicatesFragment extends BaseOsmAndFragment implements View toolbarLayout.setTitle(getString(R.string.shared_string_importing)); description.setText(UiUtilities.createSpannableString( String.format(getString(R.string.importing_from), file.getName()), - file.getName(), - new StyleSpan(Typeface.BOLD) + new StyleSpan(Typeface.BOLD), file.getName() )); progressBar.setVisibility(View.VISIBLE); list.setVisibility(View.GONE); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java index 99f1af5f37..bf3df71f48 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java @@ -234,8 +234,7 @@ public class ImportSettingsFragment extends BaseOsmAndFragment toolbarLayout.setTitle(getString(toolbarTitleRes)); description.setText(UiUtilities.createSpannableString( String.format(getString(descriptionRes), fileName), - fileName, - new StyleSpan(Typeface.BOLD) + new StyleSpan(Typeface.BOLD), fileName )); buttonsContainer.setVisibility(View.GONE); progressBar.setVisibility(View.VISIBLE); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ScreenAlertsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ScreenAlertsFragment.java index 901d0cfdc9..c07f8fca2d 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ScreenAlertsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ScreenAlertsFragment.java @@ -6,40 +6,29 @@ import android.os.Build; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.TextView; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.appcompat.widget.SwitchCompat; import androidx.core.content.ContextCompat; -import androidx.fragment.app.Fragment; -import androidx.fragment.app.FragmentActivity; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; import androidx.preference.SwitchPreferenceCompat; import net.osmand.AndroidUtils; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.dialogs.SpeedCamerasBottomSheet; -import net.osmand.plus.dialogs.SpeedCamerasBottomSheet.OnSpeedCamerasUninstallListener; -import net.osmand.plus.helpers.FontCache; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; -import java.lang.ref.WeakReference; import static net.osmand.plus.UiUtilities.CompoundButtonType.TOOLBAR; -public class ScreenAlertsFragment extends BaseSettingsFragment implements OnSpeedCamerasUninstallListener { +public class ScreenAlertsFragment extends BaseSettingsFragment implements OnPreferenceChanged { public static final String TAG = ScreenAlertsFragment.class.getSimpleName(); private static final String SHOW_ROUTING_ALARMS_INFO = "show_routing_alarms_info"; private static final String SCREEN_ALERTS_IMAGE = "screen_alerts_image"; - private static final String SHOW_CAMERAS = "show_cameras"; - private SwitchPreferenceCompat showCameras; @Override protected void setupPreferences() { @@ -47,19 +36,16 @@ public class ScreenAlertsFragment extends BaseSettingsFragment implements OnSpee SwitchPreferenceCompat showTrafficWarnings = (SwitchPreferenceCompat) findPreference(settings.SHOW_TRAFFIC_WARNINGS.getId()); SwitchPreferenceCompat showPedestrian = (SwitchPreferenceCompat) findPreference(settings.SHOW_PEDESTRIAN.getId()); SwitchPreferenceCompat showTunnels = (SwitchPreferenceCompat) findPreference(settings.SHOW_TUNNELS.getId()); - showCameras = (SwitchPreferenceCompat) findPreference(settings.SHOW_CAMERAS.getId()); showRoutingAlarmsInfo.setIcon(getContentIcon(R.drawable.ic_action_info_dark)); showTrafficWarnings.setIcon(getIcon(R.drawable.list_warnings_traffic_calming)); showPedestrian.setIcon(getIcon(R.drawable.list_warnings_pedestrian)); showTunnels.setIcon(getIcon(R.drawable.list_warnings_tunnel)); - showCameras.setIcon(getIcon(R.drawable.list_warnings_speed_camera)); setupScreenAlertsImage(); + setupShowCamerasPref(); + setupSpeedCamerasAlert(); enableDisablePreferences(settings.SHOW_ROUTING_ALARMS.getModeValue(getSelectedAppMode())); - if (settings.SPEED_CAMERAS_UNINSTALLED.get()) { - onSpeedCamerasUninstalled(); - } } @Override @@ -119,8 +105,8 @@ public class ScreenAlertsFragment extends BaseSettingsFragment implements OnSpee deviceImage.setImageDrawable(getDeviceImage()); warningIcon.setImageDrawable(getWarningIcon()); - } else if (SHOW_CAMERAS.equals(key)) { - setupSpeedCamerasAlert(app, requireMyActivity(), holder, this, isNightMode()); + } else if (settings.SPEED_CAMERAS_UNINSTALLED.getId().equals(key)) { + setupPrefRoundedBg(holder); } } } @@ -131,10 +117,20 @@ public class ScreenAlertsFragment extends BaseSettingsFragment implements OnSpee Preference routeParametersImage = findPreference(SCREEN_ALERTS_IMAGE); updatePreference(routeParametersImage); } - + if (settings.SPEED_CAMERAS_UNINSTALLED.getId().equals(preference.getKey())) { + SpeedCamerasBottomSheet.showInstance(requireActivity().getSupportFragmentManager(), this); + } return super.onPreferenceClick(preference); } + @Override + public void onPreferenceChanged(String prefId) { + if (prefId.equals(settings.SPEED_CAMERAS_UNINSTALLED.getId())) { + setupShowCamerasPref(); + setupSpeedCamerasAlert(); + } + } + private void setupScreenAlertsImage() { Preference routeParametersImage = findPreference(SCREEN_ALERTS_IMAGE); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) { @@ -163,47 +159,9 @@ public class ScreenAlertsFragment extends BaseSettingsFragment implements OnSpee return null; } - public static void setupSpeedCamerasAlert(@NonNull OsmandApplication app, - @NonNull FragmentActivity activity, - @NonNull PreferenceViewHolder holder, - @Nullable Fragment targetFragment, - boolean nightMode) { - ImageView alertIcon = (ImageView) holder.itemView.findViewById(R.id.alert_icon); - TextView alertTitle = (TextView) holder.itemView.findViewById(R.id.alert_title); - TextView alertSubTitle = (TextView) holder.itemView.findViewById(R.id.alert_subtitle); - LinearLayout alertBg = (LinearLayout) holder.itemView.findViewById(R.id.alert_bg); - - alertBg.setBackgroundDrawable(UiUtilities.getRoundedBackgroundDrawable( - app, - nightMode ? R.color.activity_background_color_dark : R.color.activity_background_color_light, - 6)); - alertIcon.setImageDrawable(app.getUIUtilities().getIcon( - R.drawable.ic_action_alert, - nightMode ? R.color.icon_color_default_dark : R.color.icon_color_default_light)); - alertTitle.setText(R.string.speed_cameras_alert); - alertTitle.setTypeface(FontCache.getRobotoMedium(app)); - alertSubTitle.setText(R.string.read_more); - alertSubTitle.setTypeface(FontCache.getRobotoMedium(app)); - alertSubTitle.setTextColor(nightMode - ? app.getResources().getColor(R.color.active_color_primary_dark) - : app.getResources().getColor(R.color.active_color_primary_light)); - final WeakReference weakActivity = new WeakReference<>(activity); - final WeakReference weakFragment = new WeakReference<>(targetFragment); - alertSubTitle.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - FragmentActivity a = weakActivity.get(); - if (a != null) { - SpeedCamerasBottomSheet.showInstance(a.getSupportFragmentManager(), weakFragment.get()); - } - } - }); - } - - @Override - public void onSpeedCamerasUninstalled() { - if (showCameras != null) { - showCameras.setVisible(false); - } + private void setupShowCamerasPref() { + SwitchPreferenceCompat showCameras = (SwitchPreferenceCompat) findPreference(settings.SHOW_CAMERAS.getId()); + showCameras.setIcon(getIcon(R.drawable.list_warnings_speed_camera)); + showCameras.setVisible(!settings.SPEED_CAMERAS_UNINSTALLED.get()); } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/VoiceAnnouncesFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/VoiceAnnouncesFragment.java index 11c1f6b72b..24a1b817d4 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/VoiceAnnouncesFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/VoiceAnnouncesFragment.java @@ -14,9 +14,10 @@ import androidx.appcompat.widget.SwitchCompat; import androidx.core.content.ContextCompat; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; +import androidx.preference.SwitchPreferenceCompat; import net.osmand.AndroidUtils; -import net.osmand.plus.dialogs.SpeedCamerasBottomSheet.OnSpeedCamerasUninstallListener; +import net.osmand.plus.dialogs.SpeedCamerasBottomSheet; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.R; @@ -34,10 +35,9 @@ import static net.osmand.plus.settings.backend.OsmandSettings.VOICE_PROVIDER_NOT import static net.osmand.plus.UiUtilities.CompoundButtonType.TOOLBAR; import static net.osmand.plus.activities.SettingsNavigationActivity.MORE_VALUE; -public class VoiceAnnouncesFragment extends BaseSettingsFragment implements OnSpeedCamerasUninstallListener { +public class VoiceAnnouncesFragment extends BaseSettingsFragment implements OnPreferenceChanged { public static final String TAG = VoiceAnnouncesFragment.class.getSimpleName(); - private Preference speakCamera; @Override protected void createToolbar(LayoutInflater inflater, View view) { @@ -98,10 +98,8 @@ public class VoiceAnnouncesFragment extends BaseSettingsFragment implements OnSp setupInterruptMusicPref(); } enableDisablePreferences(!settings.VOICE_MUTE.getModeValue(getSelectedAppMode())); - speakCamera = findPreference(settings.SPEAK_SPEED_CAMERA.getId()); - if (settings.SPEED_CAMERAS_UNINSTALLED.get()) { - onSpeedCamerasUninstalled(); - } + setupSpeakCamerasPref(); + setupSpeedCamerasAlert(); } private void setupSpeedLimitExceedPref() { @@ -231,8 +229,8 @@ public class VoiceAnnouncesFragment extends BaseSettingsFragment implements OnSp Object currentValue = ((ListPreferenceEx) preference).getValue(); imageView.setEnabled(preference.isEnabled() && !OsmandSettings.VOICE_PROVIDER_NOT_USE.equals(currentValue)); } - } else if (settings.SPEAK_SPEED_CAMERA.getId().equals(preference.getKey())) { - ScreenAlertsFragment.setupSpeedCamerasAlert(app, requireMyActivity(), holder, this, isNightMode()); + } else if (settings.SPEED_CAMERAS_UNINSTALLED.getId().equals(preference.getKey())) { + setupPrefRoundedBg(holder); } } @@ -296,9 +294,23 @@ public class VoiceAnnouncesFragment extends BaseSettingsFragment implements OnSp } @Override - public void onSpeedCamerasUninstalled() { - if (speakCamera != null) { - speakCamera.setVisible(false); + public boolean onPreferenceClick(Preference preference) { + if (settings.SPEED_CAMERAS_UNINSTALLED.getId().equals(preference.getKey())) { + SpeedCamerasBottomSheet.showInstance(requireActivity().getSupportFragmentManager(), this); + } + return super.onPreferenceClick(preference); + } + + @Override + public void onPreferenceChanged(String prefId) { + if (prefId.equals(settings.SPEED_CAMERAS_UNINSTALLED.getId())) { + setupSpeakCamerasPref(); + setupSpeedCamerasAlert(); } } + + private void setupSpeakCamerasPref() { + SwitchPreferenceCompat showCameras = (SwitchPreferenceCompat) findPreference(settings.SPEAK_SPEED_CAMERA.getId()); + showCameras.setVisible(!settings.SPEED_CAMERAS_UNINSTALLED.get()); + } } \ No newline at end of file