Merge pull request #7154 from osmandapp/Fix_7106

Fix #7106
This commit is contained in:
vshcherb 2019-07-04 16:42:15 +02:00 committed by GitHub
commit eba7918414
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 47 deletions

View file

@ -317,6 +317,8 @@ public class GPXUtilities {
public static class Metadata extends GPXExtensions {
public String desc;
public String link;
public String getArticleTitle() {
return getExtensionsToRead().get("article_title");
}
@ -1404,6 +1406,7 @@ public class GPXUtilities {
writeNotNullText(serializer, "name", trackName);
if (file.metadata != null) {
writeNotNullText(serializer, "desc", file.metadata.desc);
writeNotNullTextWithAttribute(serializer, "link", "href", file.metadata.link);
writeExtensions(serializer, file.metadata);
}
serializer.endTag(null, "metadata");
@ -1472,6 +1475,14 @@ public class GPXUtilities {
return path;
}
private static void writeNotNullTextWithAttribute(XmlSerializer serializer, String tag, String attribute, String value) throws IOException {
if (value != null) {
serializer.startTag(null, tag);
serializer.attribute(null, attribute, value);
serializer.endTag(null, tag);
}
}
private static void writeNotNullText(XmlSerializer serializer, String tag, String value) throws IOException {
if (value != null) {
serializer.startTag(null, tag);
@ -1502,11 +1513,7 @@ public class GPXUtilities {
}
writeNotNullText(serializer, "name", p.name);
writeNotNullText(serializer, "desc", p.desc);
if (p.link != null) {
serializer.startTag(null, "link");
serializer.attribute(null, "href", p.link);
serializer.endTag(null, "link");
}
writeNotNullTextWithAttribute(serializer, "link", "href", p.link);
writeNotNullText(serializer, "type", p.category);
if (p.comment != null) {
writeNotNullText(serializer, "cmt", p.comment);
@ -1697,6 +1704,9 @@ public class GPXUtilities {
if (tag.equals("desc")) {
((Metadata) parse).desc = readText(parser, "desc");
}
if (tag.equals("link")) {
((Metadata) parse).link = parser.getAttributeValue("", "href");
}
} else if (parse instanceof Route) {
if (tag.equals("name")) {
((Route) parse).name = readText(parser, "name");

View file

@ -32,15 +32,15 @@ import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.PicassoUtils;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.data.QuadRect;
import net.osmand.plus.GPXDatabase;
import net.osmand.plus.GPXDatabase.GpxDataItem;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayGroup;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType;
@ -64,6 +64,7 @@ import net.osmand.render.RenderingRulesStorage;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import gnu.trove.list.array.TIntArrayList;
@ -459,13 +460,60 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
return createTravelArticleCard(context, article);
}
if (!TextUtils.isEmpty(gpx.metadata.desc)) {
return createDescriptionCard(context, gpx.metadata.desc);
final String description = getMetadataDescription(gpx.metadata);
if (!TextUtils.isEmpty(description)) {
String link = getMetadataImageLink(gpx.metadata);
if (!TextUtils.isEmpty(link)) {
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
openGpxDescriptionDialog(description);
}
};
return createArticleCard(context, null, description, null, link, onClickListener);
} else {
return createDescriptionCard(context, description);
}
}
return null;
}
@Nullable
private String getMetadataDescription(@NonNull GPXUtilities.Metadata metadata) {
String descHtml = metadata.desc;
if (TextUtils.isEmpty(descHtml)) {
Map<String, String> extensions = metadata.getExtensionsToRead();
if (!extensions.isEmpty() && extensions.containsKey("desc")) {
descHtml = extensions.get("desc");
}
}
if (descHtml != null) {
String content = WikiArticleHelper.getPartialContent(descHtml);
if (!TextUtils.isEmpty(content)) {
return content;
}
}
return descHtml;
}
@Nullable
private String getMetadataImageLink(@NonNull GPXUtilities.Metadata metadata) {
String link = metadata.link;
if (!TextUtils.isEmpty(link)) {
String lowerCaseLink = link.toLowerCase();
if (lowerCaseLink.contains(".jpg")
|| lowerCaseLink.contains(".jpeg")
|| lowerCaseLink.contains(".png")
|| lowerCaseLink.contains(".bmp")
|| lowerCaseLink.contains(".webp")) {
return link;
}
}
return null;
}
@Nullable
private TravelArticle getTravelArticle(@NonNull GPXUtilities.Metadata metadata) {
String title = metadata.getArticleTitle();
@ -482,34 +530,11 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
}
private View createTravelArticleCard(final Context context, @NonNull final TravelArticle article) {
View card = LayoutInflater.from(context).inflate(R.layout.wikivoyage_article_card, null);
card.findViewById(R.id.background_view).setBackgroundColor(ContextCompat.getColor(context,
app.getSettings().isLightContent() ? R.color.list_item_light : R.color.list_item_dark));
((TextView) card.findViewById(R.id.title)).setText(article.getTitle());
((TextView) card.findViewById(R.id.content)).setText(WikiArticleHelper.getPartialContent(article.getContent()));
((TextView) card.findViewById(R.id.part_of)).setText(article.getGeoDescription());
final ImageView icon = (ImageView) card.findViewById(R.id.icon);
final String url = TravelArticle.getImageUrl(article.getImageTitle(), false);
final PicassoUtils picassoUtils = PicassoUtils.getPicasso(app);
RequestCreator rc = Picasso.get().load(url);
WikivoyageUtils.setupNetworkPolicy(app.getSettings(), rc);
rc.transform(new CropCircleTransformation())
.into(icon, new Callback() {
@Override
public void onSuccess() {
icon.setVisibility(View.VISIBLE);
picassoUtils.setResultLoaded(url, true);
}
@Override
public void onError(Exception e) {
picassoUtils.setResultLoaded(url, false);
}
});
TextView readBtn = (TextView) card.findViewById(R.id.left_button);
readBtn.setText(app.getString(R.string.shared_string_read));
readBtn.setCompoundDrawablesWithIntrinsicBounds(getReadIcon(), null, null, null);
readBtn.setOnClickListener(new View.OnClickListener() {
String title = article.getTitle();
String content = WikiArticleHelper.getPartialContent(article.getContent());
String geoDescription = article.getGeoDescription();
String imageUrl = TravelArticle.getImageUrl(article.getImageTitle(), false);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TrackActivity activity = getTrackActivity();
@ -518,7 +543,44 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
activity.getSupportFragmentManager(), article.getTripId(), article.getLang());
}
}
});
};
return createArticleCard(context, title, content, geoDescription, imageUrl, onClickListener);
}
private View createArticleCard(final Context context, String title, String content, String geoDescription, final String imageUrl, View.OnClickListener readBtnClickListener) {
View card = LayoutInflater.from(context).inflate(R.layout.wikivoyage_article_card, null);
card.findViewById(R.id.background_view).setBackgroundColor(ContextCompat.getColor(context,
app.getSettings().isLightContent() ? R.color.list_item_light : R.color.list_item_dark));
if (!TextUtils.isEmpty(title)) {
((TextView) card.findViewById(R.id.title)).setText(title);
} else {
card.findViewById(R.id.title).setVisibility(View.GONE);
}
((TextView) card.findViewById(R.id.content)).setText(content);
((TextView) card.findViewById(R.id.part_of)).setText(geoDescription);
final ImageView icon = (ImageView) card.findViewById(R.id.icon);
final PicassoUtils picassoUtils = PicassoUtils.getPicasso(app);
RequestCreator rc = Picasso.get().load(imageUrl);
WikivoyageUtils.setupNetworkPolicy(app.getSettings(), rc);
rc.transform(new CropCircleTransformation())
.into(icon, new Callback() {
@Override
public void onSuccess() {
icon.setVisibility(View.VISIBLE);
picassoUtils.setResultLoaded(imageUrl, true);
}
@Override
public void onError(Exception e) {
picassoUtils.setResultLoaded(imageUrl, false);
}
});
TextView readBtn = (TextView) card.findViewById(R.id.left_button);
readBtn.setText(app.getString(R.string.shared_string_read));
readBtn.setCompoundDrawablesWithIntrinsicBounds(getReadIcon(), null, null, null);
readBtn.setOnClickListener(readBtnClickListener);
card.findViewById(R.id.right_button).setVisibility(View.GONE);
card.findViewById(R.id.divider).setVisibility(View.GONE);
card.findViewById(R.id.list_item_divider).setVisibility(View.VISIBLE);
@ -537,14 +599,7 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
readBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TrackActivity activity = getTrackActivity();
if (activity != null) {
Bundle args = new Bundle();
args.putString(GpxDescriptionDialogFragment.CONTENT_KEY, descHtml);
GpxDescriptionDialogFragment fragment = new GpxDescriptionDialogFragment();
fragment.setArguments(args);
fragment.show(activity.getSupportFragmentManager(), GpxDescriptionDialogFragment.TAG);
}
openGpxDescriptionDialog(descHtml);
}
});
return card;
@ -552,6 +607,17 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
return null;
}
private void openGpxDescriptionDialog(@NonNull final String descHtml) {
TrackActivity activity = getTrackActivity();
if (activity != null) {
Bundle args = new Bundle();
args.putString(GpxDescriptionDialogFragment.CONTENT_KEY, descHtml);
GpxDescriptionDialogFragment fragment = new GpxDescriptionDialogFragment();
fragment.setArguments(args);
fragment.show(activity.getSupportFragmentManager(), GpxDescriptionDialogFragment.TAG);
}
}
public boolean isGpxFileSelected(GPXFile gpxFile) {
return gpxFile != null &&
((gpxFile.showCurrentTrack && app.getSelectedGpxHelper().getSelectedCurrentRecordingTrack() != null) ||