Merge pull request #10845 from osmandapp/fix_bookmarks

Fix bookmarks
This commit is contained in:
alex-osm 2021-02-13 10:06:34 +03:00 committed by GitHub
commit 901c33c535
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 162 additions and 11 deletions

View file

@ -605,6 +605,11 @@ public class TravelDbHelper implements TravelHelper {
return res;
}
@Override
public TravelArticle findSavedArticle(@NonNull TravelArticle savedArticle) {
return savedArticle;
}
@Nullable
@Override
public TravelArticleIdentifier getArticleId(@NonNull String title, @NonNull String lang) {

View file

@ -41,6 +41,9 @@ public interface TravelHelper {
@Nullable
TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @Nullable String lang, boolean readGpx, @Nullable GpxReadCallback callback);
@Nullable
TravelArticle findSavedArticle(@NonNull TravelArticle savedArticle);
@Nullable
TravelArticle getArticleByTitle(@NonNull String title, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);

View file

@ -1,9 +1,5 @@
package net.osmand.plus.wikivoyage.data;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -428,7 +424,7 @@ public class TravelLocalDataHelper {
if (cursor.moveToFirst()) {
do {
TravelArticle dbArticle = readSavedArticle(cursor);
TravelArticle article = context.getTravelHelper().getArticleById(dbArticle.generateIdentifier(), dbArticle.lang, false, null);
TravelArticle article = context.getTravelHelper().findSavedArticle(dbArticle);
if (article != null && article.getLastModified() > dbArticle.getLastModified()) {
updateSavedArticle(dbArticle, article);
res.add(article);

View file

@ -70,6 +70,7 @@ public class TravelObfHelper implements TravelHelper {
public static final String ROUTE_ARTICLE_POINT = "route_article_point";
public static final String ROUTE_TRACK = "route_track";
public static final int ARTICLE_SEARCH_RADIUS = 50000;
public static final int SAVED_ARTICLE_SEARCH_RADIUS = 30000;
public static final int MAX_POPULAR_ARTICLES_COUNT = 30;
public static final String REF_TAG = "ref";
public static final String NAME_TAG = "name";
@ -226,11 +227,14 @@ public class TravelObfHelper implements TravelHelper {
}
@NonNull
private SearchPoiTypeFilter getSearchFilter(final String filterSubcategory) {
private SearchPoiTypeFilter getSearchFilter(final String... filterSubcategory) {
return new SearchPoiTypeFilter() {
@Override
public boolean accept(PoiCategory type, String subcategory) {
return subcategory.equals(filterSubcategory);
for (String filterSubcategory : filterSubcategory) {
return subcategory.equals(filterSubcategory);
}
return false;
}
@Override
@ -680,7 +684,7 @@ public class TravelObfHelper implements TravelHelper {
}
private synchronized TravelArticle findArticleById(@NonNull final TravelArticleIdentifier articleId,
final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle article = null;
final boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT);
final List<Amenity> amenities = new ArrayList<>();
@ -730,17 +734,160 @@ public class TravelObfHelper implements TravelHelper {
return article;
}
@Override
public synchronized TravelArticle findSavedArticle(@NonNull TravelArticle savedArticle) {
final List<Pair<File, Amenity>> amenities = new ArrayList<>();
TravelArticle article = null;
TravelArticleIdentifier articleId = savedArticle.generateIdentifier();
String lang = savedArticle.getLang();
long lastModified = savedArticle.getLastModified();
final TravelArticleIdentifier finalArticleId = articleId;
SearchRequest<Amenity> req = null;
for (final BinaryMapIndexReader reader : getReaders()) {
try {
if (articleId.file != null && articleId.file.equals(reader.getFile())) {
if (lastModified == reader.getFile().lastModified()) {
req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0,
Algorithms.emptyIfNull(articleId.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE,
getSearchFilter(ROUTE_ARTICLE, ROUTE_TRACK), new ResultMatcher<Amenity>() {
boolean done = false;
@Override
public boolean publish(Amenity amenity) {
if (Algorithms.stringsEqual(finalArticleId.routeId,
Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID)))) {
amenities.add(new Pair<>(reader.getFile(), amenity));
done = true;
}
return false;
}
@Override
public boolean isCancelled() {
return done;
}
}, null);
req.setBBoxRadius(articleId.lat, articleId.lon, ARTICLE_SEARCH_RADIUS);
} else {
if (!Algorithms.isEmpty(articleId.title)) {
req = getEqualsTitleRequest(articleId, lang, amenities, reader);
req.setBBoxRadius(articleId.lat, articleId.lon, ARTICLE_SEARCH_RADIUS / 10);
}
}
}
if (req != null) {
if (!Double.isNaN(articleId.lat)) {
if (!Algorithms.isEmpty(articleId.title)) {
reader.searchPoiByName(req);
} else {
reader.searchPoi(req);
}
} else {
reader.searchPoi(req);
}
break;
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
}
if (amenities.isEmpty() && !Algorithms.isEmpty(articleId.title)) {
for (BinaryMapIndexReader reader : getReaders()) {
try {
req = getEqualsTitleRequest(articleId, lang, amenities, reader);
req.setBBoxRadius(articleId.lat, articleId.lon, SAVED_ARTICLE_SEARCH_RADIUS);
if (!Double.isNaN(articleId.lat)) {
reader.searchPoiByName(req);
} else {
reader.searchPoi(req);
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
}
}
if (amenities.isEmpty()) {
for (final BinaryMapIndexReader reader : getReaders()) {
try {
req = BinaryMapIndexReader.buildSearchPoiRequest(0, 0,
Algorithms.emptyIfNull(articleId.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE,
getSearchFilter(ROUTE_ARTICLE, ROUTE_TRACK), new ResultMatcher<Amenity>() {
boolean done = false;
@Override
public boolean publish(Amenity amenity) {
if (Algorithms.stringsEqual(finalArticleId.routeId,
Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_ID)))
&& Algorithms.stringsEqual(finalArticleId.routeSource,
Algorithms.emptyIfNull(amenity.getTagContent(Amenity.ROUTE_SOURCE)))) {
amenities.add(new Pair<>(reader.getFile(), amenity));
done = true;
}
return false;
}
@Override
public boolean isCancelled() {
return done;
}
}, null);
req.setBBoxRadius(articleId.lat, articleId.lon, SAVED_ARTICLE_SEARCH_RADIUS);
if (!Double.isNaN(articleId.lat)) {
if (!Algorithms.isEmpty(articleId.title)) {
reader.searchPoiByName(req);
} else {
reader.searchPoi(req);
}
} else {
reader.searchPoi(req);
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
}
}
if (!Algorithms.isEmpty(amenities)) {
article = cacheTravelArticles(amenities.get(0).first, amenities.get(0).second, lang, false, null);
}
return article;
}
private SearchRequest<Amenity> getEqualsTitleRequest(@NonNull final TravelArticleIdentifier articleId,
final String lang, final List<Pair<File, Amenity>> amenities,
final BinaryMapIndexReader reader) {
return BinaryMapIndexReader.buildSearchPoiRequest(0, 0,
Algorithms.emptyIfNull(articleId.title), 0, Integer.MAX_VALUE, 0, Integer.MAX_VALUE,
getSearchFilter(ROUTE_ARTICLE, ROUTE_TRACK), new ResultMatcher<Amenity>() {
boolean done = false;
@Override
public boolean publish(Amenity amenity) {
if (Algorithms.stringsEqual(Algorithms.emptyIfNull(articleId.title),
Algorithms.emptyIfNull(amenity.getName(lang)))) {
amenities.add(new Pair<>(reader.getFile(), amenity));
done = true;
}
return false;
}
@Override
public boolean isCancelled() {
return done;
}
}, null);
}
@Nullable
@Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang,
boolean readGpx, @Nullable GpxReadCallback callback) {
boolean readGpx, @Nullable GpxReadCallback callback) {
return getArticleByTitle(title, new QuadRect(), lang, readGpx, callback);
}
@Nullable
@Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon,
@NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
@NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
QuadRect rect = MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), ARTICLE_SEARCH_RADIUS);
return getArticleByTitle(title, rect, lang, readGpx, callback);
}
@ -748,7 +895,7 @@ public class TravelObfHelper implements TravelHelper {
@Nullable
@Override
public synchronized TravelArticle getArticleByTitle(@NonNull final String title, @NonNull QuadRect rect,
@NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
@NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle article = null;
final List<Amenity> amenities = new ArrayList<>();
int x = 0;