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

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

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

Loading…
Cancel
Save