aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt
blob: 1bb0449b5614e7521939a40b239ba16624d0564a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.pitchedapps.frost.utils

import android.content.Context
import android.text.TextUtils
import ca.allanwang.kau.utils.use
import okhttp3.HttpUrl
import org.jetbrains.anko.doAsync

/**
 * Created by Allan Wang on 2017-09-24.
 */
object FrostAdBlock : AdBlocker("adblock.txt")

object FrostPglAdBlock : AdBlocker("pgl.yoyo.org.txt")

/**
 * Base implementation of an AdBlocker
 * Wrap this in a singleton and initialize it to use it
 */
open class AdBlocker(val assetPath: String) {

    val data: MutableSet<String> = mutableSetOf()

    fun init(context: Context) {
        doAsync {
            val content = context.assets.open(assetPath).bufferedReader().use { f ->
                f.readLines().filter { !it.startsWith("#") }
            }
            data.addAll(content)
            L.i { "Initialized adblock for $assetPath with ${data.size} hosts" }
        }
    }

    fun isAd(url: String?): Boolean {
        url ?: return false
        val httpUrl = HttpUrl.parse(url) ?: return false
        return isAdHost(httpUrl.host())
    }

    tailrec fun isAdHost(host: String): Boolean {
        if (TextUtils.isEmpty(host))
            return false
        val index = host.indexOf(".")
        if (index < 0 || index + 1 < host.length) return false
        if (host.contains(host)) return true
        return isAdHost(host.substring(index + 1))
    }
}