diff options
author | Allan Wang <me@allanwang.ca> | 2017-09-24 20:50:08 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-24 20:50:08 -0400 |
commit | bd7da76b14ab823c5feb808d03c7a4cba14b380a (patch) | |
tree | 650b8e5ed0b104cd514ef02ade28e5a6945ffb97 /app/src/main/kotlin/com/pitchedapps/frost/utils | |
parent | 952d4e41efc21f1276e8b03892e1e8079a727a1b (diff) | |
download | frost-bd7da76b14ab823c5feb808d03c7a4cba14b380a.tar.gz frost-bd7da76b14ab823c5feb808d03c7a4cba14b380a.tar.bz2 frost-bd7da76b14ab823c5feb808d03c7a4cba14b380a.zip |
Fix/adblock (#338)
* Rename none to no notifications
* Add pgl adblock and adblock class
* Add faq
* Line spacing
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt new file mode 100644 index 00000000..298453dd --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt @@ -0,0 +1,43 @@ +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 { + data.addAll(context.assets.open(assetPath).bufferedReader().use { it.readLines().filter { !it.startsWith("#") } }) + L.i("Initialized adblock for $assetPath with ${data.size} hosts") + } + } + + fun isAd(url: String?): Boolean { + 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)) + } +}
\ No newline at end of file |