Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
For #6396 For #6553 - Update sync engine checkboxes and send telemetr…
Browse files Browse the repository at this point in the history
…y once
  • Loading branch information
ekager committed Jan 18, 2020
1 parent e4eba42 commit a1d5880
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/org/mozilla/fenix/settings/LoginsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import mozilla.components.concept.sync.AuthType
import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.service.fxa.SyncEngine
import mozilla.components.service.fxa.manager.SyncEnginesStorage
import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
Expand All @@ -54,6 +55,16 @@ class LoginsFragment : PreferenceFragmentCompat(), AccountObserver {

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.logins_preferences, rootKey)
updatePreferenceVisibilityForFeatureFlags()
}

private fun updatePreferenceVisibilityForFeatureFlags() {
findPreference<SwitchPreference>(getPreferenceKey(R.string.pref_key_autofill_logins))?.apply {
isVisible = FeatureFlags.logins
}
findPreference<Preference>(getPreferenceKey(R.string.pref_key_save_logins_settings))?.apply {
isVisible = FeatureFlags.logins
}
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/org/mozilla/fenix/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
lifecycleScope.launch {
updateAccountUIState(
context = context,
profile = profile ?: context.components.backgroundServices.accountManager.accountProfile()
profile = profile
?: context.components.backgroundServices.accountManager.accountProfile()
)
}
}
Expand Down Expand Up @@ -107,7 +108,11 @@ class SettingsFragment : PreferenceFragmentCompat() {
try {
context?.let { context ->
context.components.analytics.metrics.track(
Event.PreferenceToggled(key, sharedPreferences.getBoolean(key, false), context)
Event.PreferenceToggled(
key,
sharedPreferences.getBoolean(key, false),
context
)
)
}
} catch (e: IllegalArgumentException) {
Expand Down Expand Up @@ -185,7 +190,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<Preference>(getPreferenceKey(pref_key_passwords))?.apply {
isVisible = FeatureFlags.logins
}

findPreference<PreferenceCategory>(getPreferenceKey(R.string.pref_key_advanced))?.apply {
isVisible = FeatureFlags.fenixLanguagePicker
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
val historyNameKey = getPreferenceKey(R.string.pref_key_sync_history)
findPreference<CheckBoxPreference>(historyNameKey)?.apply {
setOnPreferenceChangeListener { _, newValue ->
requireComponents.analytics.metrics.track(Event.PreferenceToggled(
preferenceKey = historyNameKey,
enabled = newValue as Boolean,
context = context
))
SyncEnginesStorage(context).setStatus(SyncEngine.History, newValue)
SyncEnginesStorage(context).setStatus(SyncEngine.History, newValue as Boolean)
@Suppress("DeferredResultUnused")
context.components.backgroundServices.accountManager.syncNowAsync(SyncReason.EngineChange)
true
Expand All @@ -181,12 +176,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
val bookmarksNameKey = getPreferenceKey(R.string.pref_key_sync_bookmarks)
findPreference<CheckBoxPreference>(bookmarksNameKey)?.apply {
setOnPreferenceChangeListener { _, newValue ->
requireComponents.analytics.metrics.track(Event.PreferenceToggled(
preferenceKey = bookmarksNameKey,
enabled = newValue as Boolean,
context = context
))
SyncEnginesStorage(context).setStatus(SyncEngine.Bookmarks, newValue)
SyncEnginesStorage(context).setStatus(SyncEngine.Bookmarks, newValue as Boolean)
@Suppress("DeferredResultUnused")
context.components.backgroundServices.accountManager.syncNowAsync(SyncReason.EngineChange)
true
Expand Down Expand Up @@ -327,13 +317,25 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
}
}

private fun setEnginesDisabledWhileSyncing(isSyncing: Boolean) {
listOf(
R.string.pref_key_sync_bookmarks,
R.string.pref_key_sync_history,
R.string.pref_key_sync_logins
)
.map { getPreferenceKey(it) }
.map { findPreference<CheckBoxPreference>(it) }
.forEach { it?.isEnabled = !isSyncing }
}

private val syncStatusObserver = object : SyncStatusObserver {
override fun onStarted() {
lifecycleScope.launch {
val pref = findPreference<Preference>(getPreferenceKey(R.string.pref_key_sync_now))
view?.announceForAccessibility(getString(R.string.sync_syncing_in_progress))
pref?.title = getString(R.string.sync_syncing_in_progress)
pref?.isEnabled = false
setEnginesDisabledWhileSyncing(true)
}
}

Expand All @@ -350,6 +352,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
}
// Make sure out sync engine checkboxes are up-to-date.
updateSyncEngineStates()
setEnginesDisabledWhileSyncing(false)
}
}

Expand All @@ -359,6 +362,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
val pref = findPreference<Preference>(getPreferenceKey(R.string.pref_key_sync_now))
pref?.let {
pref.title = getString(R.string.preferences_sync_now)
// We want to only enable the sync button, and not the checkboxes here
pref.isEnabled = true

val failedTime = getLastSynced(requireContext())
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/res/xml/logins_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
<!-- 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/. -->
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.Preference
android:key="@string/pref_key_save_logins_settings"
android:summary="@string/preferences_passwords_save_logins_ask_to_save"
android:title="@string/preferences_passwords_save_logins" />
android:title="@string/preferences_passwords_save_logins"
app:isPreferenceVisible="false" />
<SwitchPreference
android:defaultValue="true"
android:key="@string/pref_key_autofill_logins"
android:title="@string/preferences_passwords_autofill" />
android:title="@string/preferences_passwords_autofill"
app:isPreferenceVisible="false" />
<androidx.preference.Preference
android:key="@string/pref_key_password_sync_logins"
android:summary="@string/preferences_passwords_sync_logins_off"
Expand Down

0 comments on commit a1d5880

Please sign in to comment.