Remove redundant AsyncTask; search travel index item by file name

This commit is contained in:
Alex Sytnyk 2018-04-28 13:50:05 +03:00
parent dcafa52e59
commit a3462e443f
3 changed files with 32 additions and 68 deletions

View file

@ -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<IndexItem> 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;
}
}

View file

@ -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);

View file

@ -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<BaseTravelCard> 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<Void, Void, Boolean> {
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<Void, TravelDbHelper, List<TravelArticle>> {
private TravelDbHelper travelDbHelper;