From 2bde2da819f7b6fd4543a58ca186c8b584780def Mon Sep 17 00:00:00 2001 From: simon Date: Tue, 17 Nov 2020 17:07:16 +0200 Subject: [PATCH] adding image id code --- .../builders/cards/ImageCard.java | 75 ++++++++++++------- 1 file changed, 50 insertions(+), 25 deletions(-) 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 d7b1d69895..1108692934 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -21,6 +21,7 @@ import net.osmand.Location; import net.osmand.PlatformUtil; import net.osmand.data.Amenity; import net.osmand.data.LatLon; +import net.osmand.plus.BuildConfig; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; @@ -39,13 +40,7 @@ import org.json.JSONObject; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import java.util.*; public abstract class ImageCard extends AbstractCard { @@ -408,6 +403,28 @@ public abstract class ImageCard extends AbstractCard { } } + private static String[] getIdFromResponse(String response) { + try { + JSONArray obj = new JSONObject(response).getJSONArray("objects"); + JSONArray images = (JSONArray) ((JSONObject) obj.get(0)).get("id"); + return toStringArray(images); + } catch (JSONException e) { + e.printStackTrace(); + } + return new String[0]; + } + + private static String[] toStringArray(JSONArray array) { + if (array == null) + return null; + + String[] arr = new String[array.length()]; + for (int i = 0; i < arr.length; i++) { + arr[i] = array.optString(i); + } + return arr; + } + public static class GetImageCardsTask extends AsyncTask> { private MapActivity mapActivity; @@ -425,7 +442,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; @@ -441,7 +458,15 @@ public abstract class ImageCard extends AbstractCard { if (o instanceof Amenity) { Amenity am = (Amenity) o; long amenityId = am.getId() >> 1; - getPicturesForPlace(result, amenityId); + String url = BuildConfig.OPR_BASE_URL + "api/objects-by-index?type=opr.place&index=osmid&key=" + amenityId; + String response = AndroidNetworkUtils.sendRequest(app, url, Collections.emptyMap(), + "Requesting location images...", false, false); + if (response != null) { + getPicturesForPlace(result, response); + String[] id = getIdFromResponse(response); + //TODO perform something with image + //listener.onOPRPlaceIdAcquired(id); + } } try { final Map pms = new LinkedHashMap<>(); @@ -503,27 +528,27 @@ public abstract class ImageCard extends AbstractCard { return result; } - private void getPicturesForPlace(List result, long l) { - String url = "https://test.openplacereviews.org/api/objects-by-index?type=opr.place&index=osmid&limit=1&key=" + l; - String response = AndroidNetworkUtils.sendRequest(app, url, Collections.emptyMap(), - "Requesting location images...", false, false); + private void getPicturesForPlace(List result, String response) { try { if (!Algorithms.isEmpty(response)) { - //TODO extract place id JSONArray obj = new JSONObject(response).getJSONArray("objects"); - JSONArray images = ((JSONObject) ((JSONObject) obj.get(0)).get("images")).getJSONArray("outdoor"); - if (images.length() > 0) { - for (int i = 0; i < images.length(); i++) { - try { - JSONObject imageObject = (JSONObject) images.get(i); - if (imageObject != JSONObject.NULL) { - ImageCard imageCard = ImageCard.createCardOpr(mapActivity, imageObject); - if (imageCard != null) { - result.add(imageCard); + JSONObject imagesWrapper = ((JSONObject) ((JSONObject) obj.get(0)).get("images")); + Iterator it = imagesWrapper.keys(); + while (it.hasNext()) { + JSONArray images = imagesWrapper.getJSONArray(it.next()); + if (images.length() > 0) { + for (int i = 0; i < images.length(); i++) { + try { + JSONObject imageObject = (JSONObject) images.get(i); + if (imageObject != JSONObject.NULL) { + ImageCard imageCard = ImageCard.createCardOpr(mapActivity, imageObject); + if (imageCard != null) { + result.add(imageCard); + } } + } catch (JSONException e) { + LOG.error(e); } - } catch (JSONException e) { - LOG.error(e); } } }