package acr.browser.lightning;

import android.content.Context;
import android.content.SharedPreferences;
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;

public class AdBlock {

	private static SortedMap<String, Integer> mAdBlockMap =
			new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);

	private SharedPreferences mPreferences;

	private boolean mBlockAds;

	public AdBlock(Context context) {
		if (mAdBlockMap.isEmpty()) {
			fillSearchTree(context);
		}
		mPreferences = context.getSharedPreferences(
				PreferenceConstants.PREFERENCES, 0);
		mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS,
				false);
	}

	public void updatePreference() {
		mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS,
				false);
	}

	public void fillSearchTree(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();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}

			}

		});
		thread.start();
	}

	public boolean isAd(String url) {
		if (!mBlockAds) {
			return false;
		}
		String domain;
		try {
			domain = getDomainName(url);
		} catch (URISyntaxException e) {
			e.printStackTrace();
			return false;
		}
		return mAdBlockMap.containsKey(domain);
	}

	private static String getDomainName(String url) throws URISyntaxException {
		int index = url.indexOf('/', 8);
		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;
	}
}

@@ -204,36 +204,8 @@
 on;%0A
-import java.io.InputStream;%0A
 impo
@@ -308,25 +308,23 @@
 va.util.
-SortedMap
+HashSet
 ;%0Aimport
@@ -334,23 +334,19 @@
 va.util.
-TreeMap
+Set
 ;%0A%0Apubli
@@ -384,110 +384,144 @@
 tic 
-SortedMap%3CString, Integer
+final String BLOCKED_DOMAINS_LIST_FILE_NAME = %22hosts.txt%22;%0A%0A%09private static final Set%3CString
 %3E m
-Ad
 Block
-Map =%0A%09%09%09new TreeMap%3CString, Integer%3E(String.CASE_INSENSITIVE_ORDER
+edDomainsList = new HashSet%3CString%3E(
 );%0A%0A
@@ -633,26 +633,34 @@
 %0A%09%09if (m
-Ad
 Block
-Map
+edDomainsList
 .isEmpty
@@ -668,30 +668,38 @@
 )) %7B%0A%09%09%09
-fillSearchTree
+loadBlockedDomainsList
 (context
@@ -1003,33 +1003,42 @@
 %0A%0A%09p
-ublic void fillSearchTree
+rivate void loadBlockedDomainsList
 (fin
@@ -1207,69 +1207,16 @@
 %09%09%09%09
-InputStream input = asset.open(%22hosts.txt%22);%0A%09%09%09%09%09InputStream
+Buffered
 Read
@@ -1226,108 +1226,139 @@
 read
+er
  = new 
-InputStreamReader(input);%0A%09%09%09%09%09BufferedReader reader = new BufferedReader(read);%0A%09%09%09%09%09String 
+BufferedReader(%0A%09%09%09%09%09%09%09new InputStreamReader(asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME)));%0A%09%09%09%09%09String line;%0A%09%09%09%09%09while ((
 line
@@ -1381,26 +1381,9 @@
 ne()
-;%0A%09%09%09%09%09while (line
+)
  != 
@@ -1401,63 +1401,57 @@
 %09%09%09m
-Ad
 Block
-Map.put(line, 1);%0A%09%09%09%09%09%09line = reader.readLin
+edDomainsList.add(line.trim().toLowerCas
 e()
+)
 ;%0A%09%09
@@ -1521,15 +1521,13 @@
 %09%09%7D%0A
-%0A
 %09%09%09%7D%0A
-%0A
 %09%09%7D)
@@ -1626,16 +1626,17 @@
 se;%0A%09%09%7D%0A
+%0A
 %09%09String
@@ -1760,24 +1760,25 @@
  false;%0A%09%09%7D%0A
+%0A
 %09%09return mAd
@@ -1779,18 +1779,26 @@
 rn m
-Ad
 Block
-Map
+edDomainsList
 .con
@@ -1806,18 +1806,29 @@
 ains
-Key
 (domain
+.toLowerCase()
 );%0A%09
@@ -2001,16 +2001,17 @@
 x);%0A%09%09%7D%0A
+%0A
 %09%09URI ur
@@ -2104,16 +2104,17 @@
 rl;%0A%09%09%7D%0A
+%0A
 %09%09return

Fix AdBlock collection type and other problems