From 6656724a533bbf5fa57a8808c008aab7f6cd8c0c Mon Sep 17 00:00:00 2001 From: Shinokuni Date: Thu, 31 Jan 2019 15:04:31 +0000 Subject: [PATCH] Adding url field verification to add feed dialog (still lacks other http errors) --- .idea/caches/build_file_checksums.ser | Bin 592 -> 592 bytes .../java/com/readrops/app/AddFeedDialog.java | 51 ++++++++++++++---- app/src/main/java/com/readrops/app/Utils.java | 25 +++++++++ app/src/main/res/layout/add_feed_layout.xml | 3 ++ app/src/main/res/values-fr-rFR/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + .../readrops/readropslibrary/HtmlParser.java | 28 +++++----- 7 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/com/readrops/app/Utils.java diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 2aa44f3055d94d69a13d3dac3949005b907191c5..b93664995a09c075515f6d2f05011d39fc16a60a 100644 GIT binary patch delta 33 rcmV++0N(%51kePKm;|7RC@!&_qX7{QeB{9=hlQMvqQ_(UL!vEs^(hZC delta 33 rcmV++0N(%51kePKm;{gI83M7KqX7{+;Ma|jgs5+zpY!x{!9-| diff --git a/app/src/main/java/com/readrops/app/AddFeedDialog.java b/app/src/main/java/com/readrops/app/AddFeedDialog.java index b1bebd54..f51c1750 100644 --- a/app/src/main/java/com/readrops/app/AddFeedDialog.java +++ b/app/src/main/java/com/readrops/app/AddFeedDialog.java @@ -11,6 +11,7 @@ import android.support.design.widget.TextInputEditText; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.util.Patterns; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; @@ -18,6 +19,8 @@ import android.widget.ProgressBar; import com.readrops.readropslibrary.HtmlParser; import com.readrops.readropslibrary.ParsingResult; +import java.io.IOException; +import java.nio.charset.MalformedInputException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executors; @@ -53,19 +56,49 @@ public class AddFeedDialog extends Dialog implements View.OnClickListener { @Override public void onClick(View view) { - if (recyclerView != null && recyclerView.getVisibility() == View.VISIBLE) - recyclerView.setVisibility(View.GONE); - progressBar.setVisibility(View.VISIBLE); + if (isValidUrl()) { + if (recyclerView != null && recyclerView.getVisibility() == View.VISIBLE) + recyclerView.setVisibility(View.GONE); + progressBar.setVisibility(View.VISIBLE); - parseUrl(textInputEditText.getText().toString()); + parseUrl(); + } } - private void parseUrl(String url) { - Executors.newSingleThreadExecutor().execute(() -> { - List results = HtmlParser.getFeedLink(url); + private boolean isValidUrl() { + String url = textInputEditText.getText().toString().trim(); + + if (url.isEmpty()) { + textInputEditText.setError(getContext().getString(R.string.add_feed_empty_field)); + return false; + } else if (!Patterns.WEB_URL.matcher(url).matches()) { + textInputEditText.setError(getContext().getString(R.string.add_feed_wrong_url)); + return false; + } else + return true; + } + + private void parseUrl() { + String url = textInputEditText.getText().toString().trim(); + + final String finalUrl; + if (!(url.contains(Utils.HTTP_PREFIX) || url.contains(Utils.HTTPS_PREFIX))) + finalUrl = Utils.HTTPS_PREFIX + url; + else + finalUrl = url; + + + Executors.newSingleThreadExecutor().execute(() -> { + try { + List results = HtmlParser.getFeedLink(finalUrl); + + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(() -> displayResults(results)); + } catch (Exception e) { + e.printStackTrace(); + } + - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(() -> displayResults(results)); }); } diff --git a/app/src/main/java/com/readrops/app/Utils.java b/app/src/main/java/com/readrops/app/Utils.java new file mode 100644 index 00000000..63d34bdf --- /dev/null +++ b/app/src/main/java/com/readrops/app/Utils.java @@ -0,0 +1,25 @@ +package com.readrops.app; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.widget.Toast; + +public final class Utils { + + public static final String HTTP_PREFIX = "http://"; + + public static final String HTTPS_PREFIX = "https://"; + + public static void displayErrorInMainThread(Context context, String message) { + Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG); + + if (!(Looper.myLooper() == Looper.getMainLooper())) { + Handler handler = new Handler(Looper.getMainLooper()); + Looper.prepare(); + handler.post(toast::show); + } else + toast.show(); + + } +} diff --git a/app/src/main/res/layout/add_feed_layout.xml b/app/src/main/res/layout/add_feed_layout.xml index 2ce04fd0..d643b808 100644 --- a/app/src/main/res/layout/add_feed_layout.xml +++ b/app/src/main/res/layout/add_feed_layout.xml @@ -15,6 +15,9 @@ android:layout_marginEnd="10dp" android:text="@string/add_feed_title" android:textAlignment="center" + android:textSize="22sp" + android:textColor="#FF3D3A3A" + android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> diff --git a/app/src/main/res/values-fr-rFR/strings.xml b/app/src/main/res/values-fr-rFR/strings.xml index d1e0af11..b077cdea 100644 --- a/app/src/main/res/values-fr-rFR/strings.xml +++ b/app/src/main/res/values-fr-rFR/strings.xml @@ -11,5 +11,7 @@ Ajouter un flux Adresse du flux Valider + Le champ ne peut pas ĂȘtre vide + La valeur n\'est pas une adresse web valide \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7ea603e9..5ab3b283 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,4 +12,6 @@ Add feed Feed url Validate + Field can\'t be empty + Input is not a valid URL diff --git a/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java b/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java index d7ff6d18..d38f939a 100644 --- a/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java +++ b/readropslibrary/src/main/java/com/readrops/readropslibrary/HtmlParser.java @@ -20,30 +20,26 @@ public final class HtmlParser { * @param url url to request * @return a list of rss urls with their title */ - public static List getFeedLink(String url) { + public static List getFeedLink(String url) throws Exception { List results = new ArrayList<>(); - try { - Document document = Jsoup.connect(url).get(); - Elements elements = document.select("link"); + Document document = Jsoup.connect(url).get(); - for (Element element : elements) { - String type = element.attributes().get("type"); + Elements elements = document.select("link"); - if (isTypeRssFeed(type)) { - String feedUrl = element.attributes().get("href"); - String label = element.attributes().get("title"); + for (Element element : elements) { + String type = element.attributes().get("type"); - results.add(new ParsingResult(feedUrl, label)); - } + if (isTypeRssFeed(type)) { + String feedUrl = element.attributes().get("href"); + String label = element.attributes().get("title"); + + results.add(new ParsingResult(feedUrl, label)); } - - return results; - } catch (Exception e) { - e.printStackTrace(); } - return null; + return results; + } private static boolean isTypeRssFeed(String type) {