forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#26224 - New TCP CFR popup
If Total Cookie Protection is enabled when first accessing a normal tab (not a custom tab) a new Contextual Feature Recommendation popup will be shown informing about the added protection and allowing the user to open a support page with more data about the new option for privacy protection.
- Loading branch information
Showing
9 changed files
with
495 additions
and
5 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
app/src/main/java/org/mozilla/fenix/components/toolbar/CFRPresenter.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,120 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.components.toolbar | ||
|
||
import android.content.Context | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.material.Text | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.style.TextDecoration | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.mapNotNull | ||
import kotlinx.coroutines.flow.transformWhile | ||
import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab | ||
import mozilla.components.browser.state.store.BrowserStore | ||
import mozilla.components.browser.toolbar.BrowserToolbar | ||
import mozilla.components.lib.state.ext.flowScoped | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.compose.cfr.CFRPopup | ||
import org.mozilla.fenix.compose.cfr.CFRPopupProperties | ||
import org.mozilla.fenix.ext.components | ||
import org.mozilla.fenix.settings.SupportUtils | ||
import org.mozilla.fenix.settings.SupportUtils.SumoTopic.TOTAL_COOKIE_PROTECTION | ||
import org.mozilla.fenix.theme.FirefoxTheme | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
/** | ||
* Delegate for handling all the business logic for showing CFRs in the toolbar. | ||
* | ||
* @param context [Context] used for various Android interactions. | ||
* @param store [BrowserStore] which will be observed for tabs updates | ||
* @param settings [Settings] allowing to read and write persistent user settings | ||
* @param toolbar [BrowserToolbar] that will serve as anchor for the CFRs | ||
* @param sessionId [String] optional custom tab id. | ||
*/ | ||
class CFRPresenter( | ||
private val context: Context, | ||
private val store: BrowserStore, | ||
private val settings: Settings, | ||
private val toolbar: BrowserToolbar, | ||
private val sessionId: String? = null | ||
) { | ||
@VisibleForTesting | ||
internal var tcpCfrScope: CoroutineScope? = null | ||
@VisibleForTesting | ||
internal var tcpCfr: CFRPopup? = null | ||
|
||
/** | ||
* Start observing [store] for updates which may trigger showing a CFR. | ||
*/ | ||
@Suppress("MagicNumber") | ||
fun start() { | ||
if (settings.shouldShowTotalCookieProtectionCFR) { | ||
tcpCfrScope = store.flowScoped { flow -> | ||
flow | ||
.mapNotNull { it.findCustomTabOrSelectedTab(sessionId)?.content?.progress } | ||
// The "transformWhile" below ensures that the 100% progress is only collected once. | ||
.transformWhile { progress -> | ||
emit(progress) | ||
progress != 100 | ||
} | ||
.filter { it == 100 } | ||
.collect { | ||
tcpCfrScope?.cancel() | ||
showTcpCfr() | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Stop listening for [store] updates. | ||
* CFRs already shown are not automatically dismissed. | ||
*/ | ||
fun stop() { | ||
tcpCfrScope?.cancel() | ||
} | ||
|
||
@VisibleForTesting | ||
internal fun showTcpCfr() { | ||
CFRPopup( | ||
text = context.getString(R.string.tcp_cfr_message), | ||
anchor = toolbar.findViewById( | ||
R.id.mozac_browser_toolbar_security_indicator | ||
), | ||
properties = CFRPopupProperties( | ||
indicatorDirection = if (settings.toolbarPosition == ToolbarPosition.TOP) { | ||
CFRPopup.IndicatorDirection.UP | ||
} else { | ||
CFRPopup.IndicatorDirection.DOWN | ||
}, | ||
), | ||
) { | ||
Text( | ||
text = context.getString(R.string.tcp_cfr_learn_more), | ||
color = FirefoxTheme.colors.textOnColorPrimary, | ||
modifier = Modifier.clickable { | ||
context.components.useCases.tabsUseCases.selectOrAddTab.invoke( | ||
SupportUtils.getSumoURLForTopic( | ||
context, | ||
TOTAL_COOKIE_PROTECTION | ||
) | ||
) | ||
tcpCfr?.dismiss() | ||
}, | ||
style = FirefoxTheme.typography.body2.copy( | ||
textDecoration = TextDecoration.Underline | ||
) | ||
) | ||
}.apply { | ||
settings.shouldShowTotalCookieProtectionCFR = false | ||
tcpCfr = this | ||
show() | ||
} | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.