remove regEx parsing

improve and fix
This commit is contained in:
Nazar 2019-07-15 19:07:41 +03:00 committed by crimean
parent 108b2f6730
commit 1b184ad999
7 changed files with 204 additions and 211 deletions

View file

@ -684,34 +684,10 @@ public class MenuBuilder {
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
//todo extract to MenuBuilder method
//the same logic uses for AmenityMenuBuilder and can be generalized
final String[] phones = text.split(",|;");
for (int i = 0; i < phones.length; i++) {
phones[i] = phones[i].trim();
}
if (phones.length > 1) {
AlertDialog.Builder dlg = new AlertDialog.Builder(v.getContext());
dlg.setNegativeButton(R.string.shared_string_cancel, null);
dlg.setItems(phones, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phones[which]));
v.getContext().startActivity(intent);
showDialog(text, Intent.ACTION_DIAL, "tel:", v);
}
});
dlg.show();
} else {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + text));
v.getContext().startActivity(intent);
}
}
});
} else if (isEmail) { //todo the same for the phones
} else if (isEmail) {
ll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
@ -731,6 +707,29 @@ public class MenuBuilder {
return ll;
}
protected void showDialog(String text, final String actionType, final String dataPrefix, final View v) {
final String[] items = text.split("[,;]");
final Intent intent = new Intent(actionType);
if (items.length > 1) {
for (int i = 0; i < items.length; i++) {
items[i] = items[i].trim();
}
AlertDialog.Builder dlg = new AlertDialog.Builder(v.getContext());
dlg.setNegativeButton(R.string.shared_string_cancel, null);
dlg.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
intent.setData(Uri.parse(dataPrefix + items[which]));
v.getContext().startActivity(intent);
}
});
dlg.show();
} else {
intent.setData(Uri.parse(dataPrefix + text));
v.getContext().startActivity(intent);
}
}
protected void setDividerWidth(boolean matchWidthDivider) {
this.matchWidthDivider = matchWidthDivider;
}

View file

