Browse Source

TopicActivity minor improvements (sanitized URL, added Unauthorized support)

pull/24/head
Ezerous 7 years ago
parent
commit
9fdbdee6c7
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 4
      app/src/main/AndroidManifest.xml
  2. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardActivity.java
  3. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsActivity.java
  4. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java
  5. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java
  6. 4
      app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java
  7. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/latestPosts/LatestPostsFragment.java
  8. 2
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java
  9. 85
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  10. 26
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicParser.java
  11. 6
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  12. 16
      app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java
  13. 28
      app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsReceiver.java
  14. 19
      app/src/main/java/gr/thmmy/mthmmy/services/downloads/DownloadsService.java
  15. 2
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  16. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseException.java
  17. 2
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseHelpers.java
  18. 3
      app/src/main/java/gr/thmmy/mthmmy/utils/parsing/ParseTask.java

4
app/src/main/AndroidManifest.xml

@ -117,7 +117,7 @@
</provider> </provider>
<service <service
android:name=".services.DownloadService" android:name=".services.downloads.DownloadsService"
android:exported="false" /> android:exported="false" />
<service <service
android:name=".services.NotificationService" android:name=".services.NotificationService"
@ -128,7 +128,7 @@
</service> </service>
<receiver <receiver
android:name=".receiver.Receiver" android:name=".services.downloads.DownloadsReceiver"
android:enabled="true" android:enabled="true"
android:exported="false"> android:exported="false">
<intent-filter> <intent-filter>

4
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.Bookmark;
import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.model.Topic; import gr.thmmy.mthmmy.model.Topic;
import gr.thmmy.mthmmy.utils.ParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import timber.log.Timber; import timber.log.Timber;

4
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.base.BaseActivity;
import gr.thmmy.mthmmy.model.Download; import gr.thmmy.mthmmy.model.Download;
import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.ParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import timber.log.Timber; import timber.log.Timber;

4
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.model.Category;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.ParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Request; import okhttp3.Request;

4
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.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.ParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import timber.log.Timber; import timber.log.Timber;

4
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.model.TopicSummary;
import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.SessionManager;
import gr.thmmy.mthmmy.utils.CustomRecyclerView; import gr.thmmy.mthmmy.utils.CustomRecyclerView;
import gr.thmmy.mthmmy.utils.ParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseTask;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseException;
import me.zhanghai.android.materialprogressbar.MaterialProgressBar; import me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.Request; import okhttp3.Request;
import timber.log.Timber; import timber.log.Timber;

2
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.BaseActivity;
import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.base.BaseFragment;
import gr.thmmy.mthmmy.model.PostSummary; 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 me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;

2
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 java.util.Objects;
import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.R;
import gr.thmmy.mthmmy.utils.ParseHelpers; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber; import timber.log.Timber;

