1
0
Fork 0

For #2214 - Use Settings + SharedPreferences for Private Mode (#2215)

* For #2214 - Use Settings + SharedPreferences for Private Mode

* Update other values to use SharedPreferences
master
Emily Kager 2019-05-02 08:59:13 -07:00 committed by Colin Lee
parent 2953b54a84
commit 1c7f48be5b
6 changed files with 72 additions and 47 deletions

View File

@ -4,7 +4,7 @@
package org.mozilla.fenix package org.mozilla.fenix
import android.preference.PreferenceManager import org.mozilla.fenix.utils.Settings
interface BrowsingModeManager { interface BrowsingModeManager {
enum class Mode { enum class Mode {
@ -20,8 +20,8 @@ class DefaultBrowsingModeManager(private val homeActivity: HomeActivity) : Brows
get() = temporaryModeStorage!! get() = temporaryModeStorage!!
set(value) { set(value) {
temporaryModeStorage = value temporaryModeStorage = value
updateTheme(value)
setPreference() setPreference()
updateTheme(value)
} }
private fun updateTheme(mode: BrowsingModeManager.Mode) { private fun updateTheme(mode: BrowsingModeManager.Mode) {
@ -35,14 +35,12 @@ class DefaultBrowsingModeManager(private val homeActivity: HomeActivity) : Brows
} }
private fun setPreference() { private fun setPreference() {
PreferenceManager.getDefaultSharedPreferences(homeActivity) Settings.getInstance(homeActivity).setPrivateMode(isPrivate)
.edit().putBoolean(homeActivity.getString(R.string.pref_key_private_mode), isPrivate).apply()
} }
init { init {
if (temporaryModeStorage == null) { if (temporaryModeStorage == null) {
mode = when (PreferenceManager.getDefaultSharedPreferences(homeActivity) mode = when (Settings.getInstance(homeActivity).usePrivateMode) {
.getBoolean(homeActivity.getString(R.string.pref_key_private_mode), false)) {
true -> BrowsingModeManager.Mode.Private true -> BrowsingModeManager.Mode.Private
false -> BrowsingModeManager.Mode.Normal false -> BrowsingModeManager.Mode.Normal
} }

View File

@ -7,12 +7,11 @@ package org.mozilla.fenix
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Application import android.app.Application
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.Deferred
import mozilla.components.concept.fetch.Client import mozilla.components.concept.fetch.Client
import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient
import mozilla.components.service.fretboard.Fretboard import mozilla.components.service.fretboard.Fretboard
@ -212,14 +211,12 @@ open class FenixApplication : Application() {
AppCompatDelegate.setDefaultNightMode( AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
) )
PreferenceManager.getDefaultSharedPreferences(this).edit() Settings.getInstance(this).setFollowDeviceTheme(true)
.putBoolean(getString(R.string.pref_key_follow_device_theme), true).apply()
} else { } else {
AppCompatDelegate.setDefaultNightMode( AppCompatDelegate.setDefaultNightMode(
AppCompatDelegate.MODE_NIGHT_NO AppCompatDelegate.MODE_NIGHT_NO
) )
PreferenceManager.getDefaultSharedPreferences(this).edit() Settings.getInstance(this).setLightTheme(true)
.putBoolean(getString(R.string.pref_key_light_theme), true).apply()
} }
} }
} }

View File

@ -7,7 +7,6 @@ package org.mozilla.fenix
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager
import android.util.AttributeSet import android.util.AttributeSet
import android.view.View import android.view.View
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -33,6 +32,7 @@ import org.mozilla.fenix.library.bookmarks.selectfolder.SelectBookmarkFolderFrag
import org.mozilla.fenix.library.history.HistoryFragmentDirections import org.mozilla.fenix.library.history.HistoryFragmentDirections
import org.mozilla.fenix.search.SearchFragmentDirections import org.mozilla.fenix.search.SearchFragmentDirections
import org.mozilla.fenix.settings.SettingsFragmentDirections import org.mozilla.fenix.settings.SettingsFragmentDirections
import org.mozilla.fenix.utils.Settings
@SuppressWarnings("TooManyFunctions") @SuppressWarnings("TooManyFunctions")
open class HomeActivity : AppCompatActivity() { open class HomeActivity : AppCompatActivity() {
@ -56,8 +56,7 @@ open class HomeActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
themeManager.temporaryThemeManagerStorage = themeManager.temporaryThemeManagerStorage =
when (PreferenceManager.getDefaultSharedPreferences(this) when (Settings.getInstance(this).usePrivateMode) {
.getBoolean(this.getString(R.string.pref_key_private_mode), false)) {
true -> ThemeManager.Theme.Private true -> ThemeManager.Theme.Private
false -> ThemeManager.Theme.Normal false -> ThemeManager.Theme.Normal
} }

View File

@ -7,11 +7,11 @@ package org.mozilla.fenix
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager
import mozilla.components.browser.session.tab.CustomTabConfig import mozilla.components.browser.session.tab.CustomTabConfig
import mozilla.components.support.utils.SafeIntent import mozilla.components.support.utils.SafeIntent
import org.mozilla.fenix.customtabs.CustomTabActivity import org.mozilla.fenix.customtabs.CustomTabActivity
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.utils.Settings
class IntentReceiverActivity : Activity() { class IntentReceiverActivity : Activity() {
@ -23,8 +23,7 @@ class IntentReceiverActivity : Activity() {
// the HomeActivity. // the HomeActivity.
val intent = intent?.let { Intent(intent) } ?: Intent() val intent = intent?.let { Intent(intent) } ?: Intent()
val isPrivate = PreferenceManager val isPrivate = Settings.getInstance(this).usePrivateMode
.getDefaultSharedPreferences(this).getBoolean(getString(R.string.pref_key_private_mode), false)
if (isPrivate) { if (isPrivate) {
components.utils.privateIntentProcessor.process(intent) components.utils.privateIntentProcessor.process(intent)

View File

@ -4,13 +4,14 @@
package org.mozilla.fenix.settings.quicksettings package org.mozilla.fenix.settings.quicksettings
import android.content.Context
import android.content.pm.PackageManager.PERMISSION_GRANTED import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.view.ContextThemeWrapper
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.preference.PreferenceManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -18,6 +19,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import mozilla.components.feature.sitepermissions.SitePermissions import mozilla.components.feature.sitepermissions.SitePermissions
import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.BrowserFragment import org.mozilla.fenix.browser.BrowserFragment
import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.requireComponents
@ -25,9 +27,8 @@ import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.getAutoDisposeObservable import org.mozilla.fenix.mvi.getAutoDisposeObservable
import org.mozilla.fenix.mvi.getManagedEmitter import org.mozilla.fenix.mvi.getManagedEmitter
import org.mozilla.fenix.settings.PhoneFeature import org.mozilla.fenix.settings.PhoneFeature
import org.mozilla.fenix.utils.Settings
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import androidx.appcompat.view.ContextThemeWrapper
import org.mozilla.fenix.R
private const val KEY_URL = "KEY_URL" private const val KEY_URL = "KEY_URL"
private const val KEY_IS_SECURED = "KEY_IS_SECURED" private const val KEY_IS_SECURED = "KEY_IS_SECURED"
@ -133,6 +134,23 @@ class QuickSettingsSheetDialogFragment : BottomSheetDialogFragment(), CoroutineS
private fun arePermissionsGranted(requestCode: Int, grantResults: IntArray) = private fun arePermissionsGranted(requestCode: Int, grantResults: IntArray) =
requestCode == REQUEST_CODE_QUICK_SETTINGS_PERMISSIONS && grantResults.all { it == PERMISSION_GRANTED } requestCode == REQUEST_CODE_QUICK_SETTINGS_PERMISSIONS && grantResults.all { it == PERMISSION_GRANTED }
private fun toggleTrackingProtection(trackingEnabled: Boolean, context: Context) {
with(requireComponents.core) {
val policy =
createTrackingProtectionPolicy(trackingEnabled)
Settings.getInstance(context).setTrackingProtection(trackingEnabled)
engine.settings.trackingProtectionPolicy = policy
with(sessionManager) {
sessions.forEach {
getEngineSession(it)?.enableTrackingProtection(
policy
)
}
}
}
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
getAutoDisposeObservable<QuickSettingsAction>() getAutoDisposeObservable<QuickSettingsAction>()
@ -150,24 +168,7 @@ class QuickSettingsSheetDialogFragment : BottomSheetDialogFragment(), CoroutineS
} }
is QuickSettingsAction.ToggleTrackingProtection -> { is QuickSettingsAction.ToggleTrackingProtection -> {
val trackingEnabled = it.trackingProtection val trackingEnabled = it.trackingProtection
with(requireComponents.core) { context?.let { toggleTrackingProtection(trackingEnabled, it) }
val policy =
createTrackingProtectionPolicy(trackingEnabled)
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putBoolean(
context!!.getString(R.string.pref_key_tracking_protection),
trackingEnabled
).apply()
engine.settings.trackingProtectionPolicy = policy
with(sessionManager) {
sessions.forEach {
getEngineSession(it)?.enableTrackingProtection(
policy
)
}
}
}
launch(Dispatchers.Main) { launch(Dispatchers.Main) {
getManagedEmitter<QuickSettingsChange>().onNext( getManagedEmitter<QuickSettingsChange>().onNext(
QuickSettingsChange.Change( QuickSettingsChange.Change(

View File

@ -5,8 +5,8 @@ package org.mozilla.fenix.utils
file, You can obtain one at http://mozilla.org/MPL/2.0/. */ file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import android.content.Context import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences import android.content.SharedPreferences
import android.preference.PreferenceManager
import mozilla.components.feature.sitepermissions.SitePermissionsRules import mozilla.components.feature.sitepermissions.SitePermissionsRules
import org.mozilla.fenix.BuildConfig import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.R import org.mozilla.fenix.R
@ -21,6 +21,7 @@ class Settings private constructor(context: Context) {
companion object { companion object {
const val autoBounceMaximumCount = 2 const val autoBounceMaximumCount = 2
const val FENIX_PREFERENCES = "fenix_preferences"
var instance: Settings? = null var instance: Settings? = null
@ -36,8 +37,15 @@ class Settings private constructor(context: Context) {
private val appContext = context.applicationContext private val appContext = context.applicationContext
private val preferences: SharedPreferences = val preferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context) appContext.getSharedPreferences(FENIX_PREFERENCES, MODE_PRIVATE)
val usePrivateMode: Boolean
get() = preferences.getBoolean(appContext.getPreferenceKey(R.string.pref_key_private_mode), false)
fun setPrivateMode(newValue: Boolean) {
preferences.edit().putBoolean(appContext.getPreferenceKey(R.string.pref_key_private_mode), newValue).apply()
}
val defaultSearchEngineName: String val defaultSearchEngineName: String
get() = preferences.getString(appContext.getPreferenceKey(R.string.pref_key_search_engine), "") ?: "" get() = preferences.getString(appContext.getPreferenceKey(R.string.pref_key_search_engine), "") ?: ""
@ -61,6 +69,13 @@ class Settings private constructor(context: Context) {
false false
) )
fun setLightTheme(newValue: Boolean) {
preferences.edit().putBoolean(
appContext.getPreferenceKey(R.string.pref_key_light_theme),
newValue
).apply()
}
val shouldShowVisitedSitesBookmarks: Boolean val shouldShowVisitedSitesBookmarks: Boolean
get() = preferences.getBoolean( get() = preferences.getBoolean(
appContext.getPreferenceKey(R.string.pref_key_show_visited_sites_bookmarks), appContext.getPreferenceKey(R.string.pref_key_show_visited_sites_bookmarks),
@ -79,12 +94,26 @@ class Settings private constructor(context: Context) {
false false
) )
fun setFollowDeviceTheme(newValue: Boolean) {
preferences.edit().putBoolean(
appContext.getPreferenceKey(R.string.pref_key_follow_device_theme),
newValue
).apply()
}
val shouldUseTrackingProtection: Boolean val shouldUseTrackingProtection: Boolean
get() = preferences.getBoolean( get() = preferences.getBoolean(
appContext.getPreferenceKey(R.string.pref_key_tracking_protection), appContext.getPreferenceKey(R.string.pref_key_tracking_protection),
true true
) )
fun setTrackingProtection(newValue: Boolean) {
preferences.edit().putBoolean(
appContext.getPreferenceKey(R.string.pref_key_tracking_protection),
newValue
).apply()
}
val shouldUseAutoBatteryTheme: Boolean val shouldUseAutoBatteryTheme: Boolean
get() = preferences.getBoolean( get() = preferences.getBoolean(
appContext.getPreferenceKey(R.string.pref_key_auto_battery_theme), appContext.getPreferenceKey(R.string.pref_key_auto_battery_theme),
@ -107,8 +136,10 @@ class Settings private constructor(context: Context) {
get() = (preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action), 0)) get() = (preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action), 0))
fun incrementAutomaticBounceQuickActionSheetCount() { fun incrementAutomaticBounceQuickActionSheetCount() {
preferences.edit().putInt(appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action), preferences.edit().putInt(
autoBounceQuickActionSheetCount + 1).apply() appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action),
autoBounceQuickActionSheetCount + 1
).apply()
} }
fun setDefaultSearchEngineByName(name: String) { fun setDefaultSearchEngineByName(name: String) {
@ -141,7 +172,7 @@ class Settings private constructor(context: Context) {
fun getSitePermissionsPhoneFeatureMicrophoneAction(): SitePermissionsRules.Action { fun getSitePermissionsPhoneFeatureMicrophoneAction(): SitePermissionsRules.Action {
return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_microphone), 1) return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_microphone), 1)
.toSitePermissionsRulesAction() .toSitePermissionsRulesAction()
} }
fun setSitePermissionsPhoneFeatureNotificationAction(action: SitePermissionsRules.Action) { fun setSitePermissionsPhoneFeatureNotificationAction(action: SitePermissionsRules.Action) {
@ -152,7 +183,7 @@ class Settings private constructor(context: Context) {
fun getSitePermissionsPhoneFeatureNotificationAction(): SitePermissionsRules.Action { fun getSitePermissionsPhoneFeatureNotificationAction(): SitePermissionsRules.Action {
return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_notification), 1) return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_notification), 1)
.toSitePermissionsRulesAction() .toSitePermissionsRulesAction()
} }
fun setSitePermissionsPhoneFeatureLocation(action: SitePermissionsRules.Action) { fun setSitePermissionsPhoneFeatureLocation(action: SitePermissionsRules.Action) {
@ -163,7 +194,7 @@ class Settings private constructor(context: Context) {
fun getSitePermissionsPhoneFeatureLocation(): SitePermissionsRules.Action { fun getSitePermissionsPhoneFeatureLocation(): SitePermissionsRules.Action {
return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_location), 1) return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_location), 1)
.toSitePermissionsRulesAction() .toSitePermissionsRulesAction()
} }
fun getSitePermissionsCustomSettingsRules(): SitePermissionsRules { fun getSitePermissionsCustomSettingsRules(): SitePermissionsRules {