|
|
|
@ -21,6 +21,7 @@ public class AdBlock {
@@ -21,6 +21,7 @@ public class AdBlock {
|
|
|
|
|
private static final String TAG = "AdBlock"; |
|
|
|
|
private static final String BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt"; |
|
|
|
|
private static final String LOCAL_IP_V4 = "127.0.0.1"; |
|
|
|
|
private static final String LOCAL_IP_V4_ALT = "0.0.0.0"; |
|
|
|
|
private static final String LOCAL_IP_V6 = "::1"; |
|
|
|
|
private static final String LOCALHOST = "localhost"; |
|
|
|
|
private static final String COMMENT = "#"; |
|
|
|
@ -72,6 +73,12 @@ public class AdBlock {
@@ -72,6 +73,12 @@ public class AdBlock {
|
|
|
|
|
thread.start(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* a method that determines if the given URL is an ad or not. It performs |
|
|
|
|
* a search of the URL's domain on the blocked domain hash set. |
|
|
|
|
* @param url the URL to check for being an ad |
|
|
|
|
* @return true if it is an ad, false if it is not an ad |
|
|
|
|
*/ |
|
|
|
|
public boolean isAd(String url) { |
|
|
|
|
if (!mBlockAds || url == null) { |
|
|
|
|
return false; |
|
|
|
@ -92,6 +99,12 @@ public class AdBlock {
@@ -92,6 +99,12 @@ public class AdBlock {
|
|
|
|
|
return isOnBlacklist; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the probable domain name for a given URL |
|
|
|
|
* @param url the url to parse |
|
|
|
|
* @return returns the domain |
|
|
|
|
* @throws URISyntaxException throws an exception if the string cannot form a URI |
|
|
|
|
*/ |
|
|
|
|
private static String getDomainName(String url) throws URISyntaxException { |
|
|
|
|
int index = url.indexOf('/', 8); |
|
|
|
|
if (index != -1) { |
|
|
|
@ -107,6 +120,14 @@ public class AdBlock {
@@ -107,6 +120,14 @@ public class AdBlock {
|
|
|
|
|
return domain.startsWith("www.") ? domain.substring(4) : domain; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* This method reads through a hosts file and extracts the domains that should |
|
|
|
|
* be redirected to localhost (a.k.a. IP address 127.0.0.1). It can handle files that |
|
|
|
|
* simply have a list of hostnames to block, or it can handle a full blown hosts file. |
|
|
|
|
* It will strip out comments, references to the base IP address and just extract the |
|
|
|
|
* domains to be used |
|
|
|
|
* @param context the context needed to read the file |
|
|
|
|
*/ |
|
|
|
|
private void loadHostsFile(final Context context) { |
|
|
|
|
Thread thread = new Thread(new Runnable() { |
|
|
|
|
|
|
|
|
@ -120,7 +141,10 @@ public class AdBlock {
@@ -120,7 +141,10 @@ public class AdBlock {
|
|
|
|
|
String line; |
|
|
|
|
while ((line = reader.readLine()) != null) { |
|
|
|
|
if (!line.isEmpty() && !line.startsWith(COMMENT)) { |
|
|
|
|
line = line.replace(LOCAL_IP_V4, EMPTY).replace(LOCAL_IP_V6, EMPTY).replace(TAB, EMPTY); |
|
|
|
|
line = line.replace(LOCAL_IP_V4, EMPTY) |
|
|
|
|
.replace(LOCAL_IP_V4_ALT, EMPTY) |
|
|
|
|
.replace(LOCAL_IP_V6, EMPTY) |
|
|
|
|
.replace(TAB, EMPTY); |
|
|
|
|
int comment = line.indexOf(COMMENT); |
|
|
|
|
if (comment >= 0) { |
|
|
|
|
line = line.substring(0, comment); |
|
|
|
@ -128,9 +152,10 @@ public class AdBlock {
@@ -128,9 +152,10 @@ public class AdBlock {
|
|
|
|
|
line = line.trim(); |
|
|
|
|
if (!line.isEmpty() && !line.equals(LOCALHOST)) { |
|
|
|
|
while (line.contains(SPACE)) { |
|
|
|
|
String host = line.substring(0, line.indexOf(SPACE)); |
|
|
|
|
int space = line.indexOf(SPACE); |
|
|
|
|
String host = line.substring(0, space); |
|
|
|
|
mBlockedDomainsList.add(host.trim()); |
|
|
|
|
line = line.substring(line.indexOf(SPACE), line.length()).trim(); |
|
|
|
|
line = line.substring(space, line.length()).trim(); |
|
|
|
|
} |
|
|
|
|
mBlockedDomainsList.add(line.trim()); |
|
|
|
|
} |
|
|
|
|