Add to bookmarks, build gpx track

This commit is contained in:
Dima-1 2021-01-26 23:41:44 +02:00
parent a914efaa28
commit fea90fd37e
5 changed files with 198 additions and 34 deletions

View file

@ -1,6 +1,12 @@
package net.osmand.plus.wikivoyage.data;
public class TravelGpx extends TravelArticle {
public static final String DISTANCE = "distance";
public static final String DIFF_ELE_UP = "diff_ele_up";
public static final String DIFF_ELE_DOWN = "diff_ele_down";
public static final String USER = "user";
public String user;
public float totalDistance = 0;
public double diffElevationUp = 0;

View file

@ -143,6 +143,9 @@ public class TravelLocalDataHelper {
@Nullable
private TravelArticle getArticle(String title, String lang) {
for (TravelArticle article : savedArticles) {
if (article.lang == null && lang == null && article.title != null && article.title.equals(title)) {
return article;
}
if (article.title != null && article.title.equals(title) && article.lang != null && article.lang.equals(lang)) {
return article;
}
@ -477,12 +480,23 @@ public class TravelLocalDataHelper {
SQLiteConnection conn = openConnection(false);
if (conn != null) {
try {
conn.execSQL("DELETE FROM " + BOOKMARKS_TABLE_NAME +
" WHERE " + BOOKMARKS_COL_ARTICLE_TITLE + " = ?" +
" AND " + BOOKMARKS_COL_ROUTE_ID + " = ?" +
" AND " + BOOKMARKS_COL_LANG + " = ?" +
" AND " + BOOKMARKS_COL_TRAVEL_BOOK + " = ?",
new Object[]{article.title, article.routeId, article.lang, travelBook});
String query;
Object[] parameters;
if (article.lang == null) {
query = "DELETE FROM " + BOOKMARKS_TABLE_NAME +
" WHERE " + BOOKMARKS_COL_ARTICLE_TITLE + " = ?" +
" AND " + BOOKMARKS_COL_ROUTE_ID + " = ?" +
" AND " + BOOKMARKS_COL_TRAVEL_BOOK + " = ?";
parameters = new Object[]{article.title, article.routeId, travelBook};
} else {
query = "DELETE FROM " + BOOKMARKS_TABLE_NAME +
" WHERE " + BOOKMARKS_COL_ARTICLE_TITLE + " = ?" +
" AND " + BOOKMARKS_COL_ROUTE_ID + " = ?" +
" AND " + BOOKMARKS_COL_LANG + " = ?" +
" AND " + BOOKMARKS_COL_TRAVEL_BOOK + " = ?";
parameters = new Object[]{article.title, article.routeId, article.lang, travelBook};
}
conn.execSQL(query, parameters);
} finally {
conn.close();
}
@ -537,7 +551,12 @@ public class TravelLocalDataHelper {
@NonNull
private TravelArticle readSavedArticle(SQLiteCursor cursor) {
TravelArticle res = new TravelArticle();
TravelArticle res;
if (cursor.getString(cursor.getColumnIndex(BOOKMARKS_COL_LANG)) == null) {
res = new TravelGpx();
} else {
res = new TravelArticle();
}
res.title = cursor.getString(cursor.getColumnIndex(BOOKMARKS_COL_ARTICLE_TITLE));
res.lang = cursor.getString(cursor.getColumnIndex(BOOKMARKS_COL_LANG));
res.aggregatedPartOf = cursor.getString(cursor.getColumnIndex(BOOKMARKS_COL_IS_PART_OF));

View file

@ -8,11 +8,13 @@ import androidx.annotation.Nullable;
import net.osmand.Collator;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.IndexConstants;
import net.osmand.OsmAndCollator;
import net.osmand.PlatformUtil;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapDataObject;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.binary.BinaryMapIndexReader.SearchRequest;
import net.osmand.data.Amenity;
@ -48,6 +50,10 @@ import java.util.concurrent.ConcurrentHashMap;
import static net.osmand.GPXUtilities.WptPt;
import static net.osmand.GPXUtilities.writeGpxFile;
import static net.osmand.plus.helpers.GpxUiHelper.getGpxTitle;
import static net.osmand.plus.wikivoyage.data.TravelGpx.DIFF_ELE_DOWN;
import static net.osmand.plus.wikivoyage.data.TravelGpx.DIFF_ELE_UP;
import static net.osmand.plus.wikivoyage.data.TravelGpx.DISTANCE;
import static net.osmand.plus.wikivoyage.data.TravelGpx.USER;
import static net.osmand.util.Algorithms.capitalizeFirstLetter;
public class TravelObfHelper implements TravelHelper {
@ -168,25 +174,26 @@ public class TravelObfHelper implements TravelHelper {
private Map<String, TravelArticle> readRoutePoint(File file, Amenity amenity) {
Map<String, TravelArticle> articles = new HashMap<>();
TravelGpx res = new TravelGpx();
res.file = file;
String title = amenity.getName("en");
res.title = capitalizeFirstLetter(getGpxTitle(Algorithms.isEmpty(title) ? amenity.getName() : title));
res.routeId = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID));
try {
res.totalDistance = Float.parseFloat(Algorithms.emptyIfNull(amenity.getTagContent("distance")));
res.totalDistance = Float.parseFloat(Algorithms.emptyIfNull(amenity.getTagContent(DISTANCE)));
} catch (NumberFormatException e) {
LOG.debug(e.getMessage(), e);
}
try {
res.diffElevationUp = Double.parseDouble(Algorithms.emptyIfNull(amenity.getTagContent("diff_ele_up")));
res.diffElevationUp = Double.parseDouble(Algorithms.emptyIfNull(amenity.getTagContent(DIFF_ELE_UP)));
} catch (NumberFormatException e) {
LOG.debug(e.getMessage(), e);
}
try {
res.diffElevationDown = Double.parseDouble(Algorithms.emptyIfNull(amenity.getTagContent("diff_ele_down")));
res.diffElevationDown = Double.parseDouble(Algorithms.emptyIfNull(amenity.getTagContent(DIFF_ELE_DOWN)));
} catch (NumberFormatException e) {
LOG.debug(e.getMessage(), e);
}
res.user = Algorithms.emptyIfNull(amenity.getTagContent("user"));
res.user = Algorithms.emptyIfNull(amenity.getTagContent(USER));
articles.put("en", res);
return articles;
}
@ -255,6 +262,66 @@ public class TravelObfHelper implements TravelHelper {
return gpxFile;
}
@Nullable
private GPXFile buildTravelGpxFile(@NonNull final TravelGpx article) {
String routeId = article.getRouteId();
final String ref = routeId.substring(routeId.length() - 3);
final List<BinaryMapDataObject> segmentList = new ArrayList<>();
for (BinaryMapIndexReader reader : getReaders()) {
try {
if (article.file != null && !article.file.equals(reader.getFile())) {
continue;
}
BinaryMapIndexReader.SearchRequest<BinaryMapDataObject> sr = BinaryMapIndexReader.buildSearchRequest(
0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE, 15, null,
new ResultMatcher<BinaryMapDataObject>() {
@Override
public boolean publish(BinaryMapDataObject object) {
if (object.getPointsLength() > 1) {
if (object.getObjectNames().get(2).equals(ref)
&& capitalizeFirstLetter(getGpxTitle(object.getObjectNames().get(1))).equals(article.title)) {
segmentList.add(object);
}
}
return false;
}
@Override
public boolean isCancelled() {
return false;
}
});
reader.searchMapIndex(sr);
if (!Algorithms.isEmpty(segmentList)) {
break;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
GPXFile gpxFile = null;
if (!segmentList.isEmpty()) {
GPXUtilities.Track track = new GPXUtilities.Track();
for (BinaryMapDataObject segment : segmentList) {
List<WptPt> pointList = new ArrayList<>();
GPXUtilities.TrkSegment trkSegment = new GPXUtilities.TrkSegment();
for (int i = 0; i < segment.getPointsLength(); i++) {
WptPt point = new WptPt();
point.lat = MapUtils.get31LatitudeY(segment.getPoint31YTile(i));
point.lon = MapUtils.get31LongitudeX(segment.getPoint31XTile(i));
pointList.add(point);
}
trkSegment.points = pointList;
track.segments.add(trkSegment);
}
gpxFile = new GPXFile(article.getTitle(), article.getLang(), "");
gpxFile.tracks = new ArrayList<>();
gpxFile.tracks.add(track);
}
return gpxFile;
}
@NonNull
private List<Amenity> getPointList(@NonNull final TravelArticle article) {
final List<Amenity> pointList = new ArrayList<>();
@ -744,7 +811,12 @@ public class TravelObfHelper implements TravelHelper {
@NonNull
@Override
public File createGpxFile(@NonNull final TravelArticle article) {
final GPXFile gpx = article.getGpxFile();
final GPXFile gpx;
if (article instanceof TravelGpx) {
gpx = buildTravelGpxFile((TravelGpx) article);
} else {
gpx = article.getGpxFile();
}
File file = app.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article));
writeGpxFile(file, gpx);
return file;

View file

@ -7,6 +7,8 @@ import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
@ -16,16 +18,20 @@ import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.RequestCreator;
import net.osmand.AndroidUtils;
import net.osmand.PicassoUtils;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.widgets.tools.CropCircleTransformation;
import net.osmand.plus.wikipedia.WikiArticleHelper;
import net.osmand.plus.wikivoyage.WikivoyageUtils;
import net.osmand.plus.wikivoyage.data.TravelArticle;
import net.osmand.plus.wikivoyage.data.TravelGpx;
import net.osmand.plus.wikivoyage.data.TravelLocalDataHelper;
import net.osmand.plus.wikivoyage.explore.travelcards.TravelGpxCard;
import java.util.ArrayList;
import java.util.List;
@ -33,7 +39,8 @@ import java.util.List;
public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int HEADER_TYPE = 0;
private static final int ITEM_TYPE = 1;
private static final int ARTICLE_TYPE = 1;
private static final int GPX_TYPE = 2;
private final OsmandApplication app;
private final OsmandSettings settings;
@ -45,6 +52,7 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
private final Drawable readIcon;
private final Drawable deleteIcon;
private PicassoUtils picasso;
boolean nightMode;
public void setListener(Listener listener) {
this.listener = listener;
@ -54,21 +62,34 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
this.app = app;
this.settings = app.getSettings();
picasso = PicassoUtils.getPicasso(app);
nightMode = !app.getSettings().isLightContent();
readIcon = getActiveIcon(R.drawable.ic_action_read_article);
deleteIcon = getActiveIcon(R.drawable.ic_action_read_later_fill);
}
int colorId = settings.isLightContent()
? R.color.wikivoyage_active_light : R.color.wikivoyage_active_dark;
UiUtilities ic = app.getUIUtilities();
readIcon = ic.getIcon(R.drawable.ic_action_read_article, colorId);
deleteIcon = ic.getIcon(R.drawable.ic_action_read_later_fill, colorId);
private Drawable getActiveIcon(@DrawableRes int iconId) {
int colorId = nightMode ? R.color.wikivoyage_active_dark : R.color.wikivoyage_active_light;
return app.getUIUtilities().getIcon(iconId, colorId);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
boolean header = viewType == HEADER_TYPE;
int layoutId = header ? R.layout.wikivoyage_list_header : R.layout.wikivoyage_article_card;
View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
return header ? new HeaderVH(itemView) : new ItemVH(itemView);
switch (viewType) {
case HEADER_TYPE:
return new HeaderVH(inflate(parent, R.layout.wikivoyage_list_header));
case ARTICLE_TYPE:
return new ItemVH(inflate(parent, R.layout.wikivoyage_article_card));
case GPX_TYPE:
return new TravelGpxCard.TravelGpxVH(inflate(parent, R.layout.wikivoyage_travel_gpx_card));
default:
throw new RuntimeException("Unsupported view type: " + viewType);
}
}
@NonNull
private View inflate(@NonNull ViewGroup parent, @LayoutRes int layoutId) {
return LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
}
@Override
@ -77,7 +98,7 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
final HeaderVH holder = (HeaderVH) viewHolder;
holder.title.setText((String) getItem(position));
holder.description.setText(String.valueOf(items.size() - 1));
} else {
} else if (viewHolder instanceof ItemVH) {
final ItemVH holder = (ItemVH) viewHolder;
TravelArticle article = (TravelArticle) getItem(position);
final String url = TravelArticle.getImageUrl(article.getImageTitle(), false);
@ -111,6 +132,50 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
holder.rightButton.setCompoundDrawablesWithIntrinsicBounds(null, null, deleteIcon, null);
holder.divider.setVisibility(lastItem ? View.GONE : View.VISIBLE);
holder.shadow.setVisibility(lastItem ? View.VISIBLE : View.GONE);
} else if (viewHolder instanceof TravelGpxCard.TravelGpxVH) {
final TravelGpx article = (TravelGpx) getItem(position);
final TravelGpxCard.TravelGpxVH holder = (TravelGpxCard.TravelGpxVH) viewHolder;
holder.title.setText(article.getTitle());
Drawable icon = getActiveIcon(R.drawable.ic_action_user_account_16);
holder.user.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
holder.user.setText(WikiArticleHelper.getPartialContent(article.user));
AndroidUtils.setBackground(app, holder.user, nightMode, R.drawable.btn_border_bg_light, R.drawable.btn_border_bg_dark);
holder.distance.setText(OsmAndFormatter.getFormattedDistance(article.totalDistance, app));
holder.diffElevationUp.setText(OsmAndFormatter.getFormattedAlt(article.diffElevationUp, app));
holder.diffElevationDown.setText(OsmAndFormatter.getFormattedAlt(article.diffElevationDown, app));
holder.leftButton.setText(app.getString(R.string.shared_string_view));
View.OnClickListener readClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
app.getTravelHelper().createGpxFile(article);
}
};
holder.leftButton.setOnClickListener(readClickListener);
holder.itemView.setOnClickListener(readClickListener);
holder.leftButton.setCompoundDrawablesWithIntrinsicBounds(readIcon, null, null, null);
updateSaveButton(holder, article);
}
}
private void updateSaveButton(final TravelGpxCard.TravelGpxVH holder, final TravelGpx article) {
if (article != null) {
final TravelLocalDataHelper helper = app.getTravelHelper().getBookmarksHelper();
final boolean saved = helper.isArticleSaved(article);
Drawable icon = getActiveIcon(saved ? R.drawable.ic_action_read_later_fill : R.drawable.ic_action_read_later);
holder.rightButton.setText(saved ? R.string.shared_string_remove : R.string.shared_string_save);
holder.rightButton.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);
holder.rightButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (saved) {
helper.removeArticleFromSaved(article);
} else {
app.getTravelHelper().createGpxFile(article);
helper.addArticleToSaved(article);
}
updateSaveButton(holder, article);
}
});
}
}
@ -118,8 +183,10 @@ public class SavedArticlesRvAdapter extends RecyclerView.Adapter<RecyclerView.Vi
public int getItemViewType(int position) {
if (getItem(position) instanceof String) {
return HEADER_TYPE;
} else if (getItem(position) instanceof TravelGpx) {
return GPX_TYPE;
}
return ITEM_TYPE;
return ARTICLE_TYPE;
}
@Override

View file

@ -89,15 +89,15 @@ public class TravelGpxCard extends BaseTravelCard {
public static class TravelGpxVH extends RecyclerView.ViewHolder {
final TextView title;
final TextView user;
final TextView distance;
final TextView diffElevationUp;
final TextView diffElevationDown;
final TextView leftButton;
final TextView rightButton;
final View divider;
final View shadow;
public final TextView title;
public final TextView user;
public final TextView distance;
public final TextView diffElevationUp;
public final TextView diffElevationDown;
public final TextView leftButton;
public final TextView rightButton;
public final View divider;
public final View shadow;
public TravelGpxVH(final View itemView) {
super(itemView);