Merge pull request #8833 from osmandapp/custom_download_fixes

Description download actions
This commit is contained in:
max-klaus 2020-04-24 11:30:57 +03:00 committed by GitHub
commit 72089a6ae1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 300 additions and 250 deletions

View file

@ -16,7 +16,8 @@
<net.osmand.plus.LockableViewPager
android:id="@+id/images_pager"
android:layout_width="match_parent"
android:layout_height="132dp" />
android:layout_height="@dimen/download_description_images_height"
tools:visibility="gone" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/description"

View file

@ -22,47 +22,47 @@
</com.google.android.material.appbar.AppBarLayout>
<include
layout="@layout/free_version_banner"
android:background="#F00" />
<include layout="@layout/progress_with_two_texts" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="?attr/activity_background_basic">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/activity_background_basic">
android:scrollbars="none">
<LinearLayout
android:id="@+id/description_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/dialog_button_ex_height">
android:orientation="vertical">
<ImageView
android:id="@+id/item_image"
<net.osmand.plus.LockableViewPager
android:id="@+id/images_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:background="?attr/bg_color"
android:maxHeight="132dp"
android:scaleType="fitCenter"
tools:src="@drawable/extension_stub" />
android:layout_height="@dimen/download_description_images_height"
tools:visibility="gone" />
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/item_description"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/bg_color"
android:background="?attr/list_background_color"
android:letterSpacing="@dimen/text_button_letter_spacing"
android:paddingStart="@dimen/content_padding"
android:paddingLeft="@dimen/content_padding"
android:paddingTop="@dimen/content_padding_small"
android:paddingEnd="@dimen/content_padding"
android:paddingTop="@dimen/list_header_settings_top_margin"
android:paddingRight="@dimen/content_padding"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/default_list_text_size"
app:typeface="@string/font_roboto_regular"
tools:text="@string/lorem_ipsum" />
tools:text="@string/plugin_disabled_descr" />
<include layout="@layout/card_bottom_divider" />
@ -71,14 +71,12 @@
</ScrollView>
<LinearLayout
android:id="@+id/buttons_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?attr/list_background_color">
<include layout="@layout/bottom_buttons" />
</LinearLayout>
android:background="?attr/list_background_color"
android:orientation="vertical" />
</FrameLayout>

View file

@ -241,4 +241,5 @@
<dimen name="settings_divider_margin_start">108dp</dimen>
<dimen name="setting_list_item_large_height">108dp</dimen>
<dimen name="download_description_images_height">198dp</dimen>
</resources>

View file

@ -367,4 +367,6 @@
<dimen name="mtrl_slider_track_side_padding">@dimen/slider_thumb_size</dimen>
<dimen name="list_item_move_header_min_height">65dp</dimen>
<dimen name="download_description_images_height">132dp</dimen>
</resources>

View file

