Improve wikivoyage download / update card
This commit is contained in:
parent
5e3cc47816
commit
4946440e57
5 changed files with 139 additions and 37 deletions
|
@ -55,6 +55,7 @@
|
|||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/file_data_container"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="@dimen/content_padding"
|
||||
|
|
|
@ -12,14 +12,14 @@ import android.content.Intent;
|
|||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.AsyncTask.Status;
|
||||
import android.os.Build;
|
||||
import android.os.StatFs;
|
||||
import android.support.annotation.UiThread;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.support.v4.app.NotificationCompat.Builder;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import net.osmand.IndexConstants;
|
||||
import net.osmand.PlatformUtil;
|
||||
import net.osmand.map.WorldRegion;
|
||||
|
@ -52,7 +52,7 @@ public class DownloadIndexesThread {
|
|||
private static final int NOTIFICATION_ID = 45;
|
||||
private OsmandApplication app;
|
||||
|
||||
private DownloadEvents uiActivity = null;
|
||||
private Set<DownloadEvents> uiCallbacks = new HashSet<>();
|
||||
private DatabaseHelper dbHelper;
|
||||
private DownloadFileHelper downloadFileHelper;
|
||||
private List<BasicProgressAsyncTask<?, ?, ?, ?>> currentRunningTask = Collections.synchronizedList(new ArrayList<BasicProgressAsyncTask<?, ?, ?, ?>>());
|
||||
|
@ -87,20 +87,20 @@ 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() {
|
||||
for (DownloadEvents uiActivity : uiCallbacks) {
|
||||
if (uiActivity != null) {
|
||||
uiActivity.downloadInProgress();
|
||||
}
|
||||
}
|
||||
updateNotification();
|
||||
}
|
||||
|
||||
|
@ -151,9 +151,11 @@ public class DownloadIndexesThread {
|
|||
|
||||
@UiThread
|
||||
protected void downloadHasFinished() {
|
||||
for (DownloadEvents uiActivity : uiCallbacks) {
|
||||
if (uiActivity != null) {
|
||||
uiActivity.downloadHasFinished();
|
||||
}
|
||||
}
|
||||
updateNotification();
|
||||
}
|
||||
|
||||
|
@ -186,10 +188,12 @@ public class DownloadIndexesThread {
|
|||
|
||||
@UiThread
|
||||
protected void newDownloadIndexes() {
|
||||
for (DownloadEvents uiActivity : uiCallbacks) {
|
||||
if (uiActivity != null) {
|
||||
uiActivity.newDownloadIndexes();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// PUBLIC API
|
||||
|
@ -254,9 +258,11 @@ public class DownloadIndexesThread {
|
|||
return;
|
||||
}
|
||||
}
|
||||
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)) {
|
||||
indexItemDownloading.add(item);
|
||||
|
@ -454,12 +460,14 @@ public class DownloadIndexesThread {
|
|||
currentRunningTask.add(this);
|
||||
super.onPreExecute();
|
||||
downloadFileHelper.setInterruptDownloading(false);
|
||||
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,12 +476,14 @@ public class DownloadIndexesThread {
|
|||
if (result != null && result.length() > 0) {
|
||||
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
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);
|
||||
indexes.updateFilesToUpdate();
|
||||
downloadHasFinished();
|
||||
|
|
|
@ -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<IndexItem> 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) {
|
||||
|
|
|
@ -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<BaseTravelCard> generateItems() {
|
||||
final List<BaseTravelCard> 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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue