From 4946440e573517845121a61acbf96ade1a036c7b Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Thu, 26 Apr 2018 18:31:43 +0300 Subject: [PATCH] Improve wikivoyage download / update card --- .../layout/travel_download_update_card.xml | 1 + .../plus/download/DownloadIndexesThread.java | 56 ++++++++++------- .../plus/download/DownloadResources.java | 21 +++++++ .../explore/ExploreTabFragment.java | 61 ++++++++++++++++--- .../travelcards/TravelDownloadUpdateCard.java | 37 +++++++++-- 5 files changed, 139 insertions(+), 37 deletions(-) diff --git a/OsmAnd/res/layout/travel_download_update_card.xml b/OsmAnd/res/layout/travel_download_update_card.xml index ef7254a882..d73fe6fcfc 100644 --- a/OsmAnd/res/layout/travel_download_update_card.xml +++ b/OsmAnd/res/layout/travel_download_update_card.xml @@ -55,6 +55,7 @@ uiCallbacks = new HashSet<>(); private DatabaseHelper dbHelper; private DownloadFileHelper downloadFileHelper; private List> currentRunningTask = Collections.synchronizedList(new ArrayList>()); @@ -87,19 +87,19 @@ public class DownloadIndexesThread { /// UI notifications methods public void setUiActivity(DownloadEvents uiActivity) { - this.uiActivity = uiActivity; + uiCallbacks.add(uiActivity); } public void resetUiActivity(DownloadEvents uiActivity) { - if (this.uiActivity == uiActivity) { - this.uiActivity = null; - } + uiCallbacks.remove(uiActivity); } @UiThread protected void downloadInProgress() { - if (uiActivity != null) { - uiActivity.downloadInProgress(); + for (DownloadEvents uiActivity : uiCallbacks) { + if (uiActivity != null) { + uiActivity.downloadInProgress(); + } } updateNotification(); } @@ -151,8 +151,10 @@ public class DownloadIndexesThread { @UiThread protected void downloadHasFinished() { - if (uiActivity != null) { - uiActivity.downloadHasFinished(); + for (DownloadEvents uiActivity : uiCallbacks) { + if (uiActivity != null) { + uiActivity.downloadHasFinished(); + } } updateNotification(); } @@ -186,8 +188,10 @@ public class DownloadIndexesThread { @UiThread protected void newDownloadIndexes() { - if (uiActivity != null) { - uiActivity.newDownloadIndexes(); + for (DownloadEvents uiActivity : uiCallbacks) { + if (uiActivity != null) { + uiActivity.newDownloadIndexes(); + } } } @@ -254,8 +258,10 @@ public class DownloadIndexesThread { return; } } - if(uiActivity instanceof Activity) { - app.logEvent((Activity) uiActivity, "download_files"); + for (DownloadEvents uiActivity : uiCallbacks) { + if(uiActivity instanceof Activity) { + app.logEvent((Activity) uiActivity, "download_files"); + } } for(IndexItem item : items) { if (!item.equals(currentDownloadingItem) && !indexItemDownloading.contains(item)) { @@ -454,10 +460,12 @@ public class DownloadIndexesThread { currentRunningTask.add(this); super.onPreExecute(); downloadFileHelper.setInterruptDownloading(false); - if (uiActivity instanceof Activity) { - View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); - if (mainView != null) { - mainView.setKeepScreenOn(true); + for (DownloadEvents uiActivity : uiCallbacks) { + if (uiActivity instanceof Activity) { + View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); + if (mainView != null) { + mainView.setKeepScreenOn(true); + } } } startTask(ctx.getString(R.string.shared_string_downloading) + ctx.getString(R.string.shared_string_ellipsis), -1); @@ -468,10 +476,12 @@ public class DownloadIndexesThread { if (result != null && result.length() > 0) { Toast.makeText(ctx, result, Toast.LENGTH_LONG).show(); } - if (uiActivity instanceof Activity) { - View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); - if (mainView != null) { - mainView.setKeepScreenOn(false); + for (DownloadEvents uiActivity : uiCallbacks) { + if (uiActivity instanceof Activity) { + View mainView = ((Activity) uiActivity).findViewById(R.id.MainLayout); + if (mainView != null) { + mainView.setKeepScreenOn(false); + } } } currentRunningTask.remove(this); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 46c7420309..7147eb1625 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -71,6 +71,27 @@ public class DownloadResources extends DownloadResourceGroup { return worldMap; } + public IndexItem getWorldWikivoyageItem() { + DownloadResourceGroup travelGroup = getSubGroupById(DownloadResourceGroupType.TRAVEL_GROUP.getDefaultId()); + if (travelGroup != null) { + DownloadResourceGroup wikivoyageMaps = travelGroup.getSubGroupById(DownloadResourceGroupType.WIKIVOYAGE_MAPS.getDefaultId()); + if (wikivoyageMaps != null) { + DownloadResourceGroup wikivoyageHeader = wikivoyageMaps.getSubGroupById(DownloadResourceGroupType.WIKIVOYAGE_HEADER.getDefaultId()); + if (wikivoyageHeader != null) { + List items = wikivoyageHeader.getIndividualResources(); + if (items != null) { + for (IndexItem ii : items) { + if (ii.getBasename().equalsIgnoreCase(DownloadResources.WORLD_WIKIVOYAGE_NAME)) { + return ii; + } + } + } + } + } + } + return null; + } + public IndexItem getIndexItem(String fileName) { IndexItem res = null; if (rawResources == null) { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index c9c0587e11..cd58c8b073 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -17,7 +17,9 @@ 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.IndexItem; import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; import net.osmand.plus.wikivoyage.data.TravelArticle; import net.osmand.plus.wikivoyage.data.TravelDbHelper; @@ -32,7 +34,7 @@ import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; -public class ExploreTabFragment extends BaseOsmAndFragment { +public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIndexesThread.DownloadEvents { private static final int DOWNLOAD_UPDATE_CARD_POSITION = 0; @@ -41,6 +43,9 @@ public class ExploreTabFragment extends BaseOsmAndFragment { private boolean nightMode; + private boolean worldWikivoyageDownloaded; + private boolean downloadIndexesRequested; + @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -57,11 +62,52 @@ public class ExploreTabFragment extends BaseOsmAndFragment { return mainView; } + @Override + public void onResume() { + super.onResume(); + getMyApplication().getDownloadThread().setUiActivity(this); + } + + @Override + public void onPause() { + super.onPause(); + getMyApplication().getDownloadThread().resetUiActivity(this); + } + + @Override + public void newDownloadIndexes() { + if (downloadIndexesRequested) { + downloadIndexesRequested = false; + OsmandApplication app = getMyApplication(); + + IndexItem wikivoyageItem = app.getDownloadThread().getIndexes().getWorldWikivoyageItem(); + boolean outdated = wikivoyageItem != null && wikivoyageItem.isOutdated(); + + if (!worldWikivoyageDownloaded || outdated) { + TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, !outdated); + card.setIndexItem(wikivoyageItem); + if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) { + adapter.notifyDataSetChanged(); + } + } + } + } + + @Override + public void downloadInProgress() { + + } + + @Override + public void downloadHasFinished() { + + } + private List generateItems() { final List items = new ArrayList<>(); final OsmandApplication app = getMyApplication(); - addDownloadUpdateCard(); + runWorldWikivoyageFileCheck(); startEditingTravelCard = new StartEditingTravelCard(app, nightMode); addOpenBetaTravelCard(items, nightMode); items.add(startEditingTravelCard); @@ -71,17 +117,14 @@ public class ExploreTabFragment extends BaseOsmAndFragment { return items; } - private void addDownloadUpdateCard() { + private void runWorldWikivoyageFileCheck() { final OsmandApplication app = getMyApplication(); new CheckWorldWikivoyageTask(app, new CheckWorldWikivoyageTask.Callback() { @Override public void onCheckFinished(boolean worldWikivoyageDownloaded) { - if (!worldWikivoyageDownloaded && adapter != null) { - TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, true); - if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) { - adapter.notifyDataSetChanged(); - } - } + ExploreTabFragment.this.worldWikivoyageDownloaded = worldWikivoyageDownloaded; + downloadIndexesRequested = true; + app.getDownloadThread().runReloadIndexFilesSilent(); } }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } 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 d312621fb5..306d504416 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java @@ -2,6 +2,7 @@ package net.osmand.plus.wikivoyage.explore.travelcards; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; import android.support.v7.widget.RecyclerView; import android.view.View; @@ -12,6 +13,9 @@ import android.widget.Toast; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.download.IndexItem; + +import java.text.DateFormat; public class TravelDownloadUpdateCard extends BaseTravelCard { @@ -20,9 +24,19 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { private boolean download; private boolean loadingInProgress; + @Nullable + private IndexItem indexItem; + + private DateFormat dateFormat; + + public void setIndexItem(@Nullable IndexItem indexItem) { + this.indexItem = indexItem; + } + public TravelDownloadUpdateCard(OsmandApplication app, boolean nightMode, boolean download) { super(app, nightMode); this.download = download; + dateFormat = android.text.format.DateFormat.getMediumDateFormat(app); } @Override @@ -32,9 +46,14 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { holder.title.setText(getTitle()); holder.icon.setImageDrawable(getIcon()); holder.description.setText(getDescription()); - holder.fileIcon.setImageDrawable(getFileIcon()); - holder.fileTitle.setText(getFileTitle()); - holder.fileDescription.setText(getFileDescription()); + 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()); + } boolean primaryBtnVisible = updatePrimaryButton(holder); boolean secondaryBtnVisible = updateSecondaryButton(holder); holder.buttonsDivider.setVisibility(primaryBtnVisible && secondaryBtnVisible ? View.VISIBLE : View.GONE); @@ -66,12 +85,18 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { @NonNull private String getFileTitle() { - return "Some file"; // TODO + return indexItem == null ? "" : indexItem.getBasename().replace("_", " "); } @NonNull private String getFileDescription() { - return "Some description"; // TODO + 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() { @@ -129,6 +154,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { final TextView title; final ImageView icon; final TextView description; + final View fileDataContainer; final ImageView fileIcon; final TextView fileTitle; final TextView fileDescription; @@ -145,6 +171,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { 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);