Highlight domain name in open link dialog

This commit is contained in:
M66B 2021-06-22 18:15:23 +02:00
parent 2f69ed42e1
commit 1e2f8098cb
1 changed files with 20 additions and 5 deletions

View File

@ -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;
}
}