From c20e096cdade8332c11e2c1114e68e999f196b57 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Fri, 19 Jun 2020 10:07:46 +0300 Subject: [PATCH] refactor speed cameras hiding --- .../binary/BinaryMapPoiReaderAdapter.java | 8 +-- .../java/net/osmand/osm/AbstractPoiType.java | 9 --- .../main/java/net/osmand/osm/MapPoiTypes.java | 16 ++--- .../main/java/net/osmand/osm/PoiFilter.java | 8 ++- .../java/net/osmand/search/SearchUICore.java | 11 ---- OsmAnd/res/values/strings.xml | 2 + .../src/net/osmand/plus/AppInitializer.java | 5 +- OsmAnd/src/net/osmand/plus/UiUtilities.java | 15 +++-- .../plus/dialogs/SpeedCamerasBottomSheet.java | 13 +--- ...eedCamerasUninstallRestartBottomSheet.java | 64 +++++++++++++++++++ .../plus/helpers/SearchHistoryHelper.java | 3 + .../QuickSearchSubCategoriesFragment.java | 6 -- .../plus/settings/backend/OsmandSettings.java | 10 +++ 13 files changed, 108 insertions(+), 62 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasUninstallRestartBottomSheet.java diff --git a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapPoiReaderAdapter.java b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapPoiReaderAdapter.java index 5c196ebd89..4e8441a1bb 100644 --- a/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapPoiReaderAdapter.java +++ b/OsmAnd-java/src/main/java/net/osmand/binary/BinaryMapPoiReaderAdapter.java @@ -692,10 +692,11 @@ public class BinaryMapPoiReaderAdapter { StringBuilder retValue = new StringBuilder(); PoiCategory amenityType = null; LinkedList textTags = null; + boolean isForbidden = false; while (true) { int t = codedIS.readTag(); int tag = WireFormat.getTagFieldNumber(t); - if (amenityType == null && (tag > OsmandOdb.OsmAndPoiBoxDataAtom.CATEGORIES_FIELD_NUMBER || tag == 0)) { + if (amenityType == null && (tag > OsmandOdb.OsmAndPoiBoxDataAtom.CATEGORIES_FIELD_NUMBER || tag == 0) || isForbidden) { codedIS.skipRawBytes(codedIS.getBytesUntilLimit()); return null; } @@ -770,10 +771,7 @@ public class BinaryMapPoiReaderAdapter { } } subtype = poiTypes.replaceDeprecatedSubtype(type, subtype); - PoiType poiType = poiTypes.getPoiTypeByKeyInCategory(type, subtype); - if (poiType != null && poiType.isForbidden()) { - return null; - } + isForbidden = poiTypes.isKeyNameForbidden(subtype); if (req.poiTypeFilter == null || req.poiTypeFilter.accept(type, subtype)) { if (amenityType == null) { amenityType = type; diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/AbstractPoiType.java b/OsmAnd-java/src/main/java/net/osmand/osm/AbstractPoiType.java index 3d9ae43b66..19cb42f350 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/AbstractPoiType.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/AbstractPoiType.java @@ -21,7 +21,6 @@ public abstract class AbstractPoiType { private String synonyms; private String enTranslation; private String translation; - private boolean isForbidden; public AbstractPoiType(String keyName, MapPoiTypes registry) { this.keyName = keyName; @@ -68,14 +67,6 @@ public abstract class AbstractPoiType { return this instanceof PoiType && this.isAdditional(); } - public void setForbidden(boolean forbidden) { - isForbidden = forbidden; - } - - public boolean isForbidden() { - return isForbidden; - } - public String getTranslation() { if(translation == null) { translation = registry.getTranslation(this); 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 9f6b42933e..be3644ab90 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/MapPoiTypes.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/MapPoiTypes.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -34,6 +35,7 @@ public class MapPoiTypes { private static final Log log = PlatformUtil.getLog(MapRenderingTypes.class); private String resourceName; private List categories = new ArrayList(); + private Set forbiddenKeyNames = new HashSet<>(); private PoiCategory otherCategory; private PoiCategory otherMapCategory; @@ -946,13 +948,11 @@ public class MapPoiTypes { } } - public void forbidPoiType(String keyName) { - for (PoiCategory category : categories) { - for (PoiType poiType : category.getPoiTypes()) { - if (keyName.equals(poiType.getKeyName())) { - poiType.setForbidden(true); - } - } - } + public void setForbiddenKeyNames(Set forbiddenKeyNames) { + this.forbiddenKeyNames = forbiddenKeyNames; + } + + public boolean isKeyNameForbidden(String key) { + return forbiddenKeyNames.contains(key); } } diff --git a/OsmAnd-java/src/main/java/net/osmand/osm/PoiFilter.java b/OsmAnd-java/src/main/java/net/osmand/osm/PoiFilter.java index fbb5fddca7..12bbaa13f0 100644 --- a/OsmAnd-java/src/main/java/net/osmand/osm/PoiFilter.java +++ b/OsmAnd-java/src/main/java/net/osmand/osm/PoiFilter.java @@ -30,13 +30,14 @@ public class PoiFilter extends AbstractPoiType { List npoiTypes = null; Map nmap = null; for (PoiType poiType : poiTypesToAdd.values()) { - if (!map.containsKey(poiType.getKeyName())) { + String keyName = poiType.getKeyName(); + if (!map.containsKey(keyName) && !registry.isKeyNameForbidden(keyName)) { if (npoiTypes == null) { npoiTypes = new ArrayList(this.poiTypes); nmap = new LinkedHashMap<>(map); } npoiTypes.add(poiType); - nmap.put(poiType.getKeyName(), poiType); + nmap.put(keyName, poiType); } } if (npoiTypes != null) { @@ -46,6 +47,9 @@ public class PoiFilter extends AbstractPoiType { } public void addPoiType(PoiType type) { + if (registry.isKeyNameForbidden(type.keyName)) { + return; + } if (!map.containsKey(type.getKeyName())) { poiTypes.add(type); map.put(type.getKeyName(), type); 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 53c035fdaa..46b0ee4e82 100644 --- a/OsmAnd-java/src/main/java/net/osmand/search/SearchUICore.java +++ b/OsmAnd-java/src/main/java/net/osmand/search/SearchUICore.java @@ -11,7 +11,6 @@ import net.osmand.data.MapObject; import net.osmand.data.Street; import net.osmand.osm.AbstractPoiType; import net.osmand.osm.MapPoiTypes; -import net.osmand.osm.PoiType; import net.osmand.search.core.CustomSearchPoiFilter; import net.osmand.search.core.ObjectType; import net.osmand.search.core.SearchCoreAPI; @@ -734,9 +733,6 @@ public class SearchUICore { @Override public boolean publish(SearchResult object) { - if (isPoiTypeForbidden(object.object)) { - return false; - } if (phrase != null && object.otherNames != null && !phrase.getFirstUnknownNameStringMatcher().matches(object.localeName)) { for (String s : object.otherNames) { if (phrase.getFirstUnknownNameStringMatcher().matches(s)) { @@ -890,13 +886,6 @@ public class SearchUICore { } return json; } - - private boolean isPoiTypeForbidden(Object object) { - if (object instanceof PoiType) { - return ((PoiType) object).isForbidden(); - } - return false; - } } private enum ResultCompareStep { diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 6c1fdd47f7..05f81e943d 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,8 @@ Thx - Hardy --> + Uninstall and Restart + Restart is needed to completely delete speed camera data. A toggle to show or hide the Mapillary layer on the map. Show Mapillary Hide Mapillary diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index f7c1d89aba..9e06a56d20 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -86,7 +86,6 @@ import java.util.Random; import btools.routingapp.BRouterServiceConnection; import static net.osmand.plus.AppVersionUpgradeOnInit.LAST_APP_VERSION; -import static net.osmand.plus.dialogs.SpeedCamerasBottomSheet.SPEED_CAMERA_KEY_NAME; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.getPendingIntent; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceLastCheck; import static net.osmand.plus.liveupdates.LiveUpdatesHelper.preferenceLiveUpdatesOn; @@ -303,6 +302,7 @@ public class AppInitializer implements IProgress { } private void initPoiTypes() { + app.poiTypes.setForbiddenKeyNames(app.osmandSettings.getForbiddenPoiKeyNames()); if (app.getAppPath(IndexConstants.SETTINGS_DIR + "poi_types.xml").exists()) { app.poiTypes.init(app.getAppPath(IndexConstants.SETTINGS_DIR + "poi_types.xml").getAbsolutePath()); } else { @@ -407,9 +407,6 @@ public class AppInitializer implements IProgress { return null; } }); - if (app.getSettings().SPEED_CAMERAS_UNINSTALLED.get()) { - app.getPoiTypes().forbidPoiType(SPEED_CAMERA_KEY_NAME); - } } public void onCreateApplication() { diff --git a/OsmAnd/src/net/osmand/plus/UiUtilities.java b/OsmAnd/src/net/osmand/plus/UiUtilities.java index 0063d9aeea..c60117f58c 100644 --- a/OsmAnd/src/net/osmand/plus/UiUtilities.java +++ b/OsmAnd/src/net/osmand/plus/UiUtilities.java @@ -1,13 +1,11 @@ package net.osmand.plus; 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; import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.RippleDrawable; import android.hardware.Sensor; @@ -15,8 +13,6 @@ import android.hardware.SensorManager; import android.os.Build; import android.text.SpannableString; import android.text.Spanned; -import android.text.method.LinkMovementMethod; -import android.text.style.ClickableSpan; import android.text.style.StyleSpan; import android.view.Gravity; import android.view.LayoutInflater; @@ -38,7 +34,6 @@ import androidx.annotation.StringRes; import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.view.ContextThemeWrapper; import androidx.appcompat.widget.ListPopupWindow; -import androidx.appcompat.widget.AppCompatButton; import androidx.appcompat.widget.SwitchCompat; import androidx.core.content.ContextCompat; import androidx.core.graphics.drawable.DrawableCompat; @@ -55,7 +50,6 @@ import net.osmand.AndroidUtils; import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.data.LatLon; -import net.osmand.plus.helpers.FontCache; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.views.DirectionDrawable; import net.osmand.plus.widgets.TextViewEx; @@ -66,7 +60,6 @@ import org.apache.commons.logging.Log; import java.util.ArrayList; import java.util.List; -import java.lang.ref.WeakReference; import gnu.trove.map.hash.TLongObjectHashMap; @@ -87,6 +80,7 @@ public class UiUtilities { public enum DialogButtonType { PRIMARY, SECONDARY, + SECONDARY_HARMFUL, STROKED } @@ -633,6 +627,13 @@ public class UiUtilities { AndroidUtils.setBackground(ctx, buttonView, nightMode, R.drawable.dlg_btn_secondary_light, R.drawable.dlg_btn_secondary_dark); textAndIconColorResId = nightMode ? R.color.dlg_btn_secondary_text_dark : R.color.dlg_btn_secondary_text_light; break; + case SECONDARY_HARMFUL: + if (v21) { + AndroidUtils.setBackground(ctx, buttonContainer, nightMode, R.drawable.ripple_solid_light, R.drawable.ripple_solid_dark); + } + AndroidUtils.setBackground(ctx, buttonView, nightMode, R.drawable.dlg_btn_secondary_light, R.drawable.dlg_btn_secondary_dark); + textAndIconColorResId = R.color.color_osm_edit_delete; + break; case STROKED: if (v21) { AndroidUtils.setBackground(ctx, buttonContainer, nightMode, R.drawable.ripple_light, R.drawable.ripple_dark); diff --git a/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java b/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java index cd6b8f487a..fac4230c77 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasBottomSheet.java @@ -18,8 +18,6 @@ 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; public class SpeedCamerasBottomSheet extends MenuBottomSheetDialogFragment { @@ -59,14 +57,9 @@ public class SpeedCamerasBottomSheet extends MenuBottomSheetDialogFragment { @Override protected void onDismissButtonClickAction() { - OsmandPreference speedCamUninstalled = settings.SPEED_CAMERAS_UNINSTALLED; - speedCamUninstalled.set(true); - settings.SPEAK_SPEED_CAMERA.set(false); - settings.SHOW_CAMERAS.set(false); - app.getPoiTypes().forbidPoiType(SPEED_CAMERA_KEY_NAME); - Fragment targetFragment = getTargetFragment(); - if (targetFragment instanceof OnPreferenceChanged) { - ((OnPreferenceChanged) targetFragment).onPreferenceChanged(speedCamUninstalled.getId()); + FragmentManager fm = getFragmentManager(); + if (fm != null) { + SpeedCamerasUninstallRestartBottomSheet.showInstance(fm); } setDialogShowed(); } diff --git a/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasUninstallRestartBottomSheet.java b/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasUninstallRestartBottomSheet.java new file mode 100644 index 0000000000..8a5271cec9 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/dialogs/SpeedCamerasUninstallRestartBottomSheet.java @@ -0,0 +1,64 @@ +package net.osmand.plus.dialogs; + +import android.app.Activity; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.fragment.app.FragmentManager; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.UiUtilities.DialogButtonType; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.base.MenuBottomSheetDialogFragment; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.settings.backend.OsmandSettings; + +public class SpeedCamerasUninstallRestartBottomSheet extends MenuBottomSheetDialogFragment { + + public static final String TAG = SpeedCamerasUninstallRestartBottomSheet.class.getSimpleName(); + + public static void showInstance(@NonNull FragmentManager fm) { + if (!fm.isStateSaved()) { + SpeedCamerasUninstallRestartBottomSheet bottomSheet = new SpeedCamerasUninstallRestartBottomSheet(); + bottomSheet.show(fm, TAG); + } + } + + @Override + public void createMenuItems(Bundle savedInstanceState) { + items.add(new TitleItem(getString(R.string.uninstall_speed_cameras))); + items.add(new LongDescriptionItem(getString(R.string.speed_cameras_restart_descr))); + } + + @Override + protected int getDismissButtonTextId() { + return R.string.shared_string_cancel; + } + + @Override + protected int getRightBottomButtonTextId() { + return R.string.shared_string_uninstall_and_restart; + } + + @Override + protected DialogButtonType getRightBottomButtonType() { + return DialogButtonType.SECONDARY_HARMFUL; + } + + @Override + protected void onRightBottomButtonClick() { + OsmandApplication app = requiredMyApplication(); + OsmandSettings settings = app.getSettings(); + settings.SPEED_CAMERAS_UNINSTALLED.set(true); + settings.SPEAK_SPEED_CAMERA.set(false); + settings.SHOW_CAMERAS.set(false); + Activity activity = getActivity(); + if (activity instanceof MapActivity) { + MapActivity.doRestart(activity); + } else { + android.os.Process.killProcess(android.os.Process.myPid()); + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java index fb708bf8fd..828d79dbf0 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/SearchHistoryHelper.java @@ -428,6 +428,9 @@ public class SearchHistoryHelper { do { String name = query.getString(0); PointDescription p = PointDescription.deserializeFromString(name, new LatLon(query.getDouble(1), query.getDouble(2))); + if (context.getPoiTypes().isKeyNameForbidden(p.getName())){ + query.moveToNext(); + } HistoryEntry e = new HistoryEntry(query.getDouble(1), query.getDouble(2), p); long time = query.getLong(3); diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchSubCategoriesFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchSubCategoriesFragment.java index f7b58f6ab6..e61b4631c2 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchSubCategoriesFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchSubCategoriesFragment.java @@ -87,12 +87,6 @@ public class QuickSearchSubCategoriesFragment extends BaseOsmAndDialogFragment { acceptedCategories = new HashSet<>(savedInstanceState.getStringArrayList(ACCEPTED_CATEGORIES_KEY)); } poiTypeList = new ArrayList<>(poiCategory.getPoiTypes()); - Iterator iter = poiTypeList.iterator(); - while (iter.hasNext()) { - if (iter.next().isForbidden()) { - iter.remove(); - } - } Collections.sort(poiTypeList, new Comparator() { @Override public int compare(PoiType poiType, PoiType t1) { diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java index ab296c4a3f..9bc2bf0c88 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmandSettings.java @@ -74,6 +74,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -87,6 +88,7 @@ import java.util.StringTokenizer; import static net.osmand.aidlapi.OsmAndCustomizationConstants.CONFIGURE_MAP_ITEM_ID_SCHEME; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_ITEM_ID_SCHEME; import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_ACTIONS; +import static net.osmand.plus.dialogs.SpeedCamerasBottomSheet.SPEED_CAMERA_KEY_NAME; public class OsmandSettings { @@ -2111,6 +2113,14 @@ public class OsmandSettings { 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 Set getForbiddenPoiKeyNames() { + Set keyNames = new HashSet<>(); + if (SPEED_CAMERAS_UNINSTALLED.get()) { + keyNames.add(SPEED_CAMERA_KEY_NAME); + } + return keyNames; + } + public final OsmandPreference ANNOUNCE_WPT = new BooleanPreference("announce_wpt", true) { @Override protected boolean setValue(Object prefs, Boolean val) {