From 23c3c286de6773ea7babfc2af9dfbcceba959458 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Tue, 24 Mar 2020 14:23:15 -0400 Subject: [PATCH] [fenix] For issue https://github.com/mozilla-mobile/fenix/issues/8017 enable SitePermissionsWifiIntegration --- .../mozilla/fenix/browser/BaseBrowserFragment.kt | 12 ++++++++++++ .../org/mozilla/fenix/components/Components.kt | 11 +---------- .../SitePermissionsManagePhoneFeatureFragment.kt | 15 ++++++--------- ...ation.kt => SitePermissionsWifiIntegration.kt} | 10 +++++++--- 4 files changed, 26 insertions(+), 22 deletions(-) rename app/src/main/java/org/mozilla/fenix/wifi/{WifiIntegration.kt => SitePermissionsWifiIntegration.kt} (86%) diff --git a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt index 099923f417..d405d24344 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -86,6 +86,7 @@ import org.mozilla.fenix.ext.sessionsOfType import org.mozilla.fenix.ext.settings import org.mozilla.fenix.settings.SupportUtils import org.mozilla.fenix.theme.ThemeManager +import org.mozilla.fenix.wifi.SitePermissionsWifiIntegration import java.lang.ref.WeakReference /** @@ -119,6 +120,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session private val fullScreenFeature = ViewBoundFeatureWrapper() private val swipeRefreshFeature = ViewBoundFeatureWrapper() private val webchannelIntegration = ViewBoundFeatureWrapper() + private val sitePermissionWifiIntegration = ViewBoundFeatureWrapper() var customTabSessionId: String? = null @@ -392,6 +394,15 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session view = view ) + sitePermissionWifiIntegration.set( + feature = SitePermissionsWifiIntegration( + settings = context.settings(), + wifiConnectionMonitor = context.components.wifiConnectionMonitor + ), + owner = this, + view = view + ) + context.settings().setSitePermissionSettingListener(viewLifecycleOwner) { // If the user connects to WIFI while on the BrowserFragment, this will update the // SitePermissionsRules (specifically autoplay) accordingly @@ -536,6 +547,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session override fun onStart() { super.onStart() requireComponents.core.sessionManager.register(this, this, autoPause = true) + sitePermissionWifiIntegration.get()?.maybeAddWifiConnectedListener() } @CallSuper diff --git a/app/src/main/java/org/mozilla/fenix/components/Components.kt b/app/src/main/java/org/mozilla/fenix/components/Components.kt index 9b377861d8..17f5db6a04 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Components.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Components.kt @@ -19,11 +19,9 @@ import mozilla.components.lib.publicsuffixlist.PublicSuffixList import mozilla.components.support.migration.state.MigrationStore import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.HomeActivity -import org.mozilla.fenix.ext.settings import org.mozilla.fenix.test.Mockable import org.mozilla.fenix.utils.ClipboardHandler import org.mozilla.fenix.wifi.WifiConnectionMonitor -import org.mozilla.fenix.wifi.WifiIntegration import java.util.concurrent.TimeUnit private const val DAY_IN_MINUTES = 24 * 60L @@ -110,12 +108,5 @@ class Components(private val context: Context) { val migrationStore by lazy { MigrationStore() } val performance by lazy { PerformanceComponent() } val push by lazy { Push(context, analytics.crashReporter) } - val wifiIntegration by lazy { - WifiIntegration( - settings = context.settings(), - wifiConnectionMonitor = WifiConnectionMonitor( - context as Application - ) - ) - } + val wifiConnectionMonitor by lazy { WifiConnectionMonitor(context as Application) } } diff --git a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsManagePhoneFeatureFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsManagePhoneFeatureFragment.kt index d21adccac4..b8c40daf54 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsManagePhoneFeatureFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/sitepermissions/SitePermissionsManagePhoneFeatureFragment.kt @@ -4,7 +4,6 @@ package org.mozilla.fenix.settings.sitepermissions -import android.content.Context import android.content.Intent import android.graphics.Color import android.net.Uri @@ -30,7 +29,6 @@ import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.AL import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.ASK_TO_ALLOW import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action.BLOCKED import org.mozilla.fenix.R -import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.showToolbar import org.mozilla.fenix.settings.PhoneFeature @@ -122,7 +120,7 @@ class SitePermissionsManagePhoneFeatureFragment : Fragment() { // TODO replace with AUTOPLAY_ALLOW_ON_WIFI when // https://bugzilla.mozilla.org/show_bug.cgi?id=1621825 is fixed. This GV bug // makes ALLOW_ALL behave as ALLOW_ON_WIFI - saveActionInSettings(it.context, AUTOPLAY_ALLOW_ALL) + saveActionInSettings(AUTOPLAY_ALLOW_ALL) } // TODO replace with AUTOPLAY_ALLOW_ON_WIFI when // https://bugzilla.mozilla.org/show_bug.cgi?id=1621825 is fixed. This GV bug @@ -144,7 +142,7 @@ class SitePermissionsManagePhoneFeatureFragment : Fragment() { visibility = View.VISIBLE text = getString(R.string.preference_option_autoplay_block_audio2) setOnClickListener { - saveActionInSettings(it.context, AUTOPLAY_BLOCK_AUDIBLE) + saveActionInSettings(AUTOPLAY_BLOCK_AUDIBLE) } restoreState(AUTOPLAY_BLOCK_AUDIBLE) } else { @@ -162,7 +160,7 @@ class SitePermissionsManagePhoneFeatureFragment : Fragment() { getString(R.string.phone_feature_recommended) ) setOnClickListener { - saveActionInSettings(it.context, AUTOPLAY_BLOCK_ALL) + saveActionInSettings(AUTOPLAY_BLOCK_ALL) } restoreState(AUTOPLAY_BLOCK_ALL) } else { @@ -195,19 +193,18 @@ class SitePermissionsManagePhoneFeatureFragment : Fragment() { * See [Settings.setAutoplayUserSetting] kdoc for an explanation of why this cannot follow the * same code path as other permissions. */ - private fun saveActionInSettings(context: Context, autoplaySetting: Int) { + private fun saveActionInSettings(autoplaySetting: Int) { settings.setAutoplayUserSetting(autoplaySetting) val (audible, inaudible) = when (autoplaySetting) { - AUTOPLAY_ALLOW_ALL -> ALLOWED to ALLOWED + AUTOPLAY_ALLOW_ALL, AUTOPLAY_ALLOW_ON_WIFI -> { - context.components.wifiIntegration.addWifiConnectedListener() + settings.setAutoplayUserSetting(AUTOPLAY_ALLOW_ON_WIFI) return } AUTOPLAY_BLOCK_AUDIBLE -> BLOCKED to ALLOWED AUTOPLAY_BLOCK_ALL -> BLOCKED to BLOCKED else -> return } - context.components.wifiIntegration.removeWifiConnectedListener() settings.setSitePermissionsPhoneFeatureAction(AUTOPLAY_AUDIBLE, audible) settings.setSitePermissionsPhoneFeatureAction(AUTOPLAY_INAUDIBLE, inaudible) } diff --git a/app/src/main/java/org/mozilla/fenix/wifi/WifiIntegration.kt b/app/src/main/java/org/mozilla/fenix/wifi/SitePermissionsWifiIntegration.kt similarity index 86% rename from app/src/main/java/org/mozilla/fenix/wifi/WifiIntegration.kt rename to app/src/main/java/org/mozilla/fenix/wifi/SitePermissionsWifiIntegration.kt index b81df21fd8..f4aea3bac1 100644 --- a/app/src/main/java/org/mozilla/fenix/wifi/WifiIntegration.kt +++ b/app/src/main/java/org/mozilla/fenix/wifi/SitePermissionsWifiIntegration.kt @@ -5,6 +5,7 @@ package org.mozilla.fenix.wifi import mozilla.components.feature.sitepermissions.SitePermissionsRules +import mozilla.components.support.base.feature.LifecycleAwareFeature import org.mozilla.fenix.settings.PhoneFeature import org.mozilla.fenix.settings.sitepermissions.AUTOPLAY_ALLOW_ON_WIFI import org.mozilla.fenix.settings.sitepermissions.AUTOPLAY_BLOCK_ALL @@ -14,7 +15,10 @@ import org.mozilla.fenix.utils.Settings * Handles implementation details of only setting up a WIFI connectivity listener if the current * user settings require it. */ -class WifiIntegration(private val settings: Settings, private val wifiConnectionMonitor: WifiConnectionMonitor) { +class SitePermissionsWifiIntegration( + private val settings: Settings, + private val wifiConnectionMonitor: WifiConnectionMonitor +) : LifecycleAwareFeature { /** * Adds listener for autplay setting [AUTOPLAY_ALLOW_ON_WIFI]. Sets all autoplay to allowed when @@ -51,7 +55,7 @@ class WifiIntegration(private val settings: Settings, private val wifiConnection // only works while WIFI is active, so we are not using AUTOPLAY_ALLOW_ON_WIFI (or this class). // Once that is fixed, [start] and [maybeAddWifiConnectedListener] will need to be called on // activity startup. - fun start() { wifiConnectionMonitor.start() } + override fun start() { wifiConnectionMonitor.start() } - fun stop() { wifiConnectionMonitor.stop() } + override fun stop() { wifiConnectionMonitor.stop() } }