From 1e2f8098cbf3a3e0a04216ba372a687643cd598a Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 22 Jun 2021 18:15:23 +0200 Subject: [PATCH] Highlight domain name in open link dialog --- .../email/FragmentDialogOpenLink.java | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java index 12abd229a3..7f51798892 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java @@ -31,9 +31,12 @@ import android.graphics.Typeface; import android.net.Uri; import android.os.Bundle; import android.text.Editable; +import android.text.SpannableStringBuilder; +import android.text.Spanned; import android.text.TextUtils; import android.text.TextWatcher; import android.text.method.LinkMovementMethod; +import android.text.style.ForegroundColorSpan; import android.util.Pair; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -155,7 +158,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { ibDifferent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - etLink.setText(uriTitle.toString()); + etLink.setText(format(uriTitle, context)); } }); @@ -238,7 +241,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { return; Uri uri = Uri.parse(etLink.getText().toString()); - etLink.setText(secure(uri, checked).toString()); + etLink.setText(format(secure(uri, checked), context)); } }); @@ -247,7 +250,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { cbSanitize.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { - etLink.setText(secure(checked ? sanitized : uri, cbSecure.isChecked()).toString()); + etLink.setText(format(secure(checked ? sanitized : uri, cbSecure.isChecked()), context)); } }); @@ -330,11 +333,11 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { } if (host != null && !host.equals(puny)) { - etLink.setText(uri.buildUpon().encodedAuthority(puny).build().toString()); + etLink.setText(format(uri.buildUpon().encodedAuthority(puny).build(), context)); tvLink.setText(uri.toString()); tvSuspicious.setVisibility(View.VISIBLE); } else { - etLink.setText(uri.toString()); + etLink.setText(format(uri, context)); tvLink.setText(null); tvSuspicious.setVisibility(Helper.isSingleScript(host) ? View.GONE : View.VISIBLE); } @@ -479,4 +482,16 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { } else return uri; } + + private Spanned format(Uri uri, Context context) { + String text = uri.toString(); + SpannableStringBuilder ssb = new SpannableStringBuilder(text); + String host = uri.getHost(); + if (host != null) { + int textColorLink = Helper.resolveColor(context, android.R.attr.textColorLink); + int index = text.indexOf(host); + ssb.setSpan(new ForegroundColorSpan(textColorLink), index, index + host.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); + } + return ssb; + } } \ No newline at end of file