1
0
Fork 0

For #2768 - Prevent screenshots in private mode

Added a new option in Private browsing menu to allow or prevent screenshots from being taken while in private mode by adding or removing the FLAG_SECURE flag from the home activity's window.

 This method is called whenever the activity is initialized to account for the browsing mode being changed and whenever the setting from the Private browsing menu is changed.

 The setting is by default set to true (screenshots are allowed to be taken)
master
Mihai Eduard Badea 2020-04-03 12:19:39 +03:00 committed by Mugurell
parent 29539c458e
commit d66da53c9a
8 changed files with 111 additions and 1 deletions

View File

@ -48,6 +48,7 @@ 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
@ -116,6 +117,7 @@ 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

View File

@ -7,6 +7,7 @@ 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.
@ -22,3 +23,19 @@ 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)
}
}

View File

@ -11,8 +11,10 @@ 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.getPreferenceKey
import org.mozilla.fenix.ext.metrics
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
/**
@ -41,5 +43,15 @@ class PrivateBrowsingFragment : PreferenceFragmentCompat() {
findPreference<SwitchPreference>(getPreferenceKey(R.string.pref_key_open_links_in_a_private_tab))?.apply {
onPreferenceChangeListener = SharedPreferenceUpdater()
}
findPreference<SwitchPreference>(getPreferenceKey
(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()) }
}
}
}
}
}

View File

@ -132,6 +132,11 @@ class Settings private constructor(
default = false
)
var allowScreenshotsInPrivateMode by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_allow_screenshots_in_private_mode),
default = true
)
var defaultSearchEngineName by stringPreference(
appContext.getPreferenceKey(R.string.pref_key_search_engine),
default = ""

View File

@ -142,6 +142,7 @@
<!-- Privacy Settings -->
<string name="pref_key_open_links_in_a_private_tab" translatable="false">pref_key_open_links_in_a_private_tab</string>
<string name="pref_key_open_links_in_external_app" translatable="false">pref_key_open_links_in_external_app</string>
<string name="pref_key_allow_screenshots_in_private_mode" translatable="false">pref_key_allow_screenshots_in_private_mode</string>
<!-- Quick Action Sheet -->
<string name="pref_key_bounce_quick_action" translatable="false">pref_key_bounce_quick_action</string>

View File

@ -203,6 +203,8 @@
<string name="preferences_private_browsing_options">Private browsing</string>
<!-- Preference for opening links in a private tab-->
<string name="preferences_open_links_in_a_private_tab">Open links in a private tab</string>
<!-- Preference for allowing screenshots to be taken while in a private tab-->
<string name="preferences_allow_screenshots_in_private_mode">Allow screenshots in private browsing</string>
<!-- Preference for adding private browsing shortcut -->
<string name="preferences_add_private_browsing_shortcut">Add private browsing shortcut</string>
<!-- Preference for accessibility -->

View File

@ -13,4 +13,9 @@
android:key="@string/pref_key_open_links_in_a_private_tab"
android:title="@string/preferences_open_links_in_a_private_tab"
app:iconSpaceReserved="false" />
<SwitchPreference
android:defaultValue="true"
android:key="@string/pref_key_allow_screenshots_in_private_mode"
android:title="@string/preferences_allow_screenshots_in_private_mode"
app:iconSpaceReserved="false" />
</PreferenceScreen>

View File

@ -7,12 +7,15 @@ 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.robolectric.Robolectric
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.robolectric.Robolectric
import org.robolectric.Shadows.shadowOf
@RunWith(FenixRobolectricTestRunner::class)
@ -39,4 +42,67 @@ 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))
}
}