-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
329 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 0 additions & 77 deletions
77
app/src/main/java/com/mercandalli/android/browser/ad_blocker/AdBlocker.java
This file was deleted.
Oops, something went wrong.
73 changes: 73 additions & 0 deletions
73
app/src/main/java/com/mercandalli/android/browser/ad_blocker/AdBlocker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.mercandalli.android.browser.ad_blocker | ||
|
||
import android.content.Context | ||
import android.os.AsyncTask | ||
import android.text.TextUtils | ||
import android.util.Log | ||
import android.webkit.WebResourceResponse | ||
|
||
import java.io.BufferedReader | ||
import java.io.ByteArrayInputStream | ||
import java.io.IOException | ||
import java.io.InputStream | ||
import java.io.InputStreamReader | ||
import java.net.MalformedURLException | ||
import java.util.HashSet | ||
|
||
import androidx.annotation.WorkerThread | ||
|
||
/** | ||
* https://github.com/AmniX/AdBlockedWebView-Android | ||
*/ | ||
object AdBlocker { | ||
|
||
private const val AD_HOSTS_FILE = "hosts.txt" | ||
private val AD_HOSTS = HashSet<String>() | ||
|
||
fun init(context: Context) { | ||
object : AsyncTask<Void, Void, Void>() { | ||
override fun doInBackground(vararg params: Void): Void? { | ||
try { | ||
loadFromAssets(context) | ||
} catch (e: IOException) { | ||
// noop | ||
} | ||
|
||
return null | ||
} | ||
}.execute() | ||
} | ||
|
||
@WorkerThread | ||
private fun loadFromAssets(context: Context) { | ||
val stream = context.assets.open(AD_HOSTS_FILE) | ||
|
||
val inputStreamReader = InputStreamReader(stream) | ||
val readLines = inputStreamReader.readLines() | ||
AD_HOSTS.addAll(readLines) | ||
} | ||
|
||
fun isAd(url: String): Boolean { | ||
return try { | ||
isAdHost(UrlUtils.getHost(url)) | ||
} catch (e: MalformedURLException) { | ||
Log.d("AmniX", e.toString()) | ||
false | ||
} | ||
} | ||
|
||
fun createEmptyResource() = WebResourceResponse( | ||
"text/plain", | ||
"utf-8", | ||
ByteArrayInputStream("".toByteArray()) | ||
) | ||
|
||
private fun isAdHost(host: String): Boolean { | ||
if (TextUtils.isEmpty(host)) { | ||
return false | ||
} | ||
val index = host.indexOf(".") | ||
val adHost = isAdHost(host.substring(index + 1)) | ||
return index >= 0 && (AD_HOSTS.contains(host) || index + 1 < host.length && adHost) | ||
} | ||
} |
11 changes: 0 additions & 11 deletions
11
app/src/main/java/com/mercandalli/android/browser/ad_blocker/UrlUtils.java
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
app/src/main/java/com/mercandalli/android/browser/ad_blocker/UrlUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.mercandalli.android.browser.ad_blocker | ||
|
||
import java.net.MalformedURLException | ||
import java.net.URL | ||
|
||
internal object UrlUtils { | ||
|
||
@Throws(MalformedURLException::class) | ||
fun getHost(url: String): String { | ||
return URL(url).host | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
app/src/main/java/com/mercandalli/android/browser/browser/BrowserContract.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 0 additions & 129 deletions
129
app/src/main/java/com/mercandalli/android/browser/browser/NestedScrollWebView.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.