/*
 * 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.AsyncTask;
import android.os.Build;
import android.support.annotation.WorkerThread;
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) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    loadFromAssets(context);
                } catch (IOException e) {
                    Log.e(AdBlocker.class.getSimpleName(), e.toString());
                }
                return null;
            }
        }.execute();
    }

    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()));
    }

    @WorkerThread
    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();
    }

    /**
     * Recursively walking up sub domain chain until we exhaust or find a match,
     * effectively doing a longest substring matching here
     */
    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)));
    }
}

@@ -708,37 +708,8 @@
 xt;%0A
-import android.os.AsyncTask;%0A
 impo
@@ -729,16 +729,16 @@
 .Build;%0A
+
 import a
@@ -1088,16 +1088,71 @@
 io.Okio;
+%0Aimport rx.Observable;%0Aimport rx.schedulers.Schedulers;
 %0A%0Apublic
@@ -1363,126 +1363,112 @@
     
-new AsyncTask%3CVoid, Void, Void%3E() %7B%0A            @Override%0A            protected Void doInBackground(Void... params) %7B%0A
+Observable.just(null)%0A                .subscribeOn(Schedulers.io())%0A                .map(aVoid -%3E %7B%0A    
     
@@ -1505,16 +1505,20 @@
         
+   
+ 
 loadFrom
@@ -1534,16 +1534,20 @@
 ntext);%0A
+    
         
@@ -1600,16 +1600,20 @@
         
+ 
+   
 Log.e(Ad
@@ -1666,34 +1666,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
                 
@@ -1717,33 +1717,38 @@
 ll;%0A            
-%7D
+    %7D)
 %0A        %7D.execu
@@ -1744,16 +1744,25 @@
     
-%7D.execut
+        .subscrib
 e();

Replace AsyncTask with RxJava