From dd72744c441209cefea1fb4026fbc773f182f57a Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Tue, 17 Oct 2017 15:43:43 +0300 Subject: [PATCH] Fix #4346 --- .../src/net/osmand/plus/OsmandSettings.java | 2 +- .../plus/mapcontextmenu/MenuBuilder.java | 17 +- .../builders/AmenityMenuBuilder.java | 26 ++- .../builders/cards/AbstractCard.java | 28 ++- .../builders/cards/ImageCard.java | 199 ++++++++++-------- .../builders/cards/UrlImageCard.java | 15 +- 6 files changed, 188 insertions(+), 99 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 73d25f1563..9c8fcf4d5e 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -920,7 +920,7 @@ public class OsmandSettings { public final OsmandPreference SHOW_MAPILLARY = new BooleanPreference("show_mapillary", false).makeGlobal(); public final OsmandPreference MAPILLARY_FIRST_DIALOG_SHOWN = new BooleanPreference("mapillary_first_dialog_shown", false).makeGlobal(); - public final OsmandPreference MAPILLARY_MENU_COLLAPSED = new BooleanPreference("mapillary_menu_collapsed", false).makeGlobal(); + public final OsmandPreference ONLINE_PHOTOS_ROW_COLLAPSED = new BooleanPreference("mapillary_menu_collapsed", false).makeGlobal(); public final OsmandPreference WEBGL_SUPPORTED = new BooleanPreference("webgl_supported", true).makeGlobal(); // this value string is synchronized with settings_pref.xml preference name diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java index bfdf8c0c57..f58a4b72ec 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java @@ -53,6 +53,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.List; +import java.util.Map; import static android.util.TypedValue.COMPLEX_UNIT_DIP; @@ -344,7 +345,7 @@ public class MenuBuilder { onlinePhotoCardsRow = new CardsRowBuilder(this, view, false); onlinePhotoCardsRow.build(); CollapsableView collapsableView = new CollapsableView(onlinePhotoCardsRow.getContentView(), - app.getSettings().MAPILLARY_MENU_COLLAPSED); + app.getSettings().ONLINE_PHOTOS_ROW_COLLAPSED); collapsableView.setOnCollExpListener(new CollapsableView.OnCollExpListener() { @Override public void onCollapseExpand(boolean collapsed) { @@ -366,8 +367,13 @@ public class MenuBuilder { private void startLoadingImages(final MenuBuilder menuBuilder) { onlinePhotoCards = new ArrayList<>(); onlinePhotoCardsRow.setProgressCard(); - execute(new GetImageCardsTask(mapActivity, menuBuilder.getLatLon(), + execute(new GetImageCardsTask(mapActivity, menuBuilder.getLatLon(), getAdditionalCardParams(), new GetImageCardsListener() { + @Override + public void onPostProcess(List cardList) { + processOnlinePhotosCards(cardList); + } + @Override public void onFinish(List cardList) { if (!menuBuilder.isHidden()) { @@ -383,6 +389,13 @@ public class MenuBuilder { })); } + protected Map getAdditionalCardParams() { + return null; + } + + protected void processOnlinePhotosCards(List cardList) { + } + protected void buildInternal(View view) { } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java index 9a65b98354..8330458ee4 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java @@ -38,6 +38,7 @@ import net.osmand.util.OpeningHoursParser; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -289,9 +290,13 @@ public class AmenityMenuBuilder extends MenuBuilder { String key = e.getKey(); String vl = e.getValue(); + if (key.equals("image") || key.equals("mapillary")) { + continue; + } + String textPrefix = ""; CollapsableView collapsableView = null; - boolean collapsable = false; + boolean collapsable = false; boolean isWiki = false; boolean isText = false; boolean isDescription = false; @@ -465,7 +470,7 @@ public class AmenityMenuBuilder extends MenuBuilder { if (processNearstWiki() && nearestWiki.size() > 0) { AmenityInfoRow wikiInfo = new AmenityInfoRow( - "nearest_wiki", R.drawable.ic_action_wikipedia, null, app.getString(R.string.wiki_around) + " (" + nearestWiki.size()+")", true, + "nearest_wiki", R.drawable.ic_action_wikipedia, null, app.getString(R.string.wiki_around) + " (" + nearestWiki.size() + ")", true, getCollapsableWikiView(view.getContext(), true), 0, false, false, false, 1000, null, false, false); buildAmenityRow(view, wikiInfo); @@ -475,7 +480,7 @@ public class AmenityMenuBuilder extends MenuBuilder { boolean osmEditingEnabled = OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class) != null; if (osmEditingEnabled && amenity.getId() != null - && amenity.getId() > 0 && + && amenity.getId() > 0 && (amenity.getId() % 2 == 0 || (amenity.getId() >> 1) < Integer.MAX_VALUE)) { String link; if (amenity.getId() % 2 == 0) { @@ -506,6 +511,21 @@ public class AmenityMenuBuilder extends MenuBuilder { } } + @Override + protected Map getAdditionalCardParams() { + Map params = new HashMap<>(); + Map additionalInfo = amenity.getAdditionalInfo(); + String imageValue = additionalInfo.get("image"); + String mapillaryValue = additionalInfo.get("mapillary"); + if (!Algorithms.isEmpty(imageValue)) { + params.put("image", imageValue); + } + if (!Algorithms.isEmpty(mapillaryValue)) { + params.put("mapillary", mapillaryValue); + } + return params; + } + private static class AmenityInfoRow { private String key; private Drawable icon; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java index 1c265960f2..c0b9de5795 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java @@ -7,6 +7,8 @@ import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; import android.support.v7.widget.Toolbar; import android.util.TypedValue; @@ -51,8 +53,12 @@ public abstract class AbstractCard { @SuppressLint("SetJavaScriptEnabled") @SuppressWarnings("deprecation") - protected static void openUrl(Context ctx, OsmandApplication app, - String title, String url, boolean externalLink) { + protected static void openUrl(@NonNull Context ctx, + @NonNull OsmandApplication app, + @Nullable String title, + @NonNull String url, + boolean externalLink, + boolean hasImageUrl) { if (externalLink) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); @@ -84,12 +90,14 @@ public abstract class AbstractCard { final WebView wv = new WebView(ctx); WebSettings settings = wv.getSettings(); - /* - settings.setDefaultTextEncodingName("utf-8"); - settings.setBuiltInZoomControls(true); - settings.setDisplayZoomControls(false); - settings.setSupportZoom(true); + if (hasImageUrl) { + settings.setDefaultTextEncodingName("utf-8"); + settings.setBuiltInZoomControls(true); + settings.setDisplayZoomControls(false); + settings.setSupportZoom(true); + } + /* //Scale web view font size with system font size float scale = ctx.getResources().getConfiguration().fontScale; if (android.os.Build.VERSION.SDK_INT >= 14) { @@ -111,7 +119,11 @@ public abstract class AbstractCard { wv.setBackgroundColor(Color.argb(1, 0, 0, 0)); //wv.setScrollContainer(false); wv.getSettings().setJavaScriptEnabled(true); - wv.loadUrl(url); + if (hasImageUrl) { + wv.loadData("", "text/html", "UTF-8"); + } else { + wv.loadUrl(url); + } ll.addView(topBar); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0); 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 343a875060..e960ebd774 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -5,6 +5,7 @@ import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatButton; import android.view.View; import android.view.View.OnClickListener; @@ -38,29 +39,32 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import static java.text.DateFormat.FULL; - public abstract class ImageCard extends AbstractCard { - private String type; + public static String TYPE_MAPILLARY_PHOTO = "mapillary-photo"; + public static String TYPE_MAPILLARY_CONTRIBUTE = "mapillary-contribute"; + + protected String type; // Image location - private LatLon location; - // (optional) Image's camera angle in range [0, 360]. - private double ca = Double.NaN; - // Date When bitmap was captured. - private Date timestamp; - // Image key. - private String key; + protected LatLon location; + // (optional) Image's camera angle in range [0, 360] + protected double ca = Double.NaN; + // Date When bitmap was captured + protected Date timestamp; + // Image key + protected String key; + // Image title + protected String title; // User name - private String userName; + protected String userName; // Image viewer url - private String url; + protected String url; // Image bitmap url - private String imageUrl; + protected String imageUrl; // Image high resolution bitmap url - private String imageHiresUrl; + protected String imageHiresUrl; // true if external browser should to be opened, open webview otherwise - private boolean externalLink; + protected boolean externalLink; protected int topIconId; protected int buttonIconId; @@ -76,7 +80,8 @@ public abstract class ImageCard extends AbstractCard { protected OnClickListener onClickListener; protected OnClickListener onButtonClickListener; - private static final DateFormat DATE_FORMAT = SimpleDateFormat.getDateTimeInstance(FULL, FULL, Locale.US); //"yyyy-MM-dd'T'HH:mm:ss"); + private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); + private boolean downloading; private boolean downloaded; private Bitmap bitmap; @@ -85,75 +90,80 @@ public abstract class ImageCard extends AbstractCard { public ImageCard(MapActivity mapActivity, JSONObject imageObject) { super(mapActivity); - try { - if (imageObject.has("type")) { - this.type = imageObject.getString("type"); - } - if (imageObject.has("ca") && !imageObject.isNull("ca")) { - this.ca = imageObject.getDouble("ca"); - } - if (imageObject.has("lat") && imageObject.has("lon")) { - double latitude = imageObject.getDouble("lat"); - double longitude = imageObject.getDouble("lon"); - this.location = new LatLon(latitude, longitude); - } - if (imageObject.has("timestamp")) { - try { - this.timestamp = DATE_FORMAT.parse(imageObject.getString("timestamp")); - } catch (ParseException e) { - e.printStackTrace(); + if (imageObject != null) { + try { + if (imageObject.has("type")) { + this.type = imageObject.getString("type"); } - } - if (imageObject.has("key")) { - this.key = imageObject.getString("key"); - } - if (imageObject.has("username")) { - this.userName = imageObject.getString("username"); - } - if (imageObject.has("url")) { - this.url = imageObject.getString("url"); - } - if (imageObject.has("imageUrl")) { - this.imageUrl = imageObject.getString("imageUrl"); - } - if (imageObject.has("imageHiresUrl")) { - this.imageHiresUrl = imageObject.getString("imageHiresUrl"); - } - if (imageObject.has("externalLink") && !imageObject.isNull("externalLink")) { - this.externalLink = imageObject.getBoolean("externalLink"); - } - if (imageObject.has("topIcon") && !imageObject.isNull("topIcon")) { - this.topIconId = getDrawableId(imageObject.getString("topIcon")); - } - if (imageObject.has("buttonIcon") && !imageObject.isNull("buttonIcon")) { - this.buttonIconId = getDrawableId(imageObject.getString("buttonIcon")); - } - if (imageObject.has("buttonText") && !imageObject.isNull("buttonText")) { - this.buttonText = imageObject.getString("buttonText"); - } - if (imageObject.has("buttonIconColor") && !imageObject.isNull("buttonIconColor")) { - try { - this.buttonIconColor = Algorithms.parseColor(imageObject.getString("buttonIconColor")); - } catch (IllegalArgumentException e) { - e.printStackTrace(); + if (imageObject.has("ca") && !imageObject.isNull("ca")) { + this.ca = imageObject.getDouble("ca"); } - } - if (imageObject.has("buttonColor") && !imageObject.isNull("buttonColor")) { - try { - this.buttonColor = Algorithms.parseColor(imageObject.getString("buttonColor")); - } catch (IllegalArgumentException e) { - e.printStackTrace(); + if (imageObject.has("lat") && imageObject.has("lon")) { + double latitude = imageObject.getDouble("lat"); + double longitude = imageObject.getDouble("lon"); + this.location = new LatLon(latitude, longitude); } - } - if (imageObject.has("buttonTextColor") && !imageObject.isNull("buttonTextColor")) { - try { - this.buttonTextColor = Algorithms.parseColor(imageObject.getString("buttonTextColor")); - } catch (IllegalArgumentException e) { - e.printStackTrace(); + if (imageObject.has("timestamp")) { + try { + this.timestamp = DATE_FORMAT.parse(imageObject.getString("timestamp")); + } catch (ParseException e) { + e.printStackTrace(); + } } + if (imageObject.has("key")) { + this.key = imageObject.getString("key"); + } + if (imageObject.has("title")) { + this.title = imageObject.getString("title"); + } + if (imageObject.has("username")) { + this.userName = imageObject.getString("username"); + } + if (imageObject.has("url")) { + this.url = imageObject.getString("url"); + } + if (imageObject.has("imageUrl")) { + this.imageUrl = imageObject.getString("imageUrl"); + } + if (imageObject.has("imageHiresUrl")) { + this.imageHiresUrl = imageObject.getString("imageHiresUrl"); + } + if (imageObject.has("externalLink") && !imageObject.isNull("externalLink")) { + this.externalLink = imageObject.getBoolean("externalLink"); + } + if (imageObject.has("topIcon") && !imageObject.isNull("topIcon")) { + this.topIconId = getDrawableId(imageObject.getString("topIcon")); + } + if (imageObject.has("buttonIcon") && !imageObject.isNull("buttonIcon")) { + this.buttonIconId = getDrawableId(imageObject.getString("buttonIcon")); + } + if (imageObject.has("buttonText") && !imageObject.isNull("buttonText")) { + this.buttonText = imageObject.getString("buttonText"); + } + if (imageObject.has("buttonIconColor") && !imageObject.isNull("buttonIconColor")) { + try { + this.buttonIconColor = Algorithms.parseColor(imageObject.getString("buttonIconColor")); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + if (imageObject.has("buttonColor") && !imageObject.isNull("buttonColor")) { + try { + this.buttonColor = Algorithms.parseColor(imageObject.getString("buttonColor")); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + if (imageObject.has("buttonTextColor") && !imageObject.isNull("buttonTextColor")) { + try { + this.buttonTextColor = Algorithms.parseColor(imageObject.getString("buttonTextColor")); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + } catch (Exception e) { + e.printStackTrace(); } - } catch (Exception e) { - e.printStackTrace(); } } @@ -170,9 +180,9 @@ public abstract class ImageCard extends AbstractCard { try { if (imageObject.has("type")) { String type = imageObject.getString("type"); - if ("mapillary-photo".equals(type)) { + if (TYPE_MAPILLARY_PHOTO.equals(type)) { imageCard = new MapillaryImageCard(mapActivity, imageObject); - } else if ("mapillary-contribute".equals(type)) { + } else if (TYPE_MAPILLARY_CONTRIBUTE.equals(type)) { imageCard = new MapillaryContributeCard(mapActivity, imageObject); } else { imageCard = new UrlImageCard(mapActivity, imageObject); @@ -192,6 +202,10 @@ public abstract class ImageCard extends AbstractCard { return key; } + public String getTitle() { + return title; + } + public String getType() { return type; } @@ -302,9 +316,11 @@ public abstract class ImageCard extends AbstractCard { if (view != null) { ImageView image = (ImageView) view.findViewById(R.id.image); ImageView iconImageView = (ImageView) view.findViewById(R.id.icon); + TextView urlTextView = (TextView) view.findViewById(R.id.url); TextView watermarkTextView = (TextView) view.findViewById(R.id.watermark); ProgressBar progress = (ProgressBar) view.findViewById(R.id.progress); AppCompatButton button = (AppCompatButton) view.findViewById(R.id.button); + if (icon == null && topIconId != 0) { icon = getMyApplication().getIconsCache().getIcon(topIconId); } @@ -328,6 +344,12 @@ public abstract class ImageCard extends AbstractCard { } else { progress.setVisibility(View.GONE); image.setImageBitmap(bitmap); + if (bitmap == null) { + urlTextView.setVisibility(View.VISIBLE); + urlTextView.setText(getUrl()); + } else { + urlTextView.setVisibility(View.GONE); + } } if (onClickListener != null) { view.findViewById(R.id.image_card).setOnClickListener(new OnClickListener() { @@ -377,17 +399,22 @@ public abstract class ImageCard extends AbstractCard { private MapActivity mapActivity; private OsmandApplication app; private LatLon latLon; + private Map params; private GetImageCardsListener listener; private List result; public interface GetImageCardsListener { + void onPostProcess(List cardList); + void onFinish(List cardList); } - public GetImageCardsTask(@NonNull MapActivity mapActivity, LatLon latLon, GetImageCardsListener listener) { + public GetImageCardsTask(@NonNull MapActivity mapActivity, LatLon latLon, + @Nullable Map params, GetImageCardsListener listener) { this.mapActivity = mapActivity; this.app = mapActivity.getMyApplication(); this.latLon = latLon; + this.params = params; this.listener = listener; } @@ -410,6 +437,9 @@ public abstract class ImageCard extends AbstractCard { if (!Algorithms.isEmpty(preferredLang)) { pms.put("lang", preferredLang); } + if (this.params != null) { + pms.putAll(this.params); + } String response = AndroidNetworkUtils.sendRequest(app, "https://osmand.net/api/cm_place.php", pms, "Requesting location images...", false, false); @@ -435,6 +465,9 @@ public abstract class ImageCard extends AbstractCard { } catch (Exception e) { e.printStackTrace(); } + if (listener != null) { + listener.onPostProcess(result); + } return result; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java index cae773173b..27d8525c04 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java @@ -12,11 +12,22 @@ public class UrlImageCard extends ImageCard { public UrlImageCard(MapActivity mapActivity, JSONObject imageObject) { super(mapActivity, imageObject); - if (!Algorithms.isEmpty(getUrl())) { + + final String url; + final boolean hasImageUrl; + if (Algorithms.isEmpty(getImageHiresUrl())) { + url = getUrl(); + hasImageUrl = false; + } else { + url = getImageHiresUrl(); + hasImageUrl = true; + } + + if (!Algorithms.isEmpty(url)) { OnClickListener onClickListener = new OnClickListener() { @Override public void onClick(View v) { - openUrl(getMapActivity(), getMyApplication(), "", getUrl(), isExternalLink()); + openUrl(getMapActivity(), getMyApplication(), getTitle(), url, isExternalLink(), hasImageUrl); } }; if (!Algorithms.isEmpty(buttonText)) {