Skip to content

Commit

Permalink
[AdBlocker] 🔧 Implement checkbox click to enable/disable the feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercandj committed Sep 11, 2018
1 parent 15f1206 commit 313b945
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mercandalli.android.browser.ad_blocker

import android.content.Context

class AdBloackerModule {

fun createAdBlockerManager(context: Context): AdBlockerManager {
val sharedPreferences = context.getSharedPreferences(
AdBlockerManagerImpl.PREFERENCE_NAME,
Context.MODE_PRIVATE
)
return AdBlockerManagerImpl(
sharedPreferences
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static boolean isAdHost(String host) {
}

public static WebResourceResponse createEmptyResource() {
return new WebResourceResponse("text/plain", "utf-8", new ByteArrayInputStream("".getBytes()));
return new WebResourceResponse("text/plain",
"utf-8", new ByteArrayInputStream("".getBytes()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mercandalli.android.browser.ad_blocker

interface AdBlockerManager {

fun isEnabled(): Boolean

fun setEnabled(enabled: Boolean)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.mercandalli.android.browser.ad_blocker

import android.content.SharedPreferences

class AdBlockerManagerImpl(
private val sharedPreferences: SharedPreferences
) : AdBlockerManager {

private var enabled = false
private var enabledLoaded = false

override fun isEnabled(): Boolean {
load()
return enabled
}

override fun setEnabled(enabled: Boolean) {
this.enabled = enabled
sharedPreferences.edit().putBoolean(KEY, enabled).apply()
}

private fun load() {
if (enabledLoaded) {
return
}
enabledLoaded = true
enabled = sharedPreferences.getBoolean(KEY, enabled)
}

companion object {
@JvmStatic
val PREFERENCE_NAME = "AdBlockerManager"
private const val KEY = "ad-blocker-enabled"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.net.MalformedURLException;
import java.net.URL;

public class UrlUtils {
public static String getHost(String url) throws MalformedURLException {
class UrlUtils {

static String getHost(String url) throws MalformedURLException {
return new URL(url).getHost();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BrowserView @JvmOverloads constructor(

@SuppressWarnings("deprecation")
override fun shouldInterceptRequest(view: WebView, url: String): WebResourceResponse? {
if (!MonetizationGraph.getInAppManager().isPurchased(MainApplication.SKU_SUBSCRIPTION_ADS_BLOCKER)) {
if (!ApplicationGraph.getAdBlockerManager().isEnabled()) {
return super.shouldInterceptRequest(view, url)
}
val ad: Boolean
Expand All @@ -106,7 +106,7 @@ class BrowserView @JvmOverloads constructor(

@SuppressWarnings("deprecation")
override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
if (!MonetizationGraph.getInAppManager().isPurchased(MainApplication.SKU_SUBSCRIPTION_ADS_BLOCKER)) {
if (!ApplicationGraph.getAdBlockerManager().isEnabled()) {
return super.shouldInterceptRequest(view, request)
}
val url: String = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.mercandalli.android.browser.main

import android.annotation.SuppressLint
import android.content.Context
import com.mercandalli.android.browser.ad_blocker.AdBloackerModule
import com.mercandalli.android.browser.theme.ThemeModule
import com.mercandalli.android.browser.thread.MainThreadModule
import com.mercandalli.android.browser.toast.ToastModule
Expand All @@ -11,6 +12,7 @@ class ApplicationGraph(
private val context: Context
) {

private val adBlockerManagerInternal by lazy { AdBloackerModule().createAdBlockerManager(context) }
private val mainThreadPostInternal by lazy { MainThreadModule().createMainThreadPost() }
private val themeManagerInternal by lazy { ThemeModule(context).createThemeManager() }
private val toastManagerInternal by lazy { ToastModule().createToastManager(context, mainThreadPostInternal) }
Expand All @@ -29,6 +31,9 @@ class ApplicationGraph(
}
}

@JvmStatic
fun getAdBlockerManager() = graph!!.adBlockerManagerInternal

@JvmStatic
fun getThemeManager() = graph!!.themeManagerInternal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface SettingsContract {
fun showAdBlockerRow()

fun hideAdBlockerRow()

fun setAdBlockerEnabled(enabled: Boolean)
}

interface UserAction {
Expand All @@ -38,6 +40,8 @@ interface SettingsContract {

fun onDarkThemeCheckBoxCheckedChanged(isChecked: Boolean)

fun onAdBlockerCheckBoxCheckedChanged(isChecked: Boolean)

fun onUnlockAdsBlocker(activityContainer: InAppManager.ActivityContainer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mercandalli.android.browser.settings
import android.os.Build
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.SkuDetails
import com.mercandalli.android.browser.ad_blocker.AdBlockerManager
import com.mercandalli.android.browser.main.MainApplication
import com.mercandalli.android.browser.theme.Theme
import com.mercandalli.android.browser.theme.ThemeManager
Expand All @@ -13,7 +14,8 @@ class SettingsPresenter(
private val screen: SettingsContract.Screen,
private val themeManager: ThemeManager,
private val versionManager: VersionManager,
private val inAppManager: InAppManager
private val inAppManager: InAppManager,
private val adBlockerManager: AdBlockerManager
) : SettingsContract.UserAction {

private val themeListener = createThemeListener()
Expand All @@ -36,6 +38,11 @@ class SettingsPresenter(
themeManager.setDarkEnable(isChecked)
}

override fun onAdBlockerCheckBoxCheckedChanged(isChecked: Boolean) {
adBlockerManager.setEnabled(isChecked)
syncAdBlockerRows()
}

override fun onUnlockAdsBlocker(activityContainer: InAppManager.ActivityContainer) {
inAppManager.purchase(
activityContainer,
Expand Down Expand Up @@ -66,14 +73,18 @@ class SettingsPresenter(
screen.setSectionColor(theme.cardBackgroundColorRes)
}

private fun syncAdBlockerRows(isPurchased: Boolean = inAppManager.isPurchased(MainApplication.SKU_SUBSCRIPTION_ADS_BLOCKER)) {
private fun syncAdBlockerRows(
isPurchased: Boolean = inAppManager.isPurchased(MainApplication.SKU_SUBSCRIPTION_ADS_BLOCKER),
isEnabled: Boolean = adBlockerManager.isEnabled()
) {
if (isPurchased) {
screen.hideAdBlockerUnlockRow()
screen.showAdBlockerRow()
} else {
screen.showAdBlockerUnlockRow()
screen.hideAdBlockerRow()
}
screen.setAdBlockerEnabled(isEnabled)
}

private fun createThemeListener() = object : ThemeManager.ThemeListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class SettingsView @JvmOverloads constructor(
themeCheckBox.isChecked = isChecked
userAction.onDarkThemeCheckBoxCheckedChanged(isChecked)
}
adBlockerRow.setOnClickListener {
val isChecked = !adBlockerCheckBox.isChecked
adBlockerCheckBox.isChecked = isChecked
userAction.onAdBlockerCheckBoxCheckedChanged(isChecked)
}
adBlockerUnlockRow.setOnClickListener {
userAction.onUnlockAdsBlocker(activityContainer!!)
}
Expand Down Expand Up @@ -87,6 +92,7 @@ class SettingsView @JvmOverloads constructor(

override fun setTextPrimaryColorRes(@ColorRes textPrimaryColorRes: Int) {
val textColor = ContextCompat.getColor(context, textPrimaryColorRes)
adBlockerLabel.setTextColor(textColor)
themeLabel.setTextColor(textColor)
adBlockerCheckBox.setTextColor(textColor)
themeLabel.setTextColor(textColor)
Expand Down Expand Up @@ -136,6 +142,10 @@ class SettingsView @JvmOverloads constructor(
adBlockerRow.visibility = GONE
}

override fun setAdBlockerEnabled(enabled: Boolean) {
adBlockerCheckBox.isChecked = enabled
}

fun setActivityContainer(activityContainer: InAppManager.ActivityContainer) {
this.activityContainer = activityContainer
}
Expand All @@ -145,17 +155,20 @@ class SettingsView @JvmOverloads constructor(
override fun onAttached() {}
override fun onDetached() {}
override fun onDarkThemeCheckBoxCheckedChanged(isChecked: Boolean) {}
override fun onAdBlockerCheckBoxCheckedChanged(isChecked: Boolean) {}
override fun onUnlockAdsBlocker(activityContainer: InAppManager.ActivityContainer) {}
}
} else {
val themeManager = ApplicationGraph.getThemeManager()
val versionManager = ApplicationGraph.getVersionManager()
val inAppManager = MonetizationGraph.getInAppManager()
val adBlockerManager = ApplicationGraph.getAdBlockerManager()
SettingsPresenter(
this,
themeManager,
versionManager,
inAppManager
inAppManager,
adBlockerManager
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ internal class InAppManagerImpl(
skuDetailsResponseListener
)


val inAppPurchasesResult = playBillingManager.queryPurchases(BillingClient.SkuType.INAPP)
inAppPurchasesResult.purchasesList

Expand Down

0 comments on commit 313b945

Please sign in to comment.