Add 'Read Full' button to description in menu

This commit is contained in:
cepprice 2021-02-04 13:16:00 +05:00
parent ff0d6a535c
commit 7a089f367d
5 changed files with 164 additions and 96 deletions

View file

@ -28,13 +28,6 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.data.Amenity;
@ -74,6 +67,7 @@ import net.osmand.plus.views.layers.POIMapLayer;
import net.osmand.plus.views.layers.TransportStopsLayer;
import net.osmand.plus.widgets.TextViewEx;
import net.osmand.plus.widgets.tools.ClickableSpanTouchListener;
import net.osmand.plus.wikipedia.WikiArticleHelper;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
@ -89,6 +83,13 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import static net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask.GetImageCardsListener;
public class MenuBuilder {
@ -801,18 +802,98 @@ public class MenuBuilder {
return ll;
}
public View buildDescriptionRow(final View view, final String textPrefix, final String description, int textColor,
int textLinesLimit, boolean matchWidthDivider) {
OnClickListener clickListener = new OnClickListener() {
public View buildDescriptionRow(final View view, final String description) {
final String descriptionLabel = app.getString(R.string.shared_string_description);
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showDescriptionDialog(view.getContext(), app, description, textPrefix);
POIMapLayer.showHtmlDescriptionDialog(view.getContext(), app, description, descriptionLabel);
}
};
return buildRow(view, null, null, textPrefix, description, textColor,
null, false, null, true, textLinesLimit,
false, false, false, clickListener, matchWidthDivider);
if (!isFirstRow()) {
buildRowDivider(view);
}
LinearLayout baseView = new LinearLayout(view.getContext());
baseView.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams llBaseViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
baseView.setLayoutParams(llBaseViewParams);
LinearLayout ll = new LinearLayout(view.getContext());
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(llParams);
ll.setBackgroundResource(AndroidUtils.resolveAttribute(view.getContext(), android.R.attr.selectableItemBackground));
ll.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
copyToClipboard(description, view.getContext());
return true;
}
});
baseView.addView(ll);
// Text
LinearLayout llText = new LinearLayout(view.getContext());
llText.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams llTextViewParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
llTextViewParams.weight = 1f;
AndroidUtils.setMargins(llTextViewParams, 0, 0, dpToPx(10f), 0);
llTextViewParams.gravity = Gravity.CENTER_VERTICAL;
llText.setLayoutParams(llTextViewParams);
ll.addView(llText);
// Description label
TextViewEx textPrefixView = new TextViewEx(view.getContext());
LinearLayout.LayoutParams llTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
AndroidUtils.setMargins(llTextParams, dpToPx(16f), dpToPx(8f), 0, 0);
textPrefixView.setLayoutParams(llTextParams);
textPrefixView.setTypeface(FontCache.getRobotoRegular(view.getContext()));
textPrefixView.setTextSize(12);
textPrefixView.setTextColor(app.getResources().getColor(light ? R.color.text_color_secondary_light : R.color.text_color_secondary_dark));
textPrefixView.setMinLines(1);
textPrefixView.setMaxLines(1);
textPrefixView.setText(descriptionLabel);
llText.addView(textPrefixView);
// Description
TextViewEx textView = new TextViewEx(view.getContext());
LinearLayout.LayoutParams llDescriptionParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
AndroidUtils.setMargins(llDescriptionParams, dpToPx(16f), dpToPx(2f), 0, dpToPx(8f));
textView.setLayoutParams(llDescriptionParams);
textView.setTypeface(FontCache.getRobotoRegular(view.getContext()));
textView.setTextSize(16);
textView.setTextColor(app.getResources().getColor(light ? R.color.text_color_primary_light : R.color.text_color_primary_dark));
textView.setText(WikiArticleHelper.getPartialContent(description));
if (Linkify.addLinks(textView, Linkify.ALL)) {
textView.setMovementMethod(null);
int linkTextColor = ContextCompat.getColor(view.getContext(), light ?
R.color.ctx_menu_bottom_view_url_color_light : R.color.ctx_menu_bottom_view_url_color_dark);
textView.setLinkTextColor(linkTextColor);
textView.setOnTouchListener(new ClickableSpanTouchListener());
AndroidUtils.removeLinkUnderline(textView);
}
textView.setMinLines(1);
textView.setMaxLines(10);
textView.setEllipsize(TextUtils.TruncateAt.END);
llText.addView(textView);
// Read Full button
buildReadFullButton(llText, app.getString(R.string.context_menu_read_full), onClickListener);
if (onClickListener != null) {
ll.setOnClickListener(onClickListener);
}
((LinearLayout) view).addView(baseView);
rowBuilt();
setDividerWidth(true);
return ll;
}
protected void showDialog(String text, final String actionType, final String dataPrefix, final View v) {
@ -908,6 +989,38 @@ public class MenuBuilder {
((LinearLayout) view).addView(horizontalLine);
}
protected void buildReadFullButton(LinearLayout container, String btnText, View.OnClickListener onClickListener) {
Context ctx = container.getContext();
TextViewEx button = new TextViewEx(new ContextThemeWrapper(ctx, light ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme));
LinearLayout.LayoutParams llButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dpToPx(36f));
AndroidUtils.setMargins(llButtonParams, dpToPx(16f), 0, 0, dpToPx(16f));
button.setLayoutParams(llButtonParams);
button.setTypeface(FontCache.getRobotoMedium(app));
button.setBackgroundResource(light ? R.drawable.context_menu_controller_bg_light : R.drawable.context_menu_controller_bg_dark);
button.setTextSize(14);
int paddingSides = dpToPx(10f);
button.setPadding(paddingSides, 0, paddingSides, 0);
ColorStateList buttonColorStateList = AndroidUtils.createPressedColorStateList(ctx, !light,
R.color.ctx_menu_controller_button_text_color_light_n, R.color.ctx_menu_controller_button_text_color_light_p,
R.color.ctx_menu_controller_button_text_color_dark_n, R.color.ctx_menu_controller_button_text_color_dark_p);
button.setTextColor(buttonColorStateList);
button.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
button.setSingleLine(true);
button.setEllipsize(TextUtils.TruncateAt.END);
button.setOnClickListener(onClickListener);
button.setAllCaps(true);
button.setText(btnText);
Drawable normal = app.getUIUtilities().getIcon(R.drawable.ic_action_read_text,
light ? R.color.ctx_menu_controller_button_text_color_light_n : R.color.ctx_menu_controller_button_text_color_dark_n);
Drawable pressed = app.getUIUtilities().getIcon(R.drawable.ic_action_read_text,
light ? R.color.ctx_menu_controller_button_text_color_light_p : R.color.ctx_menu_controller_button_text_color_dark_p);
AndroidUtils.setCompoundDrawablesWithIntrinsicBounds(button, Build.VERSION.SDK_INT >= 21
? AndroidUtils.createPressedStateListDrawable(normal, pressed) : normal, null, null, null);
button.setCompoundDrawablePadding(dpToPx(8f));
container.addView(button);
}
public boolean hasCustomAddressLine() {
return false;
}

