/*
 * 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.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;
import rx.Observable;
import rx.schedulers.Schedulers;

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) {
        Observable.just(null)
                .subscribeOn(Schedulers.io())
                .map(aVoid -> {
                    try {
                        loadFromAssets(context);
                    } catch (IOException e) {
                        Log.e(AdBlocker.class.getSimpleName(), e.toString());
                    }
                    return null;
                })
                .subscribe();
    }

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

@@ -812,33 +812,8 @@
 ls;%0A
-import android.util.Log;%0A
 impo
@@ -1349,146 +1349,26 @@
 ble.
-just(null)%0A                .subscribeOn(Schedulers.io())%0A                .map(aVoid -%3E %7B%0A                    try %7B%0A                       
+fromCallable(() -%3E
  loa
@@ -1387,22 +1387,18 @@
 context)
-;%0A    
+)%0A
         
@@ -1409,184 +1409,86 @@
     
-%7D catch (IOException e) %7B%0A                        Log.e(AdBlocker.class.getSimpleName(), e.toString());%0A                    %7D%0A                    return null;%0A                %7D
+.onErrorReturn(throwable -%3E null)%0A                .subscribeOn(Schedulers.io()
 )%0A  
@@ -1936,25 +1936,25 @@
 vate static 
-v
+V
 oid loadFrom
@@ -2298,24 +2298,45 @@
 am.close();%0A
+        return null;%0A
     %7D%0A%0A    /

Use Observable.fromCallable