1
0
Fork 0

For #6304 & #7577: Persist private mode between app launches

master
Sawyer Blatz 2020-01-09 11:28:21 -08:00 committed by Emily Kager
parent 0a412a1a6a
commit a58decd708
5 changed files with 67 additions and 11 deletions

View File

@ -54,10 +54,10 @@ import org.mozilla.fenix.library.history.HistoryFragmentDirections
import org.mozilla.fenix.onboarding.FenixOnboarding
import org.mozilla.fenix.perf.HotStartPerformanceMonitor
import org.mozilla.fenix.search.SearchFragmentDirections
import org.mozilla.fenix.settings.about.AboutFragmentDirections
import org.mozilla.fenix.settings.DefaultBrowserSettingsFragmentDirections
import org.mozilla.fenix.settings.SettingsFragmentDirections
import org.mozilla.fenix.settings.TrackingProtectionFragmentDirections
import org.mozilla.fenix.settings.about.AboutFragmentDirections
import org.mozilla.fenix.theme.DefaultThemeManager
import org.mozilla.fenix.theme.ThemeManager
import org.mozilla.fenix.utils.BrowsersCache
@ -88,13 +88,10 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
final override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mode = getPrivateModeFromIntent(intent)
components.publicSuffixList.prefetch()
setupThemeAndBrowsingMode(mode)
setupThemeAndBrowsingMode(getModeFromIntentOrLastKnown(intent))
setContentView(R.layout.activity_home)
setupToolbarAndNavigation()
if (intent.getBooleanExtra(EXTRA_FINISH_ONBOARDING, false)) {
@ -159,7 +156,7 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
val intentProcessors = listOf(CrashReporterIntentProcessor()) + externalSourceIntentProcessors
intentProcessors.any { it.process(intent, navHost.navController, this.intent) }
browsingModeManager.mode = getPrivateModeFromIntent(intent)
browsingModeManager.mode = getModeFromIntentOrLastKnown(intent)
}
/**
@ -200,21 +197,21 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
/**
* External sources such as 3rd party links and shortcuts use this function to enter
* private mode directly before the content view is created.
* private mode directly before the content view is created. Returns the mode set by the intent
* otherwise falls back to the last known mode.
*/
private fun getPrivateModeFromIntent(intent: Intent?): BrowsingMode {
internal fun getModeFromIntentOrLastKnown(intent: Intent?): BrowsingMode {
intent?.toSafeIntent()?.let {
if (it.hasExtra(PRIVATE_BROWSING_MODE)) {
val startPrivateMode = it.getBooleanExtra(PRIVATE_BROWSING_MODE, false)
intent.removeExtra(PRIVATE_BROWSING_MODE)
return BrowsingMode.fromBoolean(isPrivate = startPrivateMode)
}
}
return BrowsingMode.Normal
return settings().lastKnownMode
}
private fun setupThemeAndBrowsingMode(mode: BrowsingMode) {
settings().lastKnownMode = mode
browsingModeManager = createBrowsingModeManager(mode)
themeManager = createThemeManager()
themeManager.setActivityTheme(this)

View File

@ -4,6 +4,8 @@
package org.mozilla.fenix.browser.browsingmode
import org.mozilla.fenix.utils.Settings
/**
* Enum that represents whether or not private browsing is active.
*/
@ -42,5 +44,6 @@ class DefaultBrowsingModeManager(
set(value) {
_mode = value
modeDidChange(value)
Settings.instance?.lastKnownMode = value
}
}

View File

@ -24,6 +24,7 @@ import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MozillaProductDetector
import org.mozilla.fenix.ext.components
@ -209,6 +210,32 @@ class Settings private constructor(
return touchExplorationIsEnabled || switchServiceIsEnabled
}
var lastKnownMode: BrowsingMode = BrowsingMode.Normal
get() {
val lastKnownModeWasPrivate = preferences.getBoolean(
appContext.getPreferenceKey(R.string.pref_key_last_known_mode_private),
false
)
return if (lastKnownModeWasPrivate) {
BrowsingMode.Private
} else {
BrowsingMode.Normal
}
}
set(value) {
val lastKnownModeWasPrivate = (value == BrowsingMode.Private)
preferences.edit()
.putBoolean(
appContext.getPreferenceKey(R.string.pref_key_last_known_mode_private),
lastKnownModeWasPrivate)
.apply()
field = value
}
var shouldDeleteBrowsingDataOnQuit by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_delete_browsing_data_on_quit),
default = false

View File

@ -26,6 +26,8 @@
<string name="pref_key_delete_caches_on_quit" translatable="false">pref_key_delete_caches_on_quit</string>
<string name="pref_key_delete_permissions_on_quit" translatable="false">pref_key_delete_permissions_on_quit</string>
<string name="pref_key_delete_browsing_data_on_quit_categories" translatable="false">pref_key_delete_browsing_data_on_quit_categories</string>
<string name="pref_key_last_known_mode_private" translatable="false">pref_key_last_known_mode_private</string>
<string name="pref_key_last_maintenance" translatable="false">pref_key_last_maintenance</string>
<string name="pref_key_help" translatable="false">pref_key_help</string>

View File

@ -5,12 +5,17 @@
package org.mozilla.fenix
import android.content.Intent
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.utils.toSafeIntent
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.HomeActivity.Companion.PRIVATE_BROWSING_MODE
import org.mozilla.fenix.browser.browsingmode.BrowsingMode
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.settings
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
@ -33,4 +38,26 @@ class HomeActivityTest {
val otherIntent = Intent().toSafeIntent()
assertNull(activity.getIntentSource(otherIntent))
}
@Test
fun `getModeFromIntentOrLastKnown returns mode from settings when intent does not set`() {
val activity = HomeActivity()
testContext.settings().lastKnownMode = BrowsingMode.Private
assertEquals(testContext.settings().lastKnownMode, activity.getModeFromIntentOrLastKnown(null))
}
@Test
fun `getModeFromIntentOrLastKnown returns mode from intent when set`() {
val activity = HomeActivity()
testContext.settings().lastKnownMode = BrowsingMode.Normal
val intent = Intent()
intent.putExtra(PRIVATE_BROWSING_MODE, true)
assertNotEquals(testContext.settings().lastKnownMode, activity.getModeFromIntentOrLastKnown(intent))
assertEquals(BrowsingMode.Private, activity.getModeFromIntentOrLastKnown(intent))
}
}