From ef1d2c229e425da3d9e59e4ed005c17f1d68d5cd Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 2 Sep 2021 20:28:27 +0200 Subject: [PATCH] Refactoring --- .../email/FragmentDialogOpenLink.java | 22 +---------------- .../java/eu/faircode/email/UriHelper.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java index 4567f69cdc..8059b9619c 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogOpenLink.java @@ -46,7 +46,6 @@ import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.view.inputmethod.EditorInfo; -import android.webkit.URLUtil; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; @@ -139,26 +138,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase { // Preload web view Helper.customTabsWarmup(context); - final Uri uri; - if (_uri.getScheme() == null) { - String url = _uri.toString(); - if (Helper.EMAIL_ADDRESS.matcher(url).matches()) - uri = Uri.parse("mailto:" + _uri.toString()); - else if (android.util.Patterns.PHONE.matcher(url).matches()) - // Alternative: PhoneNumberUtils.isGlobalPhoneNumber() - uri = Uri.parse("tel:" + _uri.toString()); - else { - Uri g = Uri.parse(URLUtil.guessUrl(url)); - String scheme = g.getScheme(); - if (scheme != null) { - if ("http".equals(scheme)) - scheme = "https"; - uri = Uri.parse(scheme + "://" + _uri.toString()); - } else - uri = _uri; - } - } else - uri = _uri; + final Uri uri = UriHelper.guessScheme(_uri); // Process link final Uri sanitized; diff --git a/app/src/main/java/eu/faircode/email/UriHelper.java b/app/src/main/java/eu/faircode/email/UriHelper.java index 2679d3f80e..73175b0749 100644 --- a/app/src/main/java/eu/faircode/email/UriHelper.java +++ b/app/src/main/java/eu/faircode/email/UriHelper.java @@ -20,7 +20,9 @@ package eu.faircode.email; */ import android.content.Context; +import android.net.Uri; import android.text.TextUtils; +import android.webkit.URLUtil; import androidx.annotation.NonNull; @@ -96,6 +98,28 @@ public class UriHelper { return null; } + static @NonNull + Uri guessScheme(@NonNull Uri uri) { + if (uri.getScheme() != null) + return uri; + + String url = uri.toString(); + if (Helper.EMAIL_ADDRESS.matcher(url).matches()) + return Uri.parse("mailto:" + url); + else if (android.util.Patterns.PHONE.matcher(url).matches()) + // Alternative: PhoneNumberUtils.isGlobalPhoneNumber() + return Uri.parse("tel:" + url); + else { + Uri g = Uri.parse(URLUtil.guessUrl(url)); + String scheme = g.getScheme(); + if (scheme == null) + return uri; + else if ("http".equals(scheme)) + scheme = "https"; + return Uri.parse(scheme + "://" + url); + } + } + static void ensureSuffixList(Context context) { synchronized (suffixList) { if (suffixList.size() > 0)