You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.5 KiB
100 lines
2.5 KiB
10 years ago
|
package acr.browser.lightning;
|
||
|
|
||
10 years ago
|
import android.content.Context;
|
||
|
import android.content.res.AssetManager;
|
||
10 years ago
|
import android.util.Log;
|
||
10 years ago
|
|
||
10 years ago
|
import java.io.BufferedReader;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStreamReader;
|
||
|
import java.net.URI;
|
||
|
import java.net.URISyntaxException;
|
||
10 years ago
|
import java.util.HashSet;
|
||
10 years ago
|
import java.util.Locale;
|
||
10 years ago
|
import java.util.Set;
|
||
10 years ago
|
|
||
|
public class AdBlock {
|
||
|
|
||
10 years ago
|
private static final String TAG = "AdBlock";
|
||
10 years ago
|
private static final String BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt";
|
||
|
private static final Set<String> mBlockedDomainsList = new HashSet<String>();
|
||
10 years ago
|
private boolean mBlockAds;
|
||
10 years ago
|
private static final Locale mLocale = Locale.getDefault();
|
||
9 years ago
|
private static AdBlock mInstance;
|
||
10 years ago
|
|
||
9 years ago
|
public static AdBlock getInstance(Context context) {
|
||
|
if (mInstance == null) {
|
||
|
mInstance = new AdBlock(context);
|
||
|
}
|
||
|
return mInstance;
|
||
|
}
|
||
|
|
||
|
private AdBlock(Context context) {
|
||
9 years ago
|
if (mBlockedDomainsList.isEmpty() && Constants.FULL_VERSION) {
|
||
10 years ago
|
loadBlockedDomainsList(context);
|
||
10 years ago
|
}
|
||
9 years ago
|
mBlockAds = PreferenceManager.getInstance().getAdBlockEnabled();
|
||
10 years ago
|
}
|
||
|
|
||
|
public void updatePreference() {
|
||
9 years ago
|
mBlockAds = PreferenceManager.getInstance().getAdBlockEnabled();
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
private void loadBlockedDomainsList(final Context context) {
|
||
10 years ago
|
Thread thread = new Thread(new Runnable() {
|
||
|
|
||
|
@Override
|
||
|
public void run() {
|
||
|
AssetManager asset = context.getAssets();
|
||
|
try {
|
||
10 years ago
|
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||
|
asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME)));
|
||
10 years ago
|
String line;
|
||
|
while ((line = reader.readLine()) != null) {
|
||
10 years ago
|
mBlockedDomainsList.add(line.trim().toLowerCase(mLocale));
|
||
10 years ago
|
}
|
||
|
} catch (IOException e) {
|
||
10 years ago
|
Log.wtf(TAG, "Reading blocked domains list from file '"
|
||
|
+ BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
});
|
||
|
thread.start();
|
||
|
}
|
||
|
|
||
|
public boolean isAd(String url) {
|
||
9 years ago
|
if (!mBlockAds || url == null) {
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
String domain;
|
||
10 years ago
|
try {
|
||
|
domain = getDomainName(url);
|
||
|
} catch (URISyntaxException e) {
|
||
9 years ago
|
Log.d(TAG, "URL '" + url + "' is invalid", e);
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
boolean isOnBlacklist = mBlockedDomainsList.contains(domain.toLowerCase(mLocale));
|
||
10 years ago
|
if (isOnBlacklist) {
|
||
|
Log.d(TAG, "URL '" + url + "' is an ad");
|
||
|
}
|
||
|
return isOnBlacklist;
|
||
10 years ago
|
}
|
||
|
|
||
|
private static String getDomainName(String url) throws URISyntaxException {
|
||
10 years ago
|
int index = url.indexOf('/', 8);
|
||
10 years ago
|
if (index != -1) {
|
||
|
url = url.substring(0, index);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
URI uri = new URI(url);
|
||
|
String domain = uri.getHost();
|
||
|
if (domain == null) {
|
||
|
return url;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
return domain.startsWith("www.") ? domain.substring(4) : domain;
|
||
|
}
|
||
|
}
|