Added progress bar while reading travel article gpx

This commit is contained in:
max-klaus 2021-01-26 19:02:55 +03:00
parent cf1866d8d5
commit 93093ea428
10 changed files with 181 additions and 87 deletions

View file

@ -145,6 +145,14 @@
tools:drawableStart="@drawable/ic_action_markers_dark" tools:drawableStart="@drawable/ic_action_markers_dark"
android:paddingEnd="@dimen/bottom_sheet_content_padding_small" /> android:paddingEnd="@dimen/bottom_sheet_content_padding_small" />
<ProgressBar
android:id="@+id/gpx_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/bottom_sheet_content_padding_small"
android:visibility="gone" />
<net.osmand.plus.widgets.TextViewEx <net.osmand.plus.widgets.TextViewEx
android:id="@+id/save_button" android:id="@+id/save_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View file

@ -429,7 +429,7 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
String title = metadata.getArticleTitle(); String title = metadata.getArticleTitle();
String lang = metadata.getArticleLang(); String lang = metadata.getArticleLang();
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(lang)) { if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(lang)) {
return app.getTravelHelper().getArticleByTitle(title, gpx.getRect(), lang); return app.getTravelHelper().getArticleByTitle(title, gpx.getRect(), lang, false, null);
} }
return null; return null;
} }

View file

@ -15,6 +15,7 @@ import android.view.ViewGroup;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -27,7 +28,6 @@ import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentManager.BackStackEntry; import androidx.fragment.app.FragmentManager.BackStackEntry;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -45,6 +45,7 @@ import net.osmand.plus.wikivoyage.WikivoyageWebViewClient;
import net.osmand.plus.wikivoyage.data.TravelArticle; import net.osmand.plus.wikivoyage.data.TravelArticle;
import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier; import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier;
import net.osmand.plus.wikivoyage.data.TravelHelper; import net.osmand.plus.wikivoyage.data.TravelHelper;
import net.osmand.plus.wikivoyage.data.TravelHelper.GpxReadCallback;
import net.osmand.plus.wikivoyage.data.TravelLocalDataHelper; import net.osmand.plus.wikivoyage.data.TravelLocalDataHelper;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -75,6 +76,7 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
private TravelArticle article; private TravelArticle article;
private TextView trackButton; private TextView trackButton;
private ProgressBar gpxProgress;
private TextView saveBtn; private TextView saveBtn;
private WikivoyageWebViewClient webViewClient; private WikivoyageWebViewClient webViewClient;
@ -158,6 +160,9 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
startActivity(newIntent); startActivity(newIntent);
} }
}); });
trackButton.setVisibility(View.GONE);
gpxProgress = mainView.findViewById(R.id.gpx_progress);
gpxProgress.setVisibility(View.GONE);
saveBtn = (TextView) mainView.findViewById(R.id.save_button); saveBtn = (TextView) mainView.findViewById(R.id.save_button);
@ -305,19 +310,23 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
selectedLang = langs.get(0); selectedLang = langs.get(0);
} }
articleToolbarText.setText(""); articleToolbarText.setText("");
article = getMyApplication().getTravelHelper().getArticleById(articleId, selectedLang); article = getMyApplication().getTravelHelper().getArticleById(articleId, selectedLang, true,
new GpxReadCallback() {
@Override
public void onGpxFileReading() {
updateTrackButton(true, null);
}
@Override
public void onGpxFileRead(@Nullable GPXFile gpxFile) {
updateTrackButton(false, gpxFile);
}
});
if (article == null) { if (article == null) {
return; return;
} }
webViewClient.setArticle(article); webViewClient.setArticle(article);
articleToolbarText.setText(article.getTitle()); articleToolbarText.setText(article.getTitle());
GPXFile gpxFile = article.getGpxFile();
if (gpxFile != null && gpxFile.getPointsSize() > 0) {
trackButton.setVisibility(View.VISIBLE);
trackButton.setText(getString(R.string.shared_string_gpx_points) + " (" + gpxFile.getPointsSize() + ")");
} else {
trackButton.setVisibility(View.GONE);
}
TravelLocalDataHelper ldh = getMyApplication().getTravelHelper().getBookmarksHelper(); TravelLocalDataHelper ldh = getMyApplication().getTravelHelper().getBookmarksHelper();
ldh.addToHistory(article); ldh.addToHistory(article);
@ -327,6 +336,21 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(), "text/html", "UTF-8", null); contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(), "text/html", "UTF-8", null);
} }
private void updateTrackButton(boolean processing, @Nullable GPXFile gpxFile) {
if (processing) {
trackButton.setVisibility(View.GONE);
gpxProgress.setVisibility(View.VISIBLE);
} else {
if (gpxFile != null && gpxFile.getPointsSize() > 0) {
trackButton.setVisibility(View.VISIBLE);
trackButton.setText(getString(R.string.shared_string_gpx_points) + " (" + gpxFile.getPointsSize() + ")");
} else {
trackButton.setVisibility(View.GONE);
}
gpxProgress.setVisibility(View.GONE);
}
}
@NonNull @NonNull
@Override @Override
protected String createHtmlContent() { protected String createHtmlContent() {

View file

@ -20,6 +20,7 @@ import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.MenuBottomSheetDialogFragment;
@ -75,14 +76,15 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr
return; return;
} }
article = getMyApplication().getTravelHelper().getArticleById(articleId, selectedLang); OsmandApplication app = requiredMyApplication();
article = app.getTravelHelper().getArticleById(articleId, selectedLang, false, null);
if (article == null) { if (article == null) {
return; return;
} }
parentsList = new ArrayList<>(Arrays.asList(article.getAggregatedPartOf().split(","))); parentsList = new ArrayList<>(Arrays.asList(article.getAggregatedPartOf().split(",")));
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> navigationMap Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> navigationMap
= getMyApplication().getTravelHelper().getNavigationMap(article); = app.getTravelHelper().getNavigationMap(article);
items.add(new TitleItem(getString(R.string.shared_string_navigation))); items.add(new TitleItem(getString(R.string.shared_string_navigation)));