@ -143,15 +143,15 @@ 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<String> descrImageUrl = JsonUtils.jsonArrayToList("image-description-url", itemJson);
Map<String, String> indexNames = JsonUtils.getLocalizedMapFromJson("name", itemJson);
Map<String, String> descriptions = JsonUtils.getLocalizedMapFromJson("description", itemJson);
Map<String, String> webButtonText = JsonUtils.getLocalizedMapFromJson("web-button-text", itemJson);
Map<String, String> firstSubNames = JsonUtils.getLocalizedMapFromJson("firstsubname", itemJson);
Map<String, String> secondSubNames = JsonUtils.getLocalizedMapFromJson("secondsubname", itemJson);
DownloadDescriptionInfo descriptionInfo = DownloadDescriptionInfo.fromJson(itemJson.optJSONObject("description"));
DownloadActivityType type = DownloadActivityType.getIndexType(indexType);
if (type != null) {
@ -160,10 +160,9 @@ public class CustomRegion extends WorldRegion {
.setSubfolder(subfolder)
.setDownloadUrl(downloadUrl)
.setNames(indexNames)
.setDescriptions(descriptions)
.setImageDescrUrl(descrImageUrl)
.setWebUrl(webUrl)
.setWebButtonText(webButtonText)
.setFirstSubNames(firstSubNames)
.setSecondSubNames(secondSubNames)
.setDescriptionInfo(descriptionInfo)
.setTimestamp(timestamp)
.setSize(size)
.setContentSize(contentSize)

View file

@ -7,44 +7,42 @@ 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<String> imageDescrUrl;
private Map<String, String> names;
private Map<String, String> descriptions;
private Map<String, String> webButtonTexts;
private Map<String, String> firstSubNames;
private Map<String, String> secondSubNames;
private DownloadDescriptionInfo descriptionInfo;
public CustomIndexItem(String fileName,
String subfolder,
String downloadUrl,
String webUrl,
String size,
long timestamp,
long contentSize,
long containerSize,
List<String> imageDescrUrl,
Map<String, String> names,
Map<String, String> descriptions,
Map<String, String> webButtonTexts,
@NonNull DownloadActivityType type) {
Map<String, String> firstSubNames,
Map<String, String> 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.webUrl = webUrl;
this.imageDescrUrl = imageDescrUrl;
this.names = names;
this.descriptions = descriptions;
this.webButtonTexts = webButtonTexts;
this.descriptionInfo = descriptionInfo;
}
@Override
@ -76,21 +74,26 @@ public class CustomIndexItem extends IndexItem {
return JsonUtils.getLocalizedResFromMap(ctx, names, name);
}
public List<String> getDescriptionImageUrl() {
return imageDescrUrl;
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 getLocalizedDescription(Context ctx) {
String description = super.getDescription();
return JsonUtils.getLocalizedResFromMap(ctx, descriptions, description);
public String getFirstSubName(Context ctx) {
return JsonUtils.getLocalizedResFromMap(ctx, firstSubNames, null);
}
public String getWebUrl() {
return webUrl;
public String getSecondSubName(Context ctx) {
return JsonUtils.getLocalizedResFromMap(ctx, secondSubNames, null);
}
public String getWebButtonText(Context ctx) {
return JsonUtils.getLocalizedResFromMap(ctx, webButtonTexts, null);
public DownloadDescriptionInfo getDescriptionInfo() {
return descriptionInfo;
}
public static class CustomIndexItemBuilder {
@ -98,19 +101,18 @@ 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<String> imageDescrUrl;
private Map<String, String> names;
private Map<String, String> descriptions;
private Map<String, String> webButtonText;
private Map<String, String> firstSubNames;
private Map<String, String> secondSubNames;
private DownloadActivityType type;
private DownloadDescriptionInfo descriptionInfo;
public CustomIndexItemBuilder setFileName(String fileName) {
this.fileName = fileName;
@ -127,11 +129,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 +149,23 @@ public class CustomIndexItem extends IndexItem {
return this;
}
public CustomIndexItemBuilder setImageDescrUrl(List<String> imageDescrUrl) {
this.imageDescrUrl = imageDescrUrl;
return this;
}
public CustomIndexItemBuilder setNames(Map<String, String> names) {
this.names = names;
return this;
}
public CustomIndexItemBuilder setDescriptions(Map<String, String> descriptions) {
this.descriptions = descriptions;
public CustomIndexItemBuilder setFirstSubNames(Map<String, String> firstSubNames) {
this.firstSubNames = firstSubNames;
return this;
}
public CustomIndexItemBuilder setWebButtonText(Map<String, String> webButtonText) {
this.webButtonText = webButtonText;
public CustomIndexItemBuilder setSecondSubNames(Map<String, String> secondSubNames) {
this.secondSubNames = secondSubNames;
return this;
}
public CustomIndexItemBuilder setDescriptionInfo(DownloadDescriptionInfo descriptionInfo) {
this.descriptionInfo = descriptionInfo;
return this;
}
@ -181,16 +178,15 @@ public class CustomIndexItem extends IndexItem {
return new CustomIndexItem(fileName,
subfolder,
downloadUrl,
webUrl,
size,
timestamp,
contentSize,
containerSize,
imageDescrUrl,
names,
descriptions,
webButtonText,
type);
firstSubNames,
secondSubNames,
type,
descriptionInfo);
}
}
}

View file

@ -21,14 +21,14 @@ public class DownloadDescriptionInfo {
private JSONArray buttonsJson;
private List<String> imageUrls;
private Map<String, String> texts;
private Map<String, String> localizedDescription;
public List<String> 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;

View file

@ -2,37 +2,43 @@ 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;
import net.osmand.plus.download.CustomIndexItem;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
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 {
public class DownloadItemFragment extends DialogFragment implements DownloadEvents {
public static final String ITEM_ID_DLG_KEY = "index_item_dialog_key";
@ -41,14 +47,14 @@ public class DownloadItemFragment extends DialogFragment {
private String regionId = "";
private int itemIndex = -1;
private BannerAndDownloadFreeVersion banner;
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 +64,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 +90,131 @@ public class DownloadItemFragment extends DialogFragment {
}
});
description = view.findViewById(R.id.item_description);
image = view.findViewById(R.id.item_image);
banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity(), false);
View dismissButton = view.findViewById(R.id.dismiss_button);
dismissButton.setOnClickListener(new View.OnClickListener() {
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);
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);
}
@Override
public void newDownloadIndexes() {
if (banner != null) {
banner.updateBannerInProgress();
}
reloadData();
}
@Override
public void downloadHasFinished() {
if (banner != null) {
banner.updateBannerInProgress();
}
reloadData();
}
@Override
public void downloadInProgress() {
if (banner != null) {
banner.updateBannerInProgress();
}
}
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<ActionButton> 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);
}
}

View file

@ -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<String> imageUrls;
public ImagesPagerAdapter(List<String> 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;
}
}
}

View file

@ -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<String> imageUrls;
public ImagesPagerAdapter(@NonNull OsmandApplication app, List<String> 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;
}
}

View file

@ -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,12 +180,14 @@ 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));