From 878129ce93c17b688fc516530ef3806d10d59cb9 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 7 Oct 2020 17:01:51 +0300 Subject: [PATCH] photos added --- .../builders/cards/IPFSImageCard.java | 47 +++++ .../builders/cards/ImageCard.java | 183 +++++++++--------- 2 files changed, 143 insertions(+), 87 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/IPFSImageCard.java diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/IPFSImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/IPFSImageCard.java new file mode 100644 index 0000000000..b029c7cdfc --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/IPFSImageCard.java @@ -0,0 +1,47 @@ +package net.osmand.plus.mapcontextmenu.builders.cards; + +import android.view.View; +import net.osmand.plus.activities.MapActivity; +import net.osmand.util.Algorithms; +import org.json.JSONException; +import org.json.JSONObject; + +public class IPFSImageCard extends ImageCard { + private static final String BASE_URL = "https://test.openplacereviews.org/api/ipfs/image-ipfs?cid="; + + public IPFSImageCard(MapActivity mapActivity, JSONObject imageObject) { + super(mapActivity, imageObject); + try { + this.url = BASE_URL + imageObject.get("cid"); + this.imageHiresUrl = BASE_URL + imageObject.get("cid"); + this.imageUrl = BASE_URL + imageObject.get("cid"); + } catch (JSONException e) { + e.printStackTrace(); + } + if (!Algorithms.isEmpty(getSuitableUrl())) { + View.OnClickListener onClickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + openUrl(getMapActivity(), getMyApplication(), getTitle(), getSuitableUrl(), + isExternalLink() || Algorithms.isEmpty(getImageHiresUrl()), + !Algorithms.isEmpty(getImageHiresUrl())); + } + }; + if (!Algorithms.isEmpty(buttonText)) { + this.onButtonClickListener = onClickListener; + } else { + this.onClickListener = onClickListener; + } + } + } + + private String getSuitableUrl() { + final String url; + if (Algorithms.isEmpty(getImageHiresUrl())) { + url = getUrl(); + } else { + url = getImageHiresUrl(); + } + return url; + } +} \ No newline at end of file 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 84a63cb72a..667b234956 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -4,6 +4,7 @@ import android.content.res.ColorStateList; import android.graphics.Bitmap; import android.graphics.PointF; import android.graphics.drawable.Drawable; +import android.net.TrafficStats; import android.os.AsyncTask; import android.view.View; import android.view.View.OnClickListener; @@ -15,10 +16,9 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatButton; -import net.osmand.AndroidNetworkUtils; -import net.osmand.AndroidUtils; -import net.osmand.GPXUtilities; -import net.osmand.Location; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import net.osmand.*; import net.osmand.data.Amenity; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; @@ -28,6 +28,7 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.SettingsBaseActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapillary.MapillaryContributeCard; import net.osmand.plus.mapillary.MapillaryImageCard; @@ -36,6 +37,7 @@ import net.osmand.plus.views.layers.ContextMenuLayer; import net.osmand.plus.wikimedia.WikiImageHelper; import net.osmand.util.Algorithms; +import org.apache.commons.logging.Log; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -43,12 +45,7 @@ import org.json.JSONObject; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; +import java.util.*; import static net.osmand.data.FavouritePoint.DEFAULT_BACKGROUND_TYPE; @@ -57,6 +54,7 @@ public abstract class ImageCard extends AbstractCard { public static String TYPE_MAPILLARY_PHOTO = "mapillary-photo"; public static String TYPE_MAPILLARY_CONTRIBUTE = "mapillary-contribute"; + private static final Log LOG = PlatformUtil.getLog(ImageCard.class); protected String type; // Image location protected LatLon location; @@ -200,6 +198,14 @@ public abstract class ImageCard extends AbstractCard { return imageCard; } + private static ImageCard createCardOpr(MapActivity mapActivity, JSONObject imageObject) { + ImageCard imageCard = null; + if (imageObject.has("cid")) { + imageCard = new IPFSImageCard(mapActivity, imageObject); + } + return imageCard; + } + public double getCa() { return ca; } @@ -430,101 +436,104 @@ public abstract class ImageCard extends AbstractCard { @Override protected List doInBackground(Void... params) { + final int THREAD_ID = 10104; + TrafficStats.setThreadStatsTag(THREAD_ID); List result = new ArrayList<>(); RotatedTileBox rtb = mapActivity.getMapView().getCurrentRotatedTileBox(); Object o = mapActivity.getMapLayers().getContextMenuLayer().getSelectedObject(); - 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); - } - } if (o instanceof Amenity) { Amenity am = (Amenity) o; - System.out.println("POINT OSM ID: " + am.getId() + " " + am.getType().ordinal()); + long amenityId = (am.getId() >> 1); + getPicturesForPlace(result, amenityId); + } + try { final Map pms = new LinkedHashMap<>(); - pms.put("osmid", "" + am.getId()); + pms.put("lat", "" + (float) latLon.getLatitude()); + pms.put("lon", "" + (float) latLon.getLongitude()); + Location myLocation = app.getLocationProvider().getLastKnownLocation(); + if (myLocation != null) { + pms.put("mloc", "" + (float) myLocation.getLatitude() + "," + (float) myLocation.getLongitude()); + } + pms.put("app", Version.isPaidVersion(app) ? "paid" : "free"); + String preferredLang = app.getSettings().MAP_PREFERRED_LOCALE.get(); + if (Algorithms.isEmpty(preferredLang)) { + preferredLang = app.getLanguage(); + } + 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); + } String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms, "Requesting location images...", false, false); if (!Algorithms.isEmpty(response)) { - JSONObject obj = null; - try { - obj = new JSONObject(response); - JSONArray images = null; - images = obj.getJSONArray("features"); - 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.createCard(mapActivity, imageObject); - if (imageCard != null) { - result.add(imageCard); - } + JSONObject obj = new JSONObject(response); + JSONArray images = obj.getJSONArray("features"); + 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.createCard(mapActivity, imageObject); + if (imageCard != null) { + result.add(imageCard); } - } catch (JSONException e) { - e.printStackTrace(); } + } catch (JSONException e) { + e.printStackTrace(); } } - } catch (JSONException e) { - e.printStackTrace(); } } + } catch (Exception e) { + e.printStackTrace(); + } + if (listener != null) { + listener.onPostProcess(result); } return result; -// try { -// final Map pms = new LinkedHashMap<>(); -// pms.put("lat", "" + (float) latLon.getLatitude()); -// pms.put("lon", "" + (float) latLon.getLongitude()); -// Location myLocation = app.getLocationProvider().getLastKnownLocation(); -// if (myLocation != null) { -// pms.put("mloc", "" + (float) myLocation.getLatitude() + "," + (float) myLocation.getLongitude()); -// } -// pms.put("app", Version.isPaidVersion(app) ? "paid" : "free"); -// String preferredLang = app.getSettings().MAP_PREFERRED_LOCALE.get(); -// if (Algorithms.isEmpty(preferredLang)) { -// preferredLang = app.getLanguage(); -// } -// if (!Algorithms.isEmpty(preferredLang)) { -// pms.put("lang", preferredLang); -// } -// String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms, -// "Requesting location images...", false, false); -// -// if (!Algorithms.isEmpty(response)) { -// JSONObject obj = new JSONObject(response); -// JSONArray images = obj.getJSONArray("features"); -// 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.createCard(mapActivity, imageObject); -// if (imageCard != null) { -// result.add(imageCard); -// } -// } -// } catch (JSONException e) { -// e.printStackTrace(); -// } -// } -// } -// } -// } catch (Exception e) { -// e.printStackTrace(); -// } -// if (listener != null) { -// listener.onPostProcess(result); -// } -// 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); + + try { + if (!Algorithms.isEmpty(response)) { + 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); + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + } + } + } catch (Exception e) { + LOG.error(e); + } } @Override