Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Add Tracking Protection Panel, Tweak TP Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ekager committed Sep 10, 2019
1 parent 228cb91 commit 9315148
Show file tree
Hide file tree
Showing 58 changed files with 7,376 additions and 143 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ dependencies {

implementation Deps.autodispose

implementation Deps.lottie

implementation Deps.adjust
implementation Deps.installreferrer // Required by Adjust

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/org/mozilla/fenix/BrowserDirection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ enum class BrowserDirection(@IdRes val fragmentId: Int) {
FromBookmarks(R.id.bookmarkFragment),
FromHistory(R.id.historyFragment),
FromExceptions(R.id.exceptionsFragment),
FromAbout(R.id.aboutFragment)
FromAbout(R.id.aboutFragment),
FromTrackingProtection(R.id.trackingProtectionFragment)
}
5 changes: 5 additions & 0 deletions app/src/main/java/org/mozilla/fenix/FeatureFlags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ object FeatureFlags {
*/
const val mediaIntegration = true

/**
* Displays the categories blocked by ETP in a panel in the toolbar
*/
val etpCategories = nightly or debug

/**
* Granular data deletion provides additional choices on the Delete Browsing Data
* setting screen for cookies, cached images and files, and site permissions.
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import org.mozilla.fenix.library.history.HistoryFragmentDirections
import org.mozilla.fenix.search.SearchFragmentDirections
import org.mozilla.fenix.settings.AboutFragmentDirections
import org.mozilla.fenix.settings.SettingsFragmentDirections
import org.mozilla.fenix.settings.TrackingProtectionFragmentDirections
import org.mozilla.fenix.share.ShareFragment
import org.mozilla.fenix.theme.DefaultThemeManager
import org.mozilla.fenix.theme.ThemeManager
Expand Down Expand Up @@ -236,6 +237,10 @@ open class HomeActivity : AppCompatActivity(), ShareFragment.TabsSharedCallback
)
BrowserDirection.FromAbout ->
AboutFragmentDirections.actionAboutFragmentToBrowserFragment(customTabSessionId)
BrowserDirection.FromTrackingProtection ->
TrackingProtectionFragmentDirections.actionTrackingProtectionFragmentToBrowserFragment(
customTabSessionId
)
}

private fun load(
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs
showQuickSettingsDialog()
}

browserToolbarView.view.setOnTrackingProtectionClickedListener {
showTrackingProtectionPanel()
}

contextMenuFeature.set(
feature = ContextMenuFeature(
requireFragmentManager(),
Expand Down Expand Up @@ -487,6 +491,8 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs

protected abstract fun navToQuickSettingsSheet(session: Session, sitePermissions: SitePermissions?)

protected abstract fun navToTrackingProtectionPanel(session: Session)

/**
* Returns the top and bottom margins.
*/
Expand Down Expand Up @@ -530,6 +536,13 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs
}
}

private fun showTrackingProtectionPanel() {
val session = getSessionById() ?: return
view?.let {
navToTrackingProtectionPanel(session)
}
}

/**
* Returns the current session.
*/
Expand Down
117 changes: 109 additions & 8 deletions app/src/main/java/org/mozilla/fenix/browser/BrowserFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

package org.mozilla.fenix.browser

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.RadioButton
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
Expand All @@ -18,19 +23,23 @@ import androidx.navigation.fragment.findNavController
import androidx.transition.TransitionInflater
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.fragment_browser.view.*
import kotlinx.android.synthetic.main.tracking_protection_onboarding_popup.view.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.appservices.places.BookmarkRoot
import mozilla.components.browser.session.Session
import mozilla.components.concept.toolbar.Toolbar
import mozilla.components.feature.readerview.ReaderViewFeature
import mozilla.components.feature.sitepermissions.SitePermissions
import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.support.base.feature.BackHandler
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import mozilla.components.support.ktx.android.util.dpToPx
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.readermode.DefaultReaderModeController
Expand All @@ -41,15 +50,20 @@ import org.mozilla.fenix.components.toolbar.BrowserInteractor
import org.mozilla.fenix.components.toolbar.BrowserToolbarController
import org.mozilla.fenix.components.toolbar.BrowserToolbarViewInteractor
import org.mozilla.fenix.components.toolbar.QuickActionSheetAction
import org.mozilla.fenix.exceptions.ExceptionDomains
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.increaseTapArea
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.home.sessioncontrol.SessionControlChange
import org.mozilla.fenix.home.sessioncontrol.TabCollection
import org.mozilla.fenix.mvi.getManagedEmitter
import org.mozilla.fenix.quickactionsheet.DefaultQuickActionSheetController
import org.mozilla.fenix.quickactionsheet.QuickActionSheetSessionObserver
import org.mozilla.fenix.quickactionsheet.QuickActionSheetView
import java.net.MalformedURLException
import java.net.URL