85
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -1,5 +1,6 @@
package gr.thmmy.mthmmy.activities.topic; package gr.thmmy.mthmmy.activities.topic;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Rect; 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.Post;
import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.ThmmyPage;
import gr.thmmy.mthmmy.utils.CustomLinearLayoutManager; 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 me.zhanghai.android.materialprogressbar.MaterialProgressBar;
import okhttp3.MultipartBody; import okhttp3.MultipartBody;
import okhttp3.Request; import okhttp3.Request;
@ -204,6 +206,8 @@ public class TopicActivity extends BaseActivity {
finish(); finish();
} }
topicPageUrl = ThmmyPage.sanitizeTopicUrl(topicPageUrl);
thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl), true); thisPageBookmark = new Bookmark(topicTitle, ThmmyPage.getTopicId(topicPageUrl), true);
//Initializes graphics //Initializes graphics
@ -275,7 +279,7 @@ public class TopicActivity extends BaseActivity {
//Gets posts //Gets posts
topicTask = new TopicTask(); topicTask = new TopicTask();
topicTask.execute(extras.getString(BUNDLE_TOPIC_URL)); //Attempt data parsing topicTask.execute(topicPageUrl); //Attempt data parsing
} }
@Override @Override
@ -413,6 +417,7 @@ public class TopicActivity extends BaseActivity {
} }
} }
@SuppressLint("ClickableViewAccessibility")
private void initIncrementButton(ImageButton increment, final int step) { private void initIncrementButton(ImageButton increment, final int step) {
// Increment once for a click // Increment once for a click
increment.setOnClickListener(new View.OnClickListener() { increment.setOnClickListener(new View.OnClickListener() {
@ -461,6 +466,7 @@ public class TopicActivity extends BaseActivity {
}); });
} }
@SuppressLint("ClickableViewAccessibility")
private void initDecrementButton(ImageButton decrement, final int step) { private void initDecrementButton(ImageButton decrement, final int step) {
// Decrement once for a click // Decrement once for a click
decrement.setOnClickListener(new View.OnClickListener() { decrement.setOnClickListener(new View.OnClickListener() {
@ -537,6 +543,10 @@ public class TopicActivity extends BaseActivity {
} }
} }
//------------------------------------BOTTOM NAV BAR METHODS END------------------------------------ //------------------------------------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 * An {@link AsyncTask} that handles asynchronous fetching of this topic page and parsing of its
@ -544,12 +554,7 @@ public class TopicActivity extends BaseActivity {
* <p>TopicTask's {@link AsyncTask#execute execute} method needs a topic's url as String * <p>TopicTask's {@link AsyncTask#execute execute} method needs a topic's url as String
* parameter.</p> * parameter.</p>
*/ */
class TopicTask extends AsyncTask<String, Void, Integer> { class TopicTask extends AsyncTask<String, Void, ResultCode> {
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;
ArrayList<Post> localPostsList; ArrayList<Post> localPostsList;
@Override @Override
@ -559,8 +564,8 @@ public class TopicActivity extends BaseActivity {
if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(false); if (replyFAB.getVisibility() != View.GONE) replyFAB.setEnabled(false);
} }
protected Integer doInBackground(String... strings) { protected ResultCode doInBackground(String... strings) {
Document document; Document document = null;
String newPageUrl = strings[0]; String newPageUrl = strings[0];
//Finds the index of message focus if present //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 (!reloadingPage && !Objects.equals(loadedPageUrl, "") && newPageUrl.contains(base_url)) {
if (newPageUrl.contains("topicseen#new") || newPageUrl.contains("#new")) if (newPageUrl.contains("topicseen#new") || newPageUrl.contains("#new"))
if (thisPage == numberOfPages) if (thisPage == numberOfPages)
return SAME_PAGE; return ResultCode.SAME_PAGE;
if (newPageUrl.contains("msg")) { if (newPageUrl.contains("msg")) {
String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3); String tmpUrlSbstr = newPageUrl.substring(newPageUrl.indexOf("msg") + 3);
if (tmpUrlSbstr.contains("msg")) if (tmpUrlSbstr.contains("msg"))
@ -586,12 +591,12 @@ public class TopicActivity extends BaseActivity {
int testAgainst = Integer.parseInt(tmpUrlSbstr); int testAgainst = Integer.parseInt(tmpUrlSbstr);
for (Post post : postsList) { for (Post post : postsList) {
if (post.getPostIndex() == testAgainst) { if (post.getPostIndex() == testAgainst) {
return SAME_PAGE; return ResultCode.SAME_PAGE;
} }
} }
} else if ((Objects.equals(newPageUrl, base_url) && thisPage == 1) || } else if ((Objects.equals(newPageUrl, base_url) && thisPage == 1) ||
Integer.parseInt(newPageUrl.substring(base_url.length() + 1)) / 15 + 1 == thisPage) 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; } else if (!Objects.equals(loadedPageUrl, "")) topicTitle = null;
if (reloadingPage) reloadingPage = !reloadingPage; if (reloadingPage) reloadingPage = !reloadingPage;
@ -614,17 +619,22 @@ public class TopicActivity extends BaseActivity {
break; break;
} }
} }
return SUCCESS; return ResultCode.SUCCESS;
} catch (IOException e) { } catch (IOException e) {
Timber.i(e, "IO Exception"); 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) { } catch (Exception e) {
Timber.e(e, "Exception"); Timber.e(e, "Exception");
return OTHER_ERROR; return ResultCode.OTHER_ERROR;
} }
} }
protected void onPostExecute(Integer parseResult) { protected void onPostExecute(ResultCode parseResult) {
switch (parseResult) { switch (parseResult) {
case SUCCESS: case SUCCESS:
if (topicTitle == null || Objects.equals(topicTitle, "") if (topicTitle == null || Objects.equals(topicTitle, "")
@ -635,7 +645,7 @@ public class TopicActivity extends BaseActivity {
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
if (!(postsList.isEmpty() || postsList.size() == 0)) { if (!postsList.isEmpty()) {
recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug recyclerView.getRecycledViewPool().clear(); //Avoid inconsistency detected bug
postsList.clear(); postsList.clear();
topicAdapter.notifyItemRangeRemoved(0, postsList.size() - 1); topicAdapter.notifyItemRangeRemoved(0, postsList.size() - 1);
@ -661,16 +671,14 @@ public class TopicActivity extends BaseActivity {
Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show(); Toast.makeText(getBaseContext(), "Network Error", Toast.LENGTH_SHORT).show();
break; break;
case SAME_PAGE: case SAME_PAGE:
progressBar.setVisibility(ProgressBar.INVISIBLE); stopLoading();
if (replyPageUrl == null) { Toast.makeText(getBaseContext(), "That's the same page", Toast.LENGTH_SHORT).show();
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();
//TODO change focus //TODO change focus
break; break;
case UNAUTHORIZED:
stopLoading();
Toast.makeText(getBaseContext(), "This topic is either missing or off limits to you", Toast.LENGTH_SHORT).show();
break;
default: default:
//Parse failed - should never happen //Parse failed - should never happen
Timber.d("Parse failed!"); //TODO report ParseException!!! Timber.d("Parse failed!"); //TODO report ParseException!!!
@ -680,13 +688,24 @@ 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. * All the parsing a topic needs.
* *
* @param topic {@link Document} object containing this topic's source code * @param topic {@link Document} object containing this topic's source code
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
private ArrayList<Post> parse(Document topic) { private ArrayList<Post> parse(Document topic) throws ParseException{
try {
ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic); ParseHelpers.Language language = ParseHelpers.Language.getLanguage(topic);
//Finds topic's tree, mods and users viewing //Finds topic's tree, mods and users viewing
@ -729,6 +748,16 @@ public class TopicActivity extends BaseActivity {
} }
return TopicParser.parseTopic(topic, language); return TopicParser.parseTopic(topic, language);
} catch (Exception e) {
throw new ParseException("Parsing failed (TopicTask)");
}
}
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) { private void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span) {
@ -881,7 +910,7 @@ public class TopicActivity extends BaseActivity {
//TODO this... //TODO this...
return true; return true;
default: default:
Timber.e("Malformed post. Request string:\n" + post.toString()); Timber.e("Malformed post. Request string: %s", post.toString());
return true; return true;
} }
} catch (IOException e) { } catch (IOException e) {

26
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.Post;
import gr.thmmy.mthmmy.model.ThmmyFile; import gr.thmmy.mthmmy.model.ThmmyFile;
import gr.thmmy.mthmmy.utils.ParseHelpers; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers;
import timber.log.Timber; import timber.log.Timber;
/** /**
* Singleton used for parsing a topic. * Singleton used for parsing a topic.
* <p>Class contains the methods:<ul><li>{@link #parseUsersViewingThisTopic(Document, * <p>Class contains the methods:<ul><li>{@link #parseUsersViewingThisTopic(Document,
* gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li> * ParseHelpers.Language)}</li>
* <li>{@link #parseCurrentPageIndex(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li> * <li>{@link #parseCurrentPageIndex(Document, ParseHelpers.Language)}</li>
* <li>{@link #parseTopicNumberOfPages(Document, int, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li> * <li>{@link #parseTopicNumberOfPages(Document, int, ParseHelpers.Language)}</li>
* <li>{@link #parseTopic(Document, gr.thmmy.mthmmy.utils.ParseHelpers.Language)}</li> * <li>{@link #parseTopic(Document, ParseHelpers.Language)}</li>
*/ */
class TopicParser { class TopicParser {
//User colors //User colors
@ -42,9 +42,9 @@ class TopicParser {
* Returns users currently viewing this topic. * Returns users currently viewing this topic.
* *
* @param topic {@link Document} object containing this topic's source code * @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 * 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 * @return String containing html with the usernames of users
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
@ -58,9 +58,9 @@ class TopicParser {
* Returns current topic's page index. * Returns current topic's page index.
* *
* @param topic {@link Document} object containing this topic's source code * @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 * 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 * @return int containing parsed topic's current page
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
@ -96,9 +96,9 @@ class TopicParser {
* *
* @param topic {@link Document} object containing this topic's source code * @param topic {@link Document} object containing this topic's source code
* @param currentPage an int containing current page of this topic * @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 * 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 * @return int containing the number of pages
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */
@ -134,9 +134,9 @@ class TopicParser {
* This method parses all the information of a topic and it's posts. * 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 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 * 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 * @return {@link ArrayList} of {@link Post}s
* @see org.jsoup.Jsoup Jsoup * @see org.jsoup.Jsoup Jsoup
*/ */

6
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.activities.profile.ProfileActivity;
import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.model.Bookmark;
import gr.thmmy.mthmmy.model.ThmmyFile; 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 gr.thmmy.mthmmy.session.SessionManager;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
@ -611,7 +611,7 @@ public abstract class BaseActivity extends AppCompatActivity {
public void launchDownloadService(ThmmyFile thmmyFile) { public void launchDownloadService(ThmmyFile thmmyFile) {
if (checkPerms()) if (checkPerms())
DownloadService.startActionDownload(this, thmmyFile.getFileUrl().toString()); DownloadsService.startActionDownload(this, thmmyFile.getFileUrl().toString());
else { else {
tempThmmyFile = thmmyFile; tempThmmyFile = thmmyFile;
requestPerms(); requestPerms();
@ -621,7 +621,7 @@ public abstract class BaseActivity extends AppCompatActivity {
//Uses temp file - called after permission grant //Uses temp file - called after permission grant
private void launchDownloadService() { private void launchDownloadService() {
if (checkPerms()) if (checkPerms())
DownloadService.startActionDownload(this, tempThmmyFile.getFileUrl().toString()); DownloadsService.startActionDownload(this, tempThmmyFile.getFileUrl().toString());
} }

16
app/src/main/java/gr/thmmy/mthmmy/model/ThmmyPage.java

@ -3,6 +3,8 @@ package gr.thmmy.mthmmy.model;
import android.net.Uri; import android.net.Uri;
import java.util.Objects; import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import timber.log.Timber; import timber.log.Timber;
@ -187,4 +189,18 @@ public class ThmmyPage {
} }
return null; 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;
}
} }

28
app/src/main/java/gr/thmmy/mthmmy/receiver/Receiver.java → 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.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
@ -16,23 +16,23 @@ import java.io.File;
import timber.log.Timber; import timber.log.Timber;
import static gr.thmmy.mthmmy.services.DownloadService.ACTION_DOWNLOAD; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.ACTION_DOWNLOAD;
import static gr.thmmy.mthmmy.services.DownloadService.COMPLETED; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.COMPLETED;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_ID; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_DOWNLOAD_ID;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_DOWNLOAD_STATE; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_DOWNLOAD_STATE;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_FILE_NAME; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_FILE_NAME;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TEXT; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_NOTIFICATION_TEXT;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TICKER; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_NOTIFICATION_TICKER;
import static gr.thmmy.mthmmy.services.DownloadService.EXTRA_NOTIFICATION_TITLE; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.EXTRA_NOTIFICATION_TITLE;
import static gr.thmmy.mthmmy.services.DownloadService.SAVE_DIR; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.SAVE_DIR;
import static gr.thmmy.mthmmy.services.DownloadService.STARTED; import static gr.thmmy.mthmmy.services.downloads.DownloadsService.STARTED;
public class Receiver extends BroadcastReceiver { public class DownloadsReceiver extends BroadcastReceiver {
private static final String NOTIFICATION_TAG = "DOWNLOADS"; private static final String NOTIFICATION_TAG = "DOWNLOADS";
private static final String DOWNLOADS_CHANNEL_ID = "Downloads"; private static final String DOWNLOADS_CHANNEL_ID = "Downloads";
private static final String DOWNLOADS_CHANNEL_NAME = "Downloads"; private static final String DOWNLOADS_CHANNEL_NAME = "Downloads";
public Receiver() {} public DownloadsReceiver() {}
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {

19
app/src/main/java/gr/thmmy/mthmmy/services/DownloadService.java → 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.DownloadManager;
import android.app.IntentService; import android.app.IntentService;
@ -14,7 +14,6 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.receiver.Receiver;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
@ -26,11 +25,11 @@ import timber.log.Timber;
* An {@link IntentService} subclass for handling asynchronous task requests in * An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread. * a service on a separate handler thread.
*/ */
public class DownloadService extends IntentService { public class DownloadsService extends IntentService {
private static final String TAG = "DownloadService"; private static final String TAG = "DownloadsService";
private static int sDownloadId = 0; private static int sDownloadId = 0;
private Receiver receiver; private DownloadsReceiver receiver;
public static final String SAVE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "mthmmy"; 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 static final String FAILED = "Failed";
public DownloadService() { public DownloadsService() {
super("DownloadService"); super("DownloadsService");
} }
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
final IntentFilter filter = new IntentFilter(DownloadService.ACTION_DOWNLOAD); final IntentFilter filter = new IntentFilter(DownloadsService.ACTION_DOWNLOAD);
receiver = new Receiver(); receiver = new DownloadsReceiver();
registerReceiver(receiver, filter); registerReceiver(receiver, filter);
} }
@ -75,7 +74,7 @@ public class DownloadService extends IntentService {
* @see IntentService * @see IntentService
*/ */
public static void startActionDownload(Context context, String downloadUrl) { 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.setAction(ACTION_DOWNLOAD);
intent.putExtra(EXTRA_DOWNLOAD_URL, downloadUrl); intent.putExtra(EXTRA_DOWNLOAD_URL, downloadUrl);
context.startService(intent); context.startService(intent);

2
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.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import gr.thmmy.mthmmy.utils.exceptions.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseException;
import okhttp3.Cookie; import okhttp3.Cookie;
import okhttp3.FormBody; import okhttp3.FormBody;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;

2
app/src/main/java/gr/thmmy/mthmmy/utils/exceptions/ParseException.java → 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. * ParseException is to be used for errors while parsing.

2
app/src/main/java/gr/thmmy/mthmmy/utils/ParseHelpers.java → 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.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;

3
app/src/main/java/gr/thmmy/mthmmy/utils/ParseTask.java → 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.os.AsyncTask;
import android.widget.Toast; import android.widget.Toast;
@ -9,7 +9,6 @@ import org.jsoup.nodes.Document;
import java.io.IOException; import java.io.IOException;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.utils.exceptions.ParseException;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import timber.log.Timber; import timber.log.Timber;
Loading…
Cancel
Save