From 5223b1c1b3c4fe6572464d2a3762bc53c02fab90 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 23 Apr 2020 19:28:48 +0300 Subject: [PATCH 1/3] Add description for download item --- OsmAnd/res/layout/group_description_item.xml | 3 +- OsmAnd/res/layout/item_info_fragment.xml | 38 ++--- OsmAnd/res/values-large/sizes.xml | 1 + OsmAnd/res/values/sizes.xml | 2 + OsmAnd/src/net/osmand/plus/CustomRegion.java | 10 +- .../osmand/plus/download/CustomIndexItem.java | 77 ++------- .../download/ui/DownloadDescriptionInfo.java | 10 +- .../download/ui/DownloadItemFragment.java | 161 +++++++++++------- .../ui/DownloadResourceGroupFragment.java | 107 ++---------- .../plus/download/ui/ImagesPagerAdapter.java | 74 ++++++++ 10 files changed, 222 insertions(+), 261 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/download/ui/ImagesPagerAdapter.java diff --git a/OsmAnd/res/layout/group_description_item.xml b/OsmAnd/res/layout/group_description_item.xml index 512e65a606..0c73281929 100644 --- a/OsmAnd/res/layout/group_description_item.xml +++ b/OsmAnd/res/layout/group_description_item.xml @@ -16,7 +16,8 @@ + android:layout_height="@dimen/download_description_images_height" + tools:visibility="gone" /> + android:scrollbars="none"> + android:orientation="vertical"> - + android:layout_height="@dimen/download_description_images_height" + tools:visibility="gone" /> + tools:text="@string/plugin_disabled_descr" /> @@ -71,14 +65,12 @@ - - - - + android:background="?attr/list_background_color" + android:orientation="vertical" /> diff --git a/OsmAnd/res/values-large/sizes.xml b/OsmAnd/res/values-large/sizes.xml index d503d642db..131f2840a2 100644 --- a/OsmAnd/res/values-large/sizes.xml +++ b/OsmAnd/res/values-large/sizes.xml @@ -241,4 +241,5 @@ 108dp 108dp + 198dp \ No newline at end of file diff --git a/OsmAnd/res/values/sizes.xml b/OsmAnd/res/values/sizes.xml index e46c3a8df8..d047390cb7 100644 --- a/OsmAnd/res/values/sizes.xml +++ b/OsmAnd/res/values/sizes.xml @@ -367,4 +367,6 @@ @dimen/slider_thumb_size 65dp + + 132dp \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/CustomRegion.java b/OsmAnd/src/net/osmand/plus/CustomRegion.java index a6f72891df..2be92ba26a 100644 --- a/OsmAnd/src/net/osmand/plus/CustomRegion.java +++ b/OsmAnd/src/net/osmand/plus/CustomRegion.java @@ -143,15 +143,12 @@ public class CustomRegion extends WorldRegion { long containerSize = itemJson.optLong("containerSize"); String indexType = itemJson.optString("type", type); - String webUrl = itemJson.optString("weburl"); String fileName = itemJson.optString("filename"); String downloadUrl = itemJson.optString("downloadurl"); String size = new DecimalFormat("#.#").format(containerSize / (1024f * 1024f)); - List descrImageUrl = JsonUtils.jsonArrayToList("image-description-url", itemJson); Map indexNames = JsonUtils.getLocalizedMapFromJson("name", itemJson); - Map descriptions = JsonUtils.getLocalizedMapFromJson("description", itemJson); - Map webButtonText = JsonUtils.getLocalizedMapFromJson("web-button-text", itemJson); + DownloadDescriptionInfo descriptionInfo = DownloadDescriptionInfo.fromJson(itemJson.optJSONObject("description")); DownloadActivityType type = DownloadActivityType.getIndexType(indexType); if (type != null) { @@ -160,10 +157,7 @@ public class CustomRegion extends WorldRegion { .setSubfolder(subfolder) .setDownloadUrl(downloadUrl) .setNames(indexNames) - .setDescriptions(descriptions) - .setImageDescrUrl(descrImageUrl) - .setWebUrl(webUrl) - .setWebButtonText(webButtonText) + .setDescriptionInfo(descriptionInfo) .setTimestamp(timestamp) .setSize(size) .setContentSize(contentSize) diff --git a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java index 6bacdb237a..f8448b90f8 100644 --- a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java @@ -7,44 +7,36 @@ import androidx.annotation.NonNull; import net.osmand.JsonUtils; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.download.ui.DownloadDescriptionInfo; import net.osmand.util.Algorithms; import java.io.File; -import java.util.List; import java.util.Map; public class CustomIndexItem extends IndexItem { private String subfolder; private String downloadUrl; - private String webUrl; - private List imageDescrUrl; private Map names; - private Map descriptions; - private Map webButtonTexts; + + private DownloadDescriptionInfo descriptionInfo; public CustomIndexItem(String fileName, String subfolder, String downloadUrl, - String webUrl, String size, long timestamp, long contentSize, long containerSize, - List imageDescrUrl, Map names, - Map descriptions, - Map webButtonTexts, - @NonNull DownloadActivityType type) { + @NonNull DownloadActivityType type, + DownloadDescriptionInfo descriptionInfo) { super(fileName, null, timestamp, size, contentSize, containerSize, type); + this.names = names; this.subfolder = subfolder; this.downloadUrl = downloadUrl; - this.webUrl = webUrl; - this.imageDescrUrl = imageDescrUrl; - this.names = names; - this.descriptions = descriptions; - this.webButtonTexts = webButtonTexts; + this.descriptionInfo = descriptionInfo; } @Override @@ -76,21 +68,8 @@ public class CustomIndexItem extends IndexItem { return JsonUtils.getLocalizedResFromMap(ctx, names, name); } - public List getDescriptionImageUrl() { - return imageDescrUrl; - } - - public String getLocalizedDescription(Context ctx) { - String description = super.getDescription(); - return JsonUtils.getLocalizedResFromMap(ctx, descriptions, description); - } - - public String getWebUrl() { - return webUrl; - } - - public String getWebButtonText(Context ctx) { - return JsonUtils.getLocalizedResFromMap(ctx, webButtonTexts, null); + public DownloadDescriptionInfo getDescriptionInfo() { + return descriptionInfo; } public static class CustomIndexItemBuilder { @@ -98,19 +77,16 @@ public class CustomIndexItem extends IndexItem { private String fileName; private String subfolder; private String downloadUrl; - private String webUrl; private String size; private long timestamp; private long contentSize; private long containerSize; - private List imageDescrUrl; private Map names; - private Map descriptions; - private Map webButtonText; private DownloadActivityType type; + private DownloadDescriptionInfo descriptionInfo; public CustomIndexItemBuilder setFileName(String fileName) { this.fileName = fileName; @@ -127,11 +103,6 @@ public class CustomIndexItem extends IndexItem { return this; } - public CustomIndexItemBuilder setWebUrl(String webUrl) { - this.webUrl = webUrl; - return this; - } - public CustomIndexItemBuilder setSize(String size) { this.size = size; return this; @@ -152,23 +123,13 @@ public class CustomIndexItem extends IndexItem { return this; } - public CustomIndexItemBuilder setImageDescrUrl(List imageDescrUrl) { - this.imageDescrUrl = imageDescrUrl; - return this; - } - public CustomIndexItemBuilder setNames(Map names) { this.names = names; return this; } - public CustomIndexItemBuilder setDescriptions(Map descriptions) { - this.descriptions = descriptions; - return this; - } - - public CustomIndexItemBuilder setWebButtonText(Map webButtonText) { - this.webButtonText = webButtonText; + public CustomIndexItemBuilder setDescriptionInfo(DownloadDescriptionInfo descriptionInfo) { + this.descriptionInfo = descriptionInfo; return this; } @@ -178,19 +139,7 @@ public class CustomIndexItem extends IndexItem { } public CustomIndexItem create() { - return new CustomIndexItem(fileName, - subfolder, - downloadUrl, - webUrl, - size, - timestamp, - contentSize, - containerSize, - imageDescrUrl, - names, - descriptions, - webButtonText, - type); + return new CustomIndexItem(fileName, subfolder, downloadUrl, size, timestamp, contentSize, containerSize, names, type, descriptionInfo); } } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadDescriptionInfo.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadDescriptionInfo.java index 20f685bb1c..2a110f15da 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadDescriptionInfo.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadDescriptionInfo.java @@ -21,14 +21,14 @@ public class DownloadDescriptionInfo { private JSONArray buttonsJson; private List imageUrls; - private Map texts; + private Map localizedDescription; public List getImageUrls() { return imageUrls; } public CharSequence getLocalizedDescription(Context ctx) { - String description = JsonUtils.getLocalizedResFromMap(ctx, texts, null); + String description = JsonUtils.getLocalizedResFromMap(ctx, localizedDescription, null); return description != null ? Html.fromHtml(description) : null; } @@ -63,7 +63,7 @@ public class DownloadDescriptionInfo { if (json != null) { DownloadDescriptionInfo downloadDescriptionInfo = new DownloadDescriptionInfo(); try { - downloadDescriptionInfo.texts = JsonUtils.getLocalizedMapFromJson("text", json); + downloadDescriptionInfo.localizedDescription = JsonUtils.getLocalizedMapFromJson("text", json); downloadDescriptionInfo.imageUrls = JsonUtils.jsonArrayToList("image", json); downloadDescriptionInfo.buttonsJson = json.optJSONArray("button"); } catch (JSONException e) { @@ -77,7 +77,7 @@ public class DownloadDescriptionInfo { public JSONObject toJson() throws JSONException { JSONObject descrJson = new JSONObject(); - JsonUtils.writeLocalizedMapToJson("text", descrJson, texts); + JsonUtils.writeLocalizedMapToJson("text", descrJson, localizedDescription); JsonUtils.writeStringListToJson("image", descrJson, imageUrls); descrJson.putOpt("button", buttonsJson); @@ -87,6 +87,8 @@ public class DownloadDescriptionInfo { public static class ActionButton { + public static final String DOWNLOAD_ACTION = "download"; + private String actionType; private String name; private String url; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java index bd709d80ac..2901f29b21 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java @@ -2,24 +2,23 @@ package net.osmand.plus.download.ui; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageView; +import android.view.ViewTreeObserver; import android.widget.TextView; +import android.widget.Toast; +import androidx.annotation.LayoutRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.DialogFragment; - -import com.squareup.picasso.Callback; -import com.squareup.picasso.Picasso; -import com.squareup.picasso.RequestCreator; +import androidx.viewpager.widget.ViewPager; import net.osmand.AndroidUtils; -import net.osmand.PicassoUtils; -import net.osmand.map.WorldRegion; -import net.osmand.plus.CustomRegion; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; @@ -27,9 +26,14 @@ import net.osmand.plus.download.CustomIndexItem; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadResourceGroup; import net.osmand.plus.download.DownloadResources; +import net.osmand.plus.download.IndexItem; +import net.osmand.plus.download.ui.DownloadDescriptionInfo.ActionButton; +import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.wikipedia.WikipediaDialogFragment; import net.osmand.util.Algorithms; +import java.util.List; + import static net.osmand.plus.download.ui.DownloadResourceGroupFragment.REGION_ID_DLG_KEY; public class DownloadItemFragment extends DialogFragment { @@ -42,13 +46,12 @@ public class DownloadItemFragment extends DialogFragment { private int itemIndex = -1; private DownloadResourceGroup group; - private CustomIndexItem indexItem; - private View view; private Toolbar toolbar; - private ImageView image; private TextView description; - private TextView buttonTextView; + private ViewPager imagesPager; + private View descriptionContainer; + private ViewGroup buttonsContainer; private boolean nightMode; @@ -58,12 +61,11 @@ public class DownloadItemFragment extends DialogFragment { nightMode = !getMyApplication().getSettings().isLightContent(); int themeId = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; setStyle(STYLE_NO_FRAME, themeId); - } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - view = inflater.inflate(R.layout.item_info_fragment, container, false); + final View view = inflater.inflate(R.layout.item_info_fragment, container, false); if (savedInstanceState != null) { regionId = savedInstanceState.getString(REGION_ID_DLG_KEY); @@ -85,77 +87,106 @@ public class DownloadItemFragment extends DialogFragment { } }); - description = view.findViewById(R.id.item_description); - image = view.findViewById(R.id.item_image); + description = view.findViewById(R.id.description); + imagesPager = view.findViewById(R.id.images_pager); + buttonsContainer = view.findViewById(R.id.buttons_container); + descriptionContainer = view.findViewById(R.id.description_container); - View dismissButton = view.findViewById(R.id.dismiss_button); - dismissButton.setOnClickListener(new View.OnClickListener() { + reloadData(); + + view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override - public void onClick(View v) { - if (indexItem != null && !Algorithms.isEmpty(indexItem.getWebUrl())) { - WikipediaDialogFragment.showFullArticle(v.getContext(), Uri.parse(indexItem.getWebUrl()), nightMode); + public void onGlobalLayout() { + ViewTreeObserver obs = view.getViewTreeObserver(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + obs.removeOnGlobalLayoutListener(this); + } else { + obs.removeGlobalOnLayoutListener(this); } + descriptionContainer.setPadding(0, 0, 0, buttonsContainer.getHeight()); } }); - UiUtilities.setupDialogButton(nightMode, dismissButton, UiUtilities.DialogButtonType.PRIMARY, ""); - buttonTextView = (TextView) dismissButton.findViewById(R.id.button_text); return view; } @Override - public void onResume() { - super.onResume(); - reloadData(); - } - - @Override - public void onSaveInstanceState(Bundle outState) { + public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); outState.putString(REGION_ID_DLG_KEY, regionId); outState.putInt(ITEM_ID_DLG_KEY, itemIndex); } private void reloadData() { - DownloadActivity downloadActivity = getDownloadActivity(); - if (downloadActivity != null) { - OsmandApplication app = downloadActivity.getMyApplication(); - DownloadResources indexes = getDownloadActivity().getDownloadThread().getIndexes(); - group = indexes.getGroupById(regionId); - indexItem = (CustomIndexItem) group.getItemByIndex(itemIndex); - if (indexItem != null) { - toolbar.setTitle(indexItem.getVisibleName(app, app.getRegions())); - WorldRegion region = group.getRegion(); - if (region instanceof CustomRegion) { - CustomRegion customRegion = (CustomRegion) region; - int color = customRegion.getHeaderColor(); - if (color != -1) { - toolbar.setBackgroundColor(color); + DownloadActivity activity = getDownloadActivity(); + OsmandApplication app = activity.getMyApplication(); + DownloadResources indexes = activity.getDownloadThread().getIndexes(); + group = indexes.getGroupById(regionId); + CustomIndexItem indexItem = (CustomIndexItem) group.getItemByIndex(itemIndex); + if (indexItem != null) { + toolbar.setTitle(indexItem.getVisibleName(app, app.getRegions())); + + DownloadDescriptionInfo descriptionInfo = indexItem.getDescriptionInfo(); + if (descriptionInfo != null) { + updateDescription(app, descriptionInfo, description); + updateImagesPager(app, descriptionInfo, imagesPager); + updateActionButtons(activity, descriptionInfo, indexItem, buttonsContainer, R.layout.bottom_buttons, nightMode); + } + } + } + + static void updateActionButtons(final DownloadActivity ctx, DownloadDescriptionInfo descriptionInfo, + @Nullable final IndexItem indexItem, ViewGroup buttonsContainer, + @LayoutRes int layoutId, final boolean nightMode) { + buttonsContainer.removeAllViews(); + + List actionButtons = descriptionInfo.getActionButtons(ctx); + if (Algorithms.isEmpty(actionButtons) && indexItem != null && !indexItem.isDownloaded()) { + actionButtons.add(new ActionButton(ActionButton.DOWNLOAD_ACTION, ctx.getString(R.string.shared_string_download), null)); + } + + for (final ActionButton actionButton : actionButtons) { + View buttonView = UiUtilities.getInflater(ctx, nightMode).inflate(layoutId, buttonsContainer, false); + View button = buttonView.findViewById(R.id.dismiss_button); + if (button != null) { + UiUtilities.setupDialogButton(nightMode, button, UiUtilities.DialogButtonType.PRIMARY, actionButton.getName()); + } else { + TextView buttonText = buttonView.findViewById(R.id.button_text); + buttonText.setText(actionButton.getName()); + } + buttonView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (actionButton.getUrl() != null) { + WikipediaDialogFragment.showFullArticle(ctx, Uri.parse(actionButton.getUrl()), nightMode); + } else if (ActionButton.DOWNLOAD_ACTION.equalsIgnoreCase(actionButton.getActionType()) && indexItem != null) { + boolean isDownloading = ctx.getDownloadThread().isDownloading(indexItem); + if (!isDownloading) { + ctx.startDownload(indexItem); + } + } else { + String text = ctx.getString(R.string.download_unsupported_action, actionButton.getActionType()); + Toast.makeText(ctx, text, Toast.LENGTH_SHORT).show(); } } + }); + buttonsContainer.addView(buttonView); + } + } - description.setText(indexItem.getLocalizedDescription(app)); - buttonTextView.setText(indexItem.getWebButtonText(app)); + static void updateDescription(OsmandApplication app, DownloadDescriptionInfo descriptionInfo, TextView descriptionView) { + CharSequence descr = descriptionInfo.getLocalizedDescription(app); + descriptionView.setText(descr); + AndroidUiHelper.updateVisibility(descriptionView, !Algorithms.isEmpty(descr)); + } - final PicassoUtils picassoUtils = PicassoUtils.getPicasso(app); - Picasso picasso = Picasso.get(); - for (final String imageUrl : indexItem.getDescriptionImageUrl()) { - RequestCreator rc = picasso.load(imageUrl); - rc.into(image, new Callback() { - @Override - public void onSuccess() { - image.setVisibility(View.VISIBLE); - picassoUtils.setResultLoaded(imageUrl, true); - } - - @Override - public void onError(Exception e) { - image.setVisibility(View.GONE); - picassoUtils.setResultLoaded(imageUrl, false); - } - }); - } - } + static void updateImagesPager(OsmandApplication app, DownloadDescriptionInfo descriptionInfo, ViewPager viewPager) { + if (!Algorithms.isEmpty(descriptionInfo.getImageUrls())) { + ImagesPagerAdapter adapter = new ImagesPagerAdapter(app, descriptionInfo.getImageUrls()); + viewPager.setAdapter(adapter); + viewPager.setVisibility(View.VISIBLE); + } else { + viewPager.setVisibility(View.GONE); } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 2904339a51..adeb95ea28 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -5,7 +5,6 @@ import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.res.Resources; import android.graphics.drawable.Drawable; -import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.TypedValue; @@ -22,19 +21,13 @@ import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.TextView; -import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.Toolbar; import androidx.core.view.MenuItemCompat; import androidx.fragment.app.DialogFragment; -import androidx.viewpager.widget.PagerAdapter; - -import com.squareup.picasso.Callback; -import com.squareup.picasso.Picasso; import net.osmand.AndroidNetworkUtils; import net.osmand.AndroidUtils; -import net.osmand.PicassoUtils; import net.osmand.plus.CustomRegion; import net.osmand.plus.LockableViewPager; import net.osmand.plus.OsmandApplication; @@ -51,12 +44,9 @@ import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; -import net.osmand.plus.download.ui.DownloadDescriptionInfo.ActionButton; -import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseListener; import net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseTaskType; -import net.osmand.plus.wikipedia.WikipediaDialogFragment; import net.osmand.util.Algorithms; import org.json.JSONException; @@ -68,6 +58,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import static net.osmand.plus.download.ui.DownloadItemFragment.updateActionButtons; +import static net.osmand.plus.download.ui.DownloadItemFragment.updateDescription; +import static net.osmand.plus.download.ui.DownloadItemFragment.updateImagesPager; + public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents, InAppPurchaseListener, OnChildClickListener { public static final int RELOAD_ID = 0; @@ -254,40 +248,17 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow if (descriptionView != null) { if (group != null && group.getRegion() instanceof CustomRegion) { CustomRegion customRegion = (CustomRegion) group.getRegion(); - DownloadDescriptionInfo downloadDescriptionInfo = customRegion.getDescriptionInfo(); - if (downloadDescriptionInfo != null) { + DownloadDescriptionInfo descriptionInfo = customRegion.getDescriptionInfo(); + if (descriptionInfo != null) { + OsmandApplication app = activity.getMyApplication(); TextView description = descriptionView.findViewById(R.id.description); - CharSequence descr = downloadDescriptionInfo.getLocalizedDescription(activity); - description.setText(descr); - AndroidUiHelper.updateVisibility(description, !Algorithms.isEmpty(descr)); + updateDescription(app, descriptionInfo, description); ViewGroup buttonsContainer = descriptionView.findViewById(R.id.buttons_container); - buttonsContainer.removeAllViews(); - for (final ActionButton actionButton : downloadDescriptionInfo.getActionButtons(activity)) { - String name = actionButton.getName(); - if (!Algorithms.isEmpty(name)) { - TextView buttonText = (TextView) activity.getLayoutInflater().inflate(R.layout.download_description_button, buttonsContainer, false); - buttonText.setText(name); - buttonText.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - if (actionButton.getUrl() != null) { - WikipediaDialogFragment.showFullArticle(activity, Uri.parse(actionButton.getUrl()), nightMode); - } else { - activity.getMyApplication().showShortToastMessage(R.string.download_unsupported_action, actionButton.getActionType()); - } - } - }); - buttonsContainer.addView(buttonText); - } - } + updateActionButtons(activity, descriptionInfo, null, buttonsContainer, R.layout.download_description_button, nightMode); + LockableViewPager viewPager = descriptionView.findViewById(R.id.images_pager); - if (!Algorithms.isEmpty(downloadDescriptionInfo.getImageUrls())) { - ImagesPagerAdapter adapter = new ImagesPagerAdapter(downloadDescriptionInfo.getImageUrls()); - viewPager.setAdapter(adapter); - } else { - viewPager.setVisibility(View.GONE); - } + updateImagesPager(app, descriptionInfo, viewPager); descriptionView.findViewById(R.id.container).setVisibility(View.VISIBLE); return; @@ -798,60 +769,4 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow return true; } } - - private class ImagesPagerAdapter extends PagerAdapter { - - private PicassoUtils picassoUtils; - - private List imageUrls; - - public ImagesPagerAdapter(List imageUrls) { - this.imageUrls = imageUrls; - picassoUtils = PicassoUtils.getPicasso(getMyApplication()); - } - - @Override - public int getCount() { - return imageUrls.size(); - } - - @Override - public Object instantiateItem(ViewGroup container, int position) { - View view = createImageView(position); - container.addView(view, 0); - - return view; - } - - @Override - public void destroyItem(ViewGroup collection, int position, @NonNull Object view) { - collection.removeView((View) view); - } - - @Override - public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { - return view == object; - } - - private View createImageView(int position) { - final ImageView imageView = new ImageView(getContext()); - imageView.setScaleType(ImageView.ScaleType.FIT_XY); - - final String imageUrl = imageUrls.get(position); - Picasso.get().load(imageUrl).into(imageView, new Callback() { - @Override - public void onSuccess() { - imageView.setVisibility(View.VISIBLE); - picassoUtils.setResultLoaded(imageUrl, true); - } - - @Override - public void onError(Exception e) { - imageView.setVisibility(View.INVISIBLE); - picassoUtils.setResultLoaded(imageUrl, false); - } - }); - return imageView; - } - } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ImagesPagerAdapter.java b/OsmAnd/src/net/osmand/plus/download/ui/ImagesPagerAdapter.java new file mode 100644 index 0000000000..af3cea834c --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/ui/ImagesPagerAdapter.java @@ -0,0 +1,74 @@ +package net.osmand.plus.download.ui; + +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; + +import androidx.annotation.NonNull; +import androidx.viewpager.widget.PagerAdapter; + +import com.squareup.picasso.Callback; +import com.squareup.picasso.Picasso; + +import net.osmand.PicassoUtils; +import net.osmand.plus.OsmandApplication; + +import java.util.List; + +public class ImagesPagerAdapter extends PagerAdapter { + + private OsmandApplication app; + private PicassoUtils picassoUtils; + + private List imageUrls; + + public ImagesPagerAdapter(@NonNull OsmandApplication app, List imageUrls) { + this.app = app; + this.imageUrls = imageUrls; + picassoUtils = PicassoUtils.getPicasso(app); + } + + @Override + public int getCount() { + return imageUrls.size(); + } + + @Override + public Object instantiateItem(ViewGroup container, int position) { + View view = createImageView(position); + container.addView(view, 0); + + return view; + } + + @Override + public void destroyItem(ViewGroup collection, int position, @NonNull Object view) { + collection.removeView((View) view); + } + + @Override + public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { + return view == object; + } + + private View createImageView(int position) { + final ImageView imageView = new ImageView(app); + imageView.setScaleType(ImageView.ScaleType.FIT_XY); + + final String imageUrl = imageUrls.get(position); + Picasso.get().load(imageUrl).into(imageView, new Callback() { + @Override + public void onSuccess() { + imageView.setVisibility(View.VISIBLE); + picassoUtils.setResultLoaded(imageUrl, true); + } + + @Override + public void onError(Exception e) { + imageView.setVisibility(View.INVISIBLE); + picassoUtils.setResultLoaded(imageUrl, false); + } + }); + return imageView; + } +} \ No newline at end of file From a16b85850b64686dccb84c168741accb9e9fa72c Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 23 Apr 2020 19:45:16 +0300 Subject: [PATCH 2/3] Add download progress to description screen --- OsmAnd/res/layout/item_info_fragment.xml | 10 +++++-- .../download/ui/DownloadItemFragment.java | 29 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/OsmAnd/res/layout/item_info_fragment.xml b/OsmAnd/res/layout/item_info_fragment.xml index f1149a1ac3..2beb514f90 100644 --- a/OsmAnd/res/layout/item_info_fragment.xml +++ b/OsmAnd/res/layout/item_info_fragment.xml @@ -4,7 +4,6 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:background="?attr/activity_background_basic" android:orientation="vertical"> + + + + + android:layout_height="match_parent" + android:background="?attr/activity_background_basic"> Date: Thu, 23 Apr 2020 21:33:27 +0300 Subject: [PATCH 3/3] Add subName to custom download item --- OsmAnd/src/net/osmand/plus/CustomRegion.java | 5 ++ .../osmand/plus/download/CustomIndexItem.java | 49 ++++++++++++++++++- .../download/ui/DownloadItemFragment.java | 1 + .../plus/download/ui/ItemViewHolder.java | 11 +++-- 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/CustomRegion.java b/OsmAnd/src/net/osmand/plus/CustomRegion.java index 2be92ba26a..6280c0d866 100644 --- a/OsmAnd/src/net/osmand/plus/CustomRegion.java +++ b/OsmAnd/src/net/osmand/plus/CustomRegion.java @@ -148,6 +148,9 @@ public class CustomRegion extends WorldRegion { String size = new DecimalFormat("#.#").format(containerSize / (1024f * 1024f)); Map indexNames = JsonUtils.getLocalizedMapFromJson("name", itemJson); + Map firstSubNames = JsonUtils.getLocalizedMapFromJson("firstsubname", itemJson); + Map secondSubNames = JsonUtils.getLocalizedMapFromJson("secondsubname", itemJson); + DownloadDescriptionInfo descriptionInfo = DownloadDescriptionInfo.fromJson(itemJson.optJSONObject("description")); DownloadActivityType type = DownloadActivityType.getIndexType(indexType); @@ -157,6 +160,8 @@ public class CustomRegion extends WorldRegion { .setSubfolder(subfolder) .setDownloadUrl(downloadUrl) .setNames(indexNames) + .setFirstSubNames(firstSubNames) + .setSecondSubNames(secondSubNames) .setDescriptionInfo(descriptionInfo) .setTimestamp(timestamp) .setSize(size) diff --git a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java index f8448b90f8..6d154d5cc4 100644 --- a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java @@ -19,6 +19,8 @@ public class CustomIndexItem extends IndexItem { private String downloadUrl; private Map names; + private Map firstSubNames; + private Map secondSubNames; private DownloadDescriptionInfo descriptionInfo; @@ -30,10 +32,14 @@ public class CustomIndexItem extends IndexItem { long contentSize, long containerSize, Map names, + Map firstSubNames, + Map secondSubNames, @NonNull DownloadActivityType type, DownloadDescriptionInfo descriptionInfo) { super(fileName, null, timestamp, size, contentSize, containerSize, type); this.names = names; + this.firstSubNames = firstSubNames; + this.secondSubNames = secondSubNames; this.subfolder = subfolder; this.downloadUrl = downloadUrl; this.descriptionInfo = descriptionInfo; @@ -68,6 +74,24 @@ public class CustomIndexItem extends IndexItem { return JsonUtils.getLocalizedResFromMap(ctx, names, name); } + public String getSubName(Context ctx) { + String subName = getFirstSubName(ctx); + + String secondSubName = getSecondSubName(ctx); + if (secondSubName != null) { + subName = subName == null ? secondSubName : subName + " • " + secondSubName; + } + return subName; + } + + public String getFirstSubName(Context ctx) { + return JsonUtils.getLocalizedResFromMap(ctx, firstSubNames, null); + } + + public String getSecondSubName(Context ctx) { + return JsonUtils.getLocalizedResFromMap(ctx, secondSubNames, null); + } + public DownloadDescriptionInfo getDescriptionInfo() { return descriptionInfo; } @@ -84,6 +108,8 @@ public class CustomIndexItem extends IndexItem { private long containerSize; private Map names; + private Map firstSubNames; + private Map secondSubNames; private DownloadActivityType type; private DownloadDescriptionInfo descriptionInfo; @@ -128,6 +154,16 @@ public class CustomIndexItem extends IndexItem { return this; } + public CustomIndexItemBuilder setFirstSubNames(Map firstSubNames) { + this.firstSubNames = firstSubNames; + return this; + } + + public CustomIndexItemBuilder setSecondSubNames(Map secondSubNames) { + this.secondSubNames = secondSubNames; + return this; + } + public CustomIndexItemBuilder setDescriptionInfo(DownloadDescriptionInfo descriptionInfo) { this.descriptionInfo = descriptionInfo; return this; @@ -139,7 +175,18 @@ public class CustomIndexItem extends IndexItem { } public CustomIndexItem create() { - return new CustomIndexItem(fileName, subfolder, downloadUrl, size, timestamp, contentSize, containerSize, names, type, descriptionInfo); + return new CustomIndexItem(fileName, + subfolder, + downloadUrl, + size, + timestamp, + contentSize, + containerSize, + names, + firstSubNames, + secondSubNames, + type, + descriptionInfo); } } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java index 012ab25d2c..7d38a92f73 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadItemFragment.java @@ -135,6 +135,7 @@ public class DownloadItemFragment extends DialogFragment implements DownloadEven if (banner != null) { banner.updateBannerInProgress(); } + reloadData(); } @Override diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 0190cfb5e7..ec1c70fd41 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -27,6 +27,7 @@ import net.osmand.plus.activities.LocalIndexHelper.LocalIndexType; import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.chooseplan.ChoosePlanDialogFragment; import net.osmand.plus.download.CityItem; +import net.osmand.plus.download.CustomIndexItem; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadResourceGroup; @@ -179,22 +180,24 @@ public class ItemViewHolder { if (!isDownloading) { progressBar.setVisibility(View.GONE); descrTextView.setVisibility(View.VISIBLE); - if (indexItem.getType() == DownloadActivityType.DEPTH_CONTOUR_FILE && !depthContoursPurchased) { + if (indexItem instanceof CustomIndexItem && (((CustomIndexItem) indexItem).getSubName(context) != null)) { + descrTextView.setText(((CustomIndexItem) indexItem).getSubName(context)); + } else if (indexItem.getType() == DownloadActivityType.DEPTH_CONTOUR_FILE && !depthContoursPurchased) { descrTextView.setText(context.getString(R.string.depth_contour_descr)); } else if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE || indexItem.getType() == DownloadActivityType.SLOPE_FILE) && srtmDisabled) { - if(showTypeInName) { + if (showTypeInName) { descrTextView.setText(""); } else { descrTextView.setText(indexItem.getType().getString(context)); } } else if (showTypeInDesc) { - descrTextView.setText(indexItem.getType().getString(context) + + descrTextView.setText(indexItem.getType().getString(context) + " • " + indexItem.getSizeDescription(context) + " • " + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); } else { - descrTextView.setText(indexItem.getSizeDescription(context) + " • " + + descrTextView.setText(indexItem.getSizeDescription(context) + " • " + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); }