/*
 * Copyright (c) 2016 Ha Duy Trung
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.hidroh.materialistic;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebResourceResponse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import okhttp3.HttpUrl;
import okio.BufferedSource;
import okio.Okio;

public class AdBlocker {
    private static final String AD_HOSTS_FILE = "pgl.yoyo.org.txt";
    private static final Set<String> AD_HOSTS = new HashSet<>();

    public static void init(Context context) {
        try {
            loadFromAssets(context);
        } catch (IOException e) {
            Log.e(AdBlocker.class.getSimpleName(), e.toString());
        }
    }

    public static boolean isAd(String url) {
        HttpUrl httpUrl = HttpUrl.parse(url);
        return isAdHost(httpUrl != null ? httpUrl.host() : "");
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static WebResourceResponse createEmptyResource() {
        return new WebResourceResponse("text/plain", "utf-8", new ByteArrayInputStream("".getBytes()));
    }

    private static void loadFromAssets(Context context) throws IOException {
        InputStream stream = context.getAssets().open(AD_HOSTS_FILE);
        BufferedSource buffer = Okio.buffer(Okio.source(stream));
        String line;
        while ((line = buffer.readUtf8Line()) != null) {
            AD_HOSTS.add(line);
        }
        buffer.close();
        stream.close();
    }

    private static boolean isAdHost(String host) {
        if (TextUtils.isEmpty(host)) {
            return false;
        }
        int index = host.indexOf(".");
        return index >= 0 && (AD_HOSTS.contains(host) ||
                index + 1 < host.length() && isAdHost(host.substring(index + 1)));
    }
}

@@ -722,20 +722,97 @@
 roid.os.
-Buil
+AsyncTask;%0Aimport android.os.Build;%0Aimport android.support.annotation.WorkerThrea
 d;%0Aimpor
@@ -1321,24 +1321,158 @@
  context) %7B%0A
+        new AsyncTask%3CVoid, Void, Void%3E() %7B%0A            @Override%0A            protected Void doInBackground(Void... params) %7B%0A        
         try 
@@ -1473,16 +1473,24 @@
   try %7B%0A
+        
         
@@ -1526,16 +1526,24 @@
         
+  
+      
 %7D catch 
@@ -1556,24 +1556,32 @@
 eption e) %7B%0A
+        
             
@@ -1638,25 +1638,97 @@
 ));%0A        
-%7D
+        %7D%0A                return null;%0A            %7D%0A        %7D.execute();
 %0A    %7D%0A%0A    
@@ -2096,32 +2096,50 @@
 tes()));%0A    %7D%0A%0A
+    @WorkerThread%0A
     private stat
@@ -2506,24 +2506,180 @@
 e();%0A    %7D%0A%0A
+    /**%0A     * Recursively walking up sub domain chain until we exhaust or find a match,%0A     * effectively doing a longest substring matching here%0A     */%0A
     private 

Load ad hosts in background