diff --git a/OsmAnd/res/layout/read_wikipedia_ofline_banner.xml b/OsmAnd/res/layout/read_wikipedia_ofline_banner.xml new file mode 100644 index 0000000000..1093ad41f1 --- /dev/null +++ b/OsmAnd/res/layout/read_wikipedia_ofline_banner.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/search_banner_button_list_item.xml b/OsmAnd/res/layout/search_banner_button_list_item.xml new file mode 100644 index 0000000000..65d048029f --- /dev/null +++ b/OsmAnd/res/layout/search_banner_button_list_item.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/search_banner_list_item.xml b/OsmAnd/res/layout/search_banner_list_item.xml new file mode 100644 index 0000000000..48f0875c9c --- /dev/null +++ b/OsmAnd/res/layout/search_banner_list_item.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OsmAnd/res/layout/search_more_list_item.xml b/OsmAnd/res/layout/search_more_list_item.xml index 41b15480b4..d2de7ef530 100644 --- a/OsmAnd/res/layout/search_more_list_item.xml +++ b/OsmAnd/res/layout/search_more_list_item.xml @@ -74,7 +74,7 @@ android:visibility="gone"/> + Get information about points of interest from Wikipedia. It is your pocket offline guide - just enable Wikipedia plugin and enjoy articles about objects around you. + Download Wikipedia maps The current destination point on the route will be deleted. If it will be the Destination, navigation will stop. Please provide a name for the point Volume buttons as zoom diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index 24cc0e2eea..bf9eccf4b4 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -21,6 +21,8 @@ import net.osmand.IProgress; import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.access.AccessibilityPlugin; +import net.osmand.data.Amenity; +import net.osmand.data.MapObject; import net.osmand.map.WorldRegion; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.TabActivity.TabItem; @@ -39,14 +41,18 @@ import net.osmand.plus.myplaces.FavoritesActivity; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.osmedit.OsmEditingPlugin; import net.osmand.plus.parkingpoint.ParkingPositionPlugin; +import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.quickaction.QuickActionType; import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin; +import net.osmand.plus.search.QuickSearchDialogFragment; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.settings.fragments.BaseSettingsFragment; import net.osmand.plus.skimapsplugin.SkiMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.plus.wikipedia.WikipediaPlugin; +import net.osmand.search.core.SearchPhrase; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -201,6 +207,10 @@ public abstract class OsmandPlugin { return Collections.emptyList(); } + protected List getCustomPoiFilters() { + return Collections.emptyList(); + } + /** * Plugin was installed */ @@ -258,12 +268,9 @@ public abstract class OsmandPlugin { Set enabledPlugins = app.getSettings().getEnabledPlugins(); allPlugins.clear(); - allPlugins.add(new MapillaryPlugin(app)); - if (!enabledPlugins.contains(MapillaryPlugin.ID) && !app.getSettings().getPlugins().contains("-" + MapillaryPlugin.ID)) { - enabledPlugins.add(MapillaryPlugin.ID); - app.getSettings().enablePlugin(MapillaryPlugin.ID, true); - } + enableHiddenPlugin(app, enabledPlugins, new MapillaryPlugin(app)); + enableHiddenPlugin(app, enabledPlugins, new WikipediaPlugin(app)); allPlugins.add(new OsmandRasterMapsPlugin(app)); allPlugins.add(new OsmandMonitoringPlugin(app)); @@ -368,6 +375,14 @@ public abstract class OsmandPlugin { } } + private static void enableHiddenPlugin(@NonNull OsmandApplication app, @NonNull Set enabledPlugins, @NonNull OsmandPlugin plugin) { + allPlugins.add(plugin); + if (!enabledPlugins.contains(plugin.getId()) && !app.getSettings().getPlugins().contains("-" + plugin.getId())) { + enabledPlugins.add(plugin.getId()); + app.getSettings().enablePlugin(plugin.getId(), true); + } + } + private static void checkMarketPlugin(@NonNull OsmandApplication app, @NonNull Set enabledPlugins, @NonNull OsmandPlugin plugin) { if (updateMarketPlugin(app, enabledPlugins, plugin)) { allPlugins.add(plugin); @@ -532,6 +547,24 @@ public abstract class OsmandPlugin { protected void optionsMenuFragment(Activity activity, Fragment fragment, ContextMenuAdapter optionsMenuAdapter) { } + protected boolean searchFinished(QuickSearchDialogFragment searchFragment, SearchPhrase phrase, boolean isResultEmpty) { + return false; + } + + protected void newDownloadIndexes(Fragment fragment) { + } + + protected void prepareExtraTopPoiFilters(Set poiUIFilter) { + } + + protected String getMapObjectsLocale(Amenity amenity, String preferredLocale) { + return null; + } + + protected String getMapObjectPreferredLang(MapObject object, String defaultLanguage) { + return null; + } + public List indexingFiles(IProgress progress) { return null; } @@ -789,6 +822,52 @@ public abstract class OsmandPlugin { } } + public static boolean onSearchFinished(QuickSearchDialogFragment searchFragment, SearchPhrase phrase, boolean isResultEmpty) { + boolean processed = false; + for (OsmandPlugin plugin : getEnabledPlugins()) { + processed = plugin.searchFinished(searchFragment, phrase, isResultEmpty) || processed; + } + return processed; + } + + public static void onNewDownloadIndexes(Fragment fragment) { + for (OsmandPlugin plugin : getEnabledPlugins()) { + plugin.newDownloadIndexes(fragment); + } + } + + public static void onPrepareExtraTopPoiFilters(Set poiUIFilters) { + for (OsmandPlugin plugin : getEnabledPlugins()) { + plugin.prepareExtraTopPoiFilters(poiUIFilters); + } + } + + public static String onGetMapObjectPreferredLang(MapObject object, String preferredMapLang, String preferredMapAppLang) { + for (OsmandPlugin plugin : getEnabledPlugins()) { + String locale = plugin.getMapObjectPreferredLang(object, preferredMapLang); + if (locale != null) { + return locale; + } + } + return preferredMapAppLang; + } + + public static String onGetMapObjectsLocale(Amenity amenity, String preferredLocale) { + for (OsmandPlugin plugin : getEnabledPlugins()) { + String locale = plugin.getMapObjectsLocale(amenity, preferredLocale); + if (locale != null) { + return locale; + } + } + return preferredLocale; + } + + public static void registerCustomPoiFilters(List poiUIFilters) { + for (OsmandPlugin p : getEnabledPlugins()) { + poiUIFilters.addAll(p.getCustomPoiFilters()); + } + } + public static Collection getPluginsCardsList() { HashSet collection = new HashSet<>(); for (OsmandPlugin plugin : getEnabledPlugins()) { @@ -810,8 +889,6 @@ public abstract class OsmandPlugin { return installed; } - - public static boolean onMapActivityKeyUp(MapActivity mapActivity, int keyCode) { for (OsmandPlugin p : getEnabledPlugins()) { if (p.mapActivityKeyUp(mapActivity, keyCode)) diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java index c0874af31e..f6399cfa7d 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java @@ -221,10 +221,6 @@ public class ConfigureMapMenu { } else if (itemId == R.string.layer_gpx_layer && cm.getItem(pos).getSelected()) { showGpxSelectionDialog(adapter, adapter.getItem(pos)); return false; - } else if (itemId == R.string.shared_string_wikipedia) { - ma.getDashboard().setDashboardVisibility(true, DashboardOnMap.DashboardType.WIKIPEDIA, - AndroidUtils.getCenterViewCoordinates(view)); - return false; } else if (itemId == R.string.rendering_category_transport) { final ContextMenuItem item = adapter.getItem(pos); TransportLinesMenu.showTransportsDialog(ma, new CallbackWithObject() { @@ -288,18 +284,6 @@ public class ConfigureMapMenu { } else { showGpxSelectionDialog(adapter, adapter.getItem(pos)); } - } else if (itemId == R.string.shared_string_wikipedia) { - WikipediaPoiMenu.toggleWikipediaPoi(ma, isChecked, new CallbackWithObject() { - @Override - public boolean processResult(Boolean selected) { - item.setSelected(selected); - item.setColorRes(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); - item.setDescription(selected ? - WikipediaPoiMenu.getLanguagesSummary(ma.getMyApplication()) : null); - adapter.notifyDataSetChanged(); - return true; - } - }); } else if (itemId == R.string.rendering_category_transport) { boolean selected = TransportLinesMenu.isShowLines(ma.getMyApplication()); TransportLinesMenu.toggleTransportLines(ma, !selected, new CallbackWithObject() { @@ -436,17 +420,6 @@ public class ConfigureMapMenu { .setSecondaryIcon(R.drawable.ic_action_additional_option) .setListener(l).createItem()); - selected = app.getPoiFilters().isTopWikiFilterSelected(); - adapter.addItem(new ContextMenuItem.ItemBuilder() - .setId(WIKIPEDIA_ID) - .setTitleId(R.string.shared_string_wikipedia, activity) - .setDescription(selected ? WikipediaPoiMenu.getLanguagesSummary(app) : null) - .setSelected(selected) - .setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID) - .setIcon(R.drawable.ic_plugin_wikipedia) - .setSecondaryIcon(R.drawable.ic_action_additional_option) - .setListener(l).createItem()); - selected = settings.SHOW_MAP_MARKERS.get(); adapter.addItem(new ContextMenuItem.ItemBuilder() .setId(MAP_MARKERS_ID) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 9fc699665a..c11810383d 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -196,8 +196,14 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo if (intent != null && intent.getExtras() != null) { String region = getIntent().getStringExtra(REGION_TO_SEARCH); if (region != null && !region.isEmpty()) { - showDialog(this, SearchDialogFragment.createInstance(region, - getIntent().getBooleanExtra(SHOW_WIKI_KEY, false))); + if (getIntent().getBooleanExtra(SHOW_WIKI_KEY, false)) { + showDialog(this, SearchDialogFragment.createInstance( + region, true, DownloadActivityType.NORMAL_FILE, + DownloadActivityType.WIKIPEDIA_FILE)); + } else { + showDialog(this, SearchDialogFragment.createInstance( + region, true, DownloadActivityType.NORMAL_FILE)); + } } filter = intent.getExtras().getString(FILTER_KEY); filterCat = intent.getExtras().getString(FILTER_CAT); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 6f8467bf48..9381a50db6 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -402,7 +402,12 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow String filter = getDownloadActivity().getFilterAndClear(); String filterCat = getDownloadActivity().getFilterCatAndClear(); String filterGroup = getDownloadActivity().getFilterGroupAndClear(); - if (filter != null) { + if (filter != null && filterCat != null + && filterCat.equals(DownloadActivityType.WIKIPEDIA_FILE.getTag())) { + getDownloadActivity().showDialog(getActivity(), + SearchDialogFragment.createInstance(filter, false, + DownloadActivityType.WIKIPEDIA_FILE)); + } else if (filter != null) { getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance(filter)); } else if (filterCat != null) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index 0fa52dc2b7..afb24f06e5 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -70,8 +70,11 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven public static final String TAG = "SearchDialogFragment"; private static final String SEARCH_TEXT_DLG_KEY = "search_text_dlg_key"; + public static final String SHOW_GROUP_KEY = "show_group_key"; + public static final String DOWNLOAD_TYPES_TO_SHOW_KEY = "download_types_to_show"; public static final String SHOW_WIKI_KEY = "show_wiki_key"; - private boolean showWiki; + private boolean showGroup; + private ArrayList downloadTypesToShow = new ArrayList<>(); private ListView listView; private SearchListAdapter listAdapter; private BannerAndDownloadFreeVersion banner; @@ -96,18 +99,22 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven if (savedInstanceState != null) { searchText = savedInstanceState.getString(SEARCH_TEXT_DLG_KEY); - showWiki = savedInstanceState.getBoolean(SHOW_WIKI_KEY); + showGroup = savedInstanceState.getBoolean(SHOW_GROUP_KEY); + downloadTypesToShow = savedInstanceState.getStringArrayList(DOWNLOAD_TYPES_TO_SHOW_KEY); } if (searchText == null) { Bundle arguments = getArguments(); if (arguments != null) { searchText = arguments.getString(SEARCH_TEXT_DLG_KEY); - showWiki = arguments.getBoolean(SHOW_WIKI_KEY); + showGroup = arguments.getBoolean(SHOW_GROUP_KEY); + downloadTypesToShow = arguments.getStringArrayList(DOWNLOAD_TYPES_TO_SHOW_KEY); } } if (searchText == null) { searchText = ""; - showWiki = false; + showGroup = true; + downloadTypesToShow = new ArrayList<>(); + downloadTypesToShow.add(DownloadActivityType.NORMAL_FILE.getTag()); } boolean isLightContent = getMyApplication().getSettings().isLightContent(); @@ -227,7 +234,8 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven @Override public void onSaveInstanceState(Bundle outState) { outState.putString(SEARCH_TEXT_DLG_KEY, searchText); - outState.putBoolean(SHOW_WIKI_KEY, showWiki); + outState.putBoolean(SHOW_GROUP_KEY, showGroup); + outState.putStringArrayList(DOWNLOAD_TYPES_TO_SHOW_KEY, downloadTypesToShow); super.onSaveInstanceState(outState); } @@ -254,17 +262,23 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven return (DownloadActivity) getActivity(); } - public static SearchDialogFragment createInstance(String searchText, boolean showWiki) { + public static SearchDialogFragment createInstance(String searchText, boolean showGroup, + DownloadActivityType ... fileTypes) { + ArrayList typesList = new ArrayList<>(); + for (DownloadActivityType type : fileTypes) { + typesList.add(type.getTag()); + } Bundle bundle = new Bundle(); bundle.putString(SEARCH_TEXT_DLG_KEY, searchText); - bundle.putBoolean(SHOW_WIKI_KEY, showWiki); + bundle.putBoolean(SHOW_GROUP_KEY, showGroup); + bundle.putStringArrayList(DOWNLOAD_TYPES_TO_SHOW_KEY, typesList); SearchDialogFragment fragment = new SearchDialogFragment(); fragment.setArguments(bundle); return fragment; } public static SearchDialogFragment createInstance(String searchText) { - return createInstance(searchText, false); + return createInstance(searchText, true, DownloadActivityType.NORMAL_FILE); } @Override @@ -484,16 +498,19 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven && group.getParentGroup().getParentGroup().getType() != DownloadResourceGroupType.WORLD && isMatch(conds, false, name)) { - filter.add(group); + if (showGroup) { + filter.add(group); + } for (DownloadResourceGroup g : group.getGroups()) { if (g.getType() == DownloadResourceGroupType.REGION_MAPS) { if (g.getIndividualResources() != null) { for (IndexItem item : g.getIndividualResources()) { - if (item.getType() == DownloadActivityType.NORMAL_FILE - || (item.getType() == DownloadActivityType.WIKIPEDIA_FILE - && showWiki)) { - filter.add(item); + for (String fileTypeTag : downloadTypesToShow) { + DownloadActivityType type = DownloadActivityType.getIndexType(fileTypeTag); + if (type != null && type == item.getType()) { + filter.add(item); + } } } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java index 685335866b..66567bc76c 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java @@ -44,7 +44,6 @@ import net.osmand.plus.widgets.tools.ClickableSpanTouchListener; import net.osmand.plus.wikipedia.WikiArticleHelper; import net.osmand.plus.wikipedia.WikipediaArticleWikiLinkFragment; import net.osmand.plus.wikipedia.WikipediaDialogFragment; -import net.osmand.plus.wikipedia.WikipediaPoiMenu; import net.osmand.util.Algorithms; import net.osmand.util.OpeningHoursParser; @@ -408,7 +407,7 @@ public class AmenityMenuBuilder extends MenuBuilder { if (amenity.getType().isWiki()) { if (!hasWiki) { - String articleLang = WikipediaPoiMenu.getWikiArticleLanguage(app, amenity.getSupportedContentLocales(), preferredLang); + String articleLang = OsmandPlugin.onGetMapObjectsLocale(amenity, preferredLang); String lng = amenity.getContentLanguage("content", articleLang, "en"); if (Algorithms.isEmpty(lng)) { lng = "en"; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java index 3e6ad7083c..06ade3cc26 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java @@ -13,7 +13,7 @@ import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiFilter; import net.osmand.osm.PoiType; import net.osmand.plus.MapMarkersHelper.MapMarker; -import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; @@ -22,7 +22,6 @@ import net.osmand.plus.mapcontextmenu.builders.AmenityMenuBuilder; import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.transport.TransportStopRoute; import net.osmand.plus.wikipedia.WikipediaDialogFragment; -import net.osmand.plus.wikipedia.WikipediaPoiMenu; import net.osmand.util.Algorithms; import net.osmand.util.OpeningHoursParser; @@ -158,15 +157,8 @@ public class AmenityMenuController extends MenuController { @NonNull @Override public String getNameStr() { - String preferredLang = getPreferredMapLang(); - if (amenity.getType().isWiki()) { - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { - OsmandApplication app = mapActivity.getMyApplication(); - preferredLang = WikipediaPoiMenu.getWikiArticleLanguage(app, - amenity.getSupportedContentLocales(), getPreferredMapAppLang()); - } - } + String preferredLang = OsmandPlugin.onGetMapObjectPreferredLang(amenity, + getPreferredMapAppLang(), getPreferredMapLang()); String name = amenity.getName(preferredLang, isTransliterateNames()); Map additionalInfo = amenity.getAdditionalInfo(); if (additionalInfo != null) { diff --git a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java index bccc49f614..a67d5554a6 100644 --- a/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java +++ b/OsmAnd/src/net/osmand/plus/poi/PoiFiltersHelper.java @@ -1,7 +1,5 @@ package net.osmand.plus.poi; -import android.os.Bundle; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.util.Pair; @@ -11,13 +9,13 @@ import net.osmand.osm.AbstractPoiType; import net.osmand.osm.MapPoiTypes; import net.osmand.osm.PoiCategory; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.api.SQLiteAPI; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; import net.osmand.plus.api.SQLiteAPI.SQLiteStatement; import net.osmand.plus.settings.backend.ApplicationMode; -import net.osmand.plus.wikipedia.WikipediaPoiMenu; import org.apache.commons.logging.Log; import org.json.JSONArray; @@ -37,9 +35,6 @@ import java.util.Map; import java.util.Set; import java.util.TreeSet; -import static net.osmand.plus.wikipedia.WikipediaPoiMenu.ENABLED_WIKI_POI_LANGUAGES_KEY; -import static net.osmand.plus.wikipedia.WikipediaPoiMenu.GLOBAL_WIKI_POI_ENABLED_KEY; - public class PoiFiltersHelper { private static final Log LOG = PlatformUtil.getLog(PoiFiltersHelper.class); @@ -116,31 +111,6 @@ public class PoiFiltersHelper { return customPOIFilter; } - public void prepareTopWikiFilter(@NonNull PoiUIFilter wiki) { - boolean prepareByDefault = true; - Bundle wikiSettings = WikipediaPoiMenu.getWikiPoiSettings(application); - if (wikiSettings != null) { - boolean allLanguages = wikiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY); - List languages = wikiSettings - .getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY); - if (!allLanguages && languages != null) { - prepareByDefault = false; - String wikiLang = "wiki:lang:"; - StringBuilder sb = new StringBuilder(); - for (String lang : languages) { - if (sb.length() > 1) { - sb.append(" "); - } - sb.append(wikiLang).append(lang); - } - wiki.setFilterByName(sb.toString()); - } - } - if (prepareByDefault) { - wiki.setFilterByName(null); - } - } - public PoiUIFilter getTopWikiPoiFilter() { if (topWikiPoiFilter == null) { String wikiFilterId = PoiUIFilter.STD_PREFIX + "osmwiki"; @@ -270,6 +240,7 @@ public class PoiFiltersHelper { PoiUIFilter f = new PoiUIFilter(t, application, ""); top.add(f); } + OsmandPlugin.registerCustomPoiFilters(top); this.cacheTopStandardFilters = top; } List result = new ArrayList<>(); @@ -485,11 +456,9 @@ public class PoiFiltersHelper { } public void addSelectedPoiFilter(PoiUIFilter filter) { - if (filter.isTopWikiFilter()) { - prepareTopWikiFilter(filter); - } Set selectedPoiFilters = new TreeSet<>(this.selectedPoiFilters); selectedPoiFilters.add(filter); + OsmandPlugin.onPrepareExtraTopPoiFilters(selectedPoiFilters); saveSelectedPoiFilters(selectedPoiFilters); this.selectedPoiFilters = selectedPoiFilters; } @@ -579,12 +548,10 @@ public class PoiFiltersHelper { for (String f : application.getSettings().getSelectedPoiFilters()) { PoiUIFilter filter = getFilterById(f); if (filter != null) { - if (filter.isTopWikiFilter()) { - prepareTopWikiFilter(filter); - } selectedPoiFilters.add(filter); } } + OsmandPlugin.onPrepareExtraTopPoiFilters(selectedPoiFilters); this.selectedPoiFilters = selectedPoiFilters; } diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 344b879444..f8dbd7745d 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -74,11 +74,13 @@ import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.Version; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity.ShowQuickSearchMode; +import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.helpers.SearchHistoryHelper; import net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry; import net.osmand.plus.poi.PoiUIFilter; @@ -117,7 +119,8 @@ import static net.osmand.search.core.ObjectType.POI_TYPE; import static net.osmand.search.core.ObjectType.SEARCH_STARTED; import static net.osmand.search.core.SearchCoreFactory.SEARCH_AMENITY_TYPE_PRIORITY; -public class QuickSearchDialogFragment extends DialogFragment implements OsmAndCompassListener, OsmAndLocationListener { +public class QuickSearchDialogFragment extends DialogFragment implements OsmAndCompassListener, OsmAndLocationListener, + DownloadIndexesThread.DownloadEvents { private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(QuickSearchDialogFragment.class); @@ -1173,7 +1176,7 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } if (getResultCollection() != null) { updateSearchResult(getResultCollection(), false); - addMoreButton(searchUICore.isSearchMoreAvailable(searchUICore.getPhrase())); + onSearchFinished(searchUICore.getPhrase()); } break; } @@ -1707,7 +1710,8 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC searching = false; if (resultListener == null || resultListener.searchFinished(object.requiredSearchPhrase)) { hideProgressBar(); - addMoreButton(searchUICore.isSearchMoreAvailable(object.requiredSearchPhrase)); + SearchPhrase phrase = object.requiredSearchPhrase; + onSearchFinished(phrase); } } }); @@ -1975,21 +1979,23 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC } } + private void onSearchFinished(SearchPhrase phrase) { + if (!OsmandPlugin.onSearchFinished(this, phrase, isResultEmpty())) { + addMoreButton(searchUICore.isSearchMoreAvailable(phrase)); + } + } + private void addMoreButton(boolean searchMoreAvailable) { if (!paused && !cancelPrev && mainSearchFragment != null && !isTextEmpty()) { QuickSearchMoreListItem moreListItem = new QuickSearchMoreListItem(app, null, new SearchMoreItemOnClickListener() { @Override - public void increaseRadiusOnClick() { - if (!interruptedSearch) { - SearchSettings settings = searchUICore.getSearchSettings(); - searchUICore.updateSettings(settings.setRadiusLevel(settings.getRadiusLevel() + 1)); - } - runCoreSearch(searchQuery, false, true); + public void onPrimaryButtonClick() { + increaseSearchRadius(); } @Override - public void onlineSearchOnClick() { + public void onSecondaryButtonClick() { final OsmandSettings settings = app.getSettings(); if (!settings.isInternetConnectionAvailable()) { Toast.makeText(app, R.string.internet_not_available, Toast.LENGTH_LONG).show(); @@ -2003,13 +2009,27 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC }); moreListItem.setInterruptedSearch(interruptedSearch); moreListItem.setEmptySearch(isResultEmpty()); - moreListItem.setOnlineSearch(isOnlineSearch()); moreListItem.setSearchMoreAvailable(searchMoreAvailable); + moreListItem.setSecondaryButtonVisible(isOnlineSearch()); mainSearchFragment.addListItem(moreListItem); updateSendEmptySearchBottomBar(isResultEmpty() && !interruptedSearch); } } + public void increaseSearchRadius() { + if (!interruptedSearch) { + SearchSettings settings = searchUICore.getSearchSettings(); + searchUICore.updateSettings(settings.setRadiusLevel(settings.getRadiusLevel() + 1)); + } + runCoreSearch(searchQuery, false, true); + } + + public void addSearchListItem(QuickSearchListItem item) { + if (mainSearchFragment != null) { + mainSearchFragment.addListItem(item); + } + } + private void updateSearchResult(SearchResultCollection res, boolean append) { if (!paused && mainSearchFragment != null) { @@ -2305,6 +2325,29 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC updateFabHeight(); } + @Override + public void newDownloadIndexes() { + if (!searching) { + hideProgressBar(); + } + OsmandPlugin.onNewDownloadIndexes(this); + } + + @Override + public void downloadInProgress() { + } + + @Override + public void downloadHasFinished() { + } + + public void reloadIndexFiles() { + if (app.getSettings().isInternetConnectionAvailable()) { + app.getDownloadThread().runReloadIndexFiles(); + showProgressBar(); + } + } + public interface SearchResultListener { void searchStarted(SearchPhrase phrase); void publish(SearchResultCollection res, boolean append); diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java index 1538441673..2650725c11 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchListAdapter.java @@ -1,6 +1,5 @@ package net.osmand.plus.search; -import android.app.Activity; import android.content.Context; import android.graphics.drawable.Drawable; import android.text.SpannableString; @@ -11,13 +10,16 @@ import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.ListView; import android.widget.TextView; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.view.ContextThemeWrapper; import androidx.core.content.ContextCompat; import androidx.core.view.ViewCompat; +import androidx.fragment.app.FragmentActivity; import net.osmand.AndroidUtils; import net.osmand.CollatorStringMatcher; @@ -29,6 +31,9 @@ import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities.UpdateLocationViewCache; +import net.osmand.plus.chooseplan.ChoosePlanDialogFragment; +import net.osmand.plus.search.listitems.QuickSearchBannerListItem; +import net.osmand.plus.search.listitems.QuickSearchFreeBannerListItem; import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.plus.search.listitems.QuickSearchHeaderListItem; import net.osmand.plus.search.listitems.QuickSearchListItem; @@ -45,12 +50,14 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; +import static net.osmand.plus.search.listitems.QuickSearchBannerListItem.INVALID_ID; import static net.osmand.search.core.ObjectType.POI_TYPE; +import static net.osmand.plus.search.listitems.QuickSearchBannerListItem.ButtonItem; public class QuickSearchListAdapter extends ArrayAdapter { private OsmandApplication app; - private Activity activity; + private FragmentActivity activity; private AccessibilityAssistant accessibilityAssistant; private LayoutInflater inflater; @@ -74,7 +81,7 @@ public class QuickSearchListAdapter extends ArrayAdapter { void reloadData(); } - public QuickSearchListAdapter(OsmandApplication app, Activity activity) { + public QuickSearchListAdapter(OsmandApplication app, FragmentActivity activity) { super(app, R.layout.search_list_item); this.app = app; this.activity = activity; @@ -203,7 +210,67 @@ public class QuickSearchListAdapter extends ArrayAdapter { final QuickSearchListItem listItem = getItem(position); QuickSearchListItemType type = listItem.getType(); LinearLayout view; - if (type == QuickSearchListItemType.SEARCH_MORE) { + if (type == QuickSearchListItemType.BANNER) { + final QuickSearchBannerListItem banner = (QuickSearchBannerListItem) listItem; + if (convertView == null) { + view = (LinearLayout) inflater.inflate(R.layout.search_banner_list_item, null); + } else { + view = (LinearLayout) convertView; + } + + ((TextView) view.findViewById(R.id.empty_search_description)).setText(R.string.nothing_found_descr); + + SearchUICore searchUICore = app.getSearchUICore().getCore(); + SearchPhrase searchPhrase = searchUICore.getPhrase(); + + String textTitle; + int minimalSearchRadius = searchUICore.getMinimalSearchRadius(searchPhrase); + if (searchUICore.isSearchMoreAvailable(searchPhrase) && minimalSearchRadius != Integer.MAX_VALUE) { + double rd = OsmAndFormatter.calculateRoundedDist(minimalSearchRadius, app); + textTitle = app.getString(R.string.nothing_found_in_radius) + " " + + OsmAndFormatter.getFormattedDistance((float) rd, app, false); + } else { + textTitle = app.getString(R.string.search_nothing_found); + } + ((TextView) view.findViewById(R.id.empty_search_title)).setText(textTitle); + + ViewGroup buttonContainer = view.findViewById(R.id.buttons_container); + if (buttonContainer != null) { + buttonContainer.removeAllViews(); + for (ButtonItem button : banner.getButtonItems()) { + View v = inflater.inflate(R.layout.search_banner_button_list_item, null); + TextView title = v.findViewById(R.id.title); + title.setText(button.getTitle()); + ImageView icon = v.findViewById(R.id.icon); + if (button.getIconId() != INVALID_ID) { + icon.setImageResource(button.getIconId()); + icon.setVisibility(View.VISIBLE); + } else { + icon.setVisibility(View.GONE); + } + v.setOnClickListener(button.getListener()); + buttonContainer.addView(v); + } + } + } else if (type == QuickSearchListItemType.FREE_VERSION_BANNER) { + if (convertView == null) { + view = (LinearLayout) inflater.inflate( + R.layout.read_wikipedia_ofline_banner, null); + } else { + view = (LinearLayout) convertView; + } + + View btnGet = view.findViewById(R.id.btn_get); + if (btnGet != null) { + btnGet.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + ChoosePlanDialogFragment.showWikipediaInstance( + activity.getSupportFragmentManager()); + } + }); + } + } else if (type == QuickSearchListItemType.SEARCH_MORE) { if (convertView == null) { view = (LinearLayout) inflater.inflate(R.layout.search_more_list_item, null); } else { @@ -237,36 +304,27 @@ public class QuickSearchListAdapter extends ArrayAdapter { textTitle = app.getString(R.string.search_nothing_found); } ((TextView) view.findViewById(R.id.empty_search_title)).setText(textTitle); - View increaseRadiusRow = view.findViewById(R.id.increase_radius_row); + View primaryButton = view.findViewById(R.id.primary_button); - SearchWord word = searchPhrase.getLastSelectedWord(); - if (word != null && word.getType() != null && word.getType().equals(POI_TYPE)) { - float rd = (float) OsmAndFormatter.calculateRoundedDist(searchUICore.getNextSearchRadius(searchPhrase), app); - String textIncreaseRadiusTo = app.getString(R.string.increase_search_radius_to, - OsmAndFormatter.getFormattedDistance(rd, app, false)); - ((TextView) view.findViewById(R.id.title)).setText(textIncreaseRadiusTo); - } else { - ((TextView) view.findViewById(R.id.title)).setText(app.getString(R.string.increase_search_radius)); - } + ((TextView) view.findViewById(R.id.title)).setText(getIncreaseSearchButtonTitle(app, searchPhrase)); - increaseRadiusRow.setVisibility(searchMoreItem.isSearchMoreAvailable() ? View.VISIBLE : View.GONE); - increaseRadiusRow.setOnClickListener(new View.OnClickListener() { + primaryButton.setVisibility(searchMoreItem.isSearchMoreAvailable() ? View.VISIBLE : View.GONE); + primaryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - ((QuickSearchMoreListItem) listItem).increaseRadiusOnClick(); + ((QuickSearchMoreListItem) listItem).onPrimaryButtonClick(); } }); - if (!searchMoreItem.isOnlineSearch()) { - View onlineSearchRow = view.findViewById(R.id.online_search_row); - onlineSearchRow.setVisibility(View.VISIBLE); - onlineSearchRow.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - searchMoreItem.onlineSearchOnClick(); - } - }); - } + View secondaryButton = view.findViewById(R.id.secondary_button); + secondaryButton.setVisibility(searchMoreItem.isSecondaryButtonVisible() ? + View.VISIBLE : View.GONE); + secondaryButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + searchMoreItem.onSecondaryButtonClick(); + } + }); } else if (type == QuickSearchListItemType.BUTTON) { if (convertView == null) { view = (LinearLayout) inflater.inflate(R.layout.search_custom_list_item, null); @@ -451,6 +509,19 @@ public class QuickSearchListAdapter extends ArrayAdapter { return view; } + public static String getIncreaseSearchButtonTitle(OsmandApplication app, SearchPhrase searchPhrase) { + SearchWord word = searchPhrase.getLastSelectedWord(); + SearchUICore searchUICore = app.getSearchUICore().getCore(); + if (word != null && word.getType() != null && word.getType().equals(POI_TYPE)) { + float rd = (float) OsmAndFormatter.calculateRoundedDist( + searchUICore.getNextSearchRadius(searchPhrase), app); + return app.getString(R.string.increase_search_radius_to, + OsmAndFormatter.getFormattedDistance(rd, app, false)); + } else { + return app.getString(R.string.increase_search_radius); + } + } + public void toggleCheckbox(int position, CheckBox ch) { QuickSearchListItemType type = getItem(position).getType(); if (type == QuickSearchListItemType.SELECT_ALL) { diff --git a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchBannerListItem.java b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchBannerListItem.java new file mode 100644 index 0000000000..33a519a30a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchBannerListItem.java @@ -0,0 +1,64 @@ +package net.osmand.plus.search.listitems; + +import android.view.View; + +import net.osmand.plus.OsmandApplication; + +import java.util.ArrayList; +import java.util.List; + +public class QuickSearchBannerListItem extends QuickSearchListItem { + + public static final int INVALID_ID = -1; + + private List buttons; + + public QuickSearchBannerListItem(OsmandApplication app) { + super(app, null); + buttons = new ArrayList<>(); + } + + public QuickSearchListItemType getType() { + return QuickSearchListItemType.BANNER; + } + + public void addButton(String title, String description, int iconId, + View.OnClickListener listener) { + buttons.add(new ButtonItem(title, description, iconId, listener)); + } + + public List getButtonItems() { + return buttons; + } + + public static class ButtonItem { + private String title; + private String description; + private int iconId; + private View.OnClickListener listener; + + public ButtonItem(String title, String description, int iconId, + View.OnClickListener listener) { + this.title = title; + this.description = description; + this.iconId = iconId; + this.listener = listener; + } + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public int getIconId() { + return iconId; + } + + public View.OnClickListener getListener() { + return listener; + } + } +} diff --git a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchFreeBannerListItem.java b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchFreeBannerListItem.java new file mode 100644 index 0000000000..7c417e543c --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchFreeBannerListItem.java @@ -0,0 +1,14 @@ +package net.osmand.plus.search.listitems; + +import net.osmand.plus.OsmandApplication; + +public class QuickSearchFreeBannerListItem extends QuickSearchListItem { + + public QuickSearchFreeBannerListItem(OsmandApplication app) { + super(app, null); + } + + public QuickSearchListItemType getType() { + return QuickSearchListItemType.FREE_VERSION_BANNER; + } +} diff --git a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItemType.java b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItemType.java index 6a9d390385..3c6dcf4527 100644 --- a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItemType.java +++ b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchListItemType.java @@ -5,6 +5,8 @@ public enum QuickSearchListItemType { HEADER, BUTTON, SEARCH_MORE, + BANNER, + FREE_VERSION_BANNER, SELECT_ALL, TOP_SHADOW, BOTTOM_SHADOW diff --git a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchMoreListItem.java b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchMoreListItem.java index ebb5d36daa..9109320184 100644 --- a/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchMoreListItem.java +++ b/OsmAnd/src/net/osmand/plus/search/listitems/QuickSearchMoreListItem.java @@ -10,14 +10,15 @@ public class QuickSearchMoreListItem extends QuickSearchListItem { private String name; private SearchMoreItemOnClickListener onClickListener; private boolean emptySearch; - private boolean onlineSearch; private boolean searchMoreAvailable; private boolean interruptedSearch; private String findMore; private String restartSearch; private String increaseRadius; + private boolean secondaryButtonVisibility; - public QuickSearchMoreListItem(OsmandApplication app, String name, @Nullable SearchMoreItemOnClickListener onClickListener) { + public QuickSearchMoreListItem(OsmandApplication app, String name, + @Nullable SearchMoreItemOnClickListener onClickListener) { super(app, null); this.name = name; this.onClickListener = onClickListener; @@ -61,14 +62,6 @@ public class QuickSearchMoreListItem extends QuickSearchListItem { this.emptySearch = emptySearch; } - public boolean isOnlineSearch() { - return onlineSearch; - } - - public void setOnlineSearch(boolean onlineSearch) { - this.onlineSearch = onlineSearch; - } - public boolean isSearchMoreAvailable() { return searchMoreAvailable; } @@ -77,22 +70,31 @@ public class QuickSearchMoreListItem extends QuickSearchListItem { this.searchMoreAvailable = searchMoreAvailable; } - public void increaseRadiusOnClick() { + public void onPrimaryButtonClick() { if (onClickListener != null) { - onClickListener.increaseRadiusOnClick(); + onClickListener.onPrimaryButtonClick(); } } - public void onlineSearchOnClick() { + public void onSecondaryButtonClick() { if (onClickListener != null) { - onClickListener.onlineSearchOnClick(); + onClickListener.onSecondaryButtonClick(); } } + public void setSecondaryButtonVisible(boolean secondaryButtonVisibility) { + this.secondaryButtonVisibility = secondaryButtonVisibility; + } + + public boolean isSecondaryButtonVisible() { + return secondaryButtonVisibility; + } + public interface SearchMoreItemOnClickListener { - void increaseRadiusOnClick(); + void onPrimaryButtonClick(); + + void onSecondaryButtonClick(); - void onlineSearchOnClick(); } } diff --git a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java index b2dc12e0e0..18b23fd8e8 100644 --- a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java @@ -29,6 +29,7 @@ import net.osmand.data.QuadTree; import net.osmand.data.RotatedTileBox; import net.osmand.osm.PoiType; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.PointImageDrawable; @@ -38,7 +39,6 @@ import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.routing.IRouteInformationListener; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.views.MapTextLayer.MapTextProvider; -import net.osmand.plus.wikipedia.WikipediaPoiMenu; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -331,20 +331,19 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon @Override public PointDescription getObjectName(Object o) { if (o instanceof Amenity) { - Amenity a = (Amenity) o; + Amenity amenity = (Amenity) o; String preferredLang = app.getSettings().MAP_PREFERRED_LOCALE.get(); boolean transliterateNames = app.getSettings().MAP_TRANSLITERATE_NAMES.get(); - if (a.getType().isWiki()) { + if (amenity.getType().isWiki()) { if (Algorithms.isEmpty(preferredLang)) { preferredLang = app.getLanguage(); } - preferredLang = WikipediaPoiMenu.getWikiArticleLanguage(app, - a.getSupportedContentLocales(), preferredLang); + preferredLang = OsmandPlugin.onGetMapObjectsLocale(amenity, preferredLang); } return new PointDescription(PointDescription.POINT_TYPE_POI, - a.getName(preferredLang, transliterateNames)); + amenity.getName(preferredLang, transliterateNames)); } return null; } diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java b/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java index f69b6a9397..873feba53c 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/SelectWikiLanguagesBottomSheet.java @@ -11,12 +11,10 @@ import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import androidx.core.os.ConfigurationCompat; import androidx.core.os.LocaleListCompat; -import androidx.fragment.app.FragmentActivity; import net.osmand.AndroidUtils; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; @@ -26,6 +24,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerSpaceItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.LongDescriptionItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; +import net.osmand.plus.settings.backend.ApplicationMode; import java.util.ArrayList; import java.util.Collections; @@ -34,16 +33,13 @@ import java.util.List; import java.util.Locale; import java.util.Set; -import static net.osmand.plus.wikipedia.WikipediaPoiMenu.ENABLED_WIKI_POI_LANGUAGES_KEY; -import static net.osmand.plus.wikipedia.WikipediaPoiMenu.GLOBAL_WIKI_POI_ENABLED_KEY; - public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragment { public static final String TAG = SelectWikiLanguagesBottomSheet.class.getSimpleName(); private OsmandApplication app; private ApplicationMode appMode; - private OsmandSettings settings; + private WikipediaPlugin wikiPlugin; private List languageItems; @@ -54,8 +50,8 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); app = requiredMyApplication(); - settings = app.getSettings(); - appMode = settings.getApplicationMode(); + appMode = app.getSettings().getApplicationMode(); + wikiPlugin = OsmandPlugin.getPlugin(WikipediaPlugin.class); initLanguagesData(); } @@ -67,7 +63,8 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen @Override public void createMenuItems(Bundle savedInstanceState) { - final int activeColorResId = nightMode ? R.color.active_color_primary_dark : R.color.active_color_primary_light; + final int activeColorResId = nightMode ? + R.color.active_color_primary_dark : R.color.active_color_primary_light; final int profileColorResId = appMode.getIconColorInfo().getColor(nightMode); final int contentPadding = app.getResources().getDimensionPixelSize(R.dimen.content_padding); @@ -138,23 +135,21 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen preferredLocales.add(app.getLanguage()); preferredLocales.add(Locale.getDefault().getLanguage()); - Bundle wikiPoiSettings = WikipediaPoiMenu.getWikiPoiSettings(app); - List enabledWikiPoiLocales = null; - if (wikiPoiSettings != null) { - isGlobalWikiPoiEnabled = wikiPoiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY); - enabledWikiPoiLocales = wikiPoiSettings.getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY); - } - if (enabledWikiPoiLocales != null) { + isGlobalWikiPoiEnabled = wikiPlugin.isShowAllLanguages(); + if (wikiPlugin.hasLanguagesFilter()) { + List enabledWikiPoiLocales = wikiPlugin.getLanguagesToShow(); for (String locale : app.getPoiTypes().getAllAvailableWikiLocales()) { boolean checked = enabledWikiPoiLocales.contains(locale); boolean topDefined = preferredLocales.contains(locale) || checked; - languages.add(new WikiLanguageItem(locale, WikipediaPoiMenu.getTranslation(app, locale), checked, topDefined)); + languages.add(new WikiLanguageItem(locale, + wikiPlugin.getWikiLanguageTranslation(locale), checked, topDefined)); } } else { isGlobalWikiPoiEnabled = true; for (String locale : app.getPoiTypes().getAllAvailableWikiLocales()) { boolean topDefined = preferredLocales.contains(locale); - languages.add(new WikiLanguageItem(locale, WikipediaPoiMenu.getTranslation(app, locale), false, topDefined)); + languages.add(new WikiLanguageItem(locale, + wikiPlugin.getWikiLanguageTranslation(locale), false, topDefined)); } } @@ -162,8 +157,11 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen } private void setLanguageListEnable(boolean enable) { - int textColorPrimaryId = nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light; - int disableColorId = nightMode ? R.color.active_buttons_and_links_text_disabled_dark : R.color.active_buttons_and_links_text_disabled_light; + int textColorPrimaryId = nightMode ? + R.color.text_color_primary_dark : R.color.text_color_primary_light; + int disableColorId = nightMode ? + R.color.active_buttons_and_links_text_disabled_dark : + R.color.active_buttons_and_links_text_disabled_light; int profileColorId = appMode.getIconColorInfo().getColor(nightMode); for (BottomSheetItemWithCompoundButton item : languageItems) { item.getView().setEnabled(enable); @@ -171,7 +169,8 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen CompoundButton cb = item.getCompoundButton(); if (cb != null) { cb.setEnabled(enable); - UiUtilities.setupCompoundButton(nightMode, ContextCompat.getColor(app, enable ? profileColorId : disableColorId), cb); + UiUtilities.setupCompoundButton(nightMode, ContextCompat.getColor(app, enable ? + profileColorId : disableColorId), cb); } } } @@ -183,19 +182,15 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen @Override protected void onRightBottomButtonClick() { - super.onRightBottomButtonClick(); List localesForSaving = new ArrayList<>(); for (WikiLanguageItem language : languages) { if (language.isChecked()) { localesForSaving.add(language.getLocale()); } } - settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.setStringsListForProfile(appMode, localesForSaving); - settings.GLOBAL_WIKIPEDIA_POI_ENABLED.setModeValue(appMode, isGlobalWikiPoiEnabled); - MapActivity ma = getMapActivity(); - if (ma != null) { - WikipediaPoiMenu.updateWikipediaState(ma); - } + wikiPlugin.setLanguagesToShow(localesForSaving); + wikiPlugin.setShowAllLanguages(isGlobalWikiPoiEnabled); + wikiPlugin.updateWikipediaState(); dismiss(); } @@ -204,7 +199,8 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen if (app == null) { return null; } - View buttonView = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.bottom_sheet_item_title_with_swith_56dp, null); + View buttonView = UiUtilities.getInflater(getContext(), nightMode) + .inflate(R.layout.bottom_sheet_item_title_with_swith_56dp, null); CompoundButton cb = buttonView.findViewById(R.id.compound_button); int color = nightMode ? R.color.divider_color_dark : R.color.divider_color_light; @@ -228,16 +224,7 @@ public class SelectWikiLanguagesBottomSheet extends MenuBottomSheetDialogFragmen } } - @Nullable - private MapActivity getMapActivity() { - FragmentActivity activity = getActivity(); - if (activity instanceof MapActivity) { - return (MapActivity) activity; - } - return null; - } - - private class WikiLanguageItem implements Comparable { + private static class WikiLanguageItem implements Comparable { private String locale; private String title; private boolean checked; diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java index 763531e266..2e917881e4 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java @@ -282,8 +282,9 @@ public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment { WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment(); wikipediaDialogFragment.setAmenity(amenity); - lang = lang != null ? lang : WikipediaPoiMenu.getWikiArticleLanguage(app, - amenity.getSupportedContentLocales(), app.getSettings().MAP_PREFERRED_LOCALE.get()); + WikipediaPlugin wikipediaPlugin = OsmandPlugin.getPlugin(WikipediaPlugin.class); + lang = lang != null ? lang : wikipediaPlugin.getMapObjectsLocale(amenity, + app.getSettings().MAP_PREFERRED_LOCALE.get()); wikipediaDialogFragment.setLanguage(lang); wikipediaDialogFragment.setRetainInstance(true); wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG); diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java new file mode 100644 index 0000000000..6726036dde --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java @@ -0,0 +1,413 @@ +package net.osmand.plus.wikipedia; + +import android.content.Intent; +import android.view.View; +import android.widget.ArrayAdapter; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; + +import net.osmand.AndroidUtils; +import net.osmand.CallbackWithObject; +import net.osmand.data.Amenity; +import net.osmand.data.MapObject; +import net.osmand.osm.AbstractPoiType; +import net.osmand.plus.ContextMenuAdapter; +import net.osmand.plus.ContextMenuItem; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.dashboard.DashboardOnMap; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadIndexesThread; +import net.osmand.plus.download.DownloadResources; +import net.osmand.plus.poi.PoiFiltersHelper; +import net.osmand.plus.poi.PoiUIFilter; +import net.osmand.plus.search.QuickSearchDialogFragment; +import net.osmand.plus.search.QuickSearchListAdapter; +import net.osmand.plus.search.listitems.QuickSearchBannerListItem; +import net.osmand.plus.search.listitems.QuickSearchFreeBannerListItem; +import net.osmand.plus.settings.backend.OsmandSettings; +import net.osmand.plus.views.DownloadedRegionsLayer; +import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.search.core.ObjectType; +import net.osmand.search.core.SearchPhrase; +import net.osmand.util.Algorithms; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static net.osmand.aidlapi.OsmAndCustomizationConstants.WIKIPEDIA_ID; +import static net.osmand.osm.MapPoiTypes.WIKI_LANG; + +public class WikipediaPlugin extends OsmandPlugin { + + public static final String ID = "osmand.wikipedia"; + + private MapActivity mapActivity; + private OsmandSettings settings; + + private PoiUIFilter topWikiPoiFilter; + + public WikipediaPlugin(OsmandApplication app) { + super(app); + this.settings = app.getSettings(); + } + + @Override + public boolean isVisible() { + return false; + } + + @Override + public String getId() { + return ID; + } + + @Override + public int getLogoResourceId() { + return R.drawable.ic_plugin_wikipedia; + } + + @Override + public String getName() { + return app.getString(R.string.shared_string_wikipedia); + } + + @Override + public CharSequence getDescription() { + return app.getString(R.string.plugin_wikipedia_description); + } + + @Override + public void mapActivityResume(MapActivity activity) { + this.mapActivity = activity; + } + + @Override + public void mapActivityPause(MapActivity activity) { + this.mapActivity = null; + } + + @Override + protected void registerLayerContextMenuActions(OsmandMapTileView mapView, + ContextMenuAdapter adapter, + final MapActivity mapActivity) { + ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { + + @Override + public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int position) { + if (itemId == R.string.shared_string_wikipedia) { + mapActivity.getDashboard().setDashboardVisibility(true, + DashboardOnMap.DashboardType.WIKIPEDIA, + AndroidUtils.getCenterViewCoordinates(view)); + } + return false; + } + + @Override + public boolean onContextMenuClick(final ArrayAdapter adapter, int itemId, + final int pos, boolean isChecked, int[] viewCoordinates) { + if (itemId == R.string.shared_string_wikipedia) { + toggleWikipediaPoi(isChecked, new CallbackWithObject() { + @Override + public boolean processResult(Boolean selected) { + ContextMenuItem item = adapter.getItem(pos); + if (item != null) { + item.setSelected(selected); + item.setColorRes(selected ? + R.color.osmand_orange : ContextMenuItem.INVALID_ID); + item.setDescription(selected ? getLanguagesSummary() : null); + adapter.notifyDataSetChanged(); + } + return true; + } + }); + } + return false; + } + }; + + boolean selected = app.getPoiFilters().isTopWikiFilterSelected(); + adapter.addItem(new ContextMenuItem.ItemBuilder() + .setId(WIKIPEDIA_ID) + .setTitleId(R.string.shared_string_wikipedia, mapActivity) + .setDescription(selected ? getLanguagesSummary() : null) + .setSelected(selected) + .setColor(selected ? R.color.osmand_orange : ContextMenuItem.INVALID_ID) + .setIcon(R.drawable.ic_plugin_wikipedia) + .setSecondaryIcon(R.drawable.ic_action_additional_option) + .setListener(listener).createItem()); + } + + @Override + protected List getCustomPoiFilters() { + List poiFilters = new ArrayList<>(); + if (topWikiPoiFilter == null) { + AbstractPoiType poiType = app.getPoiTypes().getOsmwiki(); + topWikiPoiFilter = new PoiUIFilter(poiType, app, ""); + } + poiFilters.add(topWikiPoiFilter); + + return poiFilters; + } + + public void updateWikipediaState() { + if (isShowAllLanguages() || hasLanguagesFilter()) { + refreshWikiOnMap(); + } else { + toggleWikipediaPoi(false, null); + } + } + + public String getWikiLanguageTranslation(String locale) { + String translation = app.getLangTranslation(locale); + if (translation.equalsIgnoreCase(locale)) { + translation = getTranslationFromPhrases(locale); + } + return translation; + } + + private String getTranslationFromPhrases(String locale) { + String keyName = WIKI_LANG + "_" + locale; + try { + Field f = R.string.class.getField("poi_" + keyName); + Integer in = (Integer) f.get(null); + return app.getString(in); + } catch (Throwable e) { + return locale; + } + } + + public boolean hasCustomSettings() { + return !isShowAllLanguages() && getLanguagesToShow() != null; + } + + public boolean hasLanguagesFilter() { + return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.get() != null; + } + + public boolean isShowAllLanguages() { + return settings.GLOBAL_WIKIPEDIA_POI_ENABLED.get(); + } + + public void setShowAllLanguages(boolean showAllLanguages) { + settings.GLOBAL_WIKIPEDIA_POI_ENABLED.set(showAllLanguages); + } + + public List getLanguagesToShow() { + return settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.getStringsList(); + } + + public void setLanguagesToShow(List languagesToShow) { + settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.setStringsList(languagesToShow); + } + + public void toggleWikipediaPoi(boolean enable, CallbackWithObject callback) { + if (enable) { + showWikiOnMap(); + } else { + hideWikiFromMap(); + } + if (callback != null) { + callback.processResult(enable); + } else if (mapActivity != null) { + mapActivity.getDashboard().refreshContent(true); + } + if (mapActivity != null) { + mapActivity.refreshMap(); + } + } + + public void refreshWikiOnMap() { + if (mapActivity == null) { + return; + } + app.getPoiFilters().loadSelectedPoiFilters(); + mapActivity.getDashboard().refreshContent(true); + mapActivity.refreshMap(); + } + + private void showWikiOnMap() { + PoiFiltersHelper ph = app.getPoiFilters(); + PoiUIFilter wiki = ph.getTopWikiPoiFilter(); + ph.loadSelectedPoiFilters(); + ph.addSelectedPoiFilter(wiki); + } + + private void hideWikiFromMap() { + PoiFiltersHelper ph = app.getPoiFilters(); + PoiUIFilter wiki = ph.getTopWikiPoiFilter(); + ph.removePoiFilter(wiki); + ph.removeSelectedPoiFilter(wiki); + } + + public String getLanguagesSummary() { + if (hasCustomSettings()) { + List translations = new ArrayList<>(); + for (String locale : getLanguagesToShow()) { + translations.add(getWikiLanguageTranslation(locale)); + } + return android.text.TextUtils.join(", ", translations); + } + return app.getString(R.string.shared_string_all_languages); + } + + @Override + protected String getMapObjectsLocale(Amenity amenity, String preferredLocale) { + return getWikiArticleLanguage(amenity.getSupportedContentLocales(), preferredLocale); + } + + @Override + protected String getMapObjectPreferredLang(MapObject object, String defaultLanguage) { + if (object instanceof Amenity) { + Amenity amenity = (Amenity) object; + if (amenity.getType().isWiki()) { + return getWikiArticleLanguage(amenity.getSupportedContentLocales(), defaultLanguage); + } + } + return null; + } + + public String getWikiArticleLanguage(@NonNull Set availableArticleLangs, String preferredLanguage) { + if (!hasCustomSettings()) { + // Wikipedia with default settings + return preferredLanguage; + } + if (Algorithms.isEmpty(preferredLanguage)) { + preferredLanguage = app.getLanguage(); + } + List wikiLangs = getLanguagesToShow(); + if (!wikiLangs.contains(preferredLanguage)) { + // return first matched language from enabled Wikipedia languages + for (String language : wikiLangs) { + if (availableArticleLangs.contains(language)) { + return language; + } + } + } + return preferredLanguage; + } + + public void showDownloadWikiMapsScreen() { + if (mapActivity != null) { + OsmandMapTileView mv = mapActivity.getMapView(); + DownloadedRegionsLayer dl = mv.getLayerByClass(DownloadedRegionsLayer.class); + String filter = dl.getFilter(new StringBuilder()); + final Intent intent = new Intent(app, app.getAppCustomization().getDownloadIndexActivity()); + intent.putExtra(DownloadActivity.FILTER_KEY, filter); + intent.putExtra(DownloadActivity.FILTER_CAT, DownloadActivityType.WIKIPEDIA_FILE.getTag()); + intent.putExtra(DownloadActivity.TAB_TO_OPEN, DownloadActivity.DOWNLOAD_TAB); + mapActivity.startActivity(intent); + } + } + + public boolean hasMapsToDownload() { + try { + if (mapActivity == null) { + return false; + } + int mapsToDownloadCount = DownloadResources.findIndexItemsAt( + app, mapActivity.getMapLocation(), DownloadActivityType.WIKIPEDIA_FILE, + false, 1).size(); + return mapsToDownloadCount > 0; + } catch (IOException e) { + return false; + } + } + + @Override + protected boolean searchFinished(final QuickSearchDialogFragment searchFragment, SearchPhrase phrase, boolean isResultEmpty) { + if (isResultEmpty && isSearchByWiki(phrase)) { + if (!Version.isPaidVersion(app)) { + searchFragment.addSearchListItem(new QuickSearchFreeBannerListItem(app)); + } else { + final DownloadIndexesThread downloadThread = app.getDownloadThread(); + if (!downloadThread.getIndexes().isDownloadedFromInternet) { + searchFragment.reloadIndexFiles(); + } else { + addEmptyWikiBanner(searchFragment, phrase); + } + } + return true; + } + return false; + } + + @Override + protected void newDownloadIndexes(Fragment fragment) { + if (fragment instanceof QuickSearchDialogFragment) { + final QuickSearchDialogFragment f = (QuickSearchDialogFragment) fragment; + SearchPhrase phrase = app.getSearchUICore().getCore().getPhrase(); + if (f.isResultEmpty() && isSearchByWiki(phrase)) { + addEmptyWikiBanner(f, phrase); + } + } + } + + private void addEmptyWikiBanner(final QuickSearchDialogFragment fragment, SearchPhrase phrase) { + QuickSearchBannerListItem banner = new QuickSearchBannerListItem(app); + banner.addButton(QuickSearchListAdapter.getIncreaseSearchButtonTitle(app, phrase), + null, QuickSearchBannerListItem.INVALID_ID, new View.OnClickListener() { + @Override + public void onClick(View v) { + fragment.increaseSearchRadius(); + } + }); + if (hasMapsToDownload()) { + banner.addButton(app.getString(R.string.search_download_wikipedia_maps), + null, R.drawable.ic_world_globe_dark, new View.OnClickListener() { + @Override + public void onClick(View v) { + showDownloadWikiMapsScreen(); + } + }); + } + fragment.addSearchListItem(banner); + } + + @Override + protected void prepareExtraTopPoiFilters(Set poiUIFilters) { + for (PoiUIFilter filter : poiUIFilters) { + if (filter.isTopWikiFilter()) { + boolean prepareByDefault = true; + if (hasCustomSettings()) { + prepareByDefault = false; + String wikiLang = "wiki:lang:"; + StringBuilder sb = new StringBuilder(); + for (String lang : getLanguagesToShow()) { + if (sb.length() > 1) { + sb.append(" "); + } + sb.append(wikiLang).append(lang); + } + filter.setFilterByName(sb.toString()); + } + if (prepareByDefault) { + filter.setFilterByName(null); + } + return; + } + } + } + + private boolean isSearchByWiki(SearchPhrase phrase) { + if (phrase.isLastWord(ObjectType.POI_TYPE)) { + Object obj = phrase.getLastSelectedWord().getResult().object; + if (obj instanceof PoiUIFilter) { + PoiUIFilter pf = (PoiUIFilter) obj; + return pf.isWikiFilter(); + } else if (obj instanceof AbstractPoiType) { + AbstractPoiType pt = (AbstractPoiType) obj; + return pt.getKeyName().startsWith(WIKI_LANG); + } + } + return false; + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java index 28c1245030..391af7b7c0 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPoiMenu.java @@ -1,15 +1,11 @@ package net.osmand.plus.wikipedia; -import android.os.Bundle; import android.widget.ArrayAdapter; -import androidx.annotation.NonNull; - -import net.osmand.CallbackWithObject; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuItem; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -18,32 +14,23 @@ import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; -import net.osmand.plus.poi.PoiFiltersHelper; -import net.osmand.plus.poi.PoiUIFilter; -import net.osmand.util.Algorithms; import java.io.IOException; -import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.List; -import java.util.Set; - -import static net.osmand.osm.MapPoiTypes.WIKI_LANG; public class WikipediaPoiMenu { - public static final String GLOBAL_WIKI_POI_ENABLED_KEY = "global_wikipedia_poi_enabled_key"; - public static final String ENABLED_WIKI_POI_LANGUAGES_KEY = "enabled_wikipedia_poi_languages_key"; - private MapActivity mapActivity; private OsmandApplication app; private OsmandSettings settings; + private WikipediaPlugin wikiPlugin; private boolean nightMode; public WikipediaPoiMenu(MapActivity mapActivity) { this.mapActivity = mapActivity; this.app = mapActivity.getMyApplication(); this.settings = app.getSettings(); + this.wikiPlugin = OsmandPlugin.getPlugin(WikipediaPlugin.class); this.nightMode = app.getDaynightHelper().isNightModeForMapControls(); } @@ -65,7 +52,7 @@ public class WikipediaPoiMenu { app.runInUIThread(new Runnable() { @Override public void run() { - toggleWikipediaPoi(mapActivity, !enabled, null); + wikiPlugin.toggleWikipediaPoi(!enabled, null); } }); } else if (itemId == languageActionStringId) { @@ -96,7 +83,7 @@ public class WikipediaPoiMenu { .setLayout(R.layout.list_item_divider) .createItem()); - summary = getLanguagesSummary(app); + summary = wikiPlugin.getLanguagesSummary(); adapter.addItem(new ContextMenuItem.ItemBuilder() .setTitleId(languageActionStringId, mapActivity) .setIcon(R.drawable.ic_action_map_language) @@ -220,141 +207,8 @@ public class WikipediaPoiMenu { return adapter; } - public static void updateWikipediaState(MapActivity ma) { - final OsmandApplication app = ma.getMyApplication(); - ApplicationMode appMode = app.getSettings().getApplicationMode(); - Bundle wikiPoiSetting = getWikiPoiSettingsForProfile(app, appMode); - if (wikiPoiSetting != null) { - refreshWikipediaOnMap(ma); - } else { - toggleWikipediaPoi(ma, false, null); - } - } - - public static String getTranslation(OsmandApplication app, String locale) { - String translation = app.getLangTranslation(locale); - if (translation.equalsIgnoreCase(locale)) { - translation = getTranslationFromPhrases(app, locale); - } - return translation; - } - - private static String getTranslationFromPhrases(OsmandApplication app, String locale) { - String keyName = WIKI_LANG + "_" + locale; - try { - Field f = R.string.class.getField("poi_" + keyName); - Integer in = (Integer) f.get(null); - return app.getString(in); - } catch (Throwable e) { - return locale; - } - } - - public static Bundle getWikiPoiSettings(OsmandApplication app) { - Bundle wikiSettings = getWikiPoiSettingsForProfile(app, app.getSettings().getApplicationMode()); - if (wikiSettings == null) { - wikiSettings = getWikiPoiSettingsForProfile(app, app.getSettings().DEFAULT_APPLICATION_MODE.get()); - } - return wikiSettings; - } - - private static Bundle getWikiPoiSettingsForProfile(OsmandApplication app, ApplicationMode appMode) { - OsmandSettings settings = app.getSettings(); - boolean globalWikiPoiEnabled = settings.GLOBAL_WIKIPEDIA_POI_ENABLED.getModeValue(appMode); - List enabledWikiPoiLanguages = settings.WIKIPEDIA_POI_ENABLED_LANGUAGES.getStringsListForProfile(appMode); - if (!globalWikiPoiEnabled && Algorithms.isEmpty(enabledWikiPoiLanguages)) { - return null; - } - Bundle bundle = new Bundle(); - bundle.putBoolean(GLOBAL_WIKI_POI_ENABLED_KEY, globalWikiPoiEnabled); - if (enabledWikiPoiLanguages != null) { - bundle.putStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY, - new ArrayList<>(enabledWikiPoiLanguages)); - } - return bundle; - } - - public static void toggleWikipediaPoi(final MapActivity mapActivity, boolean enable, - CallbackWithObject callback) { - OsmandApplication app = mapActivity.getMyApplication(); - if (enable) { - showWikipediaOnMap(app); - } else { - hideWikipediaFromMap(app); - } - if (callback != null) { - callback.processResult(enable); - } else { - mapActivity.getDashboard().refreshContent(true); - } - mapActivity.refreshMap(); - } - - public static void refreshWikipediaOnMap(MapActivity mapActivity) { - OsmandApplication app = mapActivity.getMyApplication(); - app.getPoiFilters().loadSelectedPoiFilters(); - mapActivity.getDashboard().refreshContent(true); - mapActivity.refreshMap(); - } - - private static void showWikipediaOnMap(OsmandApplication app) { - PoiFiltersHelper ph = app.getPoiFilters(); - PoiUIFilter wiki = ph.getTopWikiPoiFilter(); - ph.loadSelectedPoiFilters(); - ph.addSelectedPoiFilter(wiki); - } - - private static void hideWikipediaFromMap(OsmandApplication app) { - PoiFiltersHelper ph = app.getPoiFilters(); - PoiUIFilter wiki = ph.getTopWikiPoiFilter(); - ph.removePoiFilter(wiki); - ph.removeSelectedPoiFilter(wiki); - } - - public static String getLanguagesSummary(OsmandApplication app) { - Bundle wikiSetting = getWikiPoiSettings(app); - if (wikiSetting != null) { - boolean globalWikiEnabled = wikiSetting.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY); - List enabledLocales = wikiSetting.getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY); - if (!globalWikiEnabled && enabledLocales != null) { - List translations = new ArrayList<>(); - for (String locale : enabledLocales) { - translations.add(getTranslation(app, locale)); - } - return android.text.TextUtils.join(", ", translations); - } - } - return app.getString(R.string.shared_string_all_languages); - } - public static ContextMenuAdapter createListAdapter(final MapActivity mapActivity) { return new WikipediaPoiMenu(mapActivity).createLayersItems(); } - public static String getWikiArticleLanguage(@NonNull OsmandApplication app, - @NonNull Set availableArticleLangs, - String preferredLanguage) { - Bundle wikiPoiSettings = getWikiPoiSettings(app); - if (!app.getPoiFilters().isTopWikiFilterSelected() || wikiPoiSettings == null) { - // Wikipedia POI setting disabled - return preferredLanguage; - } - if (wikiPoiSettings.getBoolean(GLOBAL_WIKI_POI_ENABLED_KEY)) { - // global Wikipedia POI filter enabled - return preferredLanguage; - } - if (Algorithms.isEmpty(preferredLanguage)) { - preferredLanguage = app.getLanguage(); - } - List wikiLangs = wikiPoiSettings.getStringArrayList(ENABLED_WIKI_POI_LANGUAGES_KEY); - if (wikiLangs != null && !wikiLangs.contains(preferredLanguage)) { - // return first matched language from enabled Wikipedia languages - for (String language : wikiLangs) { - if (availableArticleLangs.contains(language)) { - return language; - } - } - } - return preferredLanguage; - } }