2017-06-08 02:33:00 +02:00
|
|
|
/*
|
|
|
|
* ---------------------------------------------------------------------------- *
|
|
|
|
* Gregor Santner <gsantner.github.io> wrote this file. You can do whatever
|
|
|
|
* you want with this stuff. If we meet some day, and you think this stuff is
|
|
|
|
* worth it, you can buy me a coke in return. Provided as is without any kind
|
|
|
|
* of warranty. No attribution required. - Gregor Santner
|
|
|
|
*
|
2017-08-04 05:42:43 +02:00
|
|
|
* License of this file: Creative Commons Zero (CC0 1.0)
|
2017-06-08 02:33:00 +02:00
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Place adblock hosts file in raw: src/main/res/raw/adblock_domains__xyz.txt
|
|
|
|
* Always blocks both, www and non www
|
|
|
|
|
|
|
|
* Load hosts (call e.g. via Application-Object)
|
|
|
|
AdBlock.getInstance().loadHostsFromRawAssetsAsync(context);
|
|
|
|
|
|
|
|
* Override inside a WebViewClient:
|
|
|
|
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
|
|
|
|
return AdBlock.getInstance().isAdHost(url)
|
|
|
|
? AdBlock.createEmptyResponse()
|
|
|
|
: super.shouldInterceptRequest(view, url);
|
|
|
|
}
|
|
|
|
*/
|
2017-09-09 17:09:04 +02:00
|
|
|
package net.gsantner.opoc.util;
|
2017-06-08 02:33:00 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.webkit.WebResourceResponse;
|
|
|
|
|
2017-06-10 13:01:57 +02:00
|
|
|
import com.github.dfa.diaspora_android.R;
|
|
|
|
|
2017-06-08 02:33:00 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple Host-Based AdBlocker
|
|
|
|
*/
|
2017-09-09 17:09:04 +02:00
|
|
|
@SuppressWarnings({"WeakerAccess", "SpellCheckingInspection", "unused"})
|
2017-06-08 02:33:00 +02:00
|
|
|
public class AdBlock {
|
|
|
|
private static final AdBlock instance = new AdBlock();
|
|
|
|
|
|
|
|
public static AdBlock getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
//########################
|
|
|
|
//##
|
|
|
|
//## Members
|
|
|
|
//##
|
|
|
|
//########################
|
2017-09-09 17:09:04 +02:00
|
|
|
private final Set<String> _adblockHostsFromRaw = new HashSet<>();
|
|
|
|
private final Set<String> _adblockHosts = new HashSet<>();
|
|
|
|
private boolean _isLoaded;
|
2017-06-08 02:33:00 +02:00
|
|
|
|
|
|
|
//########################
|
|
|
|
//##
|
|
|
|
//## Methods
|
|
|
|
//##
|
|
|
|
//########################
|
|
|
|
private AdBlock() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isAdHost(String urlS) {
|
|
|
|
if (urlS != null && !urlS.isEmpty() && urlS.startsWith("http")) {
|
|
|
|
try {
|
|
|
|
URI url = new URI(urlS);
|
|
|
|
String host = url.getHost().trim();
|
|
|
|
if (host.startsWith("www.") && host.length() >= 4) {
|
|
|
|
host = host.substring(4);
|
|
|
|
}
|
2017-09-09 17:09:04 +02:00
|
|
|
return _adblockHosts.contains(host) || _adblockHosts.contains("www." + host);
|
2017-06-08 02:33:00 +02:00
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AdBlock reset() {
|
2017-09-09 17:09:04 +02:00
|
|
|
_adblockHosts.clear();
|
|
|
|
_adblockHosts.addAll(_adblockHostsFromRaw);
|
2017-06-08 02:33:00 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:04 +02:00
|
|
|
public boolean isLoaded() {
|
|
|
|
return _isLoaded;
|
|
|
|
}
|
|
|
|
|
2017-06-08 02:33:00 +02:00
|
|
|
public static WebResourceResponse createEmptyResponse() {
|
|
|
|
return new WebResourceResponse("text/plain", "utf-8", new ByteArrayInputStream("".getBytes()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addBlockedHosts(String... hosts) {
|
|
|
|
for (String host : hosts) {
|
|
|
|
if (host != null) {
|
|
|
|
host = host.trim();
|
|
|
|
if (host.startsWith("www.") && host.length() >= 4) {
|
|
|
|
host = host.substring(4);
|
|
|
|
}
|
|
|
|
if (!host.startsWith("#") && !host.startsWith("\"")) {
|
2017-09-09 17:09:04 +02:00
|
|
|
_adblockHosts.add(host);
|
2017-06-08 02:33:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadHostsFromRawAssetsAsync(final Context context) {
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
loadHostsFromRawAssets(context);
|
2017-09-09 17:09:04 +02:00
|
|
|
_isLoaded = true;
|
2017-06-08 02:33:00 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void loadHostsFromRawAssets(Context context) throws IOException {
|
|
|
|
BufferedReader br = null;
|
|
|
|
String host;
|
|
|
|
|
2017-09-09 17:09:04 +02:00
|
|
|
_adblockHosts.clear();
|
2017-06-08 02:33:00 +02:00
|
|
|
for (int rawId : getAdblockIdsInRaw()) {
|
|
|
|
try {
|
|
|
|
br = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(rawId)));
|
|
|
|
while ((host = br.readLine()) != null) {
|
|
|
|
addBlockedHosts(host);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(AdBlock.class.getName(), "Error: Cannot read adblock res " + rawId);
|
|
|
|
} finally {
|
|
|
|
if (br != null) {
|
|
|
|
try {
|
|
|
|
br.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 17:09:04 +02:00
|
|
|
_adblockHostsFromRaw.clear();
|
|
|
|
_adblockHostsFromRaw.addAll(_adblockHosts);
|
2017-06-08 02:33:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<Integer> getAdblockIdsInRaw() {
|
|
|
|
ArrayList<Integer> adblockResIds = new ArrayList<>();
|
|
|
|
Field[] fields = R.raw.class.getFields();
|
|
|
|
for (Field field : fields) {
|
|
|
|
try {
|
|
|
|
int resId = field.getInt(field);
|
|
|
|
String resFilename = field.getName();
|
|
|
|
if (resFilename.startsWith("adblock_domains__")) {
|
|
|
|
adblockResIds.add(resId);
|
|
|
|
}
|
2017-08-04 05:42:43 +02:00
|
|
|
} catch (IllegalAccessException | IllegalArgumentException ignored) {
|
2017-06-08 02:33:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return adblockResIds;
|
|
|
|
}
|
|
|
|
}
|