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

View File

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

View File

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

View File

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

View File

@ -4,13 +4,14 @@
package org.mozilla.fenix.settings.quicksettings
import android.content.Context
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.view.ContextThemeWrapper
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.preference.PreferenceManager
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -18,6 +19,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import mozilla.components.feature.sitepermissions.SitePermissions
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.browser.BrowserFragment
import org.mozilla.fenix.ext.components
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.getManagedEmitter
import org.mozilla.fenix.settings.PhoneFeature
import org.mozilla.fenix.utils.Settings
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_IS_SECURED = "KEY_IS_SECURED"
@ -133,6 +134,23 @@ class QuickSettingsSheetDialogFragment : BottomSheetDialogFragment(), CoroutineS
private fun arePermissionsGranted(requestCode: Int, grantResults: IntArray) =
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() {
super.onResume()
getAutoDisposeObservable<QuickSettingsAction>()
@ -150,24 +168,7 @@ class QuickSettingsSheetDialogFragment : BottomSheetDialogFragment(), CoroutineS
}
is QuickSettingsAction.ToggleTrackingProtection -> {
val trackingEnabled = it.trackingProtection
with(requireComponents.core) {
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
)
}
}
}
context?.let { toggleTrackingProtection(trackingEnabled, it) }
launch(Dispatchers.Main) {
getManagedEmitter<QuickSettingsChange>().onNext(
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/. */
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import android.preference.PreferenceManager
import mozilla.components.feature.sitepermissions.SitePermissionsRules
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.R
@ -21,6 +21,7 @@ class Settings private constructor(context: Context) {
companion object {
const val autoBounceMaximumCount = 2
const val FENIX_PREFERENCES = "fenix_preferences"
var instance: Settings? = null
@ -36,8 +37,15 @@ class Settings private constructor(context: Context) {
private val appContext = context.applicationContext
private val preferences: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
val preferences: SharedPreferences =
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
get() = preferences.getString(appContext.getPreferenceKey(R.string.pref_key_search_engine), "") ?: ""
@ -61,6 +69,13 @@ class Settings private constructor(context: Context) {
false
)
fun setLightTheme(newValue: Boolean) {
preferences.edit().putBoolean(
appContext.getPreferenceKey(R.string.pref_key_light_theme),
newValue
).apply()
}
val shouldShowVisitedSitesBookmarks: Boolean
get() = preferences.getBoolean(
appContext.getPreferenceKey(R.string.pref_key_show_visited_sites_bookmarks),
@ -79,12 +94,26 @@ class Settings private constructor(context: Context) {
false
)
fun setFollowDeviceTheme(newValue: Boolean) {
preferences.edit().putBoolean(
appContext.getPreferenceKey(R.string.pref_key_follow_device_theme),
newValue
).apply()
}
val shouldUseTrackingProtection: Boolean
get() = preferences.getBoolean(
appContext.getPreferenceKey(R.string.pref_key_tracking_protection),
true
)
fun setTrackingProtection(newValue: Boolean) {
preferences.edit().putBoolean(
appContext.getPreferenceKey(R.string.pref_key_tracking_protection),
newValue
).apply()
}
val shouldUseAutoBatteryTheme: Boolean
get() = preferences.getBoolean(
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))
fun incrementAutomaticBounceQuickActionSheetCount() {
preferences.edit().putInt(appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action),
autoBounceQuickActionSheetCount + 1).apply()
preferences.edit().putInt(
appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action),
autoBounceQuickActionSheetCount + 1
).apply()
}
fun setDefaultSearchEngineByName(name: String) {
@ -141,7 +172,7 @@ class Settings private constructor(context: Context) {
fun getSitePermissionsPhoneFeatureMicrophoneAction(): SitePermissionsRules.Action {
return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_microphone), 1)
.toSitePermissionsRulesAction()
.toSitePermissionsRulesAction()
}
fun setSitePermissionsPhoneFeatureNotificationAction(action: SitePermissionsRules.Action) {
@ -152,7 +183,7 @@ class Settings private constructor(context: Context) {
fun getSitePermissionsPhoneFeatureNotificationAction(): SitePermissionsRules.Action {
return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_notification), 1)
.toSitePermissionsRulesAction()
.toSitePermissionsRulesAction()
}
fun setSitePermissionsPhoneFeatureLocation(action: SitePermissionsRules.Action) {
@ -163,7 +194,7 @@ class Settings private constructor(context: Context) {
fun getSitePermissionsPhoneFeatureLocation(): SitePermissionsRules.Action {
return preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_phone_feature_location), 1)
.toSitePermissionsRulesAction()
.toSitePermissionsRulesAction()
}
fun getSitePermissionsCustomSettingsRules(): SitePermissionsRules {