From a4295cb43de54dd0428563d46e66a89320f6afef Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 13 Jul 2019 08:53:46 +0300 Subject: [PATCH] profile editor: validate entered URL --- .../ui/profiles/ProfileDetailFragment.java | 15 +++++++++++++-- app/src/main/res/values-bg/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java index b51ddbb5..c6dbc6a5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailFragment.java @@ -46,6 +46,8 @@ import net.ktnx.mobileledger.utils.Colors; import org.jetbrains.annotations.NotNull; +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; import java.util.Objects; @@ -321,11 +323,20 @@ public class ProfileDetailFragment extends Fragment implements HueRingDialog.Hue profileNameLayout.setError(getResources().getText(R.string.err_profile_name_empty)); } - val = String.valueOf(url.getText()); - if (val.trim().isEmpty()) { + val = String.valueOf(url.getText()).trim(); + if (val.isEmpty()) { valid = false; urlLayout.setError(getResources().getText(R.string.err_profile_url_empty)); } + try { + URL url = new URL(val); + String host = url.getHost(); + if (host == null || host.isEmpty()) throw new MalformedURLException("Missing host"); + } + catch (MalformedURLException e) { + valid = false; + urlLayout.setError(getResources().getText(R.string.err_invalid_url)); + } if (useAuthentication.isChecked()) { val = String.valueOf(userName.getText()); if (val.trim().isEmpty()) { diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 64c262e9..5b61f83e 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -109,5 +109,6 @@ Потвърдете окончателното премахване на профила Премахване Зареждане… + Грешен или непълен адрес diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7e4cf10f..6d1632d8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -151,4 +151,5 @@ Permanently remove this profile? Remove Loading… + Invalid URL -- 2.39.5