aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/AdBlocker.kt43
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