diff --git a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java index a041d6f694..38854cb33b 100644 --- a/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java +++ b/OsmAnd-java/src/main/java/net/osmand/data/Amenity.java @@ -38,6 +38,7 @@ public class Amenity extends MapObject { public static final String COLLECTION_TIMES = "collection_times"; public static final String CONTENT = "content"; public static final String CUISINE = "cuisine"; + public static final String WIKIDATA = "wikidata"; public static final String DISH = "dish"; public static final String REF = "ref"; public static final String OSM_DELETE_VALUE = "delete"; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java index 66567bc76c..d627b4e6e6 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java @@ -471,7 +471,7 @@ public class AmenityMenuBuilder extends MenuBuilder { } textPrefix = app.getString(R.string.poi_cuisine); vl = sb.toString(); - } else if (key.contains(Amenity.ROUTE)) { + } else if (key.contains(Amenity.ROUTE) || key.equals(Amenity.WIKIDATA)) { continue; } else { if (key.contains(Amenity.DESCRIPTION)) { @@ -784,12 +784,16 @@ public class AmenityMenuBuilder extends MenuBuilder { Map additionalInfo = amenity.getAdditionalInfo(); String imageValue = additionalInfo.get("image"); String mapillaryValue = additionalInfo.get("mapillary"); + String wikidataValue = additionalInfo.get("wikidata"); if (!Algorithms.isEmpty(imageValue)) { params.put("osm_image", imageValue); } if (!Algorithms.isEmpty(mapillaryValue)) { params.put("osm_mapillary_key", mapillaryValue); } + if (!Algorithms.isEmpty(wikidataValue)) { + params.put("wikidata_id", wikidataValue); + } return params; } 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 a821d8b3cf..94c045fa4d 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -25,6 +25,7 @@ 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.json.JSONArray; @@ -437,6 +438,11 @@ public abstract class ImageCard extends AbstractCard { pms.put("lang", preferredLang); } if (this.params != null) { + String wikidataId = this.params.get("wikidata_id"); + if (wikidataId != null) { + this.params.remove("wikidata_id"); + WikiImageHelper.fillWikiMediaCards(mapActivity, wikidataId, result); + } pms.putAll(this.params); } String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms, diff --git a/OsmAnd/src/net/osmand/plus/wikimedia/WikiImage.java b/OsmAnd/src/net/osmand/plus/wikimedia/WikiImage.java new file mode 100644 index 0000000000..d545b024e6 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikimedia/WikiImage.java @@ -0,0 +1,28 @@ +package net.osmand.plus.wikimedia; + +public class WikiImage { + + private String imageName; + private String imageStubUrl; + private String imageHiResUrl; + + public WikiImage(String imageName, String imageStubUrl, + String imageHiResUrl) { + this.imageName = imageName; + this.imageStubUrl = imageStubUrl; + this.imageHiResUrl = imageHiResUrl; + } + + public String getImageName() { + return imageName; + } + + public String getImageStubUrl() { + return imageStubUrl; + } + + public String getImageHiResUrl() { + return imageHiResUrl; + } + +} diff --git a/OsmAnd/src/net/osmand/plus/wikimedia/WikiImageCard.java b/OsmAnd/src/net/osmand/plus/wikimedia/WikiImageCard.java new file mode 100644 index 0000000000..e423e3daa5 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikimedia/WikiImageCard.java @@ -0,0 +1,40 @@ +package net.osmand.plus.wikimedia; + +import android.view.View; + +import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; +import net.osmand.util.Algorithms; + +public class WikiImageCard extends ImageCard { + + public WikiImageCard(final MapActivity mapActivity, + final WikiImage wikiImage) { + super(mapActivity, null); + + if (topIconId == 0) { + topIconId = R.drawable.ic_logo_wikimedia; + } + + this.imageUrl = wikiImage.getImageStubUrl(); + this.title = wikiImage.getImageName(); + this.url = this.imageUrl; + + View.OnClickListener onClickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + openUrl(getMapActivity(), getMyApplication(), getTitle(), wikiImage.getImageHiResUrl(), + isExternalLink() || Algorithms.isEmpty(getImageHiresUrl()), + !Algorithms.isEmpty(getImageHiresUrl())); + } + }; + + if (!Algorithms.isEmpty(buttonText)) { + this.onButtonClickListener = onClickListener; + } else { + this.onClickListener = onClickListener; + } + } + +} diff --git a/OsmAnd/src/net/osmand/plus/wikimedia/WikiImageHelper.java b/OsmAnd/src/net/osmand/plus/wikimedia/WikiImageHelper.java new file mode 100644 index 0000000000..eded246941 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikimedia/WikiImageHelper.java @@ -0,0 +1,126 @@ +package net.osmand.plus.wikimedia; + +import androidx.annotation.NonNull; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +import net.osmand.PlatformUtil; +import net.osmand.osm.io.NetworkUtils; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; + +import org.apache.commons.codec.binary.Hex; +import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.logging.Log; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.List; + +public class WikiImageHelper { + private static final String WIKIDATA_API_ENDPOINT = "https://www.wikidata.org/w/api.php"; + private static final String ACTION = "?action=wbgetclaims&property=P18&entity="; + private static final String FORMAT_JSON = "&format=json"; + private static final String IMAGE_BASE_URL = "https://upload.wikimedia.org/wikipedia/commons/"; + private static final String WIKIDATA_PREFIX = "Q"; + private static final int THUMB_SIZE = 500; + private static final Log LOG = PlatformUtil.getLog(WikiImageHelper.class); + + public static void fillWikiMediaCards(@NonNull MapActivity mapActivity, @NonNull String wikidata, + List images) { + if (wikidata.startsWith(WIKIDATA_PREFIX)) { + StringBuilder rawResponse = new StringBuilder(); + String url = WIKIDATA_API_ENDPOINT + ACTION + wikidata + FORMAT_JSON; + String error = NetworkUtils.sendGetRequest(url, null, rawResponse); + if (error == null) { + try { + Gson gson = new Gson(); + WikipediaResponse response = gson.fromJson(rawResponse.toString(), WikipediaResponse.class); + for (WikiImage img : getImageData(response)) { + images.add(new WikiImageCard(mapActivity, img)); + } + return; + } catch (JsonSyntaxException e) { + error = e.getLocalizedMessage(); + } + } + LOG.error(error); + } else { + LOG.error("Wrong WikiMedia ID"); + } + } + + private static List getImageData(WikipediaResponse response) { + List images = new ArrayList<>(); + for (P18 p18 : response.claims.p18) { + String imageFileName = p18.mainsnak.datavalue.value; + if (imageFileName != null) { + try { + String imageName = URLDecoder.decode(imageFileName, "UTF-8"); + imageFileName = imageName.replace(" ", "_"); + imageName = imageName.substring(0, imageName.lastIndexOf(".")); + String[] urlHashParts = getHash(imageFileName); + + String imageHiResUrl = IMAGE_BASE_URL + + urlHashParts[0] + "/" + urlHashParts[1] + "/" + + imageFileName; + String imageStubUrl = IMAGE_BASE_URL + "thumb/" + + urlHashParts[0] + "/" + urlHashParts[1] + "/" + + imageFileName + "/" + THUMB_SIZE + "px-" + + imageFileName; + images.add(new WikiImage(imageName, imageStubUrl, imageHiResUrl)); + + } catch (UnsupportedEncodingException e) { + LOG.error(e.getLocalizedMessage()); + } + } + } + return images; + } + + @NonNull + private static String[] getHash(@NonNull String s) { + String md5 = new String(Hex.encodeHex(DigestUtils.md5(s))); + return new String[]{md5.substring(0, 1), md5.substring(0, 2)}; + } + + private static class Claims { + @SerializedName("P18") + @Expose + private List p18 = null; + } + + private static class Datavalue { + @SerializedName("value") + @Expose + private String value; + @SerializedName("type") + @Expose + private String type; + } + + private static class Mainsnak { + @SerializedName("datavalue") + @Expose + private Datavalue datavalue; + @SerializedName("datatype") + @Expose + private String datatype; + } + + private static class P18 { + @SerializedName("mainsnak") + @Expose + private Mainsnak mainsnak; + } + + private static class WikipediaResponse { + @SerializedName("claims") + @Expose + private Claims claims; + } +}