2014-04-28 19:41:40 +00:00
|
|
|
package acr.browser.lightning;
|
|
|
|
|
2014-07-19 14:49:33 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.res.AssetManager;
|
|
|
|
|
2014-04-28 19:41:40 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
2014-07-21 16:22:12 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2014-04-28 19:41:40 +00:00
|
|
|
|
|
|
|
public class AdBlock {
|
|
|
|
|
2014-07-21 16:22:12 +00:00
|
|
|
private static final String BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt";
|
|
|
|
|
|
|
|
private static final Set<String> mBlockedDomainsList = new HashSet<String>();
|
2014-07-19 14:49:33 +00:00
|
|
|
|
2014-04-28 19:41:40 +00:00
|
|
|
private SharedPreferences mPreferences;
|
2014-07-19 14:49:33 +00:00
|
|
|
|
2014-07-19 15:39:51 +00:00
|
|
|
private boolean mBlockAds;
|
2014-04-28 19:41:40 +00:00
|
|
|
|
|
|
|
public AdBlock(Context context) {
|
2014-07-21 16:22:12 +00:00
|
|
|
if (mBlockedDomainsList.isEmpty()) {
|
|
|
|
loadBlockedDomainsList(context);
|
2014-04-28 19:41:40 +00:00
|
|
|
}
|
|
|
|
mPreferences = context.getSharedPreferences(
|
|
|
|
PreferenceConstants.PREFERENCES, 0);
|
|
|
|
mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updatePreference() {
|
|
|
|
mBlockAds = mPreferences.getBoolean(PreferenceConstants.BLOCK_ADS,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
2014-07-21 16:22:12 +00:00
|
|
|
private void loadBlockedDomainsList(final Context context) {
|
2014-04-28 19:41:40 +00:00
|
|
|
Thread thread = new Thread(new Runnable() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
AssetManager asset = context.getAssets();
|
|
|
|
try {
|
2014-07-21 16:22:12 +00:00
|
|
|
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());
|
2014-04-28 19:41:40 +00:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAd(String url) {
|
|
|
|
if (!mBlockAds) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-21 16:22:12 +00:00
|
|
|
|
2014-07-19 15:39:51 +00:00
|
|
|
String domain;
|
2014-04-28 19:41:40 +00:00
|
|
|
try {
|
|
|
|
domain = getDomainName(url);
|
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
2014-07-21 16:22:12 +00:00
|
|
|
|
|
|
|
return mBlockedDomainsList.contains(domain.toLowerCase());
|
2014-04-28 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String getDomainName(String url) throws URISyntaxException {
|
2014-07-19 15:39:51 +00:00
|
|
|
int index = url.indexOf('/', 8);
|
2014-04-28 19:41:40 +00:00
|
|
|
if (index != -1) {
|
|
|
|
url = url.substring(0, index);
|
|
|
|
}
|
2014-07-21 16:22:12 +00:00
|
|
|
|
2014-04-28 19:41:40 +00:00
|
|
|
URI uri = new URI(url);
|
|
|
|
String domain = uri.getHost();
|
|
|
|
if (domain == null) {
|
|
|
|
return url;
|
|
|
|
}
|
2014-07-21 16:22:12 +00:00
|
|
|
|
2014-04-28 19:41:40 +00:00
|
|
|
return domain.startsWith("www.") ? domain.substring(4) : domain;
|
|
|
|
}
|
|
|
|
}
|