View file

@ -2,10 +2,8 @@ package net.osmand.plus.mapcontextmenu.builders;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.view.Gravity;
@ -15,10 +13,6 @@ import android.widget.ImageView;
import android.widget.LinearLayout;
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.PlatformUtil;
import net.osmand.data.Amenity;
@ -31,7 +25,6 @@ import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.helpers.enums.MetricsConstants;
import net.osmand.plus.mapcontextmenu.CollapsableView;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
@ -67,6 +60,9 @@ import java.util.Locale;
import java.util.Map;
import java.util.Set;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
public class AmenityMenuBuilder extends MenuBuilder {
private static final String WIKI_LINK = ".wikipedia.org/w";
@ -259,38 +255,12 @@ public class AmenityMenuBuilder extends MenuBuilder {
}
if (isWiki) {
TextViewEx button = new TextViewEx(new ContextThemeWrapper(view.getContext(), light ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme));
LinearLayout.LayoutParams llWikiButtonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dpToPx(36f));
AndroidUtils.setMargins(llWikiButtonParams, dpToPx(16f), 0, 0, dpToPx(16f));
button.setLayoutParams(llWikiButtonParams);
button.setTypeface(FontCache.getRobotoMedium(app));
button.setBackgroundResource(light ? R.drawable.context_menu_controller_bg_light : R.drawable.context_menu_controller_bg_dark);
button.setTextSize(14);
int paddingSides = dpToPx(10f);
button.setPadding(paddingSides, 0, paddingSides, 0);
ColorStateList buttonColorStateList = AndroidUtils.createPressedColorStateList(view.getContext(), !light,
R.color.ctx_menu_controller_button_text_color_light_n, R.color.ctx_menu_controller_button_text_color_light_p,
R.color.ctx_menu_controller_button_text_color_dark_n, R.color.ctx_menu_controller_button_text_color_dark_p);
button.setTextColor(buttonColorStateList);
button.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
button.setSingleLine(true);
button.setEllipsize(TextUtils.TruncateAt.END);
button.setOnClickListener(new View.OnClickListener() {
buildReadFullButton(llText, app.getString(R.string.context_menu_read_full_article), new View.OnClickListener() {
@Override
public void onClick(View view) {
WikipediaDialogFragment.showInstance(mapActivity, amenity);
}
});
button.setAllCaps(true);
button.setText(R.string.context_menu_read_full_article);
Drawable normal = app.getUIUtilities().getIcon(R.drawable.ic_action_read_text,
light ? R.color.ctx_menu_controller_button_text_color_light_n : R.color.ctx_menu_controller_button_text_color_dark_n);
Drawable pressed = app.getUIUtilities().getIcon(R.drawable.ic_action_read_text,
light ? R.color.ctx_menu_controller_button_text_color_light_p : R.color.ctx_menu_controller_button_text_color_dark_p);
AndroidUtils.setCompoundDrawablesWithIntrinsicBounds(button, Build.VERSION.SDK_INT >= 21
? AndroidUtils.createPressedStateListDrawable(normal, pressed) : normal, null, null, null);
button.setCompoundDrawablePadding(dpToPx(8f));
llText.addView(button);
}
((LinearLayout) view).addView(baseView);

View file

@ -4,8 +4,6 @@ import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
@ -19,18 +17,18 @@ import net.osmand.osm.PoiCategory;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.CollapsableView;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
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;
import java.io.IOException;
import java.util.List;
import androidx.annotation.NonNull;
public class FavouritePointMenuBuilder extends MenuBuilder {
private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(FavouritePointMenuBuilder.class);
@ -84,23 +82,11 @@ public class FavouritePointMenuBuilder extends MenuBuilder {
}
@Override
protected void buildDescription(final View view) {
final String desc = fav.getDescription();
if (Algorithms.isEmpty(desc)) {
return;
protected void buildDescription(View view) {
String desc = fav.getDescription();
if (!Algorithms.isEmpty(desc)) {
buildDescriptionRow(view, desc);
}
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) {

View file

@ -4,10 +4,6 @@ import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.IndexConstants;
@ -30,6 +26,10 @@ import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
public class WptPtMenuBuilder extends MenuBuilder {
private final WptPt wpt;
@ -52,10 +52,22 @@ public class WptPtMenuBuilder extends MenuBuilder {
}
@Override
protected void buildDescription(View view) {
if (!Algorithms.isEmpty(wpt.desc)) {
buildDescriptionRow(view, app.getString(R.string.shared_string_description), wpt.desc, 0, 10, true);
protected void buildDescription(final View view) {
if (Algorithms.isEmpty(wpt.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.showDescriptionDialog(view.getContext(), app, wpt.desc, textPrefix);
}
};
buildRow(view, null, null, textPrefix, wpt.desc, 0,
null, false, null, true, 10,
false, false, false, clickListener, matchWidthDivider);
}
@Override

View file

@ -2,18 +2,16 @@ package net.osmand.plus.wikivoyage.menu;
import android.view.View;
import androidx.annotation.NonNull;
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;
import androidx.annotation.NonNull;
public class WikivoyageWptPtMenuBuilder extends WptPtMenuBuilder {
private final static String KEY_PHONE = "Phone: ";
@ -31,24 +29,13 @@ public class WikivoyageWptPtMenuBuilder extends WptPtMenuBuilder {
}
@Override
protected void buildDescription(final View view) {
final String desc = descTokens.get(KEY_DESCRIPTION);
if (Algorithms.isEmpty(desc)) {
return;
protected void buildDescription(View view) {
String desc = descTokens.get(KEY_DESCRIPTION);
if (!Algorithms.isEmpty(desc)) {
buildDescriptionRow(view, desc);
}
}
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
protected void prepareDescription(final WptPt wpt, View view) {