Added progress bar while reading travel article gpx
This commit is contained in:
parent
cf1866d8d5
commit
93093ea428
10 changed files with 181 additions and 87 deletions
|
@ -145,6 +145,14 @@
|
|||
tools:drawableStart="@drawable/ic_action_markers_dark"
|
||||
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
|
||||
android:id="@+id/save_button"
|
||||
android:layout_width="wrap_content"
|
||||
|
|
|
@ -429,7 +429,7 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
|
|||
String title = metadata.getArticleTitle();
|
||||
String lang = metadata.getArticleLang();
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ import android.view.ViewGroup;
|
|||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
@ -27,7 +28,6 @@ import androidx.fragment.app.FragmentManager;
|
|||
import androidx.fragment.app.FragmentManager.BackStackEntry;
|
||||
|
||||
import net.osmand.AndroidUtils;
|
||||
import net.osmand.GPXUtilities;
|
||||
import net.osmand.GPXUtilities.GPXFile;
|
||||
import net.osmand.IndexConstants;
|
||||
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.TravelArticleIdentifier;
|
||||
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.util.Algorithms;
|
||||
|
||||
|
@ -75,6 +76,7 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
|
|||
private TravelArticle article;
|
||||
|
||||
private TextView trackButton;
|
||||
private ProgressBar gpxProgress;
|
||||
private TextView saveBtn;
|
||||
|
||||
private WikivoyageWebViewClient webViewClient;
|
||||
|
@ -158,6 +160,9 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
|
|||
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);
|
||||
|
||||
|
@ -305,19 +310,23 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
|
|||
selectedLang = langs.get(0);
|
||||
}
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
webViewClient.setArticle(article);
|
||||
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();
|
||||
ldh.addToHistory(article);
|
||||
|
@ -327,6 +336,21 @@ public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragme
|
|||
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
|
||||
@Override
|
||||
protected String createHtmlContent() {
|
||||
|
|
|
@ -20,6 +20,7 @@ import androidx.core.content.ContextCompat;
|
|||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
|
||||
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
|
||||
|
@ -75,14 +76,15 @@ public class WikivoyageArticleNavigationFragment extends MenuBottomSheetDialogFr
|
|||
return;
|
||||
}
|
||||
|
||||
article = getMyApplication().getTravelHelper().getArticleById(articleId, selectedLang);
|
||||
OsmandApplication app = requiredMyApplication();
|
||||
article = app.getTravelHelper().getArticleById(articleId, selectedLang, false, null);
|
||||
if (article == null) {
|
||||
return;
|
||||
}
|
||||
parentsList = new ArrayList<>(Arrays.asList(article.getAggregatedPartOf().split(",")));
|
||||
|
||||
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> navigationMap
|
||||
= getMyApplication().getTravelHelper().getNavigationMap(article);
|
||||
= app.getTravelHelper().getNavigationMap(article);
|
||||
|
||||
items.add(new TitleItem(getString(R.string.shared_string_navigation)));
|
||||
|
||||
|
|
|
@ -44,6 +44,7 @@ public class TravelArticle {
|
|||
String aggregatedPartOf;
|
||||
|
||||
long lastModified;
|
||||
boolean gpxFileReading;
|
||||
boolean gpxFileRead;
|
||||
|
||||
@NonNull
|
||||
|
|
|
@ -549,7 +549,7 @@ public class TravelDbHelper implements TravelHelper {
|
|||
|
||||
@Override
|
||||
@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;
|
||||
SQLiteConnection conn = openConnection();
|
||||
String routeId = articleId.routeId;
|
||||
|
@ -566,24 +566,27 @@ public class TravelDbHelper implements TravelHelper {
|
|||
if (res == null) {
|
||||
res = localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang);
|
||||
}
|
||||
if (res != null && callback != null) {
|
||||
callback.onGpxFileRead(res.gpxFile);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang) {
|
||||
return getArticleByTitle(title, new QuadRect(), lang);
|
||||
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang, 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) {
|
||||
return getArticleByTitle(title, new QuadRect(), 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, readGpx, callback);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@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;
|
||||
SQLiteConnection conn = openConnection();
|
||||
if (conn != null) {
|
||||
|
@ -596,6 +599,9 @@ public class TravelDbHelper implements TravelHelper {
|
|||
cursor.close();
|
||||
}
|
||||
}
|
||||
if (res != null && callback != null) {
|
||||
callback.onGpxFileRead(res.gpxFile);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@ package net.osmand.plus.wikivoyage.data;
|
|||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import net.osmand.GPXUtilities;
|
||||
import net.osmand.GPXUtilities.GPXFile;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.data.QuadRect;
|
||||
import net.osmand.plus.wikivoyage.data.TravelArticle.TravelArticleIdentifier;
|
||||
|
@ -14,6 +16,11 @@ import java.util.Map;
|
|||
|
||||
public interface TravelHelper {
|
||||
|
||||
interface GpxReadCallback {
|
||||
void onGpxFileReading();
|
||||
void onGpxFileRead(@Nullable GPXFile gpxFile);
|
||||
}
|
||||
|
||||
TravelLocalDataHelper getBookmarksHelper();
|
||||
|
||||
void initializeDataOnAppStartup();
|
||||
|
@ -29,19 +36,19 @@ public interface TravelHelper {
|
|||
List<TravelArticle> getPopularArticles();
|
||||
|
||||
@NonNull
|
||||
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull final TravelArticle article);
|
||||
Map<WikivoyageSearchResult, List<WikivoyageSearchResult>> getNavigationMap(@NonNull TravelArticle article);
|
||||
|
||||
@Nullable
|
||||
TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang);
|
||||
TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang, boolean readGpx, @Nullable GpxReadCallback callback);
|
||||
|
||||
@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
|
||||
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
|
||||
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
|
||||
TravelArticleIdentifier getArticleId(@NonNull String title, @NonNull String lang);
|
||||
|
@ -50,10 +57,10 @@ public interface TravelHelper {
|
|||
ArrayList<String> getArticleLangs(@NonNull TravelArticleIdentifier articleId);
|
||||
|
||||
@NonNull
|
||||
String getGPXName(@NonNull final TravelArticle article);
|
||||
String getGPXName(@NonNull TravelArticle article);
|
||||
|
||||
@NonNull
|
||||
File createGpxFile(@NonNull final TravelArticle article);
|
||||
File createGpxFile(@NonNull TravelArticle article);
|
||||
|
||||
// TODO: this method should be deleted once TravelDBHelper is deleted
|
||||
@Nullable
|
||||
|
|
|
@ -420,7 +420,7 @@ public class TravelLocalDataHelper {
|
|||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
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()) {
|
||||
updateSavedArticle(dbArticle, article);
|
||||
res.add(article);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.osmand.plus.wikivoyage.data;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
@ -97,9 +98,19 @@ public class TravelObfHelper implements TravelHelper {
|
|||
location, POPULAR_ARTICLES_SEARCH_RADIUS, -1, getSearchFilter(false), null);
|
||||
List<Amenity> amenities = reader.searchPoi(req);
|
||||
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) {
|
||||
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) {
|
||||
popularArticles.add(article);
|
||||
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) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
|
@ -128,13 +129,13 @@ public class TravelObfHelper implements TravelHelper {
|
|||
}
|
||||
|
||||
@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;
|
||||
Map<String, TravelArticle> articles = readArticles(file, amenity, false);
|
||||
Map<String, TravelArticle> articles = readArticles(file, amenity);
|
||||
if (!Algorithms.isEmpty(articles)) {
|
||||
TravelArticleIdentifier newArticleId = articles.values().iterator().next().generateIdentifier();
|
||||
cachedArticles.put(newArticleId, articles);
|
||||
article = getCachedArticle(newArticleId, lang, readPoints);
|
||||
article = getCachedArticle(newArticleId, lang, readPoints, callback);
|
||||
}
|
||||
return article;
|
||||
}
|
||||
|
@ -155,17 +156,17 @@ public class TravelObfHelper implements TravelHelper {
|
|||
}
|
||||
|
||||
@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<>();
|
||||
Set<String> langs = getLanguages(amenity);
|
||||
for (String lang : langs) {
|
||||
articles.put(lang, readArticle(file, amenity, lang, readPoints));
|
||||
articles.put(lang, readArticle(file, amenity, lang));
|
||||
}
|
||||
return articles;
|
||||
}
|
||||
|
||||
@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();
|
||||
res.file = file;
|
||||
String title = amenity.getName(lang);
|
||||
|
@ -182,29 +183,11 @@ public class TravelObfHelper implements TravelHelper {
|
|||
res.lang = lang;
|
||||
res.contentsJson = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.CONTENT_JSON, lang));
|
||||
res.aggregatedPartOf = Algorithms.emptyIfNull(amenity.getTagContent(Amenity.IS_AGGR_PART, lang));
|
||||
if (readPoints) {
|
||||
res.gpxFile = buildGpxFile(res);
|
||||
res.gpxFileRead = true;
|
||||
}
|
||||
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
|
||||
private List<Amenity> getPointList(@NonNull final TravelArticle article) {
|
||||
private synchronized List<Amenity> getPointList(@NonNull final TravelArticle article) {
|
||||
final List<Amenity> pointList = new ArrayList<>();
|
||||
final String lang = article.getLang();
|
||||
for (BinaryMapIndexReader reader : getReaders()) {
|
||||
|
@ -312,7 +295,7 @@ public class TravelObfHelper implements TravelHelper {
|
|||
for (Amenity amenity : entry.getValue()) {
|
||||
Set<String> nameLangs = getLanguages(amenity);
|
||||
if (nameLangs.contains(appLang)) {
|
||||
TravelArticle article = readArticle(file, amenity, appLang, false);
|
||||
TravelArticle article = readArticle(file, amenity, appLang);
|
||||
ArrayList<String> langs = new ArrayList<>(nameLangs);
|
||||
Collections.sort(langs, new Comparator<String>() {
|
||||
@Override
|
||||
|
@ -481,20 +464,28 @@ public class TravelObfHelper implements TravelHelper {
|
|||
LOG.error(e.getMessage());
|
||||
}
|
||||
if (!Algorithms.isEmpty(amenities)) {
|
||||
article = readArticle(reader.getFile(), amenities.get(0), lang, false);
|
||||
article = readArticle(reader.getFile(), amenities.get(0), lang);
|
||||
}
|
||||
}
|
||||
return article;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang) {
|
||||
TravelArticle article = getCachedArticle(articleId, lang, true);
|
||||
return article == null ? localDataHelper.getSavedArticle(articleId.file, articleId.routeId, lang) : article;
|
||||
public TravelArticle getArticleById(@NonNull TravelArticleIdentifier articleId, @NonNull String lang,
|
||||
boolean readGpx, @Nullable GpxReadCallback callback) {
|
||||
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
|
||||
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;
|
||||
Map<String, TravelArticle> articles = cachedArticles.get(articleId);
|
||||
if (articles != null) {
|
||||
|
@ -511,16 +502,24 @@ public class TravelObfHelper implements TravelHelper {
|
|||
}
|
||||
}
|
||||
if (article == null && articles == null) {
|
||||
article = findArticleById(articleId, lang);
|
||||
article = findArticleById(articleId, lang, callback);
|
||||
}
|
||||
if (article != null && !article.gpxFileRead && forceReadPoints && !Algorithms.isEmpty(lang)) {
|
||||
article.gpxFile = buildGpxFile(article);
|
||||
article.gpxFileRead = true;
|
||||
if (article != null && forceReadPoints && !Algorithms.isEmpty(lang)) {
|
||||
readGpxFile(article, callback);
|
||||
}
|
||||
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;
|
||||
final boolean isDbArticle = articleId.file != null && articleId.file.getName().endsWith(IndexConstants.BINARY_WIKIVOYAGE_MAP_INDEX_EXT);
|
||||
final List<Amenity> amenities = new ArrayList<>();
|
||||
|
@ -563,7 +562,7 @@ public class TravelObfHelper implements TravelHelper {
|
|||
LOG.error(e.getMessage());
|
||||
}
|
||||
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;
|
||||
|
@ -571,20 +570,23 @@ public class TravelObfHelper implements TravelHelper {
|
|||
|
||||
@Nullable
|
||||
@Override
|
||||
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang) {
|
||||
return getArticleByTitle(title, new QuadRect(), lang);
|
||||
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull final String lang,
|
||||
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) {
|
||||
QuadRect rect = latLon != null ? MapUtils.calculateLatLonBbox(latLon.getLatitude(), latLon.getLongitude(), ARTICLE_SEARCH_RADIUS) : new QuadRect();
|
||||
return getArticleByTitle(title, rect, lang);
|
||||
public TravelArticle getArticleByTitle(@NonNull final String title, @NonNull LatLon latLon,
|
||||
@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);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@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;
|
||||
final List<Amenity> amenities = new ArrayList<>();
|
||||
int x = 0;
|
||||
|
@ -627,7 +629,7 @@ public class TravelObfHelper implements TravelHelper {
|
|||
LOG.error(e.getMessage());
|
||||
}
|
||||
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;
|
||||
|
@ -654,7 +656,7 @@ public class TravelObfHelper implements TravelHelper {
|
|||
}
|
||||
}
|
||||
if (a == null) {
|
||||
TravelArticle article = getArticleByTitle(title, lang);
|
||||
TravelArticle article = getArticleByTitle(title, lang, false, null);
|
||||
if (article != null) {
|
||||
a = article;
|
||||
}
|
||||
|
@ -666,7 +668,7 @@ public class TravelObfHelper implements TravelHelper {
|
|||
@Override
|
||||
public ArrayList<String> getArticleLangs(@NonNull TravelArticleIdentifier articleId) {
|
||||
ArrayList<String> res = new ArrayList<>();
|
||||
TravelArticle article = getArticleById(articleId, "");
|
||||
TravelArticle article = getArticleById(articleId, "", false, null);
|
||||
if (article != null) {
|
||||
Map<String, TravelArticle> articles = cachedArticles.get(article.generateIdentifier());
|
||||
if (articles != null) {
|
||||
|
@ -683,14 +685,14 @@ public class TravelObfHelper implements TravelHelper {
|
|||
|
||||
@NonNull
|
||||
@Override
|
||||
public String getGPXName(@NonNull final TravelArticle article) {
|
||||
public String getGPXName(@NonNull TravelArticle article) {
|
||||
return article.getTitle().replace('/', '_').replace('\'', '_')
|
||||
.replace('\"', '_') + IndexConstants.GPX_FILE_EXT;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public File createGpxFile(@NonNull final TravelArticle article) {
|
||||
public File createGpxFile(@NonNull TravelArticle article) {
|
||||
final GPXFile gpx = article.getGpxFile();
|
||||
File file = app.getAppPath(IndexConstants.GPX_TRAVEL_DIR + getGPXName(article));
|
||||
writeGpxFile(file, gpx);
|
||||
|
@ -706,4 +708,46 @@ public class TravelObfHelper implements TravelHelper {
|
|||
public String getWikivoyageFileName() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import net.osmand.GPXUtilities.WptPt;
|
|||
import net.osmand.data.LatLon;
|
||||
import net.osmand.data.PointDescription;
|
||||
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
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) {
|
||||
SelectedGpxFile selectedGpxFile = mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedGPXFile(wpt);
|
||||
OsmandApplication app = mapActivity.getMyApplication();
|
||||
SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedGPXFile(wpt);
|
||||
GPXFile gpxFile = selectedGpxFile != null ? selectedGpxFile.getGpxFile() : null;
|
||||
Metadata metadata = gpxFile != null ? gpxFile.metadata : null;
|
||||
String title = metadata != null ? metadata.getArticleTitle() : null;
|
||||
String lang = metadata != null ? metadata.getArticleLang() : null;
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue