From d8d85d84742c1eda6091269967a6e1495c4308df Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Tue, 4 Feb 2025 08:56:15 +0200 Subject: [PATCH 1/3] Combine unused code to remove in future --- .../builders/cards/ImageCard.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java index 50fccbffda2..7fd8f1b1e0f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -327,7 +327,24 @@ public void update() { progress.setVisibility(View.VISIBLE); image.setImageBitmap(null); } else if (!downloaded) { - MenuBuilder.execute(new DownloadImageTask(getMyApplication(), imageUrl, getDownloadImageListener())); + MenuBuilder.execute(new DownloadImageTask(getMyApplication(), imageUrl, new DownloadImageListener() { + @Override + public void onStartDownloading() { + downloading = true; + update(); + } + + @Override + public void onFinishDownloading(Bitmap bitmap) { + downloading = false; + downloaded = true; + ImageCard.this.bitmap = bitmap; + if (bitmap != null && Algorithms.isEmpty(getImageHiresUrl())) { + ImageCard.this.imageHiresUrl = getUrl(); + } + update(); + } + })); } else { progress.setVisibility(View.GONE); image.setImageBitmap(bitmap); @@ -370,25 +387,4 @@ public void update() { } } } - - private DownloadImageListener getDownloadImageListener() { - return new DownloadImageListener() { - @Override - public void onStartDownloading() { - downloading = true; - update(); - } - - @Override - public void onFinishDownloading(Bitmap bitmap) { - downloading = false; - downloaded = true; - ImageCard.this.bitmap = bitmap; - if (bitmap != null && Algorithms.isEmpty(getImageHiresUrl())) { - ImageCard.this.imageHiresUrl = getUrl(); - } - update(); - } - }; - } } From 999de18d2844c3b281fc819a97eeae1c36977e34 Mon Sep 17 00:00:00 2001 From: nazar-kutz <30292035+nazar-kutz@users.noreply.github.com> Date: Tue, 4 Feb 2025 09:16:32 +0200 Subject: [PATCH 2/3] Fix issues related to OSM "image" tag in Android --- .../builders/cards/UrlImageCard.java | 57 +++++++++++++------ .../gallery/GalleryPhotoPagerFragment.java | 31 ++++++---- .../gallery/GalleryPhotoViewerFragment.java | 33 ++++++----- 3 files changed, 79 insertions(+), 42 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java index 6d0c7ee0a16..d2c43de8150 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java @@ -1,8 +1,9 @@ package net.osmand.plus.mapcontextmenu.builders.cards; -import android.view.View; import android.view.View.OnClickListener; +import androidx.annotation.Nullable; + import net.osmand.plus.activities.MapActivity; import net.osmand.util.Algorithms; @@ -14,13 +15,10 @@ public UrlImageCard(MapActivity mapActivity, JSONObject imageObject) { super(mapActivity, imageObject); if (!Algorithms.isEmpty(getSuitableUrl())) { - OnClickListener onClickListener = new OnClickListener() { - @Override - public void onClick(View v) { - openUrl(getMapActivity(), getMyApplication(), getTitle(), getSuitableUrl(), - isExternalLink() || Algorithms.isEmpty(getImageHiresUrl()), - !Algorithms.isEmpty(getImageHiresUrl())); - } + OnClickListener onClickListener = v -> { + boolean hasImageUrl = !Algorithms.isEmpty(getImageHiresUrl()); + boolean externalLink = isExternalLink() || !hasImageUrl; + openUrl(mapActivity, app, getTitle(), getSuitableUrl(), externalLink, hasImageUrl); }; if (!Algorithms.isEmpty(buttonText)) { this.onButtonClickListener = onClickListener; @@ -30,13 +28,40 @@ public void onClick(View v) { } } - private String getSuitableUrl() { - String url; - if (Algorithms.isEmpty(getImageHiresUrl())) { - url = getUrl(); - } else { - url = getImageHiresUrl(); - } - return url; + /** + * Returns the thumbnail URL for the image. + * In this implementation, it always returns `null` due to the nature of the OSM "image" tag. + * Since the tag can contain any URL pointing to any external service, generating a reliable + * thumbnail format (a highly compressed low-resolution image) is not feasible. + * In such cases, the best approach is to directly load the full-size image instead. + */ + @Nullable + @Override + public String getThumbnailUrl() { + return null; + } + + /** + * Returns the URL for displaying the image in the gallery. + * Instead of using the "hires" image URL (if available), this method provides a lower-quality + * "image" URL in this case to avoid potential crashes. + * Some high-resolution images may be too large to be properly rendered on a canvas bitmap. + * To prevent crashes, this implementation uses URL with lower-quality image. + */ @Nullable + @Override + public String getGalleryFullSizeUrl() { + return getImageUrl(); + } + + /** + * Returns a suitable URL for opening in a browser. + * This may be a high-resolution image URL (if available) or the URL stored in the OSM "image" tag. + * The link can either point directly to an image file or lead to a webpage hosting the image + * on an external service. + */ + @Nullable + public String getSuitableUrl() { + String hiresUrl = getImageHiresUrl(); + return Algorithms.isEmpty(hiresUrl) ? getUrl() : hiresUrl; } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java index b1230e48b44..ebd824756de 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java @@ -36,6 +36,7 @@ import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; +import net.osmand.plus.mapcontextmenu.builders.cards.UrlImageCard; import net.osmand.plus.mapcontextmenu.gallery.GalleryController.DownloadMetadataListener; import net.osmand.plus.utils.AndroidUtils; import net.osmand.plus.utils.ColorUtilities; @@ -389,6 +390,7 @@ private void shareImage() { } public void showContextWidgetMenu(@NonNull View view) { + ImageCard card = getSelectedImageCard(); List items = new ArrayList<>(); UiUtilities uiUtilities = app.getUIUtilities(); int iconColor = ColorUtilities.getDefaultIconColor(app, nightMode); @@ -398,23 +400,28 @@ public void showContextWidgetMenu(@NonNull View view) { .setOnClickListener(item -> GalleryDetailsFragment.showInstance(getMapActivity(), selectedPosition)) .create()); - items.add(new PopUpMenuItem.Builder(app) - .setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_external_link, iconColor)) - .setTitleId(R.string.open_in_browser) - .setOnClickListener(item -> { - FragmentActivity activity = getActivity(); - ImageCard card = getSelectedImageCard(); - if (activity != null && card instanceof WikiImageCard wikiImageCard) { - AndroidUtils.openUrl(activity, wikiImageCard.getWikiImage().getUrlWithCommonAttributions(), nightMode); - } - }) - .create()); + if (card instanceof WikiImageCard || card instanceof UrlImageCard urlCard && urlCard.getSuitableUrl() != null) { + items.add(new PopUpMenuItem.Builder(app) + .setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_external_link, iconColor)) + .setTitleId(R.string.open_in_browser) + .setOnClickListener(item -> { + FragmentActivity activity = getActivity(); + if (activity != null) { + if (card instanceof WikiImageCard wikiImageCard) { + AndroidUtils.openUrl(activity, wikiImageCard.getWikiImage().getUrlWithCommonAttributions(), nightMode); + } else { + UrlImageCard urlImageCard = (UrlImageCard) card; + AndroidUtils.openUrl(activity, urlImageCard.getSuitableUrl(), nightMode); + } + } + }) + .create()); + } items.add(new PopUpMenuItem.Builder(app) .setIcon(uiUtilities.getPaintedIcon(R.drawable.ic_action_gsave_dark, iconColor)) .setTitleId(R.string.shared_string_download) .setOnClickListener(item -> { - ImageCard card = getSelectedImageCard(); String downloadUrl = card.getImageHiresUrl(); if (Algorithms.isEmpty(downloadUrl)) { downloadUrl = card.getImageUrl(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoViewerFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoViewerFragment.java index 40decb0e415..b8551d3825f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoViewerFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoViewerFragment.java @@ -83,20 +83,25 @@ public boolean onSingleTapConfirmed(@NonNull MotionEvent e) { } private void downloadThumbnail(@NonNull ImageCard imageCard) { - Picasso.get() - .load(imageCard.getThumbnailUrl()) - .into(imageView, new Callback() { - @Override - public void onSuccess() { - downloadHiResImage(imageCard); - } - - @Override - public void onError(Exception e) { - downloadHiResImage(imageCard); - LOG.error(e); - } - }); + String thumbnailUrl = imageCard.getThumbnailUrl(); + if (thumbnailUrl != null) { + Picasso.get() + .load(thumbnailUrl) + .into(imageView, new Callback() { + @Override + public void onSuccess() { + downloadHiResImage(imageCard); + } + + @Override + public void onError(Exception e) { + downloadHiResImage(imageCard); + LOG.error(e); + } + }); + } else { + downloadHiResImage(imageCard); + } } private void downloadHiResImage(@NonNull ImageCard imageCard) { From 5a450dd057e89d0eec412092926e680caab30c7f Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Tue, 11 Feb 2025 18:43:49 +0200 Subject: [PATCH 3/3] Show link if image loading failed (+ remove outdated code) --- OsmAnd/res/layout/gallery_card_item.xml | 25 +++ .../builders/cards/AbstractCard.java | 117 ------------ .../builders/cards/ImageCard.java | 168 +----------------- .../builders/cards/NoImagesCard.java | 35 ---- .../builders/cards/ProgressCard.java | 10 -- .../builders/cards/UrlImageCard.java | 16 +- .../gallery/GalleryPhotoPagerFragment.java | 4 +- .../gallery/holders/GalleryImageHolder.java | 91 +++++++--- .../gallery/tasks/DownloadImageTask.java | 47 ----- .../mapillary/MapillaryContributeCard.java | 22 --- .../plugins/mapillary/MapillaryImageCard.java | 14 -- .../osmand/plus/wikipedia/WikiImageCard.java | 12 -- 12 files changed, 102 insertions(+), 459 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/tasks/DownloadImageTask.java diff --git a/OsmAnd/res/layout/gallery_card_item.xml b/OsmAnd/res/layout/gallery_card_item.xml index d5feb23ba20..b1a9fb97e56 100644 --- a/OsmAnd/res/layout/gallery_card_item.xml +++ b/OsmAnd/res/layout/gallery_card_item.xml @@ -21,4 +21,29 @@ android:layout_marginTop="9dp" android:visibility="gone" /> + + + + + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java index e3bf2269689..2a9524d4c10 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/AbstractCard.java @@ -1,136 +1,19 @@ package net.osmand.plus.mapcontextmenu.builders.cards; -import android.annotation.SuppressLint; -import android.app.Activity; -import android.app.Dialog; -import android.content.Context; -import android.content.Intent; -import android.graphics.Color; -import android.graphics.drawable.Drawable; -import android.net.Uri; -import android.util.TypedValue; -import android.view.LayoutInflater; -import android.view.View; -import android.webkit.WebSettings; -import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.widget.LinearLayout; - import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.widget.Toolbar; -import androidx.core.content.ContextCompat; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.utils.AndroidUtils; -import net.osmand.plus.widgets.WebViewEx; public abstract class AbstractCard { protected final OsmandApplication app; protected final MapActivity mapActivity; - protected View view; - public abstract int getCardLayoutId(); public AbstractCard(@NonNull MapActivity mapActivity) { this.mapActivity = mapActivity; this.app = mapActivity.getMyApplication(); } - - public View build(@NonNull Context ctx) { - view = LayoutInflater.from(ctx).inflate(getCardLayoutId(), null); - update(); - return view; - } - - public abstract void update(); - - public MapActivity getMapActivity() { - return mapActivity; - } - - public OsmandApplication getMyApplication() { - return app; - } - - @SuppressLint("SetJavaScriptEnabled") - @SuppressWarnings("deprecation") - public static void openUrl(@NonNull Activity ctx, - @NonNull OsmandApplication app, - @Nullable String title, - @NonNull String url, - boolean externalLink, - boolean hasImageUrl) { - if (externalLink) { - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setData(Uri.parse(url)); - AndroidUtils.startActivityIfSafe(ctx, intent); - return; - } - - Dialog dialog = new Dialog(ctx, - app.getSettings().isLightContent() ? - R.style.OsmandLightTheme : - R.style.OsmandDarkTheme); - LinearLayout ll = new LinearLayout(ctx); - ll.setOrientation(LinearLayout.VERTICAL); - - Toolbar topBar = new Toolbar(ctx); - topBar.setClickable(true); - Drawable back = app.getUIUtilities().getIcon(R.drawable.ic_action_remove_dark); - topBar.setNavigationIcon(back); - topBar.setNavigationContentDescription(R.string.shared_string_close); - topBar.setTitle(title); - topBar.setBackgroundColor(ContextCompat.getColor(ctx, getResIdFromAttribute(ctx, R.attr.pstsTabBackground))); - topBar.setTitleTextColor(ContextCompat.getColor(ctx, getResIdFromAttribute(ctx, R.attr.pstsTextColor))); - topBar.setNavigationOnClickListener(v -> dialog.dismiss()); - - WebView wv = new WebViewEx(ctx); - wv.setWebViewClient(new WebViewClient() { - @Override - public boolean shouldOverrideUrlLoading(WebView view, String url) { - return false; - } - }); - - WebSettings settings = wv.getSettings(); - - if (hasImageUrl) { - settings.setDefaultTextEncodingName("utf-8"); - settings.setBuiltInZoomControls(true); - settings.setDisplayZoomControls(false); - settings.setSupportZoom(true); - } - - wv.setBackgroundColor(Color.argb(1, 0, 0, 0)); - wv.getSettings().setJavaScriptEnabled(true); - if (hasImageUrl) { - String data = ""; - wv.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null); - } else { - wv.loadUrl(url); - } - - ll.addView(topBar); - LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0); - lp.weight = 1; - ll.addView(wv, lp); - dialog.setContentView(ll); - - dialog.setCancelable(true); - dialog.show(); - } - - private static int getResIdFromAttribute(@NonNull Context ctx, int attr) { - if (attr == 0) { - return 0; - } - TypedValue value = new TypedValue(); - ctx.getTheme().resolveAttribute(attr, value, true); - return value.resourceId; - } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java index 7fd8f1b1e0f..ff784bb229a 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -1,24 +1,13 @@ package net.osmand.plus.mapcontextmenu.builders.cards; -import android.content.res.ColorStateList; -import android.graphics.Bitmap; import android.graphics.drawable.Drawable; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.ImageView; -import android.widget.ProgressBar; -import android.widget.TextView; import androidx.annotation.Nullable; -import androidx.appcompat.widget.AppCompatButton; import net.osmand.PlatformUtil; import net.osmand.data.LatLon; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.mapcontextmenu.MenuBuilder; -import net.osmand.plus.mapcontextmenu.gallery.tasks.DownloadImageTask; -import net.osmand.plus.mapcontextmenu.gallery.tasks.DownloadImageTask.DownloadImageListener; import net.osmand.plus.utils.AndroidUtils; import net.osmand.util.Algorithms; @@ -68,15 +57,10 @@ public abstract class ImageCard extends AbstractCard { private final int defaultCardLayoutId = R.layout.context_menu_card_image; protected Drawable icon; - protected Drawable buttonIcon; - protected OnClickListener onClickListener; - protected OnClickListener onButtonClickListener; private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US); - private boolean downloading; - private boolean downloaded; - private Bitmap bitmap; + private boolean imageDownloadFailed = false; private float bearingDiff = Float.NaN; private float distance = Float.NaN; @@ -126,10 +110,10 @@ public ImageCard(MapActivity mapActivity, JSONObject imageObject) { } if (imageObject.has("topIcon") && !imageObject.isNull("topIcon")) { String topIcon = imageObject.getString("topIcon"); - this.topIconId = AndroidUtils.getDrawableId(getMyApplication(), topIcon); + this.topIconId = AndroidUtils.getDrawableId(app, topIcon); } if (imageObject.has("buttonIcon") && !imageObject.isNull("buttonIcon")) { - this.buttonIconId = AndroidUtils.getDrawableId(getMyApplication(), imageObject.getString("buttonIcon")); + this.buttonIconId = AndroidUtils.getDrawableId(app, imageObject.getString("buttonIcon")); } if (imageObject.has("buttonText") && !imageObject.isNull("buttonText")) { this.buttonText = imageObject.getString("buttonText"); @@ -217,38 +201,14 @@ public String getGalleryFullSizeUrl() { return getImageHiresUrl() + "?width=" + GALLERY_FULL_SIZE_WIDTH; } - public boolean isExternalLink() { - return externalLink; - } - public int getTopIconId() { return topIconId; } - public int getButtonIconId() { - return buttonIconId; - } - public String getButtonText() { return buttonText; } - public int getButtonIconColor() { - return buttonIconColor; - } - - public int getButtonColor() { - return buttonColor; - } - - public int getButtonTextColor() { - return buttonTextColor; - } - - public int getDefaultCardLayoutId() { - return defaultCardLayoutId; - } - @Override public int getCardLayoutId() { return defaultCardLayoutId; @@ -258,33 +218,12 @@ public Drawable getIcon() { return icon; } - public OnClickListener getOnClickListener() { - return onClickListener; - } - - - public boolean isDownloading() { - return downloading; - } - - public void setDownloading(boolean downloading) { - this.downloading = downloading; - } - - public Bitmap getBitmap() { - return bitmap; - } - - public void setBitmap(Bitmap bitmap) { - this.bitmap = bitmap; + public void markImageDownloadFailed(boolean imageDownloadFailed) { + this.imageDownloadFailed = imageDownloadFailed; } - public float getBearingDiff() { - return bearingDiff; - } - - public void setBearingDiff(float bearingDiff) { - this.bearingDiff = bearingDiff; + public boolean isImageDownloadFailed() { + return imageDownloadFailed; } public float getDistance() { @@ -294,97 +233,4 @@ public float getDistance() { public void setDistance(float distance) { this.distance = distance; } - - public void update() { - if (view != null) { - ImageView image = view.findViewById(R.id.image); - ImageView iconImageView = view.findViewById(R.id.icon); - TextView urlTextView = view.findViewById(R.id.url); - TextView watermarkTextView = view.findViewById(R.id.watermark); - ProgressBar progress = view.findViewById(R.id.progress); - AppCompatButton button = view.findViewById(R.id.button); - - boolean night = getMyApplication().getDaynightHelper().isNightModeForMapControls(); - AndroidUtils.setBackground(getMapActivity(), view.findViewById(R.id.card_background), night, - R.drawable.context_menu_card_light, R.drawable.context_menu_card_dark); - - if (icon == null && topIconId != 0) { - icon = getMyApplication().getUIUtilities().getIcon(topIconId); - } - if (icon == null) { - iconImageView.setVisibility(View.GONE); - } else { - iconImageView.setImageDrawable(icon); - iconImageView.setVisibility(View.VISIBLE); - } - if (Algorithms.isEmpty(userName)) { - watermarkTextView.setVisibility(View.GONE); - } else { - watermarkTextView.setText("@" + userName); - watermarkTextView.setVisibility(View.VISIBLE); - } - if (downloading) { - progress.setVisibility(View.VISIBLE); - image.setImageBitmap(null); - } else if (!downloaded) { - MenuBuilder.execute(new DownloadImageTask(getMyApplication(), imageUrl, new DownloadImageListener() { - @Override - public void onStartDownloading() { - downloading = true; - update(); - } - - @Override - public void onFinishDownloading(Bitmap bitmap) { - downloading = false; - downloaded = true; - ImageCard.this.bitmap = bitmap; - if (bitmap != null && Algorithms.isEmpty(getImageHiresUrl())) { - ImageCard.this.imageHiresUrl = getUrl(); - } - update(); - } - })); - } else { - progress.setVisibility(View.GONE); - image.setImageBitmap(bitmap); - if (bitmap == null) { - urlTextView.setVisibility(View.VISIBLE); - urlTextView.setText(getUrl()); - } else { - urlTextView.setVisibility(View.GONE); - } - } - if (onClickListener != null) { - view.findViewById(R.id.image_card).setOnClickListener(v -> onClickListener.onClick(v)); - } else { - view.findViewById(R.id.image_card).setOnClickListener(null); - } - - if (!Algorithms.isEmpty(buttonText)) { - button.setText(buttonText); - } - if (buttonIcon == null && buttonIconId != 0) { - if (buttonIconColor != 0) { - buttonIcon = getMyApplication().getUIUtilities().getPaintedIcon(buttonIconId, buttonIconColor); - } else { - buttonIcon = getMyApplication().getUIUtilities().getIcon(buttonIconId); - } - } - button.setCompoundDrawablesWithIntrinsicBounds(buttonIcon, null, null, null); - if (buttonColor != 0) { - button.setSupportBackgroundTintList(ColorStateList.valueOf(buttonColor)); - } - if (buttonTextColor != 0) { - button.setTextColor(buttonTextColor); - } - if (onButtonClickListener != null) { - button.setVisibility(View.VISIBLE); - button.setOnClickListener(v -> onButtonClickListener.onClick(v)); - } else { - button.setVisibility(View.GONE); - button.setOnClickListener(null); - } - } - } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/NoImagesCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/NoImagesCard.java index c455d049521..1b6554b9106 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/NoImagesCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/NoImagesCard.java @@ -1,15 +1,7 @@ package net.osmand.plus.mapcontextmenu.builders.cards; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; - -import net.osmand.plus.utils.AndroidUtils; -import net.osmand.plus.utils.ColorUtilities; import net.osmand.plus.R; -import net.osmand.plus.utils.UiUtilities; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.plugins.mapillary.MapillaryPlugin; public class NoImagesCard extends AbstractCard { @@ -21,31 +13,4 @@ public NoImagesCard(MapActivity mapActivity) { public int getCardLayoutId() { return R.layout.context_menu_card_no_images; } - - @Override - public void update() { - if (view != null) { - UiUtilities ic = getMyApplication().getUIUtilities(); - boolean night = getMyApplication().getDaynightHelper().isNightModeForMapControls(); - MapActivity ctx = getMapActivity(); - AndroidUtils.setBackgroundColor(ctx, view, ColorUtilities.getListBgColorId(night)); - ((ImageView) view.findViewById(R.id.icon_sadface)).setImageDrawable(ic.getIcon(R.drawable.ic_action_sadface, - night ? R.color.card_and_list_background_light : R.color.icon_color_default_light)); - AndroidUtils.setTextPrimaryColor(ctx, view.findViewById(R.id.title), night); - AndroidUtils.setBackgroundColor(ctx, view.findViewById(R.id.button_background), night, - R.color.inactive_buttons_and_links_bg_light, R.color.inactive_buttons_and_links_bg_dark); - ((ImageView) view.findViewById(R.id.icon_add_photos)).setImageDrawable( - ic.getIcon(R.drawable.ic_action_add_photos, ColorUtilities.getActiveColorId(night))); - ((TextView) view.findViewById(R.id.app_photos_text_view)) - .setTextColor(ColorUtilities.getActiveColor(ctx, night)); - AndroidUtils.setBackground(ctx, view.findViewById(R.id.card_background), night, - R.drawable.context_menu_card_light, R.drawable.context_menu_card_dark); - view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - MapillaryPlugin.openMapillary(getMapActivity(), null); - } - }); - } - } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ProgressCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ProgressCard.java index c2ae8ea23b8..d3c8611a36e 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ProgressCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ProgressCard.java @@ -1,8 +1,5 @@ package net.osmand.plus.mapcontextmenu.builders.cards; -import net.osmand.plus.utils.AndroidUtils; -import net.osmand.plus.utils.ColorUtilities; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -16,11 +13,4 @@ public ProgressCard(MapActivity mapActivity) { public int getCardLayoutId() { return R.layout.context_menu_card_progress; } - - @Override - public void update() { - OsmandApplication app = getMyApplication(); - boolean night = app.getDaynightHelper().isNightModeForMapControls(); - AndroidUtils.setBackgroundColor(app, view, ColorUtilities.getListBgColorId(night)); - } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java index d2c43de8150..5de7d910751 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/UrlImageCard.java @@ -13,19 +13,6 @@ public class UrlImageCard extends ImageCard { public UrlImageCard(MapActivity mapActivity, JSONObject imageObject) { super(mapActivity, imageObject); - - if (!Algorithms.isEmpty(getSuitableUrl())) { - OnClickListener onClickListener = v -> { - boolean hasImageUrl = !Algorithms.isEmpty(getImageHiresUrl()); - boolean externalLink = isExternalLink() || !hasImageUrl; - openUrl(mapActivity, app, getTitle(), getSuitableUrl(), externalLink, hasImageUrl); - }; - if (!Algorithms.isEmpty(buttonText)) { - this.onButtonClickListener = onClickListener; - } else { - this.onClickListener = onClickListener; - } - } } /** @@ -47,7 +34,8 @@ public String getThumbnailUrl() { * "image" URL in this case to avoid potential crashes. * Some high-resolution images may be too large to be properly rendered on a canvas bitmap. * To prevent crashes, this implementation uses URL with lower-quality image. - */ @Nullable + */ + @Nullable @Override public String getGalleryFullSizeUrl() { return getImageUrl(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java index ebd824756de..4075ec874d7 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/GalleryPhotoPagerFragment.java @@ -95,7 +95,7 @@ public void onMetadataUpdated(@NonNull Set updatedMediaTagImages) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { updateNightMode(); - ViewGroup view = (ViewGroup) themedInflater.inflate(R.layout.gallery_photo_fragment, container, false); + ViewGroup view = (ViewGroup) inflate(R.layout.gallery_photo_fragment, container); toolbar = view.findViewById(R.id.toolbar); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); @@ -231,7 +231,7 @@ private void addImages(@NonNull List imageList, @NonNull Set listener.onImageClicked(imageCard)); + } - if (type == MAIN) { - int topIconId = imageCard.getTopIconId(); - if (topIconId != 0) { - Drawable icon = app.getUIUtilities().getIcon(imageCard.getTopIconId()); - setSourceTypeIcon(icon); - } else { - setSourceTypeIcon(null); - } - } else { - setSourceTypeIcon(null); - } + private void bindImage(@NonNull ImageCardListener listener, @NonNull ImageCard imageCard) { + LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + layoutParams.gravity = Gravity.CENTER; + ivImage.setLayoutParams(layoutParams); + ivImage.setScaleType(ImageView.ScaleType.CENTER_CROP); + ivImage.setOnClickListener(v -> listener.onImageClicked(imageCard)); + tvUrl.setVisibility(View.GONE); + border.setVisibility(View.GONE); + progressBar.setVisibility(View.GONE); + } + + private void bindUrl(@NonNull MapActivity mapActivity, @NonNull ImageCard imageCard, boolean nightMode) { + ivImage.setVisibility(View.GONE); + tvUrl.setVisibility(View.VISIBLE); + tvUrl.setText(imageCard.getUrl()); + tvUrl.setOnClickListener(v -> AndroidUtils.openUrl(mapActivity, imageCard.getUrl(), nightMode)); + border.setVisibility(View.VISIBLE); + progressBar.setVisibility(View.GONE); + setSourceTypeIcon(null); } private void setSourceTypeIcon(@Nullable Drawable icon) { - AndroidUiHelper.updateVisibility(sourceTypeView, icon != null); - sourceTypeView.setImageDrawable(icon); + AndroidUiHelper.updateVisibility(ivSourceType, icon != null); + ivSourceType.setImageDrawable(icon); } private void setupView(@NonNull MapActivity mapActivity, Integer viewWidth, boolean nightMode) { @@ -96,7 +135,9 @@ private void setupView(@NonNull MapActivity mapActivity, Integer viewWidth, bool if (viewWidth != null) { screenWidth = viewWidth; } else { - screenWidth = AndroidUiHelper.isOrientationPortrait(mapActivity) ? AndroidUtils.getScreenWidth(mapActivity) : AndroidUtils.getScreenHeight(mapActivity); + screenWidth = AndroidUiHelper.isOrientationPortrait(mapActivity) + ? AndroidUtils.getScreenWidth(mapActivity) + : AndroidUtils.getScreenHeight(mapActivity); } sizeInPx = calculateItemSize(spanCount, recyclerViewPadding, itemSpace, screenWidth); } else { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/tasks/DownloadImageTask.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/tasks/DownloadImageTask.java deleted file mode 100644 index 0a3a1adb1ac..00000000000 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/gallery/tasks/DownloadImageTask.java +++ /dev/null @@ -1,47 +0,0 @@ -package net.osmand.plus.mapcontextmenu.gallery.tasks; - -import android.graphics.Bitmap; -import android.os.AsyncTask; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.utils.AndroidNetworkUtils; - -public class DownloadImageTask extends AsyncTask { - private final String imageUrl; - private final OsmandApplication app; - private final DownloadImageListener listener; - - public DownloadImageTask(@NonNull OsmandApplication app, @NonNull String imageUrl, @Nullable DownloadImageListener listener) { - this.imageUrl = imageUrl; - this.app = app; - this.listener = listener; - } - - @Override - protected void onPreExecute() { - if (listener != null) { - listener.onStartDownloading(); - } - } - - @Override - protected Bitmap doInBackground(Void... params) { - return AndroidNetworkUtils.downloadImage(app, imageUrl); - } - - @Override - protected void onPostExecute(Bitmap bitmap) { - if (listener != null) { - listener.onFinishDownloading(bitmap); - } - } - - public interface DownloadImageListener { - void onStartDownloading(); - - void onFinishDownloading(Bitmap bitmap); - } -} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryContributeCard.java b/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryContributeCard.java index 61ef0626a26..7d839ddb0cb 100644 --- a/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryContributeCard.java +++ b/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryContributeCard.java @@ -1,9 +1,5 @@ package net.osmand.plus.plugins.mapillary; -import android.view.View; - -import net.osmand.plus.utils.AndroidUtils; -import net.osmand.plus.utils.ColorUtilities; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; @@ -20,22 +16,4 @@ public MapillaryContributeCard(MapActivity mapActivity, JSONObject imageObject) public int getCardLayoutId() { return R.layout.context_menu_card_add_mapillary_images; } - - @Override - public void update() { - if (view != null) { - boolean night = getMyApplication().getDaynightHelper().isNightModeForMapControls(); - MapActivity ctx = getMapActivity(); - AndroidUtils.setBackgroundColor(ctx, view, ColorUtilities.getListBgColorId(night)); - AndroidUtils.setTextPrimaryColor(ctx, view.findViewById(R.id.title), night); - AndroidUtils.setBackground(ctx, view.findViewById(R.id.card_background), night, - R.drawable.context_menu_card_light, R.drawable.context_menu_card_dark); - view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - MapillaryPlugin.openMapillary(getMapActivity(), null); - } - }); - } - } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryImageCard.java b/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryImageCard.java index 22e70babea8..52c852e6470 100644 --- a/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryImageCard.java +++ b/OsmAnd/src/net/osmand/plus/plugins/mapillary/MapillaryImageCard.java @@ -1,11 +1,8 @@ package net.osmand.plus.plugins.mapillary; -import android.view.View.OnClickListener; - import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; -import net.osmand.util.Algorithms; import org.json.JSONObject; @@ -16,16 +13,5 @@ public MapillaryImageCard(MapActivity mapActivity, JSONObject imageObject) { if (topIconId == 0) { topIconId = R.drawable.ic_logo_mapillary; } - OnClickListener onClickListener = v -> { - MapActivity activity = getMapActivity(); - activity.getContextMenu().close(); - MapillaryImageDialog.show(activity, getKey(), getImageHiresUrl(), getUrl(), getLocation(), - getCa(), activity.getString(R.string.mapillary), null, true); - }; - if (!Algorithms.isEmpty(buttonText)) { - this.onButtonClickListener = onClickListener; - } else { - this.onClickListener = onClickListener; - } } } diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikiImageCard.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikiImageCard.java index bcb27270c7b..7d50a587503 100644 --- a/OsmAnd/src/net/osmand/plus/wikipedia/WikiImageCard.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikiImageCard.java @@ -1,13 +1,10 @@ package net.osmand.plus.wikipedia; -import android.view.View; - import androidx.annotation.NonNull; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; -import net.osmand.util.Algorithms; import net.osmand.wiki.WikiImage; public class WikiImageCard extends ImageCard { @@ -27,15 +24,6 @@ public WikiImageCard(@NonNull MapActivity mapActivity, @NonNull WikiImage wikiIm this.title = wikiImage.getImageName(); this.url = this.imageUrl; this.imageHiresUrl = wikiImage.getImageHiResUrl(); - - View.OnClickListener listener = v -> openUrl(mapActivity, app, getTitle(), - wikiImage.getUrlWithCommonAttributions(), false, false); - - if (!Algorithms.isEmpty(buttonText)) { - this.onButtonClickListener = listener; - } else { - this.onClickListener = listener; - } } @NonNull