View file

@ -44,6 +44,7 @@ public class TravelArticle {
String aggregatedPartOf; String aggregatedPartOf;
long lastModified; long lastModified;
boolean gpxFileReading;
boolean gpxFileRead; boolean gpxFileRead;
@NonNull @NonNull

View file

@ -549,7 +549,7 @@ public class TravelDbHelper implements TravelHelper {
@Override @Override
@Nullable @Nullable
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) { public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle res = null; TravelArticle res = null;
SQLiteConnection conn = openConnection(); SQLiteConnection conn = openConnection();
String routeId = articleId.routeId; String routeId = articleId.routeId;
@ -566,24 +566,27 @@ public class TravelDbHelper implements TravelHelper {
if (res == null) { if (res == null) {
res = localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang); res = localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang);
} }
if (res != null && callback != null) {
callback.onGpxFileRead(res.gpxFile);
}
return res; return res;
} }
@Nullable @Nullable
@Override @Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang) { public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
return getArticleByTitle(title, new QuadRect(), lang); return getArticleByTitle(title, new QuadRect(), lang, readGpx, callback);
} }
@Nullable @Nullable
@Override @Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon, @NonNull final String lang) { public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon, @NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
return getArticleByTitle(title, new QuadRect(), lang); return getArticleByTitle(title, new QuadRect(), lang, readGpx, callback);
} }
@Nullable @Nullable
@Override @Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull QuadRect rect, @NonNull final String lang) { public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull QuadRect rect, @NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle res = null; TravelArticle res = null;
SQLiteConnection conn = openConnection(); SQLiteConnection conn = openConnection();
if (conn != null) { if (conn != null) {
@ -596,6 +599,9 @@ public class TravelDbHelper implements TravelHelper {
cursor.close(); cursor.close();
} }
} }
if (res != null && callback != null) {
callback.onGpxFileRead(res.gpxFile);
}
return res; return res;
} }

