Skip to content
This repository has been archived by the owner on Dec 2, 2019. It is now read-only.

Commit

Permalink
Merge pull request #18 from theGnartist/master
Browse files Browse the repository at this point in the history
Cancelable AsyncTask
  • Loading branch information
LeonardoCardoso committed Jul 31, 2017
2 parents 7cd1ee6 + e934918 commit cf18c9c
Showing 1 changed file with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,29 @@ public class TextCrawler {

private LinkPreviewCallback callback;

private AsyncTask getCodeTask;

public TextCrawler() {
}

public void makePreview(LinkPreviewCallback callback, String url) {
this.callback = callback;
new GetCode(ALL).execute(url);
makePreview(callback, url, ALL);
}

public void makePreview(LinkPreviewCallback callback, String url,
int imageQuantity) {
this.callback = callback;
new GetCode(imageQuantity).execute(url);
cancel();
getCodeTask = new GetCode(imageQuantity).execute(url);
}

public void cancel(){
if(getCodeTask != null){
getCodeTask.cancel(true);
}
}


/** Get html code */
public class GetCode extends AsyncTask<String, Void, Void> {

Expand Down Expand Up @@ -66,6 +75,11 @@ protected void onPostExecute(Void result) {
super.onPostExecute(result);
}

@Override
protected void onCancelled() {
super.onCancelled();
}

@Override
protected Void doInBackground(String... params) {
// Don't forget the http:// or https://
Expand Down Expand Up @@ -169,6 +183,9 @@ private String getTagContent(String tag, String content) {

int matchesSize = matches.size();
for (int i = 0; i < matchesSize; i++) {
if(getCodeTask.isCancelled()){
break;
}
currentMatch = stripTags(matches.get(i));
if (currentMatch.length() >= 120) {
result = extendedTrim(currentMatch);
Expand All @@ -193,6 +210,9 @@ public List<String> getImages(Document document, int imageQuantity) {
Elements media = document.select("[src]");

for (Element srcElement : media) {
if(getCodeTask.isCancelled()){
break;
}
if (srcElement.tagName().equals("img")) {
matches.add(srcElement.attr("abs:src"));
}
Expand Down Expand Up @@ -246,6 +266,9 @@ private String cannonicalPage(String url) {

int urlLength = url.length();
for (int i = 0; i < urlLength; i++) {
if(getCodeTask.isCancelled()){
break;
}
if (url.charAt(i) != '/')
cannonical += url.charAt(i);
else
Expand Down Expand Up @@ -281,6 +304,9 @@ private HashMap<String, String> getMetaTags(String content) {
Regex.METATAG_PATTERN, 1);

for (String match : matches) {
if(getCodeTask.isCancelled()){
break;
}
final String lowerCase = match.toLowerCase();
if (lowerCase.contains("property=\"og:url\"")
|| lowerCase.contains("property='og:url'")
Expand Down

0 comments on commit cf18c9c

Please sign in to comment.