/**
* Fragment used for browsing the web within the main app.
Expand All @@ -62,6 +76,7 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
private var quickActionSheetSessionObserver: QuickActionSheetSessionObserver? = null

private val readerViewFeature = ViewBoundFeatureWrapper<ReaderViewFeature>()
private var exceptionJob: Job? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -122,6 +137,8 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
themeReaderViewControlsForPrivateMode(view.readerViewControlsBar)
}

updateTrackingProtectionUIState(it)

consumeFrom(browserStore) {
quickActionSheetView.update(it)
browserToolbarView.update(it)
Expand All @@ -139,11 +156,31 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
).also { observer ->
getSessionById()?.register(observer, this, autoPause = true)
}
getSessionById()?.register(toolbarSessionObserver, this, autoPause = true)
}

private val toolbarSessionObserver = object : Session.Observer {
override fun onLoadingStateChanged(session: Session, loading: Boolean) {
if (!loading) {
updateTrackingProtectionUIState(session)
if (
context!!.settings.shouldShowTrackingProtectionOnboarding &&
session.trackerBlockingEnabled
) {
showTrackingProtectionOnboarding()
}
}
}

override fun onUrlChanged(session: Session, url: String) {
updateTrackingProtectionUIState(session)
}
}

override fun onResume() {
super.onResume()
getSessionById()?.let { quickActionSheetSessionObserver?.updateBookmarkState(it) }
getSessionById()?.let { updateTrackingProtectionUIState(it) }
requireComponents.core.tabCollectionStorage.register(collectionStorageObserver, this)
}

Expand Down Expand Up @@ -181,14 +218,26 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
}

override fun navToQuickSettingsSheet(session: Session, sitePermissions: SitePermissions?) {
val directions = BrowserFragmentDirections.actionBrowserFragmentToQuickSettingsSheetDialogFragment(
sessionId = session.id,
url = session.url,
isSecured = session.securityInfo.secure,
isTrackingProtectionOn = session.trackerBlockingEnabled,
sitePermissions = sitePermissions,
gravity = getAppropriateLayoutGravity()
)
val directions =
BrowserFragmentDirections.actionBrowserFragmentToQuickSettingsSheetDialogFragment(
sessionId = session.id,
url = session.url,
isSecured = session.securityInfo.secure,
isTrackingProtectionOn = session.trackerBlockingEnabled,
sitePermissions = sitePermissions,
gravity = getAppropriateLayoutGravity()
)
nav(R.id.browserFragment, directions)
}

override fun navToTrackingProtectionPanel(session: Session) {
val directions =
BrowserFragmentDirections.actionBrowserFragmentToTrackingProtectionPanelDialogFragment(
sessionId = session.id,
url = session.url,
trackingProtectionEnabled = session.trackerBlockingEnabled,
gravity = getAppropriateLayoutGravity()
)
nav(R.id.browserFragment, directions)
}

Expand Down Expand Up @@ -288,6 +337,25 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
override fun onSessionSelected(session: Session) {
super.onSessionSelected(session)
quickActionSheetSessionObserver?.updateBookmarkState(session)
updateTrackingProtectionUIState(session)
}

private fun updateTrackingProtectionUIState(session: Session) {
exceptionJob?.cancel()
exceptionJob = lifecycleScope.launch(IO) {
val host = try {
URL(session.url).host
} catch (e: MalformedURLException) {
session.url
}
val trackingProtectionException = ExceptionDomains.load(context!!).contains(host)
withContext(Main) {
if (trackingProtectionException) {
browserToolbarView.view.siteTrackingProtection =
Toolbar.SiteTrackingProtection.OFF_FOR_A_SITE
}
}
}
}

private val collectionStorageObserver = object : TabCollectionStorage.Observer {
Expand All @@ -309,8 +377,41 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
}
}

private fun showTrackingProtectionOnboarding() {
context?.let {
it.settings.incrementTrackingProtectionOnboardingCount()
val layout = LayoutInflater.from(it)
.inflate(R.layout.tracking_protection_onboarding_popup, null)
layout.onboarding_message.text =
it.getString(R.string.etp_onboarding_message, getString(R.string.app_name))
val trackingOnboarding =
PopupWindow(
layout,
TP_ONBOARDING_WIDTH.dpToPx(resources.displayMetrics),
LinearLayout.LayoutParams.WRAP_CONTENT,
true
)
val closeButton = layout.findViewById<ImageView>(R.id.close_onboarding)
closeButton.increaseTapArea(BUTTON_INCREASE_DPS)
closeButton.setOnClickListener {
trackingOnboarding.dismiss()
}
trackingOnboarding.isOutsideTouchable = true
trackingOnboarding.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
trackingOnboarding.showAtLocation(
browserToolbarView.view,
Gravity.BOTTOM or Gravity.START,
TP_ONBOARDING_X_OFFSET.dpToPx(resources.displayMetrics),
browserToolbarView.view.height
)
}
}

companion object {
private const val BUTTON_INCREASE_DPS = 12
private const val TP_ONBOARDING_X_OFFSET = 4
private const val SHARED_TRANSITION_MS = 200L
private const val TP_ONBOARDING_WIDTH = 256
private const val TAB_ITEM_TRANSITION_NAME = "tab_item"
const val REPORT_SITE_ISSUE_URL =
"https://webcompat.com/issues/new?url=%s&label=browser-fenix"
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/org/mozilla/fenix/components/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ class Core(private val context: Context) {
withContext(Dispatchers.IO) {
sessionStorage.restore()
}?.let { snapshot ->
sessionManager.restore(snapshot, updateSelection = (sessionManager.selectedSession == null))
sessionManager.restore(
snapshot,
updateSelection = (sessionManager.selectedSession == null)
)
}

// Now that we have restored our previous state (if there's one) let's setup auto saving the state while
Expand Down Expand Up @@ -151,7 +154,9 @@ class Core(private val context: Context) {
normalMode: Boolean = context.settings.shouldUseTrackingProtection,
privateMode: Boolean = true
): TrackingProtectionPolicy {
val trackingProtectionPolicy = TrackingProtectionPolicy.recommended()
val trackingProtectionPolicy =
if (context.settings.useStrictTrackingProtection) TrackingProtectionPolicy.strict() else
TrackingProtectionPolicy.recommended()

return when {
normalMode && privateMode -> trackingProtectionPolicy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import android.content.Context
import android.view.ViewGroup
import androidx.navigation.NavOptions
import androidx.navigation.Navigation
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.LottieDrawable
import androidx.navigation.fragment.FragmentNavigator
import mozilla.components.browser.domains.autocomplete.DomainAutocompleteProvider
import mozilla.components.browser.session.SessionManager
Expand All @@ -20,6 +22,7 @@ import mozilla.components.feature.toolbar.ToolbarPresenter
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.base.feature.LifecycleAwareFeature
import mozilla.components.support.ktx.android.view.hideKeyboard
import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.nav
Expand Down Expand Up @@ -53,6 +56,26 @@ class ToolbarIntegration(
return@run
}

val task = LottieCompositionFactory
.fromRawRes(
context,
ThemeManager.resolveAttribute(R.attr.shieldLottieFile, context)
)
task.addListener { result ->
val lottieDrawable = LottieDrawable()
lottieDrawable.composition = result
toolbar.displayTrackingProtectionIcon =
context.settings.shouldUseTrackingProtection && FeatureFlags.etpCategories
toolbar.displaySeparatorView =
context.settings.shouldUseTrackingProtection && FeatureFlags.etpCategories

toolbar.setTrackingProtectionIcons(
iconOnNoTrackersBlocked = context.getDrawable(R.drawable.ic_tracking_protection_enabled)!!,
iconOnTrackersBlocked = lottieDrawable,
iconDisabledForSite = context.getDrawable(R.drawable.ic_tracking_protection_disabled)!!
)
}

val tabsAction = TabCounterToolbarButton(
sessionManager,
{
Expand Down
Loading

0 comments on commit 9315148

Please sign in to comment.