Skip to content

Commit

Permalink
FxA WebChannels integration
Browse files Browse the repository at this point in the history
This patch includes:
- WebChannels support enabled by default, with ability to disable it via remote flag
- expanded FxA telemetry (closes mozilla-mobile#4971)

Co-authored-by: Arturo Mejia <arturomejiamarmol@gmail.com>
  • Loading branch information
2 people authored and ekager committed Sep 30, 2019
1 parent 8f97d24 commit 6fea12b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import mozilla.components.browser.search.SearchEngine
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.concept.engine.EngineView
import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.support.base.feature.BackHandler
import mozilla.components.support.ktx.kotlin.isUrl
import mozilla.components.support.ktx.kotlin.toNormalizedUrl
Expand Down Expand Up @@ -112,7 +113,7 @@ open class HomeActivity : AppCompatActivity() {
accountManager.initAsync().await()
// If we're authenticated, kick-off a sync and a device state refresh.
accountManager.authenticatedAccount()?.let {
accountManager.syncNowAsync(startup = true, debounce = true)
accountManager.syncNowAsync(SyncReason.Startup, debounce = true)
it.deviceConstellation().pollForEventsAsync().await()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import mozilla.components.feature.accounts.FxaCapability
import mozilla.components.feature.accounts.FxaWebChannelFeature
import mozilla.components.feature.app.links.AppLinksFeature
import mozilla.components.feature.contextmenu.ContextMenuFeature
Expand Down Expand Up @@ -380,7 +381,8 @@ abstract class BaseBrowserFragment : Fragment(), BackHandler, SessionManager.Obs
customTabSessionId,
requireComponents.core.engine,
requireComponents.core.sessionManager,
requireComponents.backgroundServices.accountManager
requireComponents.backgroundServices.accountManager,
setOf(FxaCapability.CHOOSE_WHAT_TO_SYNC)
),
owner = this,
view = view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class BackgroundServices(
val syncConfig = if (context.isInExperiment(Experiments.asFeatureSyncDisabled)) {
null
} else {
SyncConfig(setOf(SyncEngine.HISTORY, SyncEngine.BOOKMARKS), syncPeriodInMinutes = 240L) // four hours
SyncConfig(setOf(SyncEngine.History, SyncEngine.Bookmarks), syncPeriodInMinutes = 240L) // four hours
}

private val pushService by lazy { FirebasePush() }
Expand All @@ -103,8 +103,8 @@ class BackgroundServices(

init {
// Make the "history" and "bookmark" stores accessible to workers spawned by the sync manager.
GlobalSyncableStoreProvider.configureStore(SyncEngine.HISTORY to historyStorage)
GlobalSyncableStoreProvider.configureStore(SyncEngine.BOOKMARKS to bookmarkStorage)
GlobalSyncableStoreProvider.configureStore(SyncEngine.History to historyStorage)
GlobalSyncableStoreProvider.configureStore(SyncEngine.Bookmarks to bookmarkStorage)
}

private val deviceEventObserver = object : DeviceEventsObserver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.preference.CheckBoxPreference
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
Expand All @@ -22,13 +23,17 @@ import mozilla.components.concept.sync.AccountObserver
import mozilla.components.concept.sync.ConstellationState
import mozilla.components.concept.sync.DeviceConstellationObserver
import mozilla.components.lib.state.ext.consumeFrom
import mozilla.components.service.fxa.SyncEngine
import mozilla.components.service.fxa.manager.FxaAccountManager
import mozilla.components.service.fxa.manager.SyncEnginesStorage
import mozilla.components.service.fxa.sync.SyncReason
import mozilla.components.service.fxa.sync.SyncStatusObserver
import mozilla.components.service.fxa.sync.getLastSynced
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
import org.mozilla.fenix.components.StoreProvider
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getPreferenceKey
import org.mozilla.fenix.ext.requireComponents

Expand Down Expand Up @@ -147,6 +152,27 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
}
}

// Make sure out sync engine checkboxes are up-to-date.
updateSyncEngineStates()

val historyNameKey = getPreferenceKey(R.string.pref_key_sync_history)
findPreference<CheckBoxPreference>(historyNameKey)?.apply {
setOnPreferenceChangeListener { _, newValue ->
SyncEnginesStorage(context).setStatus(SyncEngine.History, newValue as Boolean)
context.components.backgroundServices.accountManager.syncNowAsync(SyncReason.EngineChange)
true
}
}

val bookmarksNameKey = getPreferenceKey(R.string.pref_key_sync_bookmarks)
findPreference<CheckBoxPreference>(bookmarksNameKey)?.apply {
setOnPreferenceChangeListener { _, newValue ->
SyncEnginesStorage(context).setStatus(SyncEngine.Bookmarks, newValue as Boolean)
context.components.backgroundServices.accountManager.syncNowAsync(SyncReason.EngineChange)
true
}
}

deviceConstellation?.registerDeviceObserver(deviceConstellationObserver, owner = this, autoPause = true)

// NB: ObserverRegistry will take care of cleaning up internal references to 'observer' and
Expand All @@ -156,11 +182,25 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
)
}

