From ccb532bb77e53148a416608f268dd0840436658d Mon Sep 17 00:00:00 2001 From: Tiger Oakes Date: Mon, 22 Jun 2020 17:38:58 -0700 Subject: [PATCH] For #11534 - Don't check lastUsed mode for screenshots (#11827) --- app/build.gradle | 1 + .../java/org/mozilla/fenix/HomeActivity.kt | 3 - .../fenix/browser/BaseBrowserFragment.kt | 15 +++++ .../java/org/mozilla/fenix/ext/Activity.kt | 17 ----- .../org/mozilla/fenix/home/HomeFragment.kt | 14 +++- .../fenix/settings/PrivateBrowsingFragment.kt | 8 +-- .../org/mozilla/fenix/ext/ActivityTest.kt | 66 ------------------- buildSrc/src/main/java/Dependencies.kt | 1 + 8 files changed, 30 insertions(+), 95 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d87dabb6f..1d0084bc1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -484,6 +484,7 @@ dependencies { implementation Deps.mozilla_feature_media implementation Deps.mozilla_feature_prompts implementation Deps.mozilla_feature_push + implementation Deps.mozilla_feature_privatemode implementation Deps.mozilla_feature_pwa implementation Deps.mozilla_feature_qr implementation Deps.mozilla_feature_search diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index da7c6d81a..190a4cc34 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -58,7 +58,6 @@ import org.mozilla.fenix.components.metrics.BreadcrumbsRecorder import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.exceptions.ExceptionsFragmentDirections import org.mozilla.fenix.ext.alreadyOnDestination -import org.mozilla.fenix.ext.checkAndUpdateScreenshotPermission import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.settings @@ -140,7 +139,6 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { components.publicSuffixList.prefetch() setupThemeAndBrowsingMode(getModeFromIntentOrLastKnown(intent)) - checkAndUpdateScreenshotPermission(settings()) setContentView(R.layout.activity_home) // Must be after we set the content view @@ -182,7 +180,6 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { @CallSuper override fun onResume() { - checkAndUpdateScreenshotPermission(settings()) super.onResume() components.backgroundServices.accountManagerAvailableQueue.runIfReadyOrQueue { 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 d90f4e6f3..31a49a972 100644 --- a/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/browser/BaseBrowserFragment.kt @@ -23,6 +23,7 @@ import androidx.navigation.fragment.findNavController import com.google.android.material.snackbar.Snackbar import kotlinx.android.synthetic.main.fragment_browser.* import kotlinx.android.synthetic.main.fragment_browser.view.* +import kotlinx.android.synthetic.main.fragment_installed_add_on_details.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -52,6 +53,7 @@ import mozilla.components.feature.downloads.DownloadsFeature import mozilla.components.feature.downloads.manager.FetchDownloadManager import mozilla.components.feature.intent.ext.EXTRA_SESSION_ID import mozilla.components.feature.media.fullscreen.MediaFullscreenOrientationFeature +import mozilla.components.feature.privatemode.feature.SecureWindowFeature import mozilla.components.feature.prompts.PromptFeature import mozilla.components.feature.prompts.share.ShareDelegate import mozilla.components.feature.readerview.ReaderViewFeature @@ -144,6 +146,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session private val swipeRefreshFeature = ViewBoundFeatureWrapper() private val webchannelIntegration = ViewBoundFeatureWrapper() private val sitePermissionWifiIntegration = ViewBoundFeatureWrapper() + private val secureWindowFeature = ViewBoundFeatureWrapper() private var fullScreenMediaFeature = ViewBoundFeatureWrapper() private var pipFeature: PictureInPictureFeature? = null @@ -313,6 +316,18 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session view = view ) + val allowScreenshotsInPrivateMode = context.settings().allowScreenshotsInPrivateMode + secureWindowFeature.set( + feature = SecureWindowFeature( + window = requireActivity().window, + store = store, + customTabId = customTabSessionId, + isSecure = { !allowScreenshotsInPrivateMode && it.content.private } + ), + owner = this, + view = view + ) + fullScreenMediaFeature.set( feature = MediaFullscreenOrientationFeature( requireActivity(), diff --git a/app/src/main/java/org/mozilla/fenix/ext/Activity.kt b/app/src/main/java/org/mozilla/fenix/ext/Activity.kt index 246e77113..7dd2b3dce 100644 --- a/app/src/main/java/org/mozilla/fenix/ext/Activity.kt +++ b/app/src/main/java/org/mozilla/fenix/ext/Activity.kt @@ -7,7 +7,6 @@ package org.mozilla.fenix.ext import android.app.Activity import android.view.View import android.view.WindowManager -import org.mozilla.fenix.utils.Settings /** * Attempts to call immersive mode using the View to hide the status bar and navigation buttons. @@ -23,19 +22,3 @@ fun Activity.enterToImmersiveMode() { or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) } - -/** - * Prevents or allows screenshots from being taken in private mode based on the user preferences. - * - * The default setting is set to true (screenshots are allowed to be taken in private mode), as - * described in #2768 - */ -fun Activity.checkAndUpdateScreenshotPermission(settings: Settings) { - if (!settings.allowScreenshotsInPrivateMode && - settings.lastKnownMode.isPrivate - ) { - window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) - } else { - window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) - } -} diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 7fb414bdd..5967f7ba0 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -11,6 +11,7 @@ import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.os.StrictMode +import android.view.Display.FLAG_SECURE import android.view.Gravity import android.view.LayoutInflater import android.view.View @@ -47,6 +48,7 @@ import kotlinx.android.synthetic.main.fragment_home.* import kotlinx.android.synthetic.main.fragment_home.view.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -84,10 +86,10 @@ import org.mozilla.fenix.ext.hideToolbar import org.mozilla.fenix.ext.metrics import org.mozilla.fenix.ext.nav import org.mozilla.fenix.ext.requireComponents +import org.mozilla.fenix.ext.resetPoliciesAfter import org.mozilla.fenix.ext.sessionsOfType import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.toTab -import org.mozilla.fenix.ext.resetPoliciesAfter import org.mozilla.fenix.home.sessioncontrol.DefaultSessionControlController import org.mozilla.fenix.home.sessioncontrol.SessionControlInteractor import org.mozilla.fenix.home.sessioncontrol.SessionControlView @@ -106,7 +108,8 @@ import java.lang.ref.WeakReference import kotlin.math.abs import kotlin.math.min -@SuppressWarnings("TooManyFunctions", "LargeClass") +@ExperimentalCoroutinesApi +@Suppress("TooManyFunctions", "LargeClass") class HomeFragment : Fragment() { private val args by navArgs() @@ -382,6 +385,12 @@ class HomeFragment : Fragment() { view.toolbar_wrapper?.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED) } } + + if (browsingModeManager.mode.isPrivate) { + requireActivity().window.addFlags(FLAG_SECURE) + } else { + requireActivity().window.clearFlags(FLAG_SECURE) + } } override fun onDestroyView() { @@ -389,6 +398,7 @@ class HomeFragment : Fragment() { _sessionControlInteractor = null sessionControlView = null requireView().homeAppBar.removeOnOffsetChangedListener(homeAppBarOffSetListener) + requireActivity().window.clearFlags(FLAG_SECURE) } override fun onStart() { diff --git a/app/src/main/java/org/mozilla/fenix/settings/PrivateBrowsingFragment.kt b/app/src/main/java/org/mozilla/fenix/settings/PrivateBrowsingFragment.kt index da2d9c2e4..ff0515af0 100644 --- a/app/src/main/java/org/mozilla/fenix/settings/PrivateBrowsingFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/settings/PrivateBrowsingFragment.kt @@ -11,7 +11,6 @@ import androidx.preference.SwitchPreference import org.mozilla.fenix.R import org.mozilla.fenix.components.PrivateShortcutCreateManager import org.mozilla.fenix.components.metrics.Event -import org.mozilla.fenix.ext.checkAndUpdateScreenshotPermission import org.mozilla.fenix.ext.metrics import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.showToolbar @@ -45,12 +44,7 @@ class PrivateBrowsingFragment : PreferenceFragmentCompat() { } requirePreference(R.string.pref_key_allow_screenshots_in_private_mode).apply { - onPreferenceChangeListener = object : SharedPreferenceUpdater() { - override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean { - return super.onPreferenceChange(preference, newValue).also { - requireActivity().checkAndUpdateScreenshotPermission(requireActivity().settings()) } - } - } + onPreferenceChangeListener = SharedPreferenceUpdater() } } } diff --git a/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt b/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt index dfb2816a3..4d074780e 100644 --- a/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt +++ b/app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt @@ -7,13 +7,10 @@ package org.mozilla.fenix.ext import android.app.Activity import android.view.View import android.view.WindowManager -import mozilla.components.support.test.robolectric.testContext import org.junit.Assert.assertEquals -import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith -import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.helpers.FenixRobolectricTestRunner import org.robolectric.Robolectric import org.robolectric.Shadows.shadowOf @@ -42,67 +39,4 @@ class ActivityTest { for (f in flags) assertEquals(f, window.decorView.systemUiVisibility and f) assertTrue(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)) } - - @Test - fun `testCheckAndUpdateScreenshotPermission adds flag in private mode when screenshots are not allowed `() { - // given - val activity = Robolectric.buildActivity(Activity::class.java).create().get() - val window = activity.window - testContext.settings().lastKnownMode = BrowsingMode.Private - testContext.settings().allowScreenshotsInPrivateMode = false - - // when - activity.checkAndUpdateScreenshotPermission(activity.settings()) - - // then - assertTrue(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_SECURE)) - } - - @Test - fun `testCheckAndUpdateScreenshotPermission removes flag in private mode when screenshots are allowed `() { - // given - val activity = Robolectric.buildActivity(Activity::class.java).create().get() - val window = activity.window - window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) - testContext.settings().lastKnownMode = BrowsingMode.Private - testContext.settings().allowScreenshotsInPrivateMode = true - - // when - activity.checkAndUpdateScreenshotPermission(activity.settings()) - - // then - assertFalse(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_SECURE)) - } - - @Test - fun `testCheckAndUpdateScreenshotPermission removes flag in normal mode when screenshots are allowed `() { - // given - val activity = Robolectric.buildActivity(Activity::class.java).create().get() - val window = activity.window - window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) - testContext.settings().lastKnownMode = BrowsingMode.Normal - testContext.settings().allowScreenshotsInPrivateMode = true - - // when - activity.checkAndUpdateScreenshotPermission(activity.settings()) - - // then - assertFalse(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_SECURE)) - } - - @Test - fun `testCheckAndUpdateScreenshotPermission removes flag when in normal mode screenshots are not allowed `() { - // given - val activity = Robolectric.buildActivity(Activity::class.java).create().get() - val window = activity.window - window.addFlags(WindowManager.LayoutParams.FLAG_SECURE) - testContext.settings().lastKnownMode = BrowsingMode.Normal - testContext.settings().allowScreenshotsInPrivateMode = false - - // when - activity.checkAndUpdateScreenshotPermission(activity.settings()) - - // then - assertFalse(shadowOf(window).getFlag(WindowManager.LayoutParams.FLAG_SECURE)) - } } diff --git a/buildSrc/src/main/java/Dependencies.kt b/buildSrc/src/main/java/Dependencies.kt index 49b29ca24..43b516876 100644 --- a/buildSrc/src/main/java/Dependencies.kt +++ b/buildSrc/src/main/java/Dependencies.kt @@ -102,6 +102,7 @@ object Deps { const val mozilla_feature_storage = "org.mozilla.components:feature-storage:${Versions.mozilla_android_components}" const val mozilla_feature_prompts = "org.mozilla.components:feature-prompts:${Versions.mozilla_android_components}" const val mozilla_feature_push = "org.mozilla.components:feature-push:${Versions.mozilla_android_components}" + const val mozilla_feature_privatemode = "org.mozilla.components:feature-privatemode:${Versions.mozilla_android_components}" const val mozilla_feature_pwa = "org.mozilla.components:feature-pwa:${Versions.mozilla_android_components}" const val mozilla_feature_toolbar = "org.mozilla.components:feature-toolbar:${Versions.mozilla_android_components}" const val mozilla_feature_findinpage = "org.mozilla.components:feature-findinpage:${Versions.mozilla_android_components}"