photos added

This commit is contained in:
simon 2020-10-07 17:01:51 +03:00
parent 02dbacf4fe
commit 878129ce93
2 changed files with 143 additions and 87 deletions

View file

@ -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;
}
}

View file

@ -4,6 +4,7 @@ import android.content.res.ColorStateList;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.PointF; import android.graphics.PointF;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.TrafficStats;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
@ -15,10 +16,9 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton; import androidx.appcompat.widget.AppCompatButton;
import net.osmand.AndroidNetworkUtils; import com.google.gson.JsonArray;
import net.osmand.AndroidUtils; import com.google.gson.JsonObject;
import net.osmand.GPXUtilities; import net.osmand.*;
import net.osmand.Location;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
@ -28,6 +28,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.SettingsBaseActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapillary.MapillaryContributeCard; import net.osmand.plus.mapillary.MapillaryContributeCard;
import net.osmand.plus.mapillary.MapillaryImageCard; 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.plus.wikimedia.WikiImageHelper;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
@ -43,12 +45,7 @@ import org.json.JSONObject;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static net.osmand.data.FavouritePoint.DEFAULT_BACKGROUND_TYPE; 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_PHOTO = "mapillary-photo";
public static String TYPE_MAPILLARY_CONTRIBUTE = "mapillary-contribute"; public static String TYPE_MAPILLARY_CONTRIBUTE = "mapillary-contribute";
private static final Log LOG = PlatformUtil.getLog(ImageCard.class);
protected String type; protected String type;
// Image location // Image location
protected LatLon location; protected LatLon location;
@ -200,6 +198,14 @@ public abstract class ImageCard extends AbstractCard {
return imageCard; 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() { public double getCa() {
return ca; return ca;
} }
@ -430,101 +436,104 @@ public abstract class ImageCard extends AbstractCard {
@Override @Override
protected List<ImageCard> doInBackground(Void... params) { protected List<ImageCard> doInBackground(Void... params) {
final int THREAD_ID = 10104;
TrafficStats.setThreadStatsTag(THREAD_ID);
List<ImageCard> result = new ArrayList<>(); List<ImageCard> result = new ArrayList<>();
RotatedTileBox rtb = mapActivity.getMapView().getCurrentRotatedTileBox(); RotatedTileBox rtb = mapActivity.getMapView().getCurrentRotatedTileBox();
Object o = mapActivity.getMapLayers().getContextMenuLayer().getSelectedObject(); 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) { if (o instanceof Amenity) {
Amenity am = (Amenity) o; 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<String, String> pms = new LinkedHashMap<>(); final Map<String, String> 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, String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms,
"Requesting location images...", false, false); "Requesting location images...", false, false);
if (!Algorithms.isEmpty(response)) { if (!Algorithms.isEmpty(response)) {
JSONObject obj = null; JSONObject obj = new JSONObject(response);
try { JSONArray images = obj.getJSONArray("features");
obj = new JSONObject(response); if (images.length() > 0) {
JSONArray images = null; for (int i = 0; i < images.length(); i++) {
images = obj.getJSONArray("features"); try {
if (images.length() > 0) { JSONObject imageObject = (JSONObject) images.get(i);
for (int i = 0; i < images.length(); i++) { if (imageObject != JSONObject.NULL) {
try { ImageCard imageCard = ImageCard.createCard(mapActivity, imageObject);
JSONObject imageObject = (JSONObject) images.get(i); if (imageCard != null) {
if (imageObject != JSONObject.NULL) { result.add(imageCard);
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; return result;
// try { }
// final Map<String, String> pms = new LinkedHashMap<>();
// pms.put("lat", "" + (float) latLon.getLatitude()); private void getPicturesForPlace(List<ImageCard> result, long l) {
// pms.put("lon", "" + (float) latLon.getLongitude()); String url = "https://test.openplacereviews.org/api/objects-by-index?type=opr.place&index=osmid&limit=1&key=" + l;
// Location myLocation = app.getLocationProvider().getLastKnownLocation(); String response = AndroidNetworkUtils.sendRequest(app, url, Collections.<String, String>emptyMap(),
// if (myLocation != null) { "Requesting location images...", false, false);
// pms.put("mloc", "" + (float) myLocation.getLatitude() + "," + (float) myLocation.getLongitude());
// } try {
// pms.put("app", Version.isPaidVersion(app) ? "paid" : "free"); if (!Algorithms.isEmpty(response)) {
// String preferredLang = app.getSettings().MAP_PREFERRED_LOCALE.get(); JSONArray obj = new JSONObject(response).getJSONArray("objects");
// if (Algorithms.isEmpty(preferredLang)) { JSONArray images = ((JSONObject) ((JSONObject) obj.get(0)).get("images")).getJSONArray("outdoor");
// preferredLang = app.getLanguage(); if (images.length() > 0) {
// } for (int i = 0; i < images.length(); i++) {
// if (!Algorithms.isEmpty(preferredLang)) { try {
// pms.put("lang", preferredLang); JSONObject imageObject = (JSONObject) images.get(i);
// } if (imageObject != JSONObject.NULL) {
// String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place", pms, ImageCard imageCard = ImageCard.createCardOpr(mapActivity, imageObject);
// "Requesting location images...", false, false); if (imageCard != null) {
// result.add(imageCard);
// if (!Algorithms.isEmpty(response)) { }
// JSONObject obj = new JSONObject(response); }
// JSONArray images = obj.getJSONArray("features"); } catch (JSONException e) {
// if (images.length() > 0) { e.printStackTrace();
// 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); } catch (Exception e) {
// if (imageCard != null) { LOG.error(e);
// result.add(imageCard); }
// }
// }
// } catch (JSONException e) {
// e.printStackTrace();
// }
// }
// }
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// if (listener != null) {
// listener.onPostProcess(result);
// }
// return result;
} }
@Override @Override