private fun updateSyncEngineStates() {
val syncEnginesStatus = SyncEnginesStorage(context!!).getStatus()
val bookmarksNameKey = getPreferenceKey(R.string.pref_key_sync_bookmarks)
findPreference<CheckBoxPreference>(bookmarksNameKey)?.apply {
isEnabled = syncEnginesStatus.containsKey(SyncEngine.Bookmarks)
isChecked = syncEnginesStatus.getOrElse(SyncEngine.Bookmarks) { true }
}
val historyNameKey = getPreferenceKey(R.string.pref_key_sync_history)
findPreference<CheckBoxPreference>(historyNameKey)?.apply {
isEnabled = syncEnginesStatus.containsKey(SyncEngine.History)
isChecked = syncEnginesStatus.getOrElse(SyncEngine.History) { true }
}
}

private fun syncNow() {
lifecycleScope.launch {
requireComponents.analytics.metrics.track(Event.SyncAccountSyncNow)
// Trigger a sync.
requireComponents.backgroundServices.accountManager.syncNowAsync().await()
requireComponents.backgroundServices.accountManager.syncNowAsync(SyncReason.User).await()
// Poll for device events & update devices.
accountManager.authenticatedAccount()
?.deviceConstellation()?.run {
Expand All @@ -176,10 +216,12 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
}
// This may fail, and we'll have a disparity in the UI until `updateDeviceName` is called.
lifecycleScope.launch(Main) {
accountManager.authenticatedAccount()
?.deviceConstellation()
?.setDeviceNameAsync(newValue)
?.await()
context?.let {
accountManager.authenticatedAccount()
?.deviceConstellation()
?.setDeviceNameAsync(newValue, it)
?.await()
}
}
return true
}
Expand Down Expand Up @@ -229,6 +271,8 @@ class AccountSettingsFragment : PreferenceFragmentCompat() {
val time = getLastSynced(requireContext())
accountSettingsStore.dispatch(AccountSettingsFragmentAction.SyncEnded(time))
}
// Make sure out sync engine checkboxes are up-to-date.
updateSyncEngineStates()
}
}

Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/xml/account_settings_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
<androidx.preference.CheckBoxPreference
android:key="@string/pref_key_sync_bookmarks"
android:defaultValue="true"
android:enabled="false"
android:title="@string/preferences_sync_bookmarks" />

<androidx.preference.CheckBoxPreference
android:key="@string/pref_key_sync_history"
android:defaultValue="true"
android:enabled="false"
android:title="@string/preferences_sync_history" />

<androidx.preference.EditTextPreference
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object Versions {
// that we depend on directly for the fenix-megazord (and for it's
// forUnitTest variant), and it's important that it be kept in
// sync with the version used by android-components above.
const val mozilla_appservices = "0.39.1"
const val mozilla_appservices = "0.40.0"

const val autodispose = "1.1.0"
const val adjust = "4.11.4"
Expand Down

0 comments on commit 6fea12b

Please sign in to comment.