View file

@ -3,6 +3,8 @@ package net.osmand.plus.wikivoyage.data;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.QuadRect; import net.osmand.data.QuadRect;
import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier; import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier;
@ -14,6 +16,11 @@ import java.util.Map;
public interface TravelHelper { public interface TravelHelper {
interface GpxReadCallback {
void onGpxFileReading();
void onGpxFileRead(@Nullable GPXFile gpxFile);
}
TravelLocalDataHelper getBookmarksHelper(); TravelLocalDataHelper getBookmarksHelper();
void initializeDataOnAppStartup(); void initializeDataOnAppStartup();
@ -29,19 +36,19 @@ public interface TravelHelper {
List<TravelArticle> getPopularArticles(); List<TravelArticle> getPopularArticles();
@NonNull @NonNull
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull final TravelArticle article); Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull TravelArticle article);
@Nullable @Nullable
TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang); TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);
@Nullable @Nullable
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang); TravelArticle getArticleByTitle(@NonNull String title, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);
@Nullable @Nullable
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon, @NonNull final String lang); TravelArticle getArticleByTitle(@NonNull String title, @NonNull LatLon latLon, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);
@Nullable @Nullable
TravelArticle getArticleByTitle(@NonNull String title, @NonNull QuadRect rect, @NonNull String lang); TravelArticle getArticleByTitle(@NonNull String title, @NonNull QuadRect rect, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);
@Nullable @Nullable
TravelArticleIdentifier getArticleId(@NonNull String title, @NonNull String lang); TravelArticleIdentifier getArticleId(@NonNull String title, @NonNull String lang);
@ -50,10 +57,10 @@ public interface TravelHelper {
ArrayList<String> getArticleLangs(@NonNull TravelArticleIdentifier articleId); ArrayList<String> getArticleLangs(@NonNull TravelArticleIdentifier articleId);
@NonNull @NonNull
String getGPXName(@NonNull final TravelArticle article); String getGPXName(@NonNull TravelArticle article);
@NonNull @NonNull
File createGpxFile(@NonNull final TravelArticle article); File createGpxFile(@NonNull TravelArticle article);
// TODO: this method should be deleted once TravelDBHelper is deleted // TODO: this method should be deleted once TravelDBHelper is deleted
@Nullable @Nullable

View file

@ -420,7 +420,7 @@ public class TravelLocalDataHelper {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
TravelArticle dbArticle = readSavedArticle(cursor); TravelArticle dbArticle = readSavedArticle(cursor);
TravelArticle article = context.getTravelHelper().getArticleById(dbArticle.generateIdentifier(), dbArticle.lang); TravelArticle article = context.getTravelHelper().getArticleById(dbArticle.generateIdentifier(), dbArticle.lang, false, null);
if (article != null && article.getLastModified() > dbArticle.getLastModified()) { if (article != null && article.getLastModified() > dbArticle.getLastModified()) {
updateSavedArticle(dbArticle, article); updateSavedArticle(dbArticle, article);
res.add(article); res.add(article);

View file

@ -1,5 +1,6 @@
package net.osmand.plus.wikivoyage.data; package net.osmand.plus.wikivoyage.data;
import android.os.AsyncTask;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -97,9 +98,19 @@ public class TravelObfHelper implements TravelHelper {
location, POPULAR_ARTICLES_SEARCH_RADIUS, -1, getSearchFilter(false), null); location, POPULAR_ARTICLES_SEARCH_RADIUS, -1, getSearchFilter(false), null);
List<Amenity> amenities = reader.searchPoi(req); List<Amenity> amenities = reader.searchPoi(req);
if (amenities.size() > 0) { if (amenities.size() > 0) {
Collections.sort(amenities, new Comparator<Amenity>() {
@Override
public int compare(Amenity a1, Amenity a2) {
int d1 = (int) (MapUtils.getDistance(a1.getLocation().getLatitude(), a1.getLocation().getLongitude(),
location.getLatitude(), location.getLongitude()));
int d2 = (int) (MapUtils.getDistance(a2.getLocation().getLatitude(), a2.getLocation().getLongitude(),
location.getLatitude(), location.getLongitude()));
return d1 < d2 ? -1 : (d1 == d2 ? 0 : 1);
}
});
for (Amenity amenity : amenities) { for (Amenity amenity : amenities) {
if (!Algorithms.isEmpty(amenity.getName(lang))) { if (!Algorithms.isEmpty(amenity.getName(lang))) {
TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang, false); TravelArticle article = cacheTravelArticles(reader.getFile(), amenity, lang, false, null);
if (article != null) { if (article != null) {
popularArticles.add(article); popularArticles.add(article);
if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) { if (popularArticles.size() >= MAX_POPULAR_ARTICLES_COUNT) {
@ -108,16 +119,6 @@ public class TravelObfHelper implements TravelHelper {
} }
} }
} }
Collections.sort(popularArticles, new Comparator<TravelArticle>() {
@Override
public int compare(TravelArticle article1, TravelArticle article2) {
int d1 = (int) (MapUtils.getDistance(article1.getLat(), article1.getLon(),
location.getLatitude(), location.getLongitude()));
int d2 = (int) (MapUtils.getDistance(article2.getLat(), article2.getLon(),
location.getLatitude(), location.getLongitude()));
return d1 < d2 ? -1 : (d1 == d2 ? 0 : 1);
}
});
} }
} catch (Exception e) { } catch (Exception e) {
LOG.error(e.getMessage(), e); LOG.error(e.getMessage(), e);
@ -128,13 +129,13 @@ public class TravelObfHelper implements TravelHelper {
} }
@Nullable @Nullable
private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang, boolean readPoints) { private TravelArticle cacheTravelArticles(File file, Amenity amenity, String lang, boolean readPoints, @Nullable GpxReadCallback callback) {
TravelArticle article = null; TravelArticle article = null;
Map<String, TravelArticle> articles = readArticles(file, amenity, false); Map<String, TravelArticle> articles = readArticles(file, amenity);
if (!Algorithms.isEmpty(articles)) { if (!Algorithms.isEmpty(articles)) {
TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier(); TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier();
cachedArticles.put(newArticleId, articles); cachedArticles.put(newArticleId, articles);
article = getCachedArticle(newArticleId, lang, readPoints); article = getCachedArticle(newArticleId, lang, readPoints, callback);
} }
return article; return article;
} }
@ -155,17 +156,17 @@ public class TravelObfHelper implements TravelHelper {
} }
@NonNull @NonNull
private Map<String, TravelArticle> readArticles(@NonNull File file, @NonNull Amenity amenity, boolean readPoints) { private Map<String, TravelArticle> readArticles(@NonNull File file, @NonNull Amenity amenity) {
Map<String, TravelArticle> articles = new HashMap<>(); Map<String, TravelArticle> articles = new HashMap<>();
Set<String> langs = getLanguages(amenity); Set<String> langs = getLanguages(amenity);
for (String lang : langs) { for (String lang : langs) {
articles.put(lang, readArticle(file, amenity, lang, readPoints)); articles.put(lang, readArticle(file, amenity, lang));
} }
return articles; return articles;
} }
@NonNull @NonNull
private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @NonNull String lang, boolean readPoints) { private TravelArticle readArticle(@NonNull File file, @NonNull Amenity amenity, @NonNull String lang) {
TravelArticle res = new TravelArticle(); TravelArticle res = new TravelArticle();
res.file = file; res.file = file;
String title = amenity.getName(lang); String title = amenity.getName(lang);
@ -182,29 +183,11 @@ public class TravelObfHelper implements TravelHelper {
res.lang = lang; res.lang = lang;
res.contentsJson = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang)); res.contentsJson = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang));
res.aggregatedPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_AGGR_PART, lang)); res.aggregatedPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_AGGR_PART, lang));
if (readPoints) {
res.gpxFile = buildGpxFile(res);
res.gpxFileRead = true;
}
return res; return res;
} }
private GPXFile buildGpxFile(@NonNull TravelArticle article) {
GPXFile gpxFile = null;
List<Amenity> pointList = getPointList(article);
if (!Algorithms.isEmpty(pointList)) {
gpxFile = new GPXFile(article.getTitle(), article.getLang(), article.getContent());
gpxFile.metadata.link = TravelArticle.getImageUrl(article.getImageTitle(), false);
for (Amenity amenity : pointList) {
WptPt wptPt = createWptPt(amenity, article.getLang());
gpxFile.addPoint(wptPt);
}
}
return gpxFile;
}
@NonNull @NonNull
private List<Amenity> getPointList(@NonNull final TravelArticle article) { private synchronized List<Amenity> getPointList(@NonNull final TravelArticle article) {
final List<Amenity> pointList = new ArrayList<>(); final List<Amenity> pointList = new ArrayList<>();
final String lang = article.getLang(); final String lang = article.getLang();
for (BinaryMapIndexReader reader : getReaders()) { for (BinaryMapIndexReader reader : getReaders()) {
@ -312,7 +295,7 @@ public class TravelObfHelper implements TravelHelper {
for (Amenity amenity : entry.getValue()) { for (Amenity amenity : entry.getValue()) {
Set<String> nameLangs = getLanguages(amenity); Set<String> nameLangs = getLanguages(amenity);
if (nameLangs.contains(appLang)) { if (nameLangs.contains(appLang)) {
TravelArticle article = readArticle(file, amenity, appLang, false); TravelArticle article = readArticle(file, amenity, appLang);
ArrayList<String> langs = new ArrayList<>(nameLangs); ArrayList<String> langs = new ArrayList<>(nameLangs);
Collections.sort(langs, new Comparator<String>() { Collections.sort(langs, new Comparator<String>() {
@Override @Override
@ -481,20 +464,28 @@ public class TravelObfHelper implements TravelHelper {
LOG.error(e.getMessage()); LOG.error(e.getMessage());
} }
if (!Algorithms.isEmpty(amenities)) { if (!Algorithms.isEmpty(amenities)) {
article = readArticle(reader.getFile(), amenities.get(0), lang, false); article = readArticle(reader.getFile(), amenities.get(0), lang);
} }
} }
return article; return article;
} }
@Override @Override
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) { public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang,
TravelArticle article = getCachedArticle(articleId, lang, true); boolean readGpx, @Nullable GpxReadCallback callback) {
return article == null ? localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang) : article; TravelArticle article = getCachedArticle(articleId, lang, readGpx, callback);
if (article == null) {
article = localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang);
if (article != null && callback != null && readGpx) {
callback.onGpxFileRead(article.gpxFile);
}
}
return article;
} }
@Nullable @Nullable
private TravelArticle getCachedArticle(@NonNull TravelArticleIdentifier articleId, @NonNull String lang, boolean forceReadPoints) { private TravelArticle getCachedArticle(@NonNull TravelArticleIdentifier articleId, @NonNull String lang,
boolean forceReadPoints, @Nullable GpxReadCallback callback) {
TravelArticle article = null; TravelArticle article = null;
Map<String, TravelArticle> articles = cachedArticles.get(articleId); Map<String, TravelArticle> articles = cachedArticles.get(articleId);
if (articles != null) { if (articles != null) {
@ -511,16 +502,24 @@ public class TravelObfHelper implements TravelHelper {
} }
} }
if (article == null && articles == null) { if (article == null && articles == null) {
article = findArticleById(articleId, lang); article = findArticleById(articleId, lang, callback);
} }
if (article != null && !article.gpxFileRead && forceReadPoints && !Algorithms.isEmpty(lang)) { if (article != null && forceReadPoints && !Algorithms.isEmpty(lang)) {
article.gpxFile = buildGpxFile(article); readGpxFile(article, callback);
article.gpxFileRead = true;
} }
return article; return article;
} }
private TravelArticle findArticleById(@NonNull final TravelArticleIdentifier articleId, final String lang) { private void readGpxFile(@NonNull TravelArticle article, @Nullable GpxReadCallback callback) {
if (!article.gpxFileRead) {
new GpxFileReader(article, callback).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else if (callback != null) {
callback.onGpxFileRead(article.gpxFile);
}
}
private TravelArticle findArticleById(@NonNull final TravelArticleIdentifier articleId,
final String lang, @Nullable GpxReadCallback callback) {
TravelArticle article = null; TravelArticle article = null;
final boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT); final boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT);
final List<Amenity> amenities = new ArrayList<>(); final List<Amenity> amenities = new ArrayList<>();
@ -563,7 +562,7 @@ public class TravelObfHelper implements TravelHelper {
LOG.error(e.getMessage()); LOG.error(e.getMessage());
} }
if (!Algorithms.isEmpty(amenities)) { if (!Algorithms.isEmpty(amenities)) {
article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang, true); article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang, true, callback);
} }
} }
return article; return article;
@ -571,20 +570,23 @@ public class TravelObfHelper implements TravelHelper {
@Nullable @Nullable
@Override @Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang) { public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang,
return getArticleByTitle(title, new QuadRect(), lang); boolean readGpx, @Nullable GpxReadCallback callback) {
return getArticleByTitle(title, new QuadRect(), lang, readGpx, callback);
} }
@Nullable @Nullable
@Override @Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon, @NonNull final String lang) { public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon,
QuadRect rect = latLon != null ? MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), ARTICLE_SEARCH_RADIUS) : new QuadRect(); @NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
return getArticleByTitle(title, rect, lang); QuadRect rect = MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), ARTICLE_SEARCH_RADIUS);
return getArticleByTitle(title, rect, lang, readGpx, callback);
} }
@Nullable @Nullable
@Override @Override
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull QuadRect rect, @NonNull final String lang) { public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull QuadRect rect,
@NonNull final String lang, boolean readGpx, @Nullable GpxReadCallback callback) {
TravelArticle article = null; TravelArticle article = null;
final List<Amenity> amenities = new ArrayList<>(); final List<Amenity> amenities = new ArrayList<>();
int x = 0; int x = 0;
@ -627,7 +629,7 @@ public class TravelObfHelper implements TravelHelper {
LOG.error(e.getMessage()); LOG.error(e.getMessage());
} }
if (!Algorithms.isEmpty(amenities)) { if (!Algorithms.isEmpty(amenities)) {
article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang, true); article = cacheTravelArticles(reader.getFile(), amenities.get(0), lang, readGpx, callback);
} }
} }
return article; return article;
@ -654,7 +656,7 @@ public class TravelObfHelper implements TravelHelper {
} }
} }
if (a == null) { if (a == null) {
TravelArticle article = getArticleByTitle(title, lang); TravelArticle article = getArticleByTitle(title, lang, false, null);
if (article != null) { if (article != null) {
a = article; a = article;
} }
@ -666,7 +668,7 @@ public class TravelObfHelper implements TravelHelper {
@Override @Override
public ArrayList<String> getArticleLangs(@NonNull TravelArticleIdentifier articleId) { public ArrayList<String> getArticleLangs(@NonNull TravelArticleIdentifier articleId) {
ArrayList<String> res = new ArrayList<>(); ArrayList<String> res = new ArrayList<>();
TravelArticle article = getArticleById(articleId, ""); TravelArticle article = getArticleById(articleId, "", false, null);
if (article != null) { if (article != null) {
Map<String, TravelArticle> articles = cachedArticles.get(article.generateIdentifier()); Map<String, TravelArticle> articles = cachedArticles.get(article.generateIdentifier());
if (articles != null) { if (articles != null) {
@ -683,14 +685,14 @@ public class TravelObfHelper implements TravelHelper {
@NonNull @NonNull
@Override @Override
public String getGPXName(@NonNull final TravelArticle article) { public String getGPXName(@NonNull TravelArticle article) {
return article.getTitle().replace('/', '_').replace('\'', '_') return article.getTitle().replace('/', '_').replace('\'', '_')
.replace('\"', '_') + IndexConstants.GPX_FILE_EXT; .replace('\"', '_') + IndexConstants.GPX_FILE_EXT;
} }
@NonNull @NonNull
@Override @Override
public File createGpxFile(@NonNull final TravelArticle article) { public File createGpxFile(@NonNull TravelArticle article) {
final GPXFile gpx = article.getGpxFile(); final GPXFile gpx = article.getGpxFile();
File file = app.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article)); File file = app.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article));
writeGpxFile(file, gpx); writeGpxFile(file, gpx);
@ -706,4 +708,46 @@ public class TravelObfHelper implements TravelHelper {
public String getWikivoyageFileName() { public String getWikivoyageFileName() {
return WORLD_WIKIVOYAGE_FILE_NAME; return WORLD_WIKIVOYAGE_FILE_NAME;
} }
private class GpxFileReader extends AsyncTask<Void, Void, GPXFile> {
private final TravelArticle article;
private final GpxReadCallback callback;
public GpxFileReader(@NonNull TravelArticle article, @Nullable GpxReadCallback callback) {
this.article = article;
this.callback = callback;
}
@Override
protected void onPreExecute() {
if (callback != null) {
callback.onGpxFileReading();
}
}
@Override
protected GPXFile doInBackground(Void... voids) {
GPXFile gpxFile = null;
List<Amenity> pointList = getPointList(article);
if (!Algorithms.isEmpty(pointList)) {
gpxFile = new GPXFile(article.getTitle(), article.getLang(), article.getContent());
gpxFile.metadata.link = TravelArticle.getImageUrl(article.getImageTitle(), false);
for (Amenity amenity : pointList) {
WptPt wptPt = createWptPt(amenity, article.getLang());
gpxFile.addPoint(wptPt);
}
}
return gpxFile;
}
@Override
protected void onPostExecute(GPXFile gpxFile) {
article.gpxFileRead = true;
article.gpxFile = gpxFile;
if (callback != null) {
callback.onGpxFileRead(gpxFile);
}
}
}
} }

View file

@ -10,6 +10,7 @@ import net.osmand.GPXUtilities.WptPt;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.controllers.WptPtMenuController; import net.osmand.plus.mapcontextmenu.controllers.WptPtMenuController;
@ -38,13 +39,14 @@ public class WikivoyageWptPtMenuController extends WptPtMenuController {
} }
private static TravelArticle getTravelArticle(@NonNull MapActivity mapActivity, @NonNull WptPt wpt) { private static TravelArticle getTravelArticle(@NonNull MapActivity mapActivity, @NonNull WptPt wpt) {
SelectedGpxFile selectedGpxFile = mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedGPXFile(wpt); OsmandApplication app = mapActivity.getMyApplication();
SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedGPXFile(wpt);
GPXFile gpxFile = selectedGpxFile != null ? selectedGpxFile.getGpxFile() : null; GPXFile gpxFile = selectedGpxFile != null ? selectedGpxFile.getGpxFile() : null;
Metadata metadata = gpxFile != null ? gpxFile.metadata : null; Metadata metadata = gpxFile != null ? gpxFile.metadata : null;
String title = metadata != null ? metadata.getArticleTitle() : null; String title = metadata != null ? metadata.getArticleTitle() : null;
String lang = metadata != null ? metadata.getArticleLang() : null; String lang = metadata != null ? metadata.getArticleLang() : null;
if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(lang)) { if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(lang)) {
return mapActivity.getMyApplication().getTravelHelper().getArticleByTitle(title, new LatLon(wpt.lat, wpt.lon), lang); return app.getTravelHelper().getArticleByTitle(title, new LatLon(wpt.lat, wpt.lon), lang, false, null);
} }
return null; return null;
} }