From 9fdbdee6c75bfa9dd17a1353293d0e3321ceab2c Mon Sep 17 00:00:00 2001 From: Ezerous Date: Thu, 14 Jun 2018 12:29:56 +0300 Subject: [PATCH] TopicActivity minor improvements (sanitized URL, added Unauthorized support) --- app/src/main/AndroidManifest.xml | 4 +- .../activities/board/BoardActivity.java | 4 +- .../downloads/DownloadsActivity.java | 4 +- .../activities/main/forum/ForumFragment.java | 4 +- .../main/recent/RecentFragment.java | 4 +- .../main/unread/UnreadFragment.java | 4 +- .../latestPosts/LatestPostsFragment.java | 2 +- .../profile/summary/SummaryFragment.java | 2 +- .../activities/topic/TopicActivity.java | 151 +++++++++++------- .../mthmmy/activities/topic/TopicParser.java | 26 +-- .../gr/thmmy/mthmmy/base/BaseActivity.java | 6 +- .../java/gr/thmmy/mthmmy/model/ThmmyPage.java | 16 ++ .../downloads/DownloadsReceiver.java} | 28 ++-- .../DownloadsService.java} | 19 ++- .../thmmy/mthmmy/session/SessionManager.java | 2 +- .../ParseException.java | 2 +- .../utils/{ => parsing}/ParseHelpers.java | 2 +- .../mthmmy/utils/{ => parsing}/ParseTask.java | 3 +- 18 files changed, 163 insertions(+), 120 deletions(-) rename app/src/main/java/gr/thmmy/mthmmy/{receiver/Receiver.java => services/downloads/DownloadsReceiver.java} (76%) rename app/src/main/java/gr/thmmy/mthmmy/services/{DownloadService.java => downloads/DownloadsService.java} (94%) rename app/src/main/java/gr/thmmy/mthmmy/utils/{exceptions => parsing}/ParseException.java (84%) rename app/src/main/java/gr/thmmy/mthmmy/utils/{ => parsing}/ParseHelpers.java (99%) rename app/src/main/java/gr/thmmy/mthmmy/utils/{ => parsing}/ParseTask.java (96%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 98f8b187..bca9ca45 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -117,7 +117,7 @@ diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java index 1733986f..c6620218 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java @@ -25,8 +25,8 @@ import gr.thmmy.mthmmy.model.Board; import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.Topic; -import gr.thmmy.mthmmy.utils.ParseTask; -import gr.thmmy.mthmmy.utils.exceptions.ParseException; +import gr.thmmy.mthmmy.utils.parsing.ParseTask; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import timber.log.Timber; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java index 7e3a4526..6d899d23 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java @@ -22,8 +22,8 @@ import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.model.Download; import gr.thmmy.mthmmy.model.ThmmyPage; -import gr.thmmy.mthmmy.utils.ParseTask; -import gr.thmmy.mthmmy.utils.exceptions.ParseException; +import gr.thmmy.mthmmy.utils.parsing.ParseTask; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import timber.log.Timber; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java index dcd9ab3c..02da2734 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java @@ -27,8 +27,8 @@ import gr.thmmy.mthmmy.model.Board; import gr.thmmy.mthmmy.model.Category; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; -import gr.thmmy.mthmmy.utils.ParseTask; -import gr.thmmy.mthmmy.utils.exceptions.ParseException; +import gr.thmmy.mthmmy.utils.parsing.ParseTask; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.HttpUrl; import okhttp3.Request; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index 9f9be77e..5dab8c1e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -24,8 +24,8 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; -import gr.thmmy.mthmmy.utils.ParseTask; -import gr.thmmy.mthmmy.utils.exceptions.ParseException; +import gr.thmmy.mthmmy.utils.parsing.ParseTask; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import timber.log.Timber; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index 93824a61..7b42fb20 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java @@ -25,8 +25,8 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.CustomRecyclerView; -import gr.thmmy.mthmmy.utils.ParseTask; -import gr.thmmy.mthmmy.utils.exceptions.ParseException; +import gr.thmmy.mthmmy.utils.parsing.ParseTask; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.Request; import timber.log.Timber; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java index a8d75451..277b01a0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java @@ -24,7 +24,7 @@ import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.PostSummary; -import gr.thmmy.mthmmy.utils.ParseHelpers; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.Request; import okhttp3.Response; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java index d77d1180..8993fcf0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java @@ -23,7 +23,7 @@ import java.util.ArrayList; import java.util.Objects; import gr.thmmy.mthmmy.R; -import gr.thmmy.mthmmy.utils.ParseHelpers; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import timber.log.Timber; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 0dc6ce6b..e18dc2e7 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -1,5 +1,6 @@ package gr.thmmy.mthmmy.activities.topic; +import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.graphics.Rect; @@ -47,7 +48,8 @@ import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; -import gr.thmmy.mthmmy.utils.ParseHelpers; +import gr.thmmy.mthmmy.utils.parsing.ParseException; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import okhttp3.MultipartBody; import okhttp3.Request; @@ -204,6 +206,8 @@ public class TopicActivity extends BaseActivity { finish(); } + topicPageUrl = ThmmyPage.sanitizeTopicUrl(topicPageUrl); + thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl), true); //Initializes graphics @@ -275,7 +279,7 @@ public class TopicActivity extends BaseActivity { //Gets posts topicTask = new TopicTask(); - topicTask.execute(extras.getString(BUNDLE_TOPIC_URL)); //Attempt data parsing + topicTask.execute(topicPageUrl); //Attempt data parsing } @Override @@ -413,6 +417,7 @@ public class TopicActivity extends BaseActivity { } } + @SuppressLint("ClickableViewAccessibility") private void initIncrementButton(ImageButton increment, final int step) { // Increment once for a click increment.setOnClickListener(new View.OnClickListener() { @@ -461,6 +466,7 @@ public class TopicActivity extends BaseActivity { }); } + @SuppressLint("ClickableViewAccessibility") private void initDecrementButton(ImageButton decrement, final int step) { // Decrement once for a click decrement.setOnClickListener(new View.OnClickListener() { @@ -537,6 +543,10 @@ public class TopicActivity extends BaseActivity { } } //------------------------------------BOTTOM NAV BAR METHODS END------------------------------------ + private enum ResultCode { + SUCCESS, NETWORK_ERROR, PARSING_ERROR, OTHER_ERROR, SAME_PAGE, UNAUTHORIZED + } + /** * An {@link AsyncTask} that handles asynchronous fetching of this topic page and parsing of its @@ -544,12 +554,7 @@ public class TopicActivity extends BaseActivity { *

