diff --git a/OsmAnd/res/layout/travel_download_update_card.xml b/OsmAnd/res/layout/travel_download_update_card.xml deleted file mode 100644 index 8ad3bb7779..0000000000 --- a/OsmAnd/res/layout/travel_download_update_card.xml +++ /dev/null @@ -1,198 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/OsmAnd/res/layout/travel_needed_maps_card.xml b/OsmAnd/res/layout/travel_needed_maps_card.xml index c562b1fdd3..1a5aedcb7d 100644 --- a/OsmAnd/res/layout/travel_needed_maps_card.xml +++ b/OsmAnd/res/layout/travel_needed_maps_card.xml @@ -18,16 +18,34 @@ android:layout_margin="@dimen/content_padding" android:orientation="vertical"> - + android:layout_marginBottom="@dimen/bottom_sheet_content_padding_small"> + + + + + + mainIndexItems = new ArrayList<>(); private final List neededIndexItems = new ArrayList<>(); private boolean waitForIndexes; @@ -199,17 +196,24 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv } private void removeRedundantCards() { - if (mainIndexItem != null && mainIndexItem.isDownloaded() && !mainIndexItem.isOutdated()) { - removeDownloadUpdateCard(); - } - boolean allMapsDownloaded = true; - for (IndexItem item : neededIndexItems) { + boolean allTravelGuideDownloaded = true; + for (IndexItem item : mainIndexItems) { if (!item.isDownloaded()) { - allMapsDownloaded = false; + allTravelGuideDownloaded = false; break; } } - if (allMapsDownloaded) { + if (allTravelGuideDownloaded) { + removeDownloadUpdateCard(); + } + boolean neededMapsDownloaded = true; + for (IndexItem item : neededIndexItems) { + if (!item.isDownloaded()) { + neededMapsDownloaded = false; + break; + } + } + if (neededMapsDownloaded) { removeNeededMapsCard(); } } @@ -218,8 +222,9 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv new ProcessIndexItemsTask(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - private void addIndexItemCards(IndexItem mainIndexItem, List neededIndexItems) { - this.mainIndexItem = mainIndexItem; + private void addIndexItemCards(List mainIndexItem, List neededIndexItems) { + this.mainIndexItems.clear(); + this.mainIndexItems.addAll(mainIndexItem); this.neededIndexItems.clear(); this.neededIndexItems.addAll(neededIndexItems); addDownloadUpdateCard(); @@ -228,53 +233,58 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv private void addDownloadUpdateCard() { final OsmandApplication app = getMyApplication(); - if (app != null) { - final DownloadIndexesThread downloadThread = app.getDownloadThread(); - - boolean outdated = mainIndexItem != null && mainIndexItem.isOutdated(); - boolean needsDownloading = mainIndexItem != null && !mainIndexItem.isDownloaded(); - - if (!app.getTravelHelper().isAnyTravelBookPresent() || needsDownloading || (outdated && SHOW_TRAVEL_UPDATE_CARD)) { - boolean showOtherMaps = false; - if (needsDownloading) { - List items = downloadThread.getIndexes().getWikivoyageItems(); - showOtherMaps = items != null && items.size() > 1; + if (app != null && !mainIndexItems.isEmpty() && SHOW_TRAVEL_UPDATE_CARD) { + boolean outdated = isMapsOutdated(); + downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, mainIndexItems, !outdated); + downloadUpdateCard.setListener(new TravelDownloadUpdateCard.CardListener() { + @Override + public void onPrimaryButtonClick() { + if (downloadManager != null) { + downloadManager.startDownload(getMyActivity(), getAllItemsForDownload(mainIndexItems)); + adapter.updateDownloadUpdateCard(false); + } } - downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated); - downloadUpdateCard.setShowOtherMapsBtn(showOtherMaps); - downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() { - @Override - public void onPrimaryButtonClick() { - if (mainIndexItem != null && downloadManager != null) { - downloadManager.startDownload(getMyActivity(), mainIndexItem); - adapter.updateDownloadUpdateCard(false); - } + @Override + public void onSecondaryButtonClick() { + if (downloadUpdateCard.isDownloading()) { + app.getDownloadThread().cancelDownload(mainIndexItems); + adapter.updateDownloadUpdateCard(false); + } else { + SHOW_TRAVEL_UPDATE_CARD = false; + removeDownloadUpdateCard(); } + } - @Override - public void onSecondaryButtonClick() { - if (downloadUpdateCard.isLoading()) { - downloadThread.cancelDownload(mainIndexItem); - adapter.updateDownloadUpdateCard(false); - } else if (!downloadUpdateCard.isDownload()) { - SHOW_TRAVEL_UPDATE_CARD = false; - removeDownloadUpdateCard(); - } else if (downloadUpdateCard.isShowOtherMapsBtn()) { - Activity activity = getActivity(); - if (activity != null) { - Intent newIntent = new Intent(activity, - ((OsmandApplication) activity.getApplication()).getAppCustomization().getDownloadActivity()); - newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); - activity.startActivity(newIntent); - } + @Override + public void onIndexItemClick(IndexItem item) { + if (item.getType() == DownloadActivityType.WIKIPEDIA_FILE && !Version.isPaidVersion(app)) { + FragmentManager fm = getFragmentManager(); + if (fm != null) { + ChoosePlanDialogFragment.showWikipediaInstance(fm); } + } else { + DownloadIndexesThread downloadThread = app.getDownloadThread(); + if (downloadThread.isDownloading(item)) { + downloadThread.cancelDownload(item); + } else if (!item.isDownloaded() && downloadManager != null) { + downloadManager.startDownload(getMyActivity(), item); + } + adapter.updateDownloadUpdateCard(false); } - }); - downloadUpdateCard.setIndexItem(mainIndexItem); - adapter.addDownloadUpdateCard(downloadUpdateCard); + } + }); + adapter.addDownloadUpdateCard(downloadUpdateCard); + } + } + + private boolean isMapsOutdated() { + for (IndexItem indexItem : mainIndexItems) { + if (indexItem.isOutdated()) { + return true; } } + return false; } private void addNeededMapsCard() { @@ -285,7 +295,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv @Override public void onPrimaryButtonClick() { if (downloadManager != null) { - downloadManager.startDownload(getMyActivity(), getAllItemsForDownload()); + downloadManager.startDownload(getMyActivity(), getAllItemsForDownload(neededIndexItems)); adapter.updateNeededMapsCard(false); } } @@ -323,10 +333,10 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv } } - private IndexItem[] getAllItemsForDownload() { + private IndexItem[] getAllItemsForDownload(List indexItems) { boolean paidVersion = Version.isPaidVersion(getMyApplication()); ArrayList res = new ArrayList<>(); - for (IndexItem item : neededIndexItems) { + for (IndexItem item : indexItems) { if (!item.isDownloaded() && (paidVersion || item.getType() != DownloadActivityType.WIKIPEDIA_FILE)) { res.add(item); } @@ -344,7 +354,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv neededMapsCard = null; } - private static class ProcessIndexItemsTask extends AsyncTask>> { + private static class ProcessIndexItemsTask extends AsyncTask, List>> { private static final DownloadActivityType[] types = new DownloadActivityType[]{ DownloadActivityType.NORMAL_FILE, @@ -354,40 +364,41 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadEv private final OsmandApplication app; private final WeakReference weakFragment; - private final String fileName; - ProcessIndexItemsTask(ExploreTabFragment fragment) { app = fragment.getMyApplication(); weakFragment = new WeakReference<>(fragment); - fileName = app != null ? app.getTravelHelper().getWikivoyageFileName() : null; } @Override - protected Pair> doInBackground(Void... voids) { - if (fileName != null) { - IndexItem mainItem = app.getDownloadThread().getIndexes().getWikivoyageItem(fileName); - - List neededItems = new ArrayList<>(); - for (TravelArticle article : app.getTravelHelper().getBookmarksHelper().getSavedArticles()) { - LatLon latLon = new LatLon(article.getLat(), article.getLon()); - try { - for (DownloadActivityType type : types) { - IndexItem item = DownloadResources.findSmallestIndexItemAt(app, latLon, type); - if (item != null && !item.isDownloaded() && !neededItems.contains(item)) { - neededItems.add(item); - } - } - } catch (IOException e) { - // ignore + protected Pair, List> doInBackground(Void... voids) { + List mainItems = new ArrayList<>(); + List allWikivoyageItems = app.getDownloadThread().getIndexes().getWikivoyageItems(); + if (allWikivoyageItems != null) { + for (IndexItem item : allWikivoyageItems) { + if (!item.isDownloaded() && !mainItems.contains(item)) { + mainItems.add(item); } } - return new Pair<>(mainItem, neededItems); } - return null; + List neededItems = new ArrayList<>(); + for (TravelArticle article : app.getTravelHelper().getBookmarksHelper().getSavedArticles()) { + LatLon latLon = new LatLon(article.getLat(), article.getLon()); + try { + for (DownloadActivityType type : types) { + IndexItem item = DownloadResources.findSmallestIndexItemAt(app, latLon, type); + if (item != null && !item.isDownloaded() && !neededItems.contains(item)) { + neededItems.add(item); + } + } + } catch (IOException e) { + // ignore + } + } + return new Pair<>(mainItems, neededItems); } @Override - protected void onPostExecute(Pair> res) { + protected void onPostExecute(Pair, List> res) { ExploreTabFragment fragment = weakFragment.get(); if (res != null && fragment != null && fragment.isResumed()) { fragment.addIndexItemCards(res.first, res.second); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java index d7aaa23bb7..f4a20a83f7 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java @@ -1,257 +1,47 @@ package net.osmand.plus.wikivoyage.explore.travelcards; -import android.graphics.drawable.Drawable; -import android.view.View; -import android.widget.ImageView; -import android.widget.ProgressBar; -import android.widget.TextView; - import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.StringRes; -import androidx.recyclerview.widget.RecyclerView; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.download.IndexItem; -import java.lang.ref.WeakReference; -import java.text.DateFormat; +import java.util.List; -public class TravelDownloadUpdateCard extends BaseTravelCard { +public class TravelDownloadUpdateCard extends TravelNeededMapsCard { public static final int TYPE = 50; - private boolean download; - private boolean showOtherMapsBtn; - private WeakReference ref; + private final boolean download; - private ClickListener listener; - - @Nullable - private IndexItem indexItem; - - private DateFormat dateFormat; - - public boolean isDownload() { - return download; - } - - public boolean isShowOtherMapsBtn() { - return showOtherMapsBtn; - } - - public void setShowOtherMapsBtn(boolean showOtherMapsBtn) { - this.showOtherMapsBtn = showOtherMapsBtn; - } - - public void setListener(ClickListener listener) { - this.listener = listener; - } - - public void setIndexItem(@Nullable IndexItem indexItem) { - this.indexItem = indexItem; - } - - public TravelDownloadUpdateCard(OsmandApplication app, boolean nightMode, boolean download) { - super(app, nightMode); + public TravelDownloadUpdateCard(@NonNull OsmandApplication app, boolean nightMode, @NonNull List items, + boolean download) { + super(app, nightMode, items); this.download = download; - dateFormat = android.text.format.DateFormat.getMediumDateFormat(app); + } + + public int getTitle() { + if (isDownloading()) { + return R.string.shared_string_downloading; + } + return download ? R.string.download_file : R.string.update_is_available; } @Override - public void bindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) { - if (viewHolder instanceof DownloadUpdateVH) { - boolean loading = isLoading(); - DownloadUpdateVH holder = (DownloadUpdateVH) viewHolder; - this.ref = new WeakReference(holder); - holder.title.setText(getTitle(loading)); - holder.icon.setImageResource(getIconRes()); - holder.description.setText(getDescription()); - if (indexItem == null) { - holder.fileDataContainer.setVisibility(View.GONE); - } else { - holder.fileDataContainer.setVisibility(View.VISIBLE); - holder.fileIcon.setImageDrawable(getFileIcon()); - holder.fileTitle.setText(getFileTitle()); - holder.fileDescription.setText(getFileDescription()); - holder.progressBar.setVisibility(loading ? View.VISIBLE : View.GONE); - updateProgressBar(holder); - } - boolean primaryBtnVisible = updatePrimaryButton(holder, loading); - boolean secondaryBtnVisible = updateSecondaryButton(holder, loading); - holder.buttonsDivider.setVisibility(primaryBtnVisible && secondaryBtnVisible ? View.VISIBLE : View.GONE); - } - } - - public void updateProgresBar() { - if(ref != null) { - DownloadUpdateVH holder = ref.get(); - if (holder != null && holder.itemView.isShown()) { - updateProgressBar(holder); - } + public int getDescription() { + if (!isInternetAvailable()) { + return R.string.no_index_file_to_download; } + return download ? R.string.travel_card_download_descr : R.string.travel_card_update_descr; } - private void updateProgressBar(DownloadUpdateVH holder) { - if (isLoadingInProgress()) { - int progress = app.getDownloadThread().getCurrentDownloadingItemProgress(); - holder.progressBar.setProgress(progress < 0 ? 0 : progress); - } else { - holder.progressBar.setProgress(0); - } + @Override + public int getIconRes() { + return download ? R.drawable.travel_card_download_icon : R.drawable.travel_card_update_icon; } @Override public int getCardType() { return TYPE; } - - @NonNull - 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); - } - - private int getIconRes() { - return download ? R.drawable.travel_card_download_icon : R.drawable.travel_card_update_icon; - } - - @NonNull - private String getDescription() { - if (!isInternetAvailable()) { - return app.getString(R.string.no_index_file_to_download); - } - return app.getString(download ? R.string.travel_card_download_descr : R.string.travel_card_update_descr); - } - - @NonNull - private String getFileTitle() { - return indexItem == null ? "" : indexItem.getVisibleName(app, app.getRegions(), false); - } - - @NonNull - private String getFileDescription() { - StringBuilder sb = new StringBuilder(); - if (indexItem != null) { - sb.append(app.getString(R.string.file_size_in_mb, indexItem.getArchiveSizeMB())); - sb.append(" • "); - sb.append(indexItem.getRemoteDate(dateFormat)); - } - return sb.toString(); - } - - private Drawable getFileIcon() { - return getActiveIcon(R.drawable.ic_action_read_article); - } - - /** - * @return true if button is visible, false otherwise. - */ - private boolean updateSecondaryButton(DownloadUpdateVH vh, boolean loading) { - if (loading || !download || showOtherMapsBtn) { - vh.secondaryBtnContainer.setVisibility(View.VISIBLE); - vh.secondaryBtn.setText(getSecondaryBtnTextId(loading)); - vh.secondaryBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (listener != null) { - listener.onSecondaryButtonClick(); - } - } - }); - return true; - } - vh.secondaryBtnContainer.setVisibility(View.GONE); - return false; - } - - @StringRes - private int getSecondaryBtnTextId(boolean loading) { - if (loading) { - return R.string.shared_string_cancel; - } - if (!download) { - return R.string.later; - } - return R.string.download_select_map_types; - } - - /** - * @return true if button is visible, false otherwise. - */ - private boolean updatePrimaryButton(DownloadUpdateVH vh, boolean loading) { - if (!loading) { - boolean enabled = isInternetAvailable(); - vh.primaryBtnContainer.setVisibility(View.VISIBLE); - vh.primaryBtnContainer.setBackgroundResource(getPrimaryBtnBgRes(enabled)); - vh.primaryButton.setTextColor(getResolvedColor(getPrimaryBtnTextColorRes(enabled))); - vh.primaryButton.setEnabled(enabled); - vh.primaryButton.setText(download ? R.string.shared_string_download : R.string.shared_string_update); - vh.primaryButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (listener != null) { - listener.onPrimaryButtonClick(); - } - } - }); - return true; - } - vh.primaryBtnContainer.setVisibility(View.GONE); - 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(); - - void onSecondaryButtonClick(); - } - - public static class DownloadUpdateVH extends RecyclerView.ViewHolder { - - final TextView title; - final ImageView icon; - final TextView description; - final View fileDataContainer; - final ImageView fileIcon; - final TextView fileTitle; - final TextView fileDescription; - final ProgressBar progressBar; - final View secondaryBtnContainer; - final TextView secondaryBtn; - final View buttonsDivider; - final View primaryBtnContainer; - final TextView primaryButton; - - @SuppressWarnings("RedundantCast") - public DownloadUpdateVH(View itemView) { - super(itemView); - title = (TextView) itemView.findViewById(R.id.title); - icon = (ImageView) itemView.findViewById(R.id.icon); - description = (TextView) itemView.findViewById(R.id.description); - fileDataContainer = itemView.findViewById(R.id.file_data_container); - fileIcon = (ImageView) itemView.findViewById(R.id.file_icon); - fileTitle = (TextView) itemView.findViewById(R.id.file_title); - fileDescription = (TextView) itemView.findViewById(R.id.file_description); - progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar); - secondaryBtnContainer = itemView.findViewById(R.id.secondary_btn_container); - secondaryBtn = (TextView) itemView.findViewById(R.id.secondary_button); - buttonsDivider = itemView.findViewById(R.id.buttons_divider); - primaryBtnContainer = itemView.findViewById(R.id.primary_btn_container); - primaryButton = (TextView) itemView.findViewById(R.id.primary_button); - } - } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java index 505a2095f6..900c80f1a2 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelNeededMapsCard.java @@ -9,7 +9,9 @@ import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; +import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; +import androidx.annotation.StringRes; import androidx.recyclerview.widget.RecyclerView; import net.osmand.plus.OsmandApplication; @@ -60,8 +62,12 @@ public class TravelNeededMapsCard extends BaseTravelCard { if (viewHolder instanceof NeededMapsVH) { NeededMapsVH holder = (NeededMapsVH) viewHolder; ref = new WeakReference(holder); - holder.description.setText(isInternetAvailable() - ? R.string.maps_you_need_descr : R.string.no_index_file_to_download); + holder.title.setText(getTitle()); + holder.description.setText(getDescription()); + int iconRes = getIconRes(); + if (iconRes > 0) { + holder.icon.setImageResource(iconRes); + } adjustChildCount(holder.itemsContainer); updateView(holder); @@ -71,7 +77,23 @@ public class TravelNeededMapsCard extends BaseTravelCard { holder.buttonsDivider.setVisibility(primaryBtnVisible && secondaryBtnVisible ? View.VISIBLE : View.GONE); } } - + + @StringRes + public int getTitle() { + return R.string.maps_you_need; + } + + @StringRes + public int getDescription() { + return isInternetAvailable() + ? R.string.maps_you_need_descr : R.string.no_index_file_to_download; + } + + @DrawableRes + public int getIconRes() { + return 0; + } + public void updateView() { if (ref != null) { NeededMapsVH holder = ref.get(); @@ -227,7 +249,9 @@ public class TravelNeededMapsCard extends BaseTravelCard { public static class NeededMapsVH extends RecyclerView.ViewHolder { + final TextView title; final TextView description; + final ImageView icon; final LinearLayout itemsContainer; final View secondaryBtnContainer; final TextView secondaryBtn; @@ -238,7 +262,9 @@ public class TravelNeededMapsCard extends BaseTravelCard { @SuppressWarnings("RedundantCast") public NeededMapsVH(View itemView) { super(itemView); + title = (TextView) itemView.findViewById(R.id.title); description = (TextView) itemView.findViewById(R.id.description); + icon = (ImageView) itemView.findViewById(R.id.icon); itemsContainer = (LinearLayout) itemView.findViewById(R.id.items_container); secondaryBtnContainer = itemView.findViewById(R.id.secondary_btn_container); secondaryBtn = (TextView) itemView.findViewById(R.id.secondary_button);