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,9 +436,32 @@ 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 (o instanceof Amenity) {
Amenity am = (Amenity) o;
long amenityId = (am.getId() >> 1);
getPicturesForPlace(result, amenityId);
}
try {
final Map<String, String> 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);
}
if (this.params != null) { if (this.params != null) {
String wikidataId = this.params.get(Amenity.WIKIDATA); String wikidataId = this.params.get(Amenity.WIKIDATA);
if (wikidataId != null) { if (wikidataId != null) {
@ -444,21 +473,14 @@ public abstract class ImageCard extends AbstractCard {
this.params.remove(Amenity.WIKIMEDIA_COMMONS); this.params.remove(Amenity.WIKIMEDIA_COMMONS);
WikiImageHelper.addWikimediaImageCards(mapActivity, wikimediaContent, result); WikiImageHelper.addWikimediaImageCards(mapActivity, wikimediaContent, result);
} }
pms.putAll(this.params);
} }
if (o instanceof Amenity) {
Amenity am = (Amenity) o;
System.out.println("POINT OSM ID: " + am.getId() + " " + am.getType().ordinal());
final Map<String, String> pms = new LinkedHashMap<>();
pms.put("osmid", "" + am.getId());
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);
JSONArray images = null;
images = obj.getJSONArray("features");
if (images.length() > 0) { if (images.length() > 0) {
for (int i = 0; i < images.length(); i++) { for (int i = 0; i < images.length(); i++) {
try { try {
@ -474,57 +496,44 @@ public abstract class ImageCard extends AbstractCard {
} }
} }
} }
}
} catch (Exception e) {
e.printStackTrace();
}
if (listener != null) {
listener.onPostProcess(result);
}
return result;
}
private void getPicturesForPlace(List<ImageCard> 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.<String, String>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) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
return result; }
// try { } catch (Exception e) {
// final Map<String, String> pms = new LinkedHashMap<>(); LOG.error(e);
// 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;
} }
@Override @Override