This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
/
Settings.kt
241 lines (198 loc) · 9.02 KB
/
Settings.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.PRIVATE
import mozilla.components.feature.sitepermissions.SitePermissionsRules
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.settings.PhoneFeature
import org.mozilla.fenix.settings.sharedpreferences.PreferencesHolder
import org.mozilla.fenix.settings.sharedpreferences.booleanPreference
import java.security.InvalidParameterException
/**
* A simple wrapper for SharedPreferences that makes reading preference a little bit easier.
*/
class Settings private constructor(
context: Context,
private val isCrashReportEnabledInBuild: Boolean
) : PreferencesHolder {
companion object {
const val autoBounceMaximumCount = 2
const val FENIX_PREFERENCES = "fenix_preferences"
private const val BLOCKED_INT = 0
private const val ASK_TO_ALLOW_INT = 1
private fun actionToInt(action: SitePermissionsRules.Action) = when (action) {
SitePermissionsRules.Action.BLOCKED -> BLOCKED_INT
SitePermissionsRules.Action.ASK_TO_ALLOW -> ASK_TO_ALLOW_INT
}
private fun intToAction(action: Int) = when (action) {
BLOCKED_INT -> SitePermissionsRules.Action.BLOCKED
ASK_TO_ALLOW_INT -> SitePermissionsRules.Action.ASK_TO_ALLOW
else -> throw InvalidParameterException("$action is not a valid SitePermissionsRules.Action")
}
@VisibleForTesting
internal var instance: Settings? = null
@JvmStatic
@Synchronized
fun getInstance(
context: Context,
isCrashReportEnabledInBuild: Boolean = BuildConfig.CRASH_REPORTING && Config.channel.isReleased
): Settings {
if (instance == null) {
instance = Settings(context.applicationContext, isCrashReportEnabledInBuild)
}
return instance ?: throw AssertionError("Instance cleared")
}
}
private val appContext = context.applicationContext
override val preferences: SharedPreferences =
appContext.getSharedPreferences(FENIX_PREFERENCES, MODE_PRIVATE)
var usePrivateMode by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_private_mode),
default = false
)
var defaultSearchEngineName: String
get() = preferences.getString(appContext.getPreferenceKey(R.string.pref_key_search_engine), "") ?: ""
set(name) = preferences.edit()
.putString(appContext.getPreferenceKey(R.string.pref_key_search_engine), name)
.apply()
val isCrashReportingEnabled: Boolean
get() = isCrashReportEnabledInBuild &&
preferences.getBoolean(appContext.getPreferenceKey(R.string.pref_key_crash_reporter), true)
val isRemoteDebuggingEnabled by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_remote_debugging),
default = false
)
val isTelemetryEnabled by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_telemetry),
default = true
)
val isExperimentationEnabled by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_experimentation),
default = true
)
val shouldAutoBounceQuickActionSheet: Boolean
get() = autoBounceQuickActionSheetCount < autoBounceMaximumCount
var shouldUseLightTheme by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_light_theme),
default = false
)
var shouldUseAutoSize by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_accessibility_auto_size),
default = true
)
var fontSizeFactor: Float
get() = preferences.getFloat(
appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale),
1f
)
set(value) = preferences.edit()
.putFloat(appContext.getPreferenceKey(R.string.pref_key_accessibility_font_scale), value)
.apply()
val shouldShowVisitedSitesBookmarks by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_show_visited_sites_bookmarks),
default = true
)
val shouldUseDarkTheme by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_dark_theme),
default = false
)
var shouldFollowDeviceTheme by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_follow_device_theme),
default = false
)
var shouldUseTrackingProtection by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_tracking_protection),
default = true
)
val shouldUseAutoBatteryTheme by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_auto_battery_theme),
default = false
)
var shouldDeleteBrowsingDataOnQuit by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_delete_browsing_data_on_quit),
default = false
)
var deleteTabsOnQuit by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_delete_open_tabs_on_quit),
default = false
)
var deleteHistoryOnQuit by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_delete_browsing_history_on_quit),
default = false
)
var deleteCookiesOnQuit by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_delete_cookies_on_quit),
default = false
)
var deleteCacheOnQuit by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_delete_caches_on_quit),
default = false
)
var deletePermissionsOnQuit by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_delete_permissions_on_quit),
default = false
)
fun shouldDeleteAnyDataOnQuit(): Boolean {
return deleteCacheOnQuit ||
deleteCookiesOnQuit ||
deleteHistoryOnQuit ||
deletePermissionsOnQuit ||
deleteTabsOnQuit
}
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)
}
@VisibleForTesting(otherwise = PRIVATE)
internal val autoBounceQuickActionSheetCount: Int
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()
}
val showSearchSuggestions by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_show_search_suggestions),
default = true
)
fun getSitePermissionsPhoneFeatureAction(feature: PhoneFeature) =
intToAction(preferences.getInt(feature.getPreferenceKey(appContext), ASK_TO_ALLOW_INT))
fun setSitePermissionsPhoneFeatureAction(feature: PhoneFeature, value: SitePermissionsRules.Action) {
preferences.edit().putInt(feature.getPreferenceKey(appContext), actionToInt(value)).apply()
}
fun getSitePermissionsCustomSettingsRules(): SitePermissionsRules {
return SitePermissionsRules(
notification = getSitePermissionsPhoneFeatureAction(PhoneFeature.NOTIFICATION),
microphone = getSitePermissionsPhoneFeatureAction(PhoneFeature.MICROPHONE),
location = getSitePermissionsPhoneFeatureAction(PhoneFeature.LOCATION),
camera = getSitePermissionsPhoneFeatureAction(PhoneFeature.CAMERA)
)
}
var fxaSignedIn by booleanPreference(appContext.getPreferenceKey(R.string.pref_key_fxa_signed_in), default = true)
var fxaHasSyncedItems by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_fxa_has_synced_items),
default = true
)
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
get() = 0 < preferences.getInt(appContext.getPreferenceKey(R.string.pref_key_search_widget_installed), 0)
}