Fix AdBlock collection type and other problems
This commit is contained in:
parent
d128000c73
commit
09f23553a4
@ -6,25 +6,25 @@ import android.content.res.AssetManager;
|
|||||||
|
|
||||||
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 BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt";
|
||||||
new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
|
|
||||||
|
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 +37,23 @@ 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();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
@ -66,6 +62,7 @@ public class AdBlock {
|
|||||||
if (!mBlockAds) {
|
if (!mBlockAds) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
String domain;
|
String domain;
|
||||||
try {
|
try {
|
||||||
domain = getDomainName(url);
|
domain = getDomainName(url);
|
||||||
@ -73,7 +70,8 @@ public class AdBlock {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return mAdBlockMap.containsKey(domain);
|
|
||||||
|
return mBlockedDomainsList.contains(domain.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getDomainName(String url) throws URISyntaxException {
|
private static String getDomainName(String url) throws URISyntaxException {
|
||||||
@ -81,11 +79,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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user