diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index d77ee68321..f30e085784 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -1,5 +1,8 @@ package net.osmand.plus.download; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.binary.BinaryMapDataObject; @@ -39,7 +42,6 @@ public class DownloadResources extends DownloadResourceGroup { public static final String WORLD_SEAMARKS_NAME = "World_seamarks"; public static final String WORLD_SEAMARKS_OLD_KEY = "world_seamarks_basemap"; public static final String WORLD_SEAMARKS_OLD_NAME = "World_seamarks_basemap"; - public static final String WORLD_WIKIVOYAGE_NAME = "world_wikivoyage"; private static final Log LOG = PlatformUtil.getLog(DownloadResources.class); @@ -71,7 +73,8 @@ public class DownloadResources extends DownloadResourceGroup { return worldMap; } - public IndexItem getWorldWikivoyageItem() { + @Nullable + public IndexItem getWikivoyageItem(@NonNull String fileName) { String groupId = DownloadResourceGroupType.TRAVEL_GROUP.getDefaultId() + "#" + DownloadResourceGroupType.WIKIVOYAGE_MAPS.getDefaultId() + "#" + DownloadResourceGroupType.WIKIVOYAGE_HEADER.getDefaultId(); @@ -80,7 +83,7 @@ public class DownloadResources extends DownloadResourceGroup { List items = wikivoyageHeader.getIndividualResources(); if (items != null) { for (IndexItem ii : items) { - if (ii.getBasename().equalsIgnoreCase(DownloadResources.WORLD_WIKIVOYAGE_NAME)) { + if (ii.getFileName().equals(fileName)) { return ii; } } diff --git a/OsmAnd/src/net/osmand/plus/helpers/FileNameTranslationHelper.java b/OsmAnd/src/net/osmand/plus/helpers/FileNameTranslationHelper.java index c89a3862e3..30d7b2a730 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/FileNameTranslationHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/FileNameTranslationHelper.java @@ -220,7 +220,7 @@ public class FileNameTranslationHelper { } else if (basename.equals(DownloadResources.WORLD_SEAMARKS_KEY) || basename.equals(DownloadResources.WORLD_SEAMARKS_OLD_KEY)) { return ctx.getString(R.string.index_item_world_seamarks); - } else if (basename.equals(DownloadResources.WORLD_WIKIVOYAGE_NAME)) { + } else if (basename.equals("world_wikivoyage")) { return ctx.getString(R.string.index_item_world_wikivoyage); } else if (basename.equals("depth_contours_osmand_ext")) { return ctx.getString(R.string.index_item_depth_contours_osmand_ext); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java index 7fa8fdd95f..6a1b3958b7 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java @@ -271,17 +271,17 @@ public class WikipediaDialogFragment extends DialogFragment { return (OsmandApplication) getActivity().getApplication(); } - public static boolean showInstance(AppCompatActivity activity, Amenity amenity) { + public static boolean showInstance(AppCompatActivity activity, Amenity amenity, String lang) { try { if (!amenity.getType().isWiki()) { return false; } OsmandApplication app = (OsmandApplication) activity.getApplication(); - String lang = app.getSettings().MAP_PREFERRED_LOCALE.get(); WikipediaDialogFragment wikipediaDialogFragment = new WikipediaDialogFragment(); wikipediaDialogFragment.setAmenity(amenity); - wikipediaDialogFragment.setLanguage(lang); + wikipediaDialogFragment.setLanguage(lang == null ? + app.getSettings().MAP_PREFERRED_LOCALE.get() : lang); wikipediaDialogFragment.setRetainInstance(true); wikipediaDialogFragment.show(activity.getSupportFragmentManager(), TAG); return true; @@ -289,4 +289,8 @@ public class WikipediaDialogFragment extends DialogFragment { return false; } } + + public static boolean showInstance(AppCompatActivity activity, Amenity amenity) { + return showInstance(activity, amenity, null); + } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java index 00ad198b8d..74982c22e1 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java @@ -91,8 +91,9 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall } return true; } else if (url.contains(WIKI_DOMAIN)) { - String articleName = getArticleNameFromUrl(url, getLang(url)); - getWikiArticle(articleName, url); + String lang = getLang(url); + String articleName = getArticleNameFromUrl(url, lang); + getWikiArticle(articleName, lang, url); } else if (url.startsWith(PAGE_PREFIX_HTTP) || url.startsWith(PAGE_PREFIX_HTTPS)) { warnAboutExternalLoad(url, context, nightMode); } else if (url.startsWith(PREFIX_GEO)) { @@ -181,14 +182,14 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall } } - private void getWikiArticle(String name, String url) { + private void getWikiArticle(String name, String lang, String url) { List indexes = app.getResourceManager() .getWikiAmenityRepository(article.getLat(), article.getLon()); if (indexes.isEmpty()) { WikivoyageArticleWikiLinkFragment.showInstance(fragmentManager, regionName == null ? "" : regionName, url); } else { - articleSearchTask = new WikiArticleSearchTask(name, indexes, (MapActivity) context, nightMode, url); + articleSearchTask = new WikiArticleSearchTask(name, lang, indexes, (MapActivity) context, nightMode, url); articleSearchTask.execute(); } @@ -274,10 +275,12 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall private WeakReference weakContext; private boolean isNightMode; private String url; + private String lang; - WikiArticleSearchTask(String articleName, List indexes, + WikiArticleSearchTask(String articleName, String lang, List indexes, MapActivity context, boolean isNightMode, String url) { name = articleName; + this.lang = lang; this.indexes = indexes; weakContext = new WeakReference<>(context); dialog = createProgressDialog(); @@ -317,7 +320,7 @@ public class WikivoyageWebViewClient extends WebViewClient implements RegionCall if (activity != null && !activity.isActivityDestroyed() && dialog != null) { dialog.dismiss(); if (!found.isEmpty()) { - WikipediaDialogFragment.showInstance(activity, found.get(0)); + WikipediaDialogFragment.showInstance(activity, found.get(0), lang); } else { warnAboutExternalLoad(url, weakContext.get(), isNightMode); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java index 6b859889c0..bef0832780 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java @@ -45,7 +45,6 @@ public class WikivoyageWelcomeDialogFragment extends WikivoyageBaseDialogFragmen public void onClick(View v) { FragmentActivity activity = getActivity(); if (activity != null) { - getMyApplication().getTravelDbHelper().initTravelBooks(); WikivoyageExploreDialogFragment.showInstance(activity.getSupportFragmentManager()); dismiss(); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index c4d513fe14..4aa32e1083 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -14,15 +14,11 @@ import android.widget.Toast; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.Version; -import net.osmand.plus.activities.LocalIndexHelper; -import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.download.DownloadIndexesThread; -import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; -import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; import net.osmand.plus.wikivoyage.data.TravelArticle; import net.osmand.plus.wikivoyage.data.TravelDbHelper; import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard; @@ -32,6 +28,7 @@ import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard; +import java.io.File; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; @@ -47,7 +44,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn private IndexItem indexItem; - private boolean worldWikivoyageDownloaded; + private File selectedTravelBook; private boolean downloadIndexesRequested; private boolean downloadUpdateCardAdded; @@ -71,7 +68,10 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn public void newDownloadIndexes() { if (downloadIndexesRequested) { downloadIndexesRequested = false; - indexItem = getMyApplication().getDownloadThread().getIndexes().getWorldWikivoyageItem(); + if (selectedTravelBook != null) { + indexItem = getMyApplication().getDownloadThread().getIndexes() + .getWikivoyageItem(selectedTravelBook.getName()); + } addDownloadUpdateCard(false); } } @@ -80,7 +80,9 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn public void downloadInProgress() { DownloadIndexesThread downloadThread = getMyApplication().getDownloadThread(); IndexItem current = downloadThread.getCurrentDownloadingItem(); - indexItem = downloadThread.getIndexes().getWorldWikivoyageItem(); + if (selectedTravelBook != null) { + indexItem = downloadThread.getIndexes().getWikivoyageItem(selectedTravelBook.getName()); + } if (current != null && indexItem != null && current == indexItem @@ -109,7 +111,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn boolean outdated = indexItem != null && indexItem.isOutdated(); - if (!worldWikivoyageDownloaded || outdated) { + if (selectedTravelBook == null || outdated) { downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated); downloadUpdateCard.setLoadingInProgress(loadingInProgress); downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() { @@ -150,7 +152,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn final List items = new ArrayList<>(); final OsmandApplication app = getMyApplication(); - runWorldWikivoyageFileCheck(); + checkSelectedTravelBook(); startEditingTravelCard = new StartEditingTravelCard(app, nightMode); addOpenBetaTravelCard(items, nightMode); items.add(startEditingTravelCard); @@ -163,24 +165,22 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn return items; } - private void runWorldWikivoyageFileCheck() { + private void checkSelectedTravelBook() { final OsmandApplication app = getMyApplication(); - new CheckWorldWikivoyageTask(app, new CheckWorldWikivoyageTask.Callback() { - @Override - public void onCheckFinished(boolean worldWikivoyageDownloaded) { - ExploreTabFragment.this.worldWikivoyageDownloaded = worldWikivoyageDownloaded; - DownloadIndexesThread downloadThread = app.getDownloadThread(); - if (!downloadThread.getIndexes().isDownloadedFromInternet) { - downloadIndexesRequested = true; - app.getDownloadThread().runReloadIndexFilesSilent(); - } else { - indexItem = downloadThread.getIndexes().getWorldWikivoyageItem(); - IndexItem current = downloadThread.getCurrentDownloadingItem(); - boolean loadingInProgress = current != null && indexItem != null && current == indexItem; - addDownloadUpdateCard(loadingInProgress); - } + final DownloadIndexesThread downloadThread = app.getDownloadThread(); + selectedTravelBook = app.getTravelDbHelper().getSelectedTravelBook(); + + if (!downloadThread.getIndexes().isDownloadedFromInternet) { + downloadIndexesRequested = true; + downloadThread.runReloadIndexFilesSilent(); + } else { + if (selectedTravelBook != null) { + indexItem = downloadThread.getIndexes().getWikivoyageItem(selectedTravelBook.getName()); } - }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + IndexItem current = downloadThread.getCurrentDownloadingItem(); + boolean loadingInProgress = current != null && indexItem != null && current == indexItem; + addDownloadUpdateCard(loadingInProgress); + } } private void addPopularDestinations(OsmandApplication app) { @@ -196,45 +196,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn } } - private static class CheckWorldWikivoyageTask extends AsyncTask { - - private OsmandApplication app; - private Callback callback; - - CheckWorldWikivoyageTask(OsmandApplication app, Callback callback) { - this.app = app; - this.callback = callback; - } - - @Override - protected Boolean doInBackground(Void... voids) { - final boolean[] worldWikivoyageDownloaded = new boolean[1]; - new LocalIndexHelper(app).getLocalTravelFiles(new AbstractLoadLocalIndexTask() { - @Override - public void loadFile(LocalIndexInfo... loaded) { - for (LocalIndexInfo lii : loaded) { - if (lii.getBaseName().toLowerCase().equals(DownloadResources.WORLD_WIKIVOYAGE_NAME)) { - worldWikivoyageDownloaded[0] = true; - } - } - } - }); - return worldWikivoyageDownloaded[0]; - } - - @Override - protected void onPostExecute(Boolean worldWikivoyageDownloaded) { - if (callback != null) { - callback.onCheckFinished(worldWikivoyageDownloaded); - } - callback = null; - } - - interface Callback { - void onCheckFinished(boolean worldWikivoyageDownloaded); - } - } - private static class PopularDestinationsSearchTask extends AsyncTask> { private TravelDbHelper travelDbHelper;