TopicTask's {@link AsyncTask#execute execute} method needs a topic's url as String * parameter.

*/ - class TopicTask extends AsyncTask { - private static final int SUCCESS = 0; - private static final int NETWORK_ERROR = 1; - private static final int OTHER_ERROR = 2; - private static final int SAME_PAGE = 3; - + class TopicTask extends AsyncTask { ArrayList localPostsList; @Override @@ -559,8 +564,8 @@ public class TopicActivity extends BaseActivity { if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(false); } - protected Integer doInBackground(String... strings) { - Document document; + protected ResultCode doInBackground(String... strings) { + Document document = null; String newPageUrl = strings[0]; //Finds the index of message focus if present @@ -578,7 +583,7 @@ public class TopicActivity extends BaseActivity { if (!reloadingPage && !Objects.equals(loadedPageUrl, "") && newPageUrl.contains(base_url)) { if (newPageUrl.contains("topicseen#new") || newPageUrl.contains("#new")) if (thisPage == numberOfPages) - return SAME_PAGE; + return ResultCode.SAME_PAGE; if (newPageUrl.contains("msg")) { String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); if (tmpUrlSbstr.contains("msg")) @@ -586,12 +591,12 @@ public class TopicActivity extends BaseActivity { int testAgainst = Integer.parseInt(tmpUrlSbstr); for (Post post : postsList) { if (post.getPostIndex() == testAgainst) { - return SAME_PAGE; + return ResultCode.SAME_PAGE; } } } else if ((Objects.equals(newPageUrl, base_url) && thisPage == 1) || Integer.parseInt(newPageUrl.substring(base_url.length() + 1)) / 15 + 1 == thisPage) - return SAME_PAGE; + return ResultCode.SAME_PAGE; } else if (!Objects.equals(loadedPageUrl, "")) topicTitle = null; if (reloadingPage) reloadingPage = !reloadingPage; @@ -614,17 +619,22 @@ public class TopicActivity extends BaseActivity { break; } } - return SUCCESS; + return ResultCode.SUCCESS; } catch (IOException e) { Timber.i(e, "IO Exception"); - return NETWORK_ERROR; + return ResultCode.NETWORK_ERROR; + } catch (ParseException e) { + if(isUnauthorized(document)) + return ResultCode.UNAUTHORIZED; + Timber.e(e, "Parsing Error"); + return ResultCode.PARSING_ERROR; } catch (Exception e) { Timber.e(e, "Exception"); - return OTHER_ERROR; + return ResultCode.OTHER_ERROR; } } - protected void onPostExecute(Integer parseResult) { + protected void onPostExecute(ResultCode parseResult) { switch (parseResult) { case SUCCESS: if (topicTitle == null || Objects.equals(topicTitle, "") @@ -635,7 +645,7 @@ public class TopicActivity extends BaseActivity { invalidateOptionsMenu(); } - if (!(postsList.isEmpty() || postsList.size() == 0)) { + if (!postsList.isEmpty()) { recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug postsList.clear(); topicAdapter.notifyItemRangeRemoved(0, postsList.size() - 1); @@ -661,16 +671,14 @@ public class TopicActivity extends BaseActivity { Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); break; case SAME_PAGE: - progressBar.setVisibility(ProgressBar.INVISIBLE); - if (replyPageUrl == null) { - replyFAB.hide(); - topicAdapter.resetTopic(base_url, new TopicTask(), false); - } else topicAdapter.resetTopic(base_url, new TopicTask(), true); - if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(true); - paginationEnabled(true); - Toast.makeText(TopicActivity.this, "That's the same page.", Toast.LENGTH_SHORT).show(); + stopLoading(); + Toast.makeText(getBaseContext(), "That's the same page", Toast.LENGTH_SHORT).show(); //TODO change focus break; + case UNAUTHORIZED: + stopLoading(); + Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show(); + break; default: //Parse failed - should never happen Timber.d("Parse failed!"); //TODO report ParseException!!! @@ -680,55 +688,76 @@ public class TopicActivity extends BaseActivity { } } + private void stopLoading(){ + progressBar.setVisibility(ProgressBar.INVISIBLE); + if (replyPageUrl == null) { + replyFAB.hide(); + topicAdapter.resetTopic(base_url, new TopicTask(), false); + } else topicAdapter.resetTopic(base_url, new TopicTask(), true); + if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(true); + paginationEnabled(true); + } + /** * All the parsing a topic needs. * * @param topic {@link Document} object containing this topic's source code * @see org.jsoup.Jsoup Jsoup */ - private ArrayList parse(Document topic) { - ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); + private ArrayList parse(Document topic) throws ParseException{ + try { + ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); - //Finds topic's tree, mods and users viewing - { - topicTreeAndMods = getSpannableFromHtml(topic.select("div.nav").first().html()); - topicViewers = getSpannableFromHtml(TopicParser.parseUsersViewingThisTopic(topic, language)); - } + //Finds topic's tree, mods and users viewing + { + topicTreeAndMods = getSpannableFromHtml(topic.select("div.nav").first().html()); + topicViewers = getSpannableFromHtml(TopicParser.parseUsersViewingThisTopic(topic, language)); + } - //Finds reply page url - { - Element replyButton = topic.select("a:has(img[alt=Reply])").first(); - if (replyButton == null) - replyButton = topic.select("a:has(img[alt=Απάντηση])").first(); - if (replyButton != null) replyPageUrl = replyButton.attr("href"); - } + //Finds reply page url + { + Element replyButton = topic.select("a:has(img[alt=Reply])").first(); + if (replyButton == null) + replyButton = topic.select("a:has(img[alt=Απάντηση])").first(); + if (replyButton != null) replyPageUrl = replyButton.attr("href"); + } - //Finds topic title if missing - { - parsedTitle = topic.select("td[id=top_subject]").first().text(); - if (parsedTitle.contains("Topic:")) { - parsedTitle = parsedTitle.substring(parsedTitle.indexOf("Topic:") + 7 - , parsedTitle.indexOf("(Read") - 2); - } else { - parsedTitle = parsedTitle.substring(parsedTitle.indexOf("Θέμα:") + 6 - , parsedTitle.indexOf("(Αναγνώστηκε") - 2); - Timber.d("Parsed title: %s", parsedTitle); + //Finds topic title if missing + { + parsedTitle = topic.select("td[id=top_subject]").first().text(); + if (parsedTitle.contains("Topic:")) { + parsedTitle = parsedTitle.substring(parsedTitle.indexOf("Topic:") + 7 + , parsedTitle.indexOf("(Read") - 2); + } else { + parsedTitle = parsedTitle.substring(parsedTitle.indexOf("Θέμα:") + 6 + , parsedTitle.indexOf("(Αναγνώστηκε") - 2); + Timber.d("Parsed title: %s", parsedTitle); + } } - } - { //Finds current page's index - thisPage = TopicParser.parseCurrentPageIndex(topic, language); - } - { //Finds number of pages - numberOfPages = TopicParser.parseTopicNumberOfPages(topic, thisPage, language); + { //Finds current page's index + thisPage = TopicParser.parseCurrentPageIndex(topic, language); + } + { //Finds number of pages + numberOfPages = TopicParser.parseTopicNumberOfPages(topic, thisPage, language); - for (int i = 0; i < numberOfPages; i++) { - //Generate each page's url from topic's base url +".15*numberOfPage" - pagesUrls.put(i, base_url + "." + String.valueOf(i * 15)); + for (int i = 0; i < numberOfPages; i++) { + //Generate each page's url from topic's base url +".15*numberOfPage" + pagesUrls.put(i, base_url + "." + String.valueOf(i * 15)); + } } + + return TopicParser.parseTopic(topic, language); + } catch (Exception e) { + throw new ParseException("Parsing failed (TopicTask)"); } + } - return TopicParser.parseTopic(topic, language); + private boolean isUnauthorized(Document document) { + return document != null && document.select("body:contains(The topic or board you" + + " are looking for appears to be either missing or off limits to you.)," + + "body:contains(Το θέμα ή πίνακας που ψάχνετε ή δεν υπάρχει ή δεν " + + "είναι προσβάσιμο από εσάς.)").size() > 0; } private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) { @@ -881,7 +910,7 @@ public class TopicActivity extends BaseActivity { //TODO this... return true; default: - Timber.e("Malformed post. Request string:\n" + post.toString()); + Timber.e("Malformed post. Request string: %s", post.toString()); return true; } } catch (IOException e) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java index 3589f4cf..97f134ae 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java @@ -16,17 +16,17 @@ import java.util.Objects; import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyFile; -import gr.thmmy.mthmmy.utils.ParseHelpers; +import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import timber.log.Timber; /** * Singleton used for parsing a topic. *

Class contains the methods:

  • {@link #parseUsersViewingThisTopic(Document, - * gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • - *
  • {@link #parseCurrentPageIndex(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • - *
  • {@link #parseTopicNumberOfPages(Document, int, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • - *
  • {@link #parseTopic(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}
  • + * ParseHelpers.Language)} + *
  • {@link #parseCurrentPageIndex(Document, ParseHelpers.Language)}
  • + *
  • {@link #parseTopicNumberOfPages(Document, int, ParseHelpers.Language)}
  • + *
  • {@link #parseTopic(Document, ParseHelpers.Language)}
  • */ class TopicParser { //User colors @@ -42,9 +42,9 @@ class TopicParser { * Returns users currently viewing this topic. * * @param topic {@link Document} object containing this topic's source code - * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * @param language a {@link ParseHelpers.Language} containing this topic's * language set, this is returned by - * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} + * {@link ParseHelpers.Language#getLanguage(Document)} * @return String containing html with the usernames of users * @see org.jsoup.Jsoup Jsoup */ @@ -58,9 +58,9 @@ class TopicParser { * Returns current topic's page index. * * @param topic {@link Document} object containing this topic's source code - * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * @param language a {@link ParseHelpers.Language} containing this topic's * language set, this is returned by - * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} + * {@link ParseHelpers.Language#getLanguage(Document)} * @return int containing parsed topic's current page * @see org.jsoup.Jsoup Jsoup */ @@ -96,9 +96,9 @@ class TopicParser { * * @param topic {@link Document} object containing this topic's source code * @param currentPage an int containing current page of this topic - * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * @param language a {@link ParseHelpers.Language} containing this topic's * language set, this is returned by - * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} + * {@link ParseHelpers.Language#getLanguage(Document)} * @return int containing the number of pages * @see org.jsoup.Jsoup Jsoup */ @@ -134,9 +134,9 @@ class TopicParser { * This method parses all the information of a topic and it's posts. * * @param topic {@link Document} object containing this topic's source code - * @param language a {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language} containing this topic's + * @param language a {@link ParseHelpers.Language} containing this topic's * language set, this is returned by - * {@link gr.thmmy.mthmmy.utils.ParseHelpers.Language#getLanguage(Document)} + * {@link ParseHelpers.Language#getLanguage(Document)} * @return {@link ArrayList} of {@link Post}s * @see org.jsoup.Jsoup Jsoup */ diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index ca50672c..833f7028 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -43,7 +43,7 @@ import gr.thmmy.mthmmy.activities.main.MainActivity; import gr.thmmy.mthmmy.activities.profile.ProfileActivity; import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.model.ThmmyFile; -import gr.thmmy.mthmmy.services.DownloadService; +import gr.thmmy.mthmmy.services.downloads.DownloadsService; import gr.thmmy.mthmmy.session.SessionManager; import okhttp3.OkHttpClient; @@ -611,7 +611,7 @@ public abstract class BaseActivity extends AppCompatActivity { public void launchDownloadService(ThmmyFile thmmyFile) { if (checkPerms()) - DownloadService.startActionDownload(this, thmmyFile.getFileUrl().toString()); + DownloadsService.startActionDownload(this, thmmyFile.getFileUrl().toString()); else { tempThmmyFile = thmmyFile; requestPerms(); @@ -621,7 +621,7 @@ public abstract class BaseActivity extends AppCompatActivity { //Uses temp file - called after permission grant private void launchDownloadService() { if (checkPerms()) - DownloadService.startActionDownload(this, tempThmmyFile.getFileUrl().toString()); + DownloadsService.startActionDownload(this, tempThmmyFile.getFileUrl().toString()); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java b/app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java index 8962f2b9..3478b618 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java @@ -3,6 +3,8 @@ package gr.thmmy.mthmmy.model; import android.net.Uri; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import timber.log.Timber; @@ -187,4 +189,18 @@ public class ThmmyPage { } return null; } + + /** + * This method gets a VALID topic url and strips any unnecessary stuff (like e.g. wap2) + * + * @param topicUrl a valid topic url + * @return sanitized topic url + */ + public static String sanitizeTopicUrl(String topicUrl) { + Pattern pattern = Pattern.compile("http.*topic=\\d*\\.?\\d*"); + Matcher matcher = pattern.matcher(topicUrl); + if (matcher.find()) + return matcher.group(0); + return null; + } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java b/app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsReceiver.java similarity index 76% rename from app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java rename to app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsReceiver.java index f37e5078..535372a4 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsReceiver.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.receiver; +package gr.thmmy.mthmmy.services.downloads; import android.app.NotificationChannel; import android.app.NotificationManager; @@ -16,23 +16,23 @@ import java.io.File; import timber.log.Timber; -import static gr.thmmy.mthmmy.services.DownloadService.ACTION_DOWNLOAD; -import static gr.thmmy.mthmmy.services.DownloadService.COMPLETED; -import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_ID; -import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_STATE; -import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_FILE_NAME; -import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TEXT; -import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TICKER; -import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TITLE; -import static gr.thmmy.mthmmy.services.DownloadService.SAVE_DIR; -import static gr.thmmy.mthmmy.services.DownloadService.STARTED; - -public class Receiver extends BroadcastReceiver { +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.ACTION_DOWNLOAD; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.COMPLETED; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_DOWNLOAD_ID; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_DOWNLOAD_STATE; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_FILE_NAME; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_NOTIFICATION_TEXT; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_NOTIFICATION_TICKER; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_NOTIFICATION_TITLE; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.SAVE_DIR; +import static gr.thmmy.mthmmy.services.downloads.DownloadsService.STARTED; + +public class DownloadsReceiver extends BroadcastReceiver { private static final String NOTIFICATION_TAG = "DOWNLOADS"; private static final String DOWNLOADS_CHANNEL_ID = "Downloads"; private static final String DOWNLOADS_CHANNEL_NAME = "Downloads"; - public Receiver() {} + public DownloadsReceiver() {} @Override public void onReceive(Context context, Intent intent) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java b/app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsService.java similarity index 94% rename from app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java rename to app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsService.java index be081a25..9692f5ea 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsService.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.services; +package gr.thmmy.mthmmy.services.downloads; import android.app.DownloadManager; import android.app.IntentService; @@ -14,7 +14,6 @@ import java.io.FileNotFoundException; import java.io.IOException; import gr.thmmy.mthmmy.base.BaseApplication; -import gr.thmmy.mthmmy.receiver.Receiver; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -26,11 +25,11 @@ import timber.log.Timber; * An {@link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. */ -public class DownloadService extends IntentService { - private static final String TAG = "DownloadService"; +public class DownloadsService extends IntentService { + private static final String TAG = "DownloadsService"; private static int sDownloadId = 0; - private Receiver receiver; + private DownloadsReceiver receiver; public static final String SAVE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "mthmmy"; @@ -49,15 +48,15 @@ public class DownloadService extends IntentService { public static final String FAILED = "Failed"; - public DownloadService() { - super("DownloadService"); + public DownloadsService() { + super("DownloadsService"); } @Override public void onCreate() { super.onCreate(); - final IntentFilter filter = new IntentFilter(DownloadService.ACTION_DOWNLOAD); - receiver = new Receiver(); + final IntentFilter filter = new IntentFilter(DownloadsService.ACTION_DOWNLOAD); + receiver = new DownloadsReceiver(); registerReceiver(receiver, filter); } @@ -75,7 +74,7 @@ public class DownloadService extends IntentService { * @see IntentService */ public static void startActionDownload(Context context, String downloadUrl) { - Intent intent = new Intent(context, DownloadService.class); + Intent intent = new Intent(context, DownloadsService.class); intent.setAction(ACTION_DOWNLOAD); intent.putExtra(EXTRA_DOWNLOAD_URL, downloadUrl); context.startService(intent); diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index 7f976aaa..94f8a284 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -17,7 +17,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import gr.thmmy.mthmmy.utils.exceptions.ParseException; +import gr.thmmy.mthmmy.utils.parsing.ParseException; import okhttp3.Cookie; import okhttp3.FormBody; import okhttp3.HttpUrl; diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java similarity index 84% rename from app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java index b0947f04..fea7be6b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.utils.exceptions; +package gr.thmmy.mthmmy.utils.parsing; /** * ParseException is to be used for errors while parsing. diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java similarity index 99% rename from app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java index a2e80cca..307efab0 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.utils; +package gr.thmmy.mthmmy.utils.parsing; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseTask.java similarity index 96% rename from app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseTask.java index ce7df62a..16b21583 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseTask.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.utils; +package gr.thmmy.mthmmy.utils.parsing; import android.os.AsyncTask; import android.widget.Toast; @@ -9,7 +9,6 @@ import org.jsoup.nodes.Document; import java.io.IOException; import gr.thmmy.mthmmy.base.BaseApplication; -import gr.thmmy.mthmmy.utils.exceptions.ParseException; import okhttp3.Request; import okhttp3.Response; import timber.log.Timber;