From ff0d6a535c11d832f0f2c584b63020673c5dffd4 Mon Sep 17 00:00:00 2001 From: cepprice Date: Wed, 3 Feb 2021 21:28:22 +0500 Subject: [PATCH] Add HTML support --- .../builders/FavouritePointMenuBuilder.java | 22 ++++++-- .../osmand/plus/views/layers/POIMapLayer.java | 53 +++++++++++++------ .../menu/WikivoyageWptPtMenuBuilder.java | 20 +++++-- 3 files changed, 73 insertions(+), 22 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java index ca404569b1..48b8478e5a 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/FavouritePointMenuBuilder.java @@ -22,7 +22,9 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.CollapsableView; import net.osmand.plus.myplaces.FavoritesActivity; +import net.osmand.plus.views.layers.POIMapLayer; import net.osmand.plus.widgets.TextViewEx; +import net.osmand.plus.wikipedia.WikiArticleHelper; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -82,11 +84,23 @@ public class FavouritePointMenuBuilder extends MenuBuilder { } @Override - protected void buildDescription(View view) { - String desc = fav.getDescription(); - if (!Algorithms.isEmpty(desc)) { - buildDescriptionRow(view, app.getString(R.string.shared_string_description), desc, 0, 10, true); + protected void buildDescription(final View view) { + final String desc = fav.getDescription(); + if (Algorithms.isEmpty(desc)) { + return; } + + final String textPrefix = app.getString(R.string.shared_string_description); + View.OnClickListener clickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + POIMapLayer.showHtmlDescriptionDialog(view.getContext(), app, desc, textPrefix); + } + }; + + buildRow(view, null, null, textPrefix, WikiArticleHelper.getPartialContent(desc), 0, + null, false, null, true, 10, + false, false, false, clickListener, true); } private void buildGroupFavouritesView(View view) { diff --git a/OsmAnd/src/net/osmand/plus/views/layers/POIMapLayer.java b/OsmAnd/src/net/osmand/plus/views/layers/POIMapLayer.java index 13352a81ee..dc2d6b794c 100644 --- a/OsmAnd/src/net/osmand/plus/views/layers/POIMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/layers/POIMapLayer.java @@ -3,9 +3,11 @@ package net.osmand.plus.views.layers; import android.app.Dialog; import android.content.Context; import android.graphics.Canvas; +import android.graphics.Color; import android.graphics.PointF; import android.graphics.drawable.Drawable; import android.text.util.Linkify; +import android.util.Base64; import android.util.TypedValue; import android.view.View; import android.view.ViewGroup; @@ -41,6 +43,7 @@ import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.layers.MapTextLayer.MapTextProvider; +import net.osmand.plus.widgets.WebViewEx; import net.osmand.util.Algorithms; import java.util.ArrayList; @@ -272,7 +275,39 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon } public static void showDescriptionDialog(Context ctx, OsmandApplication app, String text, String title) { - showText(ctx, app, text, title); + final TextView textView = new TextView(ctx); + LinearLayout.LayoutParams llTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + int textMargin = dpToPx(app, 10f); + boolean light = app.getSettings().isLightContent(); + textView.setLayoutParams(llTextParams); + textView.setPadding(textMargin, textMargin, textMargin, textMargin); + textView.setTextSize(16); + textView.setTextColor(ContextCompat.getColor(app, light ? R.color.text_color_primary_light : R.color.text_color_primary_dark)); + textView.setAutoLinkMask(Linkify.ALL); + textView.setLinksClickable(true); + textView.setText(text); + + showText(ctx, app, textView, title); + } + + public static void showHtmlDescriptionDialog(Context ctx, OsmandApplication app, String html, String title) { + final WebViewEx webView = new WebViewEx(ctx); + LinearLayout.LayoutParams llTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); + webView.setLayoutParams(llTextParams); + int margin = dpToPx(app, 10f); + webView.setPadding(margin, margin, margin, margin); + webView.setScrollbarFadingEnabled(true); + webView.setVerticalScrollBarEnabled(false); + webView.setBackgroundColor(Color.TRANSPARENT); + webView.getSettings().setTextZoom((int) (app.getResources().getConfiguration().fontScale * 100f)); + boolean light = app.getSettings().isLightContent(); + int textColor = ContextCompat.getColor(app, light ? R.color.text_color_primary_light : R.color.text_color_primary_dark); + String rgbHex = Integer.toHexString(textColor).substring(2, 8); + html = "" + html + ""; + String encoded = Base64.encodeToString(html.getBytes(), Base64.NO_PADDING); + webView.loadData(encoded, "text/html", "base64"); + + showText(ctx, app, webView, title); } static int getResIdFromAttribute(final Context ctx, final int attr) { @@ -284,7 +319,7 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon return typedvalueattr.resourceId; } - private static void showText(final Context ctx, final OsmandApplication app, final String text, String title) { + private static void showText(final Context ctx, final OsmandApplication app, final View view, String title) { final Dialog dialog = new Dialog(ctx, app.getSettings().isLightContent() ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme); @@ -306,24 +341,12 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon } }); - final TextView textView = new TextView(ctx); - LinearLayout.LayoutParams llTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - int textMargin = dpToPx(app, 10f); - boolean light = app.getSettings().isLightContent(); - textView.setLayoutParams(llTextParams); - textView.setPadding(textMargin, textMargin, textMargin, textMargin); - textView.setTextSize(16); - textView.setTextColor(ContextCompat.getColor(app, light ? R.color.text_color_primary_light : R.color.text_color_primary_dark)); - textView.setAutoLinkMask(Linkify.ALL); - textView.setLinksClickable(true); - textView.setText(text); - ScrollView scrollView = new ScrollView(ctx); ll.addView(topBar); LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0); lp.weight = 1; ll.addView(scrollView, lp); - scrollView.addView(textView); + scrollView.addView(view); dialog.setContentView(ll); dialog.setCancelable(true); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/menu/WikivoyageWptPtMenuBuilder.java b/OsmAnd/src/net/osmand/plus/wikivoyage/menu/WikivoyageWptPtMenuBuilder.java index 9bc991422d..387fbfeb85 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/menu/WikivoyageWptPtMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/menu/WikivoyageWptPtMenuBuilder.java @@ -8,6 +8,8 @@ import net.osmand.GPXUtilities.WptPt; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.builders.WptPtMenuBuilder; +import net.osmand.plus.views.layers.POIMapLayer; +import net.osmand.plus.wikipedia.WikiArticleHelper; import net.osmand.util.Algorithms; import java.util.HashMap; @@ -29,11 +31,23 @@ public class WikivoyageWptPtMenuBuilder extends WptPtMenuBuilder { } @Override - protected void buildDescription(View view) { + protected void buildDescription(final View view) { final String desc = descTokens.get(KEY_DESCRIPTION); - if (!Algorithms.isEmpty(desc)) { - buildDescriptionRow(view, app.getString(R.string.shared_string_description), desc, 0, 10, true); + if (Algorithms.isEmpty(desc)) { + return; } + + final String textPrefix = app.getString(R.string.shared_string_description); + View.OnClickListener clickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + POIMapLayer.showHtmlDescriptionDialog(view.getContext(), app, desc, textPrefix); + } + }; + + buildRow(view, null, null, textPrefix, WikiArticleHelper.getPartialContent(desc), 0, + null, false, null, true, 10, + false, false, false, clickListener, true); } @Override