@ -198,7 +198,7 @@ public abstract class MenuController extends BaseMenuController implements Colla
} else if (object instanceof OsmPoint) {
menuController = new EditPOIMenuController(mapActivity, pointDescription, (OsmPoint) object);
} else if (object instanceof WptPt) {
menuController = new WptPtMenuController(mapActivity, pointDescription, (WptPt) object);
menuController = WptPtMenuController.getInstance(mapActivity, pointDescription, (WptPt) object);
} else if (object instanceof DownloadMapObject) {
menuController = new MapDataMenuController(mapActivity, pointDescription, (DownloadMapObject) object);
} else if (object instanceof OpenStreetNote) {

View file

@ -282,24 +282,7 @@ public class AmenityMenuBuilder extends MenuBuilder {
ll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
final String[] phones = text.split(",|;");
if (phones.length > 1) {
AlertDialog.Builder dlg = new AlertDialog.Builder(v.getContext());
dlg.setNegativeButton(R.string.shared_string_cancel, null);
dlg.setItems(phones, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phones[which]));
v.getContext().startActivity(intent);
}
});
dlg.show();
} else {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + text));
v.getContext().startActivity(intent);
}
showDialog(text, Intent.ACTION_DIAL, "tel:", v);
}
});
} else if (isUrl) {

View file

@ -26,20 +26,10 @@ import net.osmand.util.Algorithms;
import java.io.File;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WptPtMenuBuilder extends MenuBuilder {
//todo extract
final String KEY_PHONE = "Phone: ";
final String KEY_EMAIL = "Email: ";
final String PHONE_REGEX = "(\\(?\\+?[0-9]*\\)?)?[0-9_\\- \\(\\)]*";
final String EMAIL_REGEX = "([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})";
private final WptPt wpt;
public WptPtMenuBuilder(@NonNull MapActivity mapActivity, final @NonNull WptPt wpt) {
@ -82,45 +72,10 @@ public class WptPtMenuBuilder extends MenuBuilder {
false, null, false, 0, false, null, false);
}
String phoneToken = getDescriptionToken(wpt.desc, KEY_PHONE, PHONE_REGEX);
String emailToken = getDescriptionToken(wpt.desc, KEY_EMAIL, EMAIL_REGEX);
if (!Algorithms.isEmpty(wpt.desc)) {
prepareDescription(wpt, view);
}
final ArrayList<String> phones = findAllElementsInLine(phoneToken, PHONE_REGEX);
final ArrayList<String> emails = findAllElementsInLine(emailToken, EMAIL_REGEX);
final String desc = deleteAllElementsOccurrence(wpt.desc, phoneToken, emailToken);
if (!Algorithms.isEmpty(desc)) {
final View row = buildRow(view, R.drawable.ic_action_note_dark, null, desc, 0, false, null, true, 10, false, null, false);
//todo maybe delete
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showDescriptionDialog(row.getContext(), app, desc,
row.getResources().getString(R.string.shared_string_description));
}
});
}
if (phones != null) {
String phonesCommaLine = prepareCommaLine(phones);
if (!Algorithms.isEmpty(phonesCommaLine)) {
buildRow(view, R.drawable.ic_action_call_dark,
null, phonesCommaLine, 0,
false, null, false, 0, false, true, false, null, false);
}
}
if (!Algorithms.isEmpty(wpt.link)) {
buildRow(view, R.drawable.ic_world_globe_dark,
null, wpt.link, 0,
false, null, false, 0, true, null, false);
}
if (emails != null) {
String emailsCommaLine = prepareCommaLine(emails);
if (!Algorithms.isEmpty(emailsCommaLine)) {
buildRow(view, R.drawable.ic_action_message,
null, emailsCommaLine, 0,
false, null, false, 0, false, false, true, null, false);
}
}
if (!Algorithms.isEmpty(wpt.comment)) {
final View rowc = buildRow(view, R.drawable.ic_action_note_dark, null, wpt.comment, 0,
false, null, true, 10, false, null, false);
@ -136,95 +91,19 @@ public class WptPtMenuBuilder extends MenuBuilder {
buildPlainMenuItems(view);
}
//todo extract somewhere / improve algorithm
private String getDescriptionToken(String text, String key, String ... allowedElementsRegEx) {
final String END_TOKEN_REGEX = "( +)?[.,]";
final String SPACE_REGEX = "\\s+";
if (!Algorithms.isEmpty(text)) {
int startId, endId;
if (!Algorithms.isEmpty(key) && text.contains(key)) {
startId = text.indexOf(key);
endId = 0;
int finalIndex = text.indexOf(':', startId + key.length());
if (finalIndex > 0) {
text = text.substring(0, finalIndex);
protected void prepareDescription(final WptPt wpt, View view) {
if (!Algorithms.isEmpty(wpt.desc)) {
final View row = buildRow(view, R.drawable.ic_action_note_dark, null, wpt.desc, 0, false, null, true, 10, false, null, false);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showDescriptionDialog(row.getContext(), app, wpt.desc,
row.getResources().getString(R.string.shared_string_description));
}
for (String regEx : allowedElementsRegEx) {
ArrayList<String> items = findAllElementsInLine(text, regEx);
for (String item : items) {
int currentEnd = text.indexOf(item) + item.length();
if (endId < currentEnd) {
endId = currentEnd;
});
}
}
String endedText = text.substring(endId);
if (endedText.startsWith(SPACE_REGEX)) {
endId += findAllElementsInLine(endedText, SPACE_REGEX).get(0).length();
}
}
return text.substring(startId, endId);
}
}
return null;
}
//todo extract somewhere, maybe to Algorithms
private ArrayList<String> findAllElementsInLine(String text, String ... regExArgs) {
ArrayList<String> foundItems = new ArrayList<>();
if (!Algorithms.isEmpty(text)) {
for (String regEx : regExArgs) {
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(text);
while (m.find()) {
String item = m.group().trim();
if (!Algorithms.isEmpty(item)) {
foundItems.add(item);
}
}
}
return foundItems;
}
return null;
}
//todo extract somewhere, maybe to Algorithms / improve algorithm
private String deleteAllElementsOccurrence(String text, String ... args) {
if (text != null) {
for (String s : args) {
if (s != null) {
text = text.replace(s, "");
}
}
while (text.startsWith(".") && text.length() > 1) {
text = text.substring(1);
text = text.trim();
}
text = text.trim();
}
return text;
}
//todo extract somewhere, maybe to Algorithms
private String prepareCommaLine(List<String> items) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < items.size(); i++) {
String item = items.get(i);
if (!Algorithms.isEmpty(item)) {
sb.append(item);
if (i < items.size() - 1) {
sb.append(", ");
}
}
}
return sb.toString();
}
private void buildWaypointsView(View view) {
GpxSelectionHelper gpxSelectionHelper = app.getSelectedGpxHelper();
SelectedGpxFile selectedGpxFile = gpxSelectionHelper.getSelectedGPXFile(wpt);

View file

@ -1,10 +1,9 @@
package net.osmand.plus.mapcontextmenu.controllers;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import net.osmand.GPXUtilities;
import net.osmand.data.LatLon;
@ -14,26 +13,29 @@ import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapcontextmenu.builders.WptPtMenuBuilder;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.data.TravelArticle;
import net.osmand.plus.wikivoyage.menu.WikivoyageWptPtMenuBuilder;
import net.osmand.plus.wikivoyage.menu.WikivoyageWptPtMenuController;
import net.osmand.util.Algorithms;
import java.io.File;
import java.util.Map;
public class WptPtMenuController extends MenuController {
private WptPt wpt;
private MapMarker mapMarker;
private MapActivity mapActivity;
public WptPtMenuController(@NonNull MapActivity mapActivity, @NonNull PointDescription pointDescription, @NonNull final WptPt wpt) {
super(new WptPtMenuBuilder(mapActivity, wpt), pointDescription, mapActivity);
public WptPtMenuController(@NonNull MenuBuilder menuBuilder, @NonNull MapActivity mapActivity, @NonNull PointDescription pointDescription, @NonNull final WptPt wpt) {
super(menuBuilder, pointDescription, mapActivity);
this.wpt = wpt;
this.mapActivity = mapActivity;
final MapMarkersHelper markersHelper = mapActivity.getMyApplication().getMapMarkersHelper();
mapMarker = markersHelper.getMapMarker(wpt);
@ -45,32 +47,6 @@ public class WptPtMenuController extends MenuController {
leftTitleButtonController = markerMenuController.getLeftTitleButtonController();
rightTitleButtonController = markerMenuController.getRightTitleButtonController();
}
//todo extract / simplify
final OsmandApplication app = mapActivity.getMyApplication();
SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedGPXFile(wpt);
GPXUtilities.GPXFile gpxFile = selectedGpxFile != null ? selectedGpxFile.getGpxFile() : null;
GPXUtilities.Metadata metadata = gpxFile != null ? gpxFile.metadata : null;
final TravelArticle article = metadata != null ? getTravelArticle(metadata) : null;
if (article != null) {
leftTitleButtonController = new TitleButtonController() {
@Override
public void buttonPressed() {
WikivoyageArticleDialogFragment.showInstance(app, getMapActivity().getSupportFragmentManager(), article.getTripId(), article.getLang());
}
};
leftTitleButtonController.caption = mapActivity.getString(R.string.context_menu_read_article);
leftTitleButtonController.leftIconId = R.drawable.ic_action_read_text;
}
}
//todo extract somewhere, maybe to TravelDbHelper
private TravelArticle getTravelArticle(@NonNull GPXUtilities.Metadata metadata) {
String title = metadata.getArticleTitle();
String lang = metadata.getArticleLang();
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(lang)) {
return getMapActivity().getMyApplication().getTravelDbHelper().getArticle(title, lang);
}
return null;
}
@Override
@ -169,4 +145,16 @@ public class WptPtMenuController extends MenuController {
return "";
}
}
public static WptPtMenuController getInstance(@NonNull MapActivity mapActivity, @NonNull PointDescription pointDescription, @NonNull final WptPt wpt) {
SelectedGpxFile selectedGpxFile = mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedGPXFile(wpt);
GPXUtilities.GPXFile gpxFile = selectedGpxFile != null ? selectedGpxFile.getGpxFile() : null;
GPXUtilities.Metadata metadata = gpxFile != null ? gpxFile.metadata : null;
Map<String, String> extensions = metadata != null ? metadata.getExtensionsToRead() : null;
String metadataDesc = extensions != null ? metadata.getExtensionsToRead().get("desc") : null;
if (metadataDesc != null && metadataDesc.contains("wikivoyage.org/")) {
return new WikivoyageWptPtMenuController(new WikivoyageWptPtMenuBuilder(mapActivity, wpt), mapActivity, pointDescription, wpt);
}
return new WptPtMenuController(new WptPtMenuBuilder(mapActivity, wpt), mapActivity, pointDescription, wpt);
}
}

