Browse Source

Update hosts file, create versatile hosts loading method to make way for users to load from any hosts file

master
Anthony Restaino 10 years ago
parent
commit
25a80a86a5
  1. 74920
      app/src/LightningPlus/assets/hosts.txt
  2. 49
      app/src/main/java/acr/browser/lightning/utils/AdBlock.java
  3. 2
      app/src/main/java/acr/browser/lightning/view/LightningView.java

74920
app/src/LightningPlus/assets/hosts.txt

File diff suppressed because it is too large Load Diff

49
app/src/main/java/acr/browser/lightning/utils/AdBlock.java

@ -20,6 +20,13 @@ public class AdBlock { @@ -20,6 +20,13 @@ public class AdBlock {
private static final String TAG = "AdBlock";
private static final String BLOCKED_DOMAINS_LIST_FILE_NAME = "hosts.txt";
private static final String LOCAL_IP_V4 = "127.0.0.1";
private static final String LOCAL_IP_V6 = "::1";
private static final String LOCALHOST = "localhost";
private static final String COMMENT = "#";
private static final String TAB = "\t";
private static final String SPACE = " ";
private static final String EMPTY = "";
private static final Set<String> mBlockedDomainsList = new HashSet<>();
private boolean mBlockAds;
private static final Locale mLocale = Locale.getDefault();
@ -34,7 +41,7 @@ public class AdBlock { @@ -34,7 +41,7 @@ public class AdBlock {
private AdBlock(Context context) {
if (mBlockedDomainsList.isEmpty() && Constants.FULL_VERSION) {
loadBlockedDomainsList(context);
loadHostsFile(context);
}
mBlockAds = PreferenceManager.getInstance().getAdBlockEnabled();
}
@ -99,4 +106,44 @@ public class AdBlock { @@ -99,4 +106,44 @@ public class AdBlock {
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
private void loadHostsFile(final Context context) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
AssetManager asset = context.getAssets();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
asset.open(BLOCKED_DOMAINS_LIST_FILE_NAME)));
String line;
while ((line = reader.readLine()) != null) {
if (!line.isEmpty() && !line.startsWith(COMMENT)) {
line = line.replace(LOCAL_IP_V4, EMPTY).replace(LOCAL_IP_V6, EMPTY).replace(TAB, EMPTY);
int comment = line.indexOf(COMMENT);
if (comment >= 0) {
line = line.substring(0, comment);
}
line = line.trim();
if (!line.isEmpty() && !line.equals(LOCALHOST)) {
while (line.contains(SPACE)) {
String host = line.substring(0, line.indexOf(SPACE));
mBlockedDomainsList.add(host.trim());
line = line.substring(line.indexOf(SPACE), line.length()).trim();
}
mBlockedDomainsList.add(line.trim());
}
}
}
} catch (IOException e) {
Log.wtf(TAG, "Reading blocked domains list from file '"
+ BLOCKED_DOMAINS_LIST_FILE_NAME + "' failed.", e);
} finally {
Utils.close(reader);
}
}
});
thread.start();
}
}

2
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -711,7 +711,7 @@ public class LightningView { @@ -711,7 +711,7 @@ public class LightningView {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mAdBlock.isAd(request.getUrl().getHost())) {
if (mAdBlock.isAd(request.getUrl().toString())) {
ByteArrayInputStream EMPTY = new ByteArrayInputStream("".getBytes());
return new WebResourceResponse("text/plain", "utf-8", EMPTY);
}

Loading…
Cancel
Save