Skip to content

Commit

Permalink
Show link if image loading failed (+ remove outdated code)
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-kutz committed Feb 11, 2025
1 parent 999de18 commit 5a450dd
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 459 deletions.
25 changes: 25 additions & 0 deletions OsmAnd/res/layout/gallery_card_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,29 @@
android:layout_marginTop="9dp"
android:visibility="gone" />

<TextView
android:id="@+id/url"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="@dimen/content_padding"
android:background="?attr/bg_color"
android:textColor="?attr/color_dialog_buttons"
android:textSize="@dimen/default_list_text_size"
android:visibility="gone"
tools:text="https://osmand.net/images/123456789012.jpg"/>

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"/>

<View
android:id="@+id/card_outline"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/context_menu_card"/>

</FrameLayout>
Original file line number Diff line number Diff line change
@@ -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 = "<html><body style='margin:0;padding:0'><img style='max-width:100%;max-height:100%;' src='" + url + "'/></body></html>";
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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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);
}
}
}
}
Loading

0 comments on commit 5a450dd

Please sign in to comment.