2019-03-06 20:39:38 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.fenix.utils
|
2019-03-06 20:39:38 +01:00
|
|
|
|
|
|
|
import android.content.Context
|
2019-05-02 17:59:13 +02:00
|
|
|
import android.content.Context.MODE_PRIVATE
|
2019-03-06 20:39:38 +01:00
|
|
|
import android.content.SharedPreferences
|
2019-06-08 21:12:44 +02:00
|
|
|
import androidx.annotation.VisibleForTesting
|
|
|
|
import androidx.annotation.VisibleForTesting.PRIVATE
|
2019-03-29 18:57:49 +01:00
|
|
|
import mozilla.components.feature.sitepermissions.SitePermissionsRules
|
2019-03-27 19:04:00 +01:00
|
|
|
import org.mozilla.fenix.BuildConfig
|
2019-05-10 00:05:49 +02:00
|
|
|
import org.mozilla.fenix.Config
|
2019-03-06 20:39:38 +01:00
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.ext.getPreferenceKey
|
2019-08-20 19:47:55 +02:00
|
|
|
import org.mozilla.fenix.settings.sharedpreferences.PreferencesHolder
|
|
|
|
import org.mozilla.fenix.settings.sharedpreferences.booleanPreference
|
|
|
|
import org.mozilla.fenix.settings.sharedpreferences.sitePermissionsRulesActionPreference
|
2019-03-06 20:39:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple wrapper for SharedPreferences that makes reading preference a little bit easier.
|
|
|
|
*/
|
2019-06-08 21:12:44 +02:00
|
|
|
class Settings private constructor(
|
|
|
|
context: Context,
|
|
|
|
private val isCrashReportEnabledInBuild: Boolean
|
2019-08-20 19:47:55 +02:00
|
|
|
) : PreferencesHolder {
|
2019-03-06 20:39:38 +01:00
|
|
|
|
|
|
|
companion object {
|
2019-03-20 17:28:36 +01:00
|
|
|
const val autoBounceMaximumCount = 2
|
2019-05-02 17:59:13 +02:00
|
|
|
const val FENIX_PREFERENCES = "fenix_preferences"
|
2019-03-20 17:28:36 +01:00
|
|
|
|
2019-03-06 20:39:38 +01:00
|
|
|
var instance: Settings? = null
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
@Synchronized
|
2019-06-08 21:12:44 +02:00
|
|
|
fun getInstance(
|
|
|
|
context: Context,
|
|
|
|
isCrashReportEnabledInBuild: Boolean = BuildConfig.CRASH_REPORTING && Config.channel.isReleased
|
|
|
|
): Settings {
|
2019-03-06 20:39:38 +01:00
|
|
|
if (instance == null) {
|
2019-06-08 21:12:44 +02:00
|
|
|
instance = Settings(context.applicationContext, isCrashReportEnabledInBuild)
|
2019-03-06 20:39:38 +01:00
|
|
|
}
|
|
|
|
return instance ?: throw AssertionError("Instance cleared")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private val appContext = context.applicationContext
|
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
override val preferences: SharedPreferences =
|
2019-05-02 17:59:13 +02:00
|
|
|
appContext.getSharedPreferences(FENIX_PREFERENCES, MODE_PRIVATE)
|
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var usePrivateMode by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_private_mode),
|
|
|
|
default = false
|
|
|
|
)
|
2019-05-02 17:59:13 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var defaultSearchEngineName: String
|
2019-03-11 19:27:40 +01:00
|
|
|
get() = preferences.getString(appContext.getPreferenceKey(R.string.pref_key_search_engine), "") ?: ""
|
2019-08-20 19:47:55 +02:00
|
|
|
set(name) = preferences.edit()
|
|
|
|
.putString(appContext.getPreferenceKey(R.string.pref_key_search_engine), name)
|
|
|
|
.apply()
|
2019-03-11 19:27:40 +01:00
|
|
|
|
2019-03-22 18:05:16 +01:00
|
|
|
val isCrashReportingEnabled: Boolean
|
2019-06-08 21:12:44 +02:00
|
|
|
get() = isCrashReportEnabledInBuild &&
|
|
|
|
preferences.getBoolean(appContext.getPreferenceKey(R.string.pref_key_crash_reporter), true)
|
2019-03-22 18:05:16 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
val isRemoteDebuggingEnabled by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_remote_debugging),
|
|
|
|
default = false
|
|
|
|
)
|
2019-03-22 17:18:49 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
val isTelemetryEnabled by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_telemetry),
|
|
|
|
default = true
|
|
|
|
)
|
2019-03-06 20:39:38 +01:00
|
|
|
|
2019-03-20 16:55:24 +01:00
|
|
|
val shouldAutoBounceQuickActionSheet: Boolean
|
2019-03-20 17:28:36 +01:00
|
|
|
get() = autoBounceQuickActionSheetCount < autoBounceMaximumCount
|
2019-03-20 16:55:24 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var shouldUseLightTheme by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_light_theme),
|
|
|
|
default = false
|
|
|
|
)
|
2019-05-24 00:10:35 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var shouldUseAutoSize by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_accessibility_auto_size),
|
|
|
|
default = true
|
|
|
|
)
|
2019-05-24 00:10:35 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var fontSizeFactor: Float
|
2019-05-16 01:56:34 +02:00
|
|
|
get() = preferences.getFloat(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale),
|
|
|
|
1f
|
|
|
|
)
|
2019-08-20 19:47:55 +02:00
|
|
|
set(value) = preferences.edit()
|
|
|
|
.putFloat(appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale), value)
|
|
|
|
.apply()
|
2019-05-16 01:56:34 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
val shouldShowVisitedSitesBookmarks by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_show_visited_sites_bookmarks),
|
|
|
|
default = true
|
|
|
|
)
|
2019-04-02 02:53:37 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
val shouldUseDarkTheme by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_dark_theme),
|
|
|
|
default = false
|
|
|
|
)
|
2019-05-02 17:59:13 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var shouldFollowDeviceTheme by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_follow_device_theme),
|
|
|
|
default = false
|
|
|
|
)
|
2019-04-12 23:36:46 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var shouldUseTrackingProtection by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_tracking_protection),
|
|
|
|
default = true
|
|
|
|
)
|
2019-05-02 17:59:13 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
val shouldUseAutoBatteryTheme by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_auto_battery_theme),
|
|
|
|
default = false
|
|
|
|
)
|
2019-04-02 02:53:37 +02:00
|
|
|
|
|
|
|
val themeSettingString: String
|
|
|
|
get() = when {
|
|
|
|
shouldFollowDeviceTheme -> appContext.getString(R.string.preference_follow_device_theme)
|
|
|
|
shouldUseAutoBatteryTheme -> appContext.getString(R.string.preference_auto_battery_theme)
|
|
|
|
shouldUseDarkTheme -> appContext.getString(R.string.preference_dark_theme)
|
|
|
|
shouldUseLightTheme -> appContext.getString(R.string.preference_light_theme)
|
|
|
|
else -> appContext.getString(R.string.preference_light_theme)
|
|
|
|
}
|
|
|
|
|
2019-06-08 21:12:44 +02:00
|
|
|
@VisibleForTesting(otherwise = PRIVATE)
|
|
|
|
internal val autoBounceQuickActionSheetCount: Int
|
2019-08-20 19:47:55 +02:00
|
|
|
get() = preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action), 0)
|
2019-03-20 16:55:24 +01:00
|
|
|
|
|
|
|
fun incrementAutomaticBounceQuickActionSheetCount() {
|
2019-05-02 17:59:13 +02:00
|
|
|
preferences.edit().putInt(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_bounce_quick_action),
|
|
|
|
autoBounceQuickActionSheetCount + 1
|
|
|
|
).apply()
|
2019-03-20 16:55:24 +01:00
|
|
|
}
|
2019-03-22 17:18:49 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
val showSearchSuggestions by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_show_search_suggestions),
|
|
|
|
default = true
|
|
|
|
)
|
2019-03-29 18:57:49 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var sitePermissionsPhoneFeatureCameraAction by sitePermissionsRulesActionPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_phone_feature_camera)
|
|
|
|
)
|
2019-03-29 18:57:49 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var sitePermissionsPhoneFeatureMicrophoneAction by sitePermissionsRulesActionPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_phone_feature_microphone)
|
|
|
|
)
|
2019-03-29 18:57:49 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var sitePermissionsPhoneFeatureNotificationAction by sitePermissionsRulesActionPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_phone_feature_notification)
|
|
|
|
)
|
2019-03-29 18:57:49 +01:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var sitePermissionsPhoneFeatureLocation by sitePermissionsRulesActionPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_phone_feature_location)
|
|
|
|
)
|
2019-03-29 18:57:49 +01:00
|
|
|
|
|
|
|
fun getSitePermissionsCustomSettingsRules(): SitePermissionsRules {
|
|
|
|
return SitePermissionsRules(
|
2019-08-20 19:47:55 +02:00
|
|
|
notification = sitePermissionsPhoneFeatureNotificationAction,
|
|
|
|
microphone = sitePermissionsPhoneFeatureMicrophoneAction,
|
|
|
|
location = sitePermissionsPhoneFeatureLocation,
|
|
|
|
camera = sitePermissionsPhoneFeatureCameraAction
|
2019-03-29 18:57:49 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var fxaSignedIn by booleanPreference(appContext.getPreferenceKey(R.string.pref_key_fxa_signed_in), default = true)
|
2019-08-08 00:06:44 +02:00
|
|
|
|
2019-08-20 19:47:55 +02:00
|
|
|
var fxaHasSyncedItems by booleanPreference(
|
|
|
|
appContext.getPreferenceKey(R.string.pref_key_fxa_has_synced_items),
|
|
|
|
default = true
|
|
|
|
)
|
2019-08-08 00:06:44 +02:00
|
|
|
|
2019-08-14 21:00:29 +02:00
|
|
|
fun addSearchWidgetInstalled(count: Int) {
|
|
|
|
val key = appContext.getPreferenceKey(R.string.pref_key_search_widget_installed)
|
|
|
|
val newValue = preferences.getInt(key, 0) + count
|
|
|
|
preferences.edit()
|
|
|
|
.putInt(key, newValue)
|
|
|
|
.apply()
|
|
|
|
}
|
|
|
|
|
|
|
|
val searchWidgetInstalled: Boolean
|
2019-08-20 19:47:55 +02:00
|
|
|
get() = 0 < preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_search_widget_installed), 0)
|
2019-03-06 20:39:38 +01:00
|
|
|
}
|