From 09f23553a4db1dc8bd56fef806d799a1d86081dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Mon, 21 Jul 2014 18:22:12 +0200 Subject: [PATCH 1/3] Fix AdBlock collection type and other problems --- src/acr/browser/lightning/AdBlock.java | 36 +++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/acr/browser/lightning/AdBlock.java b/src/acr/browser/lightning/AdBlock.java index fd930ea..0af0a4e 100644 --- a/src/acr/browser/lightning/AdBlock.java +++ b/src/acr/browser/lightning/AdBlock.java @@ -6,25 +6,25 @@ import android.content.res.AssetManager; 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 mAdBlockMap = - new TreeMap(String.CASE_INSENSITIVE_ORDER); + private static final String BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt"; + + private static final Set mBlockedDomainsList = new HashSet(); 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 +37,23 @@ 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(); } - } - }); thread.start(); } @@ -66,6 +62,7 @@ public class AdBlock { if (!mBlockAds) { return false; } + String domain; try { domain = getDomainName(url); @@ -73,7 +70,8 @@ public class AdBlock { e.printStackTrace(); return false; } - return mAdBlockMap.containsKey(domain); + + return mBlockedDomainsList.contains(domain.toLowerCase()); } private static String getDomainName(String url) throws URISyntaxException { @@ -81,11 +79,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; } } From 9c1b0c703e7878e471920f7da5aac1d5cf1d9340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Mon, 21 Jul 2014 18:27:09 +0200 Subject: [PATCH 2/3] Fix overwriting AdBlock instance by every new created LightningView object --- src/acr/browser/lightning/LightningView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/acr/browser/lightning/LightningView.java b/src/acr/browser/lightning/LightningView.java index 19a6b82..f26ac33 100644 --- a/src/acr/browser/lightning/LightningView.java +++ b/src/acr/browser/lightning/LightningView.java @@ -61,7 +61,7 @@ public class LightningView { private static boolean mWideViewPort; - private static AdBlock mAdBlock; + private AdBlock mAdBlock; private CookieManager mCookieManager; From d85019ec7220e8562b18fd34c733aa959c03b3a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mi=C5=82osz=20Sieradzki?= Date: Mon, 21 Jul 2014 18:55:22 +0200 Subject: [PATCH 3/3] Add logging --- src/acr/browser/lightning/AdBlock.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/acr/browser/lightning/AdBlock.java b/src/acr/browser/lightning/AdBlock.java index 0af0a4e..7c29944 100644 --- a/src/acr/browser/lightning/AdBlock.java +++ b/src/acr/browser/lightning/AdBlock.java @@ -3,6 +3,7 @@ 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; @@ -14,6 +15,8 @@ import java.util.Set; public class AdBlock { + private static final String TAG = "AdBlock"; + private static final String BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt"; private static final Set mBlockedDomainsList = new HashSet(); @@ -51,7 +54,8 @@ public class AdBlock { 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); } } }); @@ -67,11 +71,15 @@ public class AdBlock { try { domain = getDomainName(url); } catch (URISyntaxException e) { - e.printStackTrace(); + Log.e(TAG, "URL '" + url + "' is invalid", e); return false; } - return mBlockedDomainsList.contains(domain.toLowerCase()); + 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 {