From b30f65141539f2552433e59627bd65beb0464a1c Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 3 Feb 2021 15:41:31 +0200 Subject: [PATCH] Move wiki image cards to plugin --- OsmAnd/src/net/osmand/plus/OsmandPlugin.java | 12 ++++--- .../builders/cards/ImageCard.java | 22 +++---------- .../OpenPlaceReviewsPlugin.java | 7 +++- .../plus/wikipedia/WikipediaPlugin.java | 33 ++++++++++++++++++- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index cbfcf38793..3783c501ad 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -69,6 +69,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; public abstract class OsmandPlugin { @@ -211,7 +212,9 @@ public abstract class OsmandPlugin { return Collections.emptyList(); } - protected List getImageCards(@Nullable GetImageCardsListener listener) { + protected List getImageCards(@NonNull Map params, + @Nullable Map additionalParams, + @Nullable GetImageCardsListener listener) { return Collections.emptyList(); } @@ -273,9 +276,9 @@ public abstract class OsmandPlugin { allPlugins.clear(); - allPlugins.add(new MapillaryPlugin(app)); enableHiddenPlugin(app, enabledPlugins, new WikipediaPlugin(app)); + allPlugins.add(new MapillaryPlugin(app)); allPlugins.add(new OsmandRasterMapsPlugin(app)); allPlugins.add(new OsmandMonitoringPlugin(app)); checkMarketPlugin(app, enabledPlugins, new SRTMPlugin(app)); @@ -875,9 +878,10 @@ public abstract class OsmandPlugin { return collection; } - public static void populateImageCards(@NonNull List imageCards, @Nullable GetImageCardsListener listener) { + public static void populateImageCards(@NonNull List imageCards, @NonNull Map params, + @Nullable Map additionalParams, @Nullable GetImageCardsListener listener) { for (OsmandPlugin p : getEnabledPlugins()) { - imageCards.addAll(p.getImageCards(listener)); + imageCards.addAll(p.getImageCards(params, additionalParams, listener)); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java index 1c2339648e..e25c043b62 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -19,7 +19,6 @@ import net.osmand.AndroidNetworkUtils; import net.osmand.AndroidUtils; import net.osmand.Location; import net.osmand.PlatformUtil; -import net.osmand.data.Amenity; import net.osmand.data.LatLon; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; @@ -29,7 +28,6 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapillary.MapillaryContributeCard; import net.osmand.plus.mapillary.MapillaryImageCard; -import net.osmand.plus.wikimedia.WikiImageHelper; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -427,7 +425,7 @@ public abstract class ImageCard extends AbstractCard { } public GetImageCardsTask(@NonNull MapActivity mapActivity, LatLon latLon, - @Nullable Map params, GetImageCardsListener listener) { + @Nullable Map params, GetImageCardsListener listener) { this.mapActivity = mapActivity; this.app = mapActivity.getMyApplication(); this.latLon = latLon; @@ -436,10 +434,9 @@ public abstract class ImageCard extends AbstractCard { } @Override - protected List doInBackground(Void... params) { + protected List doInBackground(Void... voids) { TrafficStats.setThreadStatsTag(GET_IMAGE_CARD_THREAD_ID); List result = new ArrayList<>(); - OsmandPlugin.populateImageCards(result, listener); try { final Map pms = new LinkedHashMap<>(); pms.put("lat", "" + (float) latLon.getLatitude()); @@ -456,19 +453,8 @@ public abstract class ImageCard extends AbstractCard { if (!Algorithms.isEmpty(preferredLang)) { pms.put("lang", preferredLang); } - if (this.params != null) { - String wikidataId = this.params.get(Amenity.WIKIDATA); - if (wikidataId != null) { - this.params.remove(Amenity.WIKIDATA); - WikiImageHelper.addWikidataImageCards(mapActivity, wikidataId, result); - } - String wikimediaContent = this.params.get(Amenity.WIKIMEDIA_COMMONS); - if (wikimediaContent != null) { - this.params.remove(Amenity.WIKIMEDIA_COMMONS); - WikiImageHelper.addWikimediaImageCards(mapActivity, wikimediaContent, result); - } - pms.putAll(this.params); - } + OsmandPlugin.populateImageCards(result, pms, params, listener); + String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms, "Requesting location images...", false, false); diff --git a/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java b/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java index 7fa74b3b37..d099be448b 100644 --- a/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java +++ b/OsmAnd/src/net/osmand/plus/openplacereviews/OpenPlaceReviewsPlugin.java @@ -2,6 +2,9 @@ package net.osmand.plus.openplacereviews; import android.graphics.drawable.Drawable; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import net.osmand.AndroidNetworkUtils; import net.osmand.PlatformUtil; import net.osmand.data.Amenity; @@ -84,7 +87,9 @@ public class OpenPlaceReviewsPlugin extends OsmandPlugin { } @Override - protected List getImageCards(Map params, Map additionalParams, GetImageCardsListener listener) { + protected List getImageCards(@NonNull Map params, + @Nullable Map additionalParams, + @Nullable GetImageCardsListener listener) { List imageCards = new ArrayList<>(); if (mapActivity != null) { Object object = mapActivity.getMapLayers().getContextMenuLayer().getSelectedObject(); diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java index 88b7618185..cd47444c56 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaPlugin.java @@ -5,6 +5,7 @@ import android.view.View; import android.widget.ArrayAdapter; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import net.osmand.AndroidUtils; @@ -24,6 +25,8 @@ 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.mapcontextmenu.builders.cards.ImageCard; +import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask.GetImageCardsListener; import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.search.QuickSearchDialogFragment; @@ -32,8 +35,9 @@ import net.osmand.plus.search.listitems.QuickSearchBannerListItem; import net.osmand.plus.search.listitems.QuickSearchFreeBannerListItem; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings; -import net.osmand.plus.views.layers.DownloadedRegionsLayer; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.plus.views.layers.DownloadedRegionsLayer; +import net.osmand.plus.wikimedia.WikiImageHelper; import net.osmand.search.core.ObjectType; import net.osmand.search.core.SearchPhrase; import net.osmand.util.Algorithms; @@ -42,6 +46,7 @@ import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Set; import static net.osmand.aidlapi.OsmAndCustomizationConstants.WIKIPEDIA_ID; @@ -91,6 +96,11 @@ public class WikipediaPlugin extends OsmandPlugin { this.mapActivity = activity; } + @Override + public void mapActivityResumeOnTop(MapActivity activity) { + this.mapActivity = activity; + } + @Override public void mapActivityPause(MapActivity activity) { this.mapActivity = null; @@ -435,4 +445,25 @@ public class WikipediaPlugin extends OsmandPlugin { } return false; } + + @Override + protected List getImageCards(@NonNull Map params, @Nullable Map additionalParams, @Nullable GetImageCardsListener listener) { + List imageCards = new ArrayList<>(); + if (mapActivity != null) { + if (additionalParams != null) { + String wikidataId = additionalParams.get(Amenity.WIKIDATA); + if (wikidataId != null) { + additionalParams.remove(Amenity.WIKIDATA); + WikiImageHelper.addWikidataImageCards(mapActivity, wikidataId, imageCards); + } + String wikimediaContent = additionalParams.get(Amenity.WIKIMEDIA_COMMONS); + if (wikimediaContent != null) { + additionalParams.remove(Amenity.WIKIMEDIA_COMMONS); + WikiImageHelper.addWikimediaImageCards(mapActivity, wikimediaContent, imageCards); + } + params.putAll(additionalParams); + } + } + return imageCards; + } } \ No newline at end of file