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 6, 2019
1 parent aef087a commit 64d5fea
Show file tree
Hide file tree
Showing 62 changed files with 7,342 additions and 299 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ dependencies {

implementation Deps.autodispose

implementation Deps.lottie

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AppRequestInterceptor(private val context: Context) : RequestInterceptor {

private fun adjustTrackingProtection(host: String, context: Context, session: EngineSession) {
val trackingProtectionException = ExceptionDomains.load(context).contains(host)
val trackingProtectionEnabled = Settings.getInstance(context).shouldUseTrackingProtection
val trackingProtectionEnabled = Settings.getInstance(context).shouldUseTrackingProtectionGlobal
if (trackingProtectionException || !trackingProtectionEnabled) {
session.disableTrackingProtection()
} else {
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/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -206,6 +206,10 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs
showQuickSettingsDialog()
}

browserToolbarView.view.setOnTrackingProtectionClickedListener {
showTrackingProtectionPanel()
}

contextMenuFeature.set(
feature = ContextMenuFeature(
requireFragmentManager(),
Expand Down Expand Up @@ -486,6 +490,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 @@ -529,6 +535,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
120 changes: 112 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,6 +50,8 @@ 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.increaseTapArea
import org.mozilla.fenix.ext.nav
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.home.sessioncontrol.SessionControlChange
Expand All @@ -49,6 +60,9 @@ 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 org.mozilla.fenix.utils.Settings
import java.net.MalformedURLException
import java.net.URL

/**
* Fragment used for browsing the web within the main app.
Expand All @@ -61,6 +75,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 @@ -120,6 +135,8 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
themeReaderViewControlsForPrivateMode(view.readerViewControlsBar)
}

updateTrackingProtectionUIState(it)

consumeFrom(browserStore) {
quickActionSheetView.update(it)
browserToolbarView.update(it)
Expand All @@ -137,11 +154,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 (
Settings.getInstance(context!!).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 @@ -177,14 +214,25 @@ 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,
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 @@ -284,6 +332,29 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
override fun onSessionSelected(session: Session) {
super.onSessionSelected(session)
quickActionSheetSessionObserver?.updateBookmarkState(session)
updateTrackingProtectionUIState(session)
}

private fun updateTrackingProtectionUIState(session: Session) {
exceptionJob?.cancel()
context?.let {
if (!Settings.getInstance(it).shouldUseTrackingProtectionGlobal)
browserToolbarView.view.siteTrackingProtection =
Toolbar.SiteTrackingProtection.OFF_GLOBALLY
return
}
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 @@ -305,8 +376,41 @@ class BrowserFragment : BaseBrowserFragment(), BackHandler {
}
}

private fun showTrackingProtectionOnboarding() {
context?.let {
Settings.getInstance(it).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
11 changes: 8 additions & 3 deletions app/src/main/java/org/mozilla/fenix/components/Core.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,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 @@ -149,10 +152,12 @@ class Core(private val context: Context) {
* @return the constructed tracking protection policy based on preferences.
*/
fun createTrackingProtectionPolicy(
normalMode: Boolean = Settings.getInstance(context).shouldUseTrackingProtection,
normalMode: Boolean = Settings.getInstance(context).shouldUseTrackingProtectionGlobal,
privateMode: Boolean = true
): TrackingProtectionPolicy {
val trackingProtectionPolicy = TrackingProtectionPolicy.recommended()
val trackingProtectionPolicy =
if (Settings.getInstance(context).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 Down Expand Up @@ -39,6 +41,7 @@ class ToolbarIntegration(
) : LifecycleAwareFeature {

private var renderStyle: ToolbarFeature.RenderStyle = ToolbarFeature.RenderStyle.UncoloredUrl
private lateinit var trackingProtectionAction: BrowserToolbar.Button

init {
toolbar.setMenuBuilder(toolbarMenu.menuBuilder)
Expand All @@ -53,6 +56,23 @@ 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 =
Settings.getInstance(context).shouldUseTrackingProtectionGlobal
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,24 @@ class ExternalAppBrowserFragment : BaseBrowserFragment(), BackHandler {
sessionId = session.id,
url = session.url,
isSecured = session.securityInfo.secure,
isTrackingProtectionOn = session.trackerBlockingEnabled,
sitePermissions = sitePermissions,
gravity = getAppropriateLayoutGravity()
)
nav(R.id.externalAppBrowserFragment, directions)
}

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

override fun getEngineMargins(): Pair<Int, Int> {
val toolbarSize = resources.getDimensionPixelSize(R.dimen.browser_toolbar_height)
return toolbarSize to 0
Expand Down
Loading

0 comments on commit 64d5fea

Please sign in to comment.