Browse Source

Merge pull request #111 from kuc/fix-adblock

Fix AdBlock
master
Anthony Restaino 11 years ago
parent
commit
4069adaa06
  1. 48
      src/acr/browser/lightning/AdBlock.java
  2. 2
      src/acr/browser/lightning/LightningView.java

48
src/acr/browser/lightning/AdBlock.java

@ -3,28 +3,31 @@ package acr.browser.lightning; @@ -3,28 +3,31 @@ package acr.browser.lightning;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.HashSet;
import java.util.Set;
public class AdBlock {
private static SortedMap<String, Integer> mAdBlockMap =
new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
private static final String TAG = "AdBlock";
private static final String BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt";
private static final Set<String> mBlockedDomainsList = new HashSet<String>();
private SharedPreferences mPreferences;
private boolean mBlockAds;
public AdBlock(Context context) {
if (mAdBlockMap.isEmpty()) {
fillSearchTree(context);
if (mBlockedDomainsList.isEmpty()) {
loadBlockedDomainsList(context);
}
mPreferences = context.getSharedPreferences(
PreferenceConstants.PREFERENCES, 0);
@ -37,27 +40,24 @@ public class AdBlock { @@ -37,27 +40,24 @@ public class AdBlock {
false);
}
public void fillSearchTree(final Context context) {
private void loadBlockedDomainsList(final Context context) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
AssetManager asset = context.getAssets();
try {
InputStream input = asset.open("hosts.txt");
InputStreamReader read = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(read);
String line = reader.readLine();
while (line != null) {
mAdBlockMap.put(line, 1);
line = reader.readLine();
BufferedReader reader = new BufferedReader(
new InputStreamReader(asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME)));
String line;
while ((line = reader.readLine()) != null) {
mBlockedDomainsList.add(line.trim().toLowerCase());
}
} catch (IOException e) {
e.printStackTrace();
Log.wtf(TAG, "Reading blocked domains list from file '" +
BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e);
}
}
});
thread.start();
}
@ -66,14 +66,20 @@ public class AdBlock { @@ -66,14 +66,20 @@ public class AdBlock {
if (!mBlockAds) {
return false;
}
String domain;
try {
domain = getDomainName(url);
} catch (URISyntaxException e) {
e.printStackTrace();
Log.e(TAG, "URL '" + url + "' is invalid", e);
return false;
}
return mAdBlockMap.containsKey(domain);
boolean isOnBlacklist = mBlockedDomainsList.contains(domain.toLowerCase());
if (isOnBlacklist) {
Log.d(TAG, "URL '" + url + "' is an ad");
}
return isOnBlacklist;
}
private static String getDomainName(String url) throws URISyntaxException {
@ -81,11 +87,13 @@ public class AdBlock { @@ -81,11 +87,13 @@ public class AdBlock {
if (index != -1) {
url = url.substring(0, index);
}
URI uri = new URI(url);
String domain = uri.getHost();
if (domain == null) {
return url;
}
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
}

2
src/acr/browser/lightning/LightningView.java

@ -61,7 +61,7 @@ public class LightningView { @@ -61,7 +61,7 @@ public class LightningView {
private static boolean mWideViewPort;
private static AdBlock mAdBlock;
private AdBlock mAdBlock;
private CookieManager mCookieManager;

Loading…
Cancel
Save