View file

@ -0,0 +1,98 @@
package net.osmand.plus.wikivoyage.menu;
import android.support.annotation.NonNull;
import android.view.View;
import net.osmand.GPXUtilities;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.builders.WptPtMenuBuilder;
import net.osmand.util.Algorithms;
import java.util.HashMap;
public class WikivoyageWptPtMenuBuilder extends WptPtMenuBuilder {
final String KEY_PHONE = "Phone: ";
final String KEY_EMAIL = "Email: ";
final String KEY_WORKING_HOURS = "Working hours: ";
final String KEY_PRICE = "Price: ";
final String KEY_DIRECTIONS = "Directions: ";
final String KEY_OTHER_DESCRIPTION = "Other description";
public WikivoyageWptPtMenuBuilder(@NonNull MapActivity mapActivity, @NonNull GPXUtilities.WptPt wpt) {
super(mapActivity, wpt);
}
@Override
protected void prepareDescription(final GPXUtilities.WptPt wpt, View view) {
String description = wpt.desc;
if (!description.contains("\n")) {
super.prepareDescription(wpt, view);
return;
}
HashMap<String, String> descTokens = getDescriptionTokens(description, KEY_PHONE, KEY_EMAIL, KEY_WORKING_HOURS, KEY_PRICE, KEY_DIRECTIONS);
String phones = descTokens.get(KEY_PHONE);
String emails = descTokens.get(KEY_EMAIL);
String workingHours = descTokens.get(KEY_WORKING_HOURS);
String price = descTokens.get(KEY_PRICE);
String direction = descTokens.get(KEY_DIRECTIONS);
final String desc = descTokens.get(KEY_OTHER_DESCRIPTION);
if (!Algorithms.isEmpty(desc)) {
buildRow(view, R.drawable.ic_action_note_dark, null, desc, 0, false, null, true, 10, false, null, false);
}
if (!Algorithms.isEmpty(phones)) {
buildRow(view, R.drawable.ic_action_call_dark,
null, phones, 0,
false, null, false, 0, false, true, false, null, false);
}
if (!Algorithms.isEmpty(wpt.link)) {
buildRow(view, R.drawable.ic_world_globe_dark,
null, wpt.link, 0,
false, null, false, 0, true, null, false);
}
if (!Algorithms.isEmpty(emails)) {
buildRow(view, R.drawable.ic_action_message,
null, emails, 0,
false, null, false, 0, false, false, true, null, false);
}
if (!Algorithms.isEmpty(workingHours)) {
buildRow(view, R.drawable.ic_action_time,
null, workingHours, 0,
false, null, false, 0, false, null, false);
}
if (!Algorithms.isEmpty(direction)) {
buildRow(view, R.drawable.ic_action_gdirections_dark,
null, direction, 0,
false, null, false, 0, false, null, false);
}
if (!Algorithms.isEmpty(price)) {
buildRow(view, R.drawable.ic_action_price_tag,
null, price, 0,
false, null, false, 0, false, null, false);
}
}
private HashMap<String, String> getDescriptionTokens(String desc, String ... allowedKeys) {
String[] tokens = desc.split("\n");
HashMap<String, String> mTokens = new HashMap<>();
for (String token : tokens) {
boolean matched = false;
for (String key : allowedKeys) {
if (token.startsWith(key)) {
matched = true;
String value = token.substring(key.length()).trim();
mTokens.put(key, value);
}
}
if (!matched) {
String s = mTokens.get(KEY_OTHER_DESCRIPTION);
mTokens.put(KEY_OTHER_DESCRIPTION, s != null ? s + "\n" + token : token);
}
}
return mTokens;
}
}

