Merge pull request #10508 from osmandapp/fix_10395_incomplete_url

Fix #10395 Add support of incomplete Social Media URLs
This commit is contained in:
vshcherb 2021-01-11 15:10:10 +01:00 committed by GitHub
commit 17dbe998d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,10 +15,6 @@ import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
@ -67,6 +63,10 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import androidx.annotation.NonNull;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
public class AmenityMenuBuilder extends MenuBuilder { public class AmenityMenuBuilder extends MenuBuilder {
private static final String WIKI_LINK = ".wikipedia.org/w"; private static final String WIKI_LINK = ".wikipedia.org/w";
@ -97,14 +97,16 @@ public class AmenityMenuBuilder extends MenuBuilder {
boolean collapsable, final CollapsableView collapsableView, boolean collapsable, final CollapsableView collapsableView,
int textColor, boolean isWiki, boolean isText, boolean needLinks, int textColor, boolean isWiki, boolean isText, boolean needLinks,
boolean isPhoneNumber, boolean isUrl, boolean matchWidthDivider, int textLinesLimit) { boolean isPhoneNumber, boolean isUrl, boolean matchWidthDivider, int textLinesLimit) {
buildRow(view, iconId == 0 ? null : getRowIcon(iconId), text, textPrefix, collapsable, collapsableView, textColor, buildRow(view, iconId == 0 ? null : getRowIcon(iconId), text, textPrefix, socialMediaUrl,
collapsable, collapsableView, textColor,
isWiki, isText, needLinks, isPhoneNumber, isUrl, matchWidthDivider, textLinesLimit); isWiki, isText, needLinks, isPhoneNumber, isUrl, matchWidthDivider, textLinesLimit);
} }
protected void buildRow(final View view, Drawable icon, final String text, final String textPrefix, protected void buildRow(final View view, Drawable icon, final String text, final String textPrefix,
boolean collapsable, final CollapsableView collapsableView, final String socialMediaUrl, boolean collapsable,
int textColor, boolean isWiki, boolean isText, boolean needLinks, final CollapsableView collapsableView, int textColor, boolean isWiki,
boolean isPhoneNumber, boolean isUrl, boolean matchWidthDivider, int textLinesLimit) { boolean isText, boolean needLinks, boolean isPhoneNumber, boolean isUrl,
boolean matchWidthDivider, int textLinesLimit) {
if (!isFirstRow()) { if (!isFirstRow()) {
buildRowDivider(view); buildRowDivider(view);
@ -312,8 +314,9 @@ public class AmenityMenuBuilder extends MenuBuilder {
WikipediaArticleWikiLinkFragment.showInstance(mapActivity.getSupportFragmentManager(), text); WikipediaArticleWikiLinkFragment.showInstance(mapActivity.getSupportFragmentManager(), text);
} }
} else { } else {
String uri = socialMediaUrl == null ? text : socialMediaUrl;
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(text)); intent.setData(Uri.parse(uri));
v.getContext().startActivity(intent); v.getContext().startActivity(intent);
} }
} }
@ -366,6 +369,7 @@ public class AmenityMenuBuilder extends MenuBuilder {
continue; continue;
} }
String socialMediaUrl = null;
String textPrefix = ""; String textPrefix = "";
CollapsableView collapsableView = null; CollapsableView collapsableView = null;
boolean collapsable = false; boolean collapsable = false;
@ -397,6 +401,11 @@ public class AmenityMenuBuilder extends MenuBuilder {
if (vl.startsWith("http://") || vl.startsWith("https://") || vl.startsWith("HTTP://") || vl.startsWith("HTTPS://")) { if (vl.startsWith("http://") || vl.startsWith("https://") || vl.startsWith("HTTP://") || vl.startsWith("HTTPS://")) {
isUrl = true; isUrl = true;
} else {
socialMediaUrl = getSocialMediaUrl(key, vl);
if (socialMediaUrl != null) {
isUrl = true;
}
} }
if (pType != null && !pType.isText()) { if (pType != null && !pType.isText()) {
@ -557,16 +566,16 @@ public class AmenityMenuBuilder extends MenuBuilder {
AmenityInfoRow row; AmenityInfoRow row;
if (isDescription) { if (isDescription) {
row = new AmenityInfoRow(key, R.drawable.ic_action_note_dark, textPrefix, row = new AmenityInfoRow(key, R.drawable.ic_action_note_dark, textPrefix,
vl, collapsable, collapsableView, 0, false, true, vl, null, collapsable, collapsableView, 0, false,
true, 0, "", false, false, matchWidthDivider, 0); true, true, 0, "", false, false, matchWidthDivider, 0);
} else if (icon != null) { } else if (icon != null) {
row = new AmenityInfoRow(key, icon, textPrefix, vl, collapsable, collapsableView, row = new AmenityInfoRow(key, icon, textPrefix, vl, socialMediaUrl, collapsable,
textColor, isWiki, isText, needLinks, poiTypeOrder, poiTypeKeyName, collapsableView, textColor, isWiki, isText, needLinks, poiTypeOrder,
isPhoneNumber, isUrl, matchWidthDivider, 0); poiTypeKeyName, isPhoneNumber, isUrl, matchWidthDivider, 0);
} else { } else {
row = new AmenityInfoRow(key, iconId, textPrefix, vl, collapsable, collapsableView, row = new AmenityInfoRow(key, iconId, textPrefix, vl, socialMediaUrl, collapsable,
textColor, isWiki, isText, needLinks, poiTypeOrder, poiTypeKeyName, collapsableView, textColor, isWiki, isText, needLinks, poiTypeOrder,
isPhoneNumber, isUrl, matchWidthDivider, 0); poiTypeKeyName, isPhoneNumber, isUrl, matchWidthDivider, 0);
} }
if (isDescription) { if (isDescription) {
descriptions.add(row); descriptions.add(row);
@ -613,8 +622,11 @@ public class AmenityMenuBuilder extends MenuBuilder {
} }
boolean cuisineOrDish = categoryName.equals(Amenity.CUISINE) || categoryName.equals(Amenity.DISH); boolean cuisineOrDish = categoryName.equals(Amenity.CUISINE) || categoryName.equals(Amenity.DISH);
CollapsableView collapsableView = getPoiTypeCollapsableView(view.getContext(), true, categoryTypes, true, cuisineOrDish ? cuisineRow : null); CollapsableView collapsableView = getPoiTypeCollapsableView(view.getContext(), true, categoryTypes, true, cuisineOrDish ? cuisineRow : null);
infoRows.add(new AmenityInfoRow(poiAdditionalCategoryName, icon, pType.getPoiAdditionalCategoryTranslation(), sb.toString(), true, collapsableView, infoRows.add(new AmenityInfoRow(poiAdditionalCategoryName, icon,
0, false, false, false, pType.getOrder(), pType.getKeyName(), false, false, false, 1)); pType.getPoiAdditionalCategoryTranslation(), sb.toString(), null,
true, collapsableView, 0, false, false,
false, pType.getOrder(), pType.getKeyName(), false,
false, false, 1));
} }
} }
@ -629,8 +641,10 @@ public class AmenityMenuBuilder extends MenuBuilder {
} }
sb.append(pt.getTranslation()); sb.append(pt.getTranslation());
} }
infoRows.add(new AmenityInfoRow(poiCategory.getKeyName(), icon, poiCategory.getTranslation(), sb.toString(), true, collapsableView, infoRows.add(new AmenityInfoRow(poiCategory.getKeyName(), icon,
0, false, false, false, 40, poiCategory.getKeyName(), false, false, false, 1)); poiCategory.getTranslation(), sb.toString(), null, true,
collapsableView, 0, false, false, false, 40,
poiCategory.getKeyName(), false, false, false, 1));
} }
Collections.sort(infoRows, new Comparator<AmenityInfoRow>() { Collections.sort(infoRows, new Comparator<AmenityInfoRow>() {
@ -783,12 +797,14 @@ public class AmenityMenuBuilder extends MenuBuilder {
public void buildAmenityRow(View view, AmenityInfoRow info) { public void buildAmenityRow(View view, AmenityInfoRow info) {
if (info.icon != null) { if (info.icon != null) {
buildRow(view, info.icon, info.text, info.textPrefix, info.collapsable, info.collapsableView, buildRow(view, info.icon, info.text, info.textPrefix, info.socialMediaUrl,
info.textColor, info.isWiki, info.isText, info.needLinks, info.isPhoneNumber, info.collapsable, info.collapsableView, info.textColor, info.isWiki, info.isText,
info.needLinks, info.isPhoneNumber,
info.isUrl, info.matchWidthDivider, info.textLinesLimit); info.isUrl, info.matchWidthDivider, info.textLinesLimit);
} else { } else {
buildRow(view, info.iconId, info.text, info.textPrefix, info.collapsable, info.collapsableView, buildRow(view, info.iconId, info.text, info.textPrefix, info.socialMediaUrl,
info.textColor, info.isWiki, info.isText, info.needLinks, info.isPhoneNumber, info.collapsable, info.collapsableView, info.textColor, info.isWiki, info.isText,
info.needLinks, info.isPhoneNumber,
info.isUrl, info.matchWidthDivider, info.textLinesLimit); info.isUrl, info.matchWidthDivider, info.textLinesLimit);
} }
} }
@ -892,12 +908,45 @@ public class AmenityMenuBuilder extends MenuBuilder {
return new CollapsableView(view, this, collapsed); return new CollapsableView(view, this, collapsed);
} }
private String getSocialMediaUrl(String key, String value) {
// Remove leading and closing slashes
StringBuilder sb = new StringBuilder(value.trim());
if (sb.charAt(0) == '/') {
sb.deleteCharAt(0);
}
int lastIdx = sb.length() - 1;
if (sb.charAt(lastIdx) == '/') {
sb.deleteCharAt(lastIdx);
}
// It cannot be username
if (sb.indexOf("/") != -1) {
return "https://" + value;
}
Map<String, String> urls = new HashMap<>(7);
urls.put("facebook", "https://facebook.com/%s");
urls.put("vk", "https://vk.com/%s");
urls.put("instagram", "https://instagram.com/%s");
urls.put("twitter", "https://twitter.com/%s");
urls.put("ok", "https://ok.ru/%s");
urls.put("telegram", "https://t.me/%s");
urls.put("flickr", "https://flickr.com/%s");
if (urls.containsKey(key)) {
return String.format(urls.get(key), value);
} else {
return null;
}
}
private static class AmenityInfoRow { private static class AmenityInfoRow {
private String key; private String key;
private Drawable icon; private Drawable icon;
private int iconId; private int iconId;
private String textPrefix; private String textPrefix;
private String text; private String text;
private String socialMediaUrl;
private CollapsableView collapsableView; private CollapsableView collapsableView;
private boolean collapsable; private boolean collapsable;
private int textColor; private int textColor;
@ -912,14 +961,16 @@ public class AmenityMenuBuilder extends MenuBuilder {
private int textLinesLimit; private int textLinesLimit;
public AmenityInfoRow(String key, Drawable icon, String textPrefix, String text, public AmenityInfoRow(String key, Drawable icon, String textPrefix, String text,
boolean collapsable, CollapsableView collapsableView, String socialMediaUrl, boolean collapsable,
int textColor, boolean isWiki, boolean isText, boolean needLinks, CollapsableView collapsableView, int textColor, boolean isWiki,
int order, String name, boolean isPhoneNumber, boolean isUrl, boolean isText, boolean needLinks, int order, String name,
boolean isPhoneNumber, boolean isUrl,
boolean matchWidthDivider, int textLinesLimit) { boolean matchWidthDivider, int textLinesLimit) {
this.key = key; this.key = key;
this.icon = icon; this.icon = icon;
this.textPrefix = textPrefix; this.textPrefix = textPrefix;
this.text = text; this.text = text;
this.socialMediaUrl = socialMediaUrl;
this.collapsable = collapsable; this.collapsable = collapsable;
this.collapsableView = collapsableView; this.collapsableView = collapsableView;
this.textColor = textColor; this.textColor = textColor;
@ -935,14 +986,16 @@ public class AmenityMenuBuilder extends MenuBuilder {
} }
public AmenityInfoRow(String key, int iconId, String textPrefix, String text, public AmenityInfoRow(String key, int iconId, String textPrefix, String text,
boolean collapsable, CollapsableView collapsableView, String socialMediaUrl, boolean collapsable,
int textColor, boolean isWiki, boolean isText, boolean needLinks, CollapsableView collapsableView, int textColor, boolean isWiki,
int order, String name, boolean isPhoneNumber, boolean isUrl, boolean isText, boolean needLinks, int order, String name,
boolean isPhoneNumber, boolean isUrl,
boolean matchWidthDivider, int textLinesLimit) { boolean matchWidthDivider, int textLinesLimit) {
this.key = key; this.key = key;
this.iconId = iconId; this.iconId = iconId;
this.textPrefix = textPrefix; this.textPrefix = textPrefix;
this.text = text; this.text = text;
this.socialMediaUrl = socialMediaUrl;
this.collapsable = collapsable; this.collapsable = collapsable;
this.collapsableView = collapsableView; this.collapsableView = collapsableView;
this.textColor = textColor; this.textColor = textColor;