Skip to content

Commit

Permalink
For mozilla-mobile#11534 - Don't check lastUsed for screenshots
Browse files Browse the repository at this point in the history
  • Loading branch information
NotWoods committed Jun 22, 2020
1 parent 18cc4c9 commit ac693ec
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 94 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -182,7 +180,6 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {

@CallSuper
override fun onResume() {
checkAndUpdateScreenshotPermission(settings())
super.onResume()

components.backgroundServices.accountManagerAvailableQueue.runIfReadyOrQueue {
Expand Down
15 changes: 15 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 @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,6 +146,7 @@ abstract class BaseBrowserFragment : Fragment(), UserInteractionHandler, Session
private val swipeRefreshFeature = ViewBoundFeatureWrapper<SwipeRefreshFeature>()
private val webchannelIntegration = ViewBoundFeatureWrapper<FxaWebChannelFeature>()
private val sitePermissionWifiIntegration = ViewBoundFeatureWrapper<SitePermissionsWifiIntegration>()
private val secureWindowFeature = ViewBoundFeatureWrapper<SecureWindowFeature>()
private var fullScreenMediaFeature = ViewBoundFeatureWrapper<MediaFullscreenOrientationFeature>()
private var pipFeature: PictureInPictureFeature? = null

Expand Down Expand Up @@ -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(),
Expand Down
17 changes: 0 additions & 17 deletions app/src/main/java/org/mozilla/fenix/ext/Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
}
12 changes: 11 additions & 1 deletion app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<HomeFragmentArgs>()

Expand Down Expand Up @@ -382,13 +385,20 @@ 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() {
super.onDestroyView()
_sessionControlInteractor = null
sessionControlView = null
requireView().homeAppBar.removeOnOffsetChangedListener(homeAppBarOffSetListener)
requireActivity().window.clearFlags(FLAG_SECURE)
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,12 +44,7 @@ class PrivateBrowsingFragment : PreferenceFragmentCompat() {
}

requirePreference<SwitchPreference>(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()
}
}
}
66 changes: 0 additions & 66 deletions app/src/test/java/org/mozilla/fenix/ext/ActivityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
1 change: 1 addition & 0 deletions buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit ac693ec

Please sign in to comment.