-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add support for contingency messaging #9083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
CrisBarreiro
wants to merge
9
commits into
feature/cris/clarify-duck-player-entry-point
Choose a base branch
from
feature/cris/enable-contingency-messaging
base: feature/cris/clarify-duck-player-entry-point
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eaa857e
Add YouTube ad-blocking contingency message bottom sheet
CrisBarreiro 3db10cb
Enable contingency messaging
CrisBarreiro a9f0318
Fix race condition and small refactor
CrisBarreiro 3bf1f1a
Don't show contingency message if webView is hidden
CrisBarreiro 6b6783e
Remove comments
CrisBarreiro 540ac9e
Set status indicator to off if contingency
CrisBarreiro 7da5ae5
Fix flaky test and small refactor
CrisBarreiro a655a3a
Fix flaky test and add small arch improvement
CrisBarreiro ad28b40
Make bottom sheet more robust
CrisBarreiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
...mpl/src/main/java/com/duckduckgo/adblocking/impl/ContingencyMessageBottomSheetFragment.kt
This file contains hidden or 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,44 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import com.duckduckgo.adblocking.impl.databinding.BottomSheetContingencyMessageBinding | ||
| import com.google.android.material.bottomsheet.BottomSheetDialogFragment | ||
|
|
||
| class ContingencyMessageBottomSheetFragment : BottomSheetDialogFragment() { | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle?, | ||
| ): View { | ||
| val binding = BottomSheetContingencyMessageBinding.inflate(inflater, container, false) | ||
| binding.contingencyMessageCloseButton.setOnClickListener { dismiss() } | ||
| binding.contingencyMessagePrimaryButton.setOnClickListener { dismiss() } | ||
| return binding.root | ||
| } | ||
|
|
||
| companion object { | ||
| const val TAG = "ContingencyMessageBottomSheet" | ||
|
|
||
| fun newInstance() = ContingencyMessageBottomSheetFragment() | ||
| } | ||
| } |
95 changes: 95 additions & 0 deletions
95
...d-blocking-impl/src/main/java/com/duckduckgo/adblocking/impl/ContingencyMessageHandler.kt
This file contains hidden or 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,95 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl | ||
|
|
||
| import android.webkit.WebView | ||
| import androidx.annotation.UiThread | ||
| import androidx.core.net.toUri | ||
| import androidx.lifecycle.LifecycleOwner | ||
| import com.duckduckgo.adblocking.api.duckplayer.YOUTUBE_HOST | ||
| import com.duckduckgo.adblocking.api.duckplayer.YOUTUBE_MOBILE_HOST | ||
| import com.duckduckgo.adblocking.impl.remoteconfig.AdBlockingExtensionFeature | ||
| import com.duckduckgo.adblocking.impl.remoteconfig.ContingencyMessageStore | ||
| import com.duckduckgo.app.browser.UriString | ||
| import com.duckduckgo.app.di.AppCoroutineScope | ||
| import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver | ||
| import com.duckduckgo.common.utils.DispatcherProvider | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import com.squareup.anvil.annotations.ContributesMultibinding | ||
| import dagger.SingleInstanceIn | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.flow.distinctUntilChanged | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| interface ContingencyMessageHandler { | ||
| /** Called on page load. Shows the contingency message if all conditions are met. */ | ||
| @UiThread | ||
| fun onPageLoaded(webView: WebView, url: String?) | ||
| } | ||
|
|
||
| @SingleInstanceIn(AppScope::class) | ||
| @ContributesBinding(scope = AppScope::class, boundType = ContingencyMessageHandler::class) | ||
| @ContributesMultibinding(scope = AppScope::class, boundType = MainProcessLifecycleObserver::class) | ||
| class RealContingencyMessageHandler @Inject constructor( | ||
| private val feature: AdBlockingExtensionFeature, | ||
| private val store: ContingencyMessageStore, | ||
| private val view: ContingencyMessageView, | ||
| @AppCoroutineScope private val appScope: CoroutineScope, | ||
| private val dispatchers: DispatcherProvider, | ||
| ) : ContingencyMessageHandler, MainProcessLifecycleObserver { | ||
|
|
||
| @Volatile | ||
| private var shownInSession = false | ||
|
|
||
| override fun onCreate(owner: LifecycleOwner) { | ||
| appScope.launch(dispatchers.io()) { | ||
| feature.enableContingencyMode().enabled() | ||
| .distinctUntilChanged() | ||
| .collect { onContingencyModeChanged(it) } | ||
| } | ||
| } | ||
|
|
||
| @UiThread | ||
| override fun onPageLoaded(webView: WebView, url: String?) { | ||
| if (!webView.isShown) return | ||
| if (!shouldShow(url)) return | ||
| shownInSession = true | ||
| view.show(webView) | ||
| appScope.launch(dispatchers.io()) { store.setShown() } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| internal suspend fun onContingencyModeChanged(contingencyEnabled: Boolean) { | ||
| val shown = store.shown.value | ||
| if (!contingencyEnabled) { | ||
| shownInSession = false | ||
| if (shown) store.reset() | ||
| } | ||
| } | ||
|
|
||
| internal fun shouldShow(url: String?): Boolean { | ||
| val uxImprovements = feature.adBlockingUXImprovements().isEnabled() | ||
| val contingency = feature.enableContingencyMode().isEnabled() | ||
| val shown = shownInSession || store.shown.value | ||
| val uri = url?.toUri() | ||
| val isYouTube = uri != null && | ||
| (UriString.sameOrSubdomain(uri, YOUTUBE_HOST) || UriString.sameOrSubdomain(uri, YOUTUBE_MOBILE_HOST)) | ||
| val result = uxImprovements && contingency && isYouTube && !shown | ||
| return result | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
...g/ad-blocking-impl/src/main/java/com/duckduckgo/adblocking/impl/ContingencyMessageView.kt
This file contains hidden or 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,70 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl | ||
|
|
||
| import android.view.ViewTreeObserver | ||
| import android.webkit.WebView | ||
| import androidx.annotation.UiThread | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.FragmentManager | ||
| import androidx.lifecycle.findViewTreeLifecycleOwner | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesBinding | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import javax.inject.Inject | ||
| import kotlin.coroutines.resume | ||
|
|
||
| interface ContingencyMessageView { | ||
| @UiThread | ||
| fun show(webView: WebView) | ||
| } | ||
|
|
||
| @ContributesBinding(AppScope::class) | ||
| class RealContingencyMessageView @Inject constructor() : ContingencyMessageView { | ||
|
|
||
| override fun show(webView: WebView) { | ||
| val lifecycleOwner = webView.findViewTreeLifecycleOwner() ?: return | ||
| lifecycleOwner.lifecycleScope.launch { | ||
| webView.awaitWindowFocus() | ||
| val fragment: Fragment = FragmentManager.findFragment(webView) | ||
| val fragmentManager = fragment.childFragmentManager | ||
| if (fragmentManager.findFragmentByTag(ContingencyMessageBottomSheetFragment.TAG) == null) { | ||
| ContingencyMessageBottomSheetFragment.newInstance() | ||
| .show(fragmentManager, ContingencyMessageBottomSheetFragment.TAG) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private suspend fun WebView.awaitWindowFocus() { | ||
| if (hasWindowFocus()) return | ||
| suspendCancellableCoroutine { continuation -> | ||
| val observer = viewTreeObserver | ||
| val listener = object : ViewTreeObserver.OnWindowFocusChangeListener { | ||
| override fun onWindowFocusChanged(hasFocus: Boolean) { | ||
| if (hasFocus) { | ||
| observer.removeOnWindowFocusChangeListener(this) | ||
| continuation.resume(Unit) | ||
| } | ||
| } | ||
| } | ||
| observer.addOnWindowFocusChangeListener(listener) | ||
| continuation.invokeOnCancellation { observer.removeOnWindowFocusChangeListener(listener) } | ||
| } | ||
| } |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
...ain/java/com/duckduckgo/adblocking/impl/remoteconfig/ContingencyMessageDataStoreModule.kt
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright (c) 2026 DuckDuckGo | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.duckduckgo.adblocking.impl.remoteconfig | ||
|
|
||
| import android.content.Context | ||
| import androidx.datastore.core.DataStore | ||
| import androidx.datastore.preferences.core.Preferences | ||
| import androidx.datastore.preferences.preferencesDataStore | ||
| import com.duckduckgo.di.scopes.AppScope | ||
| import com.squareup.anvil.annotations.ContributesTo | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import javax.inject.Qualifier | ||
|
|
||
| @ContributesTo(AppScope::class) | ||
| @Module | ||
| object ContingencyMessageDataStoreModule { | ||
|
|
||
| private val Context.contingencyMessageDataStore: DataStore<Preferences> by preferencesDataStore( | ||
| name = "ad_blocking_contingency_message", | ||
| ) | ||
|
|
||
| @Provides | ||
| @ContingencyMessage | ||
| fun provideContingencyMessageDataStore(context: Context): DataStore<Preferences> = context.contingencyMessageDataStore | ||
| } | ||
|
|
||
| @Qualifier | ||
| internal annotation class ContingencyMessage |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.