Add the ability to download maps from "Maps you need" card

This commit is contained in:
Alex Sytnyk 2018-05-07 17:42:10 +03:00
parent 0f3376a427
commit 7958b7943c
3 changed files with 106 additions and 74 deletions

View file

@ -15,7 +15,6 @@ import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import net.osmand.data.LatLon;
import net.osmand.plus.OsmandApplication;
@ -53,10 +52,12 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
private TravelDownloadUpdateCard downloadUpdateCard;
private TravelNeededMapsCard neededMapsCard;
private IndexItem currentDownloadingIndexItem;
private IndexItem mainIndexItem;
private List<IndexItem> neededIndexItems;
private List<IndexItem> neededIndexItems = new ArrayList<>();
private boolean waitForIndexes;
@SuppressWarnings("RedundantCast")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -81,35 +82,26 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
@Override
public void downloadInProgress() {
if (mainIndexItem != null) {
DownloadIndexesThread downloadThread = getMyApplication().getDownloadThread();
IndexItem current = downloadThread.getCurrentDownloadingItem();
if (downloadUpdateCard != null
&& current != null
&& current == mainIndexItem
&& (!current.isDownloaded() || current.isOutdated())) {
downloadUpdateCard.setProgress(downloadThread.getCurrentDownloadingItemProgress());
adapter.updateDownloadUpdateCard();
}
IndexItem current = getMyApplication().getDownloadThread().getCurrentDownloadingItem();
if (current != null && current != currentDownloadingIndexItem) {
currentDownloadingIndexItem = current;
removeRedundantCards();
}
adapter.updateDownloadUpdateCard();
adapter.updateNeededMapsCard();
}
@Override
public void downloadHasFinished() {
if (mainIndexItem != null) {
final OsmandApplication app = getMyApplication();
File targetFile = mainIndexItem.getTargetFile(app);
if (downloadUpdateCard != null && targetFile.exists()) {
downloadUpdateCard.setLoadingInProgress(false);
removeDownloadUpdateCard();
TravelDbHelper travelDbHelper = app.getTravelDbHelper();
travelDbHelper.initTravelBooks();
travelDbHelper.selectTravelBook(targetFile);
Fragment parent = getParentFragment();
if (parent != null && parent instanceof WikivoyageExploreDialogFragment) {
((WikivoyageExploreDialogFragment) parent).populateData();
}
TravelDbHelper travelDbHelper = getMyApplication().getTravelDbHelper();
if (travelDbHelper.getSelectedTravelBook() == null) {
getMyApplication().getTravelDbHelper().initTravelBooks();
Fragment parent = getParentFragment();
if (parent != null && parent instanceof WikivoyageExploreDialogFragment) {
((WikivoyageExploreDialogFragment) parent).populateData();
}
} else {
removeRedundantCards();
}
}
@ -150,13 +142,30 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
}
}
private void removeRedundantCards() {
if (mainIndexItem != null && mainIndexItem.isDownloaded() && !mainIndexItem.isOutdated()) {
removeDownloadUpdateCard();
}
boolean allMapsDownloaded = true;
for (IndexItem item : neededIndexItems) {
if (!item.isDownloaded()) {
allMapsDownloaded = false;
break;
}
}
if (allMapsDownloaded) {
removeNeededMapsCard();
}
}
private void checkDownloadIndexes() {
new ProcessIndexItemsTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void addIndexItemCards(IndexItem mainIndexItem, List<IndexItem> neededIndexItems) {
this.mainIndexItem = mainIndexItem;
this.neededIndexItems = neededIndexItems;
this.neededIndexItems.clear();
this.neededIndexItems.addAll(neededIndexItems);
addDownloadUpdateCard();
addNeededMapsCard();
}
@ -169,9 +178,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
File selectedTravelBook = app.getTravelDbHelper().getSelectedTravelBook();
if (selectedTravelBook == null || outdated) {
IndexItem current = downloadThread.getCurrentDownloadingItem();
boolean loadingInProgress = mainIndexItem != null && current != null && mainIndexItem == current;
boolean showOtherMaps = false;
if (selectedTravelBook == null) {
List<IndexItem> items = downloadThread.getIndexes().getWikivoyageItems();
@ -180,22 +186,19 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated);
downloadUpdateCard.setShowOtherMapsBtn(showOtherMaps);
downloadUpdateCard.setLoadingInProgress(loadingInProgress);
downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() {
@Override
public void onPrimaryButtonClick() {
if (mainIndexItem != null) {
new DownloadValidationManager(app).startDownload(getMyActivity(), mainIndexItem);
downloadUpdateCard.setLoadingInProgress(true);
adapter.updateDownloadUpdateCard();
}
}
@Override
public void onSecondaryButtonClick() {
if (downloadUpdateCard.isLoadingInProgress()) {
if (downloadUpdateCard.isLoading()) {
downloadThread.cancelDownload(mainIndexItem);
downloadUpdateCard.setLoadingInProgress(false);
adapter.updateDownloadUpdateCard();
} else if (!downloadUpdateCard.isDownload()) {
removeDownloadUpdateCard();
@ -217,19 +220,22 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
private void addNeededMapsCard() {
if (!neededIndexItems.isEmpty()) {
neededMapsCard = new TravelNeededMapsCard(getMyApplication(), nightMode, neededIndexItems);
final OsmandApplication app = getMyApplication();
neededMapsCard = new TravelNeededMapsCard(app, nightMode, neededIndexItems);
neededMapsCard.setListener(new TravelNeededMapsCard.CardListener() {
@Override
public void onPrimaryButtonClick() {
if (!neededMapsCard.isDownloadingAll()) {
Toast.makeText(getContext(), "Download all", Toast.LENGTH_SHORT).show();
}
IndexItem[] items = neededIndexItems.toArray(new IndexItem[neededIndexItems.size()]);
new DownloadValidationManager(app).startDownload(getMyActivity(), items);
adapter.updateNeededMapsCard();
}
@Override
public void onSecondaryButtonClick() {
if (neededMapsCard.isDownloading()) {
Toast.makeText(getContext(), "Cancel", Toast.LENGTH_SHORT).show();
app.getDownloadThread().cancelDownload(neededIndexItems);
adapter.updateNeededMapsCard();
} else {
removeNeededMapsCard();
}

View file

@ -23,8 +23,6 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
private boolean download;
private boolean showOtherMapsBtn;
private boolean loadingInProgress;
private int progress;
private ClickListener listener;
@ -45,18 +43,6 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
this.showOtherMapsBtn = showOtherMapsBtn;
}
public boolean isLoadingInProgress() {
return loadingInProgress;
}
public void setLoadingInProgress(boolean loadingInProgress) {
this.loadingInProgress = loadingInProgress;
}
public void setProgress(int progress) {
this.progress = progress;
}
public void setListener(ClickListener listener) {
this.listener = listener;
}
@ -74,8 +60,9 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
@Override
public void bindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof DownloadUpdateVH) {
boolean loading = isLoading();
DownloadUpdateVH holder = (DownloadUpdateVH) viewHolder;
holder.title.setText(getTitle());
holder.title.setText(getTitle(loading));
holder.icon.setImageDrawable(getIcon());
holder.description.setText(getDescription());
if (indexItem == null) {
@ -85,11 +72,16 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
holder.fileIcon.setImageDrawable(getFileIcon());
holder.fileTitle.setText(getFileTitle());
holder.fileDescription.setText(getFileDescription());
holder.progressBar.setVisibility(loadingInProgress ? View.VISIBLE : View.GONE);
holder.progressBar.setProgress(progress < 0 ? 0 : progress);
holder.progressBar.setVisibility(loading ? View.VISIBLE : View.GONE);
if (isLoadingInProgress()) {
int progress = app.getDownloadThread().getCurrentDownloadingItemProgress();
holder.progressBar.setProgress(progress < 0 ? 0 : progress);
} else {
holder.progressBar.setProgress(0);
}
}
boolean primaryBtnVisible = updatePrimaryButton(holder);
boolean secondaryBtnVisible = updateSecondaryButton(holder);
boolean primaryBtnVisible = updatePrimaryButton(holder, loading);
boolean secondaryBtnVisible = updateSecondaryButton(holder, loading);
holder.buttonsDivider.setVisibility(primaryBtnVisible && secondaryBtnVisible ? View.VISIBLE : View.GONE);
}
}
@ -100,8 +92,8 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
}
@NonNull
private String getTitle() {
if (loadingInProgress) {
private String getTitle(boolean loading) {
if (loading) {
return app.getString(R.string.shared_string_downloading) + "...";
}
return app.getString(download ? R.string.download_file : R.string.update_is_available);
@ -143,10 +135,10 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
/**
* @return true if button is visible, false otherwise.
*/
private boolean updateSecondaryButton(DownloadUpdateVH vh) {
if (loadingInProgress || !download || showOtherMapsBtn) {
private boolean updateSecondaryButton(DownloadUpdateVH vh, boolean loading) {
if (loading || !download || showOtherMapsBtn) {
vh.secondaryBtnContainer.setVisibility(View.VISIBLE);
vh.secondaryBtn.setText(getSecondaryBtnTextId());
vh.secondaryBtn.setText(getSecondaryBtnTextId(loading));
vh.secondaryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -162,8 +154,8 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
}
@StringRes
private int getSecondaryBtnTextId() {
if (loadingInProgress) {
private int getSecondaryBtnTextId(boolean loading) {
if (loading) {
return R.string.shared_string_cancel;
}
if (!download) {
@ -175,8 +167,8 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
/**
* @return true if button is visible, false otherwise.
*/
private boolean updatePrimaryButton(DownloadUpdateVH vh) {
if (!loadingInProgress) {
private boolean updatePrimaryButton(DownloadUpdateVH vh, boolean loading) {
if (!loading) {
boolean enabled = isInternetAvailable();
vh.primaryBtnContainer.setVisibility(View.VISIBLE);
vh.primaryBtnContainer.setBackgroundResource(getPrimaryBtnBgRes(enabled));
@ -197,6 +189,15 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
return false;
}
public boolean isLoading() {
return indexItem != null && app.getDownloadThread().isDownloading(indexItem);
}
private boolean isLoadingInProgress() {
IndexItem current = app.getDownloadThread().getCurrentDownloadingItem();
return indexItem != null && current != null && indexItem == current;
}
public interface ClickListener {
void onPrimaryButtonClick();

View file

@ -7,6 +7,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import net.osmand.plus.OsmandApplication;
@ -24,6 +25,7 @@ public class TravelNeededMapsCard extends BaseTravelCard {
private List<IndexItem> items;
private Drawable downloadIcon;
private Drawable cancelIcon;
private CardListener listener;
@ -36,26 +38,49 @@ public class TravelNeededMapsCard extends BaseTravelCard {
downloadThread = app.getDownloadThread();
this.items = items;
downloadIcon = getActiveIcon(R.drawable.ic_action_import);
cancelIcon = getActiveIcon(R.drawable.ic_action_remove_dark);
}
@Override
public void bindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof NeededMapsVH) {
NeededMapsVH holder = (NeededMapsVH) viewHolder;
holder.description.setText(isInternetAvailable()
? R.string.maps_you_need_descr : R.string.no_index_file_to_download);
adjustChildCount(holder.itemsContainer);
for (int i = 0; i < items.size(); i++) {
boolean lastItem = i == items.size() - 1;
IndexItem item = items.get(i);
boolean downloading = downloadThread.isDownloading(item);
boolean currentDownloading = downloading && downloadThread.getCurrentDownloadingItem() == item;
boolean lastItem = i == items.size() - 1;
View view = holder.itemsContainer.getChildAt(i);
((ImageView) view.findViewById(R.id.icon))
.setImageDrawable(getActiveIcon(item.getType().getIconResource()));
((TextView) view.findViewById(R.id.title)).setText(item.getVisibleName(app, app.getRegions(), false));
((TextView) view.findViewById(R.id.title))
.setText(item.getVisibleName(app, app.getRegions(), false));
((TextView) view.findViewById(R.id.description)).setText(getItemDescription(item));
((ImageView) view.findViewById(R.id.icon_action)).setImageDrawable(downloadIcon);
ImageView iconAction = (ImageView) view.findViewById(R.id.icon_action);
iconAction.setVisibility(item.isDownloaded() ? View.GONE : View.VISIBLE);
if (!item.isDownloaded()) {
iconAction.setImageDrawable(downloading ? cancelIcon : downloadIcon);
}
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
progressBar.setVisibility(downloading ? View.VISIBLE : View.GONE);
if (currentDownloading) {
int progress = downloadThread.getCurrentDownloadingItemProgress();
progressBar.setProgress(progress < 0 ? 0 : progress);
} else {
progressBar.setProgress(0);
}
view.findViewById(R.id.divider).setVisibility(lastItem ? View.GONE : View.VISIBLE);
}
boolean primaryBtnVisible = updatePrimaryButton(holder);
boolean secondaryBtnVisible = updateSecondaryButton(holder);
holder.buttonsDivider.setVisibility(primaryBtnVisible && secondaryBtnVisible ? View.VISIBLE : View.GONE);
@ -104,7 +129,7 @@ public class TravelNeededMapsCard extends BaseTravelCard {
* @return true if button is visible, false otherwise.
*/
private boolean updatePrimaryButton(NeededMapsVH vh) {
if (!isDownloadingAll()) {
if (showPrimaryButton()) {
boolean enabled = isInternetAvailable();
vh.primaryBtnContainer.setVisibility(View.VISIBLE);
vh.primaryBtnContainer.setBackgroundResource(getPrimaryBtnBgRes(enabled));
@ -134,13 +159,13 @@ public class TravelNeededMapsCard extends BaseTravelCard {
return false;
}
public boolean isDownloadingAll() {
private boolean showPrimaryButton() {
for (IndexItem item : items) {
if (!downloadThread.isDownloading(item)) {
return false;
if (!item.isDownloaded() && !downloadThread.isDownloading(item)) {
return true;
}
}
return true;
return false;
}
private String getItemDescription(IndexItem item) {