From 7739cc91b52d8d508b920bcb874c1fc0c0f59f6a Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 2 Jul 2021 09:51:56 +0200 Subject: [PATCH] Refactoring --- app/src/main/java/eu/faircode/email/DnsHelper.java | 9 ++------- .../main/java/eu/faircode/email/EmailProvider.java | 2 +- .../java/eu/faircode/email/FragmentCompose.java | 12 +++--------- app/src/main/java/eu/faircode/email/UriHelper.java | 13 ++++++++++++- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DnsHelper.java b/app/src/main/java/eu/faircode/email/DnsHelper.java index 303e0bed78..28394aca18 100644 --- a/app/src/main/java/eu/faircode/email/DnsHelper.java +++ b/app/src/main/java/eu/faircode/email/DnsHelper.java @@ -62,15 +62,10 @@ public class DnsHelper { for (Address address : addresses) { String email = ((InternetAddress) address).getAddress(); - if (email == null) + String domain = UriHelper.getEmailDomain(email); + if (domain == null) continue; - int d = email.lastIndexOf("@"); - if (d < 0) - continue; - - String domain = email.substring(d + 1); - boolean found = true; try { SimpleResolver resolver = new SimpleResolver(getDnsServer(context)); diff --git a/app/src/main/java/eu/faircode/email/EmailProvider.java b/app/src/main/java/eu/faircode/email/EmailProvider.java index 79afb5be80..e489472339 100644 --- a/app/src/main/java/eu/faircode/email/EmailProvider.java +++ b/app/src/main/java/eu/faircode/email/EmailProvider.java @@ -219,7 +219,7 @@ public class EmailProvider implements Parcelable { @NonNull static EmailProvider fromEmail(Context context, String email, Discover discover) throws IOException { - int at = email.indexOf("@"); + int at = email.indexOf('@'); String domain = (at < 0 ? email : email.substring(at + 1)); if (at < 0) email = "someone@" + domain; diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index e2da49c79c..561792b394 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -4112,7 +4112,7 @@ public class FragmentCompose extends FragmentBase { } if (preferred != null) { String from = ((InternetAddress) preferred).getAddress(); - data.draft.extra = from.substring(0, from.indexOf("@")); + data.draft.extra = UriHelper.getEmailUser(from); } } } @@ -5229,14 +5229,8 @@ public class FragmentCompose extends FragmentBase { String[] internals = identity.internal.split(","); for (Address recipient : recipients) { String email = ((InternetAddress) recipient).getAddress(); - if (TextUtils.isEmpty(email)) - continue; - - int at = email.lastIndexOf('@'); - if (at < 0) - continue; - String domain = email.substring(at + 1).trim(); - if (TextUtils.isEmpty(domain)) + String domain = UriHelper.getEmailDomain(email); + if (domain == null) continue; boolean found = false; diff --git a/app/src/main/java/eu/faircode/email/UriHelper.java b/app/src/main/java/eu/faircode/email/UriHelper.java index df6f19d9b7..6fda799e32 100644 --- a/app/src/main/java/eu/faircode/email/UriHelper.java +++ b/app/src/main/java/eu/faircode/email/UriHelper.java @@ -31,6 +31,17 @@ public class UriHelper { return host; } + static String getEmailUser(String address) { + if (address == null) + return null; + + int at = address.indexOf('@'); + if (at > 0) + return address.substring(0, at); + + return null; + } + static String getEmailDomain(String address) { if (address == null) return null; @@ -39,6 +50,6 @@ public class UriHelper { if (at > 0) return address.substring(at + 1); - return address; + return null; } }