View file

@ -0,0 +1,46 @@
package net.osmand.plus.wikivoyage.menu;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import net.osmand.GPXUtilities;
import net.osmand.data.PointDescription;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.controllers.WptPtMenuController;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.data.TravelArticle;
public class WikivoyageWptPtMenuController extends WptPtMenuController {
public WikivoyageWptPtMenuController(@NonNull MenuBuilder menuBuilder, @NonNull final MapActivity mapActivity, @NonNull PointDescription pointDescription, @NonNull GPXUtilities.WptPt wpt) {
super(menuBuilder, mapActivity, pointDescription, wpt);
final OsmandApplication app = mapActivity.getMyApplication();
GpxSelectionHelper.SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedGPXFile(wpt);
GPXUtilities.GPXFile gpxFile = selectedGpxFile != null ? selectedGpxFile.getGpxFile() : null;
GPXUtilities.Metadata metadata = gpxFile != null ? gpxFile.metadata : null;
final TravelArticle article = metadata != null ? getTravelArticle(metadata) : null;
if (article != null) {
leftTitleButtonController = new TitleButtonController() {
@Override
public void buttonPressed() {
WikivoyageArticleDialogFragment.showInstance(app, mapActivity.getSupportFragmentManager(), article.getTripId(), article.getLang());
}
};
leftTitleButtonController.caption = mapActivity.getString(R.string.context_menu_read_article);
leftTitleButtonController.leftIconId = R.drawable.ic_action_read_text;
}
}
private TravelArticle getTravelArticle(@NonNull GPXUtilities.Metadata metadata) {
String title = metadata.getArticleTitle();
String lang = metadata.getArticleLang();
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(lang)) {
return getMapActivity().getMyApplication().getTravelDbHelper().getArticle(title, lang);
}
return null;
}
}