|
|
@ -15,8 +15,14 @@ import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
import mozilla.components.concept.sync.ConstellationState
|
|
|
|
|
|
|
|
import mozilla.components.concept.sync.DeviceConstellationObserver
|
|
|
|
import mozilla.components.concept.sync.SyncStatusObserver
|
|
|
|
import mozilla.components.concept.sync.SyncStatusObserver
|
|
|
|
import mozilla.components.feature.sync.getLastSynced
|
|
|
|
import mozilla.components.feature.sync.getLastSynced
|
|
|
|
|
|
|
|
import mozilla.components.service.fxa.FxaException
|
|
|
|
|
|
|
|
import mozilla.components.service.fxa.FxaPanicException
|
|
|
|
|
|
|
|
import mozilla.components.service.fxa.manager.FxaAccountManager
|
|
|
|
|
|
|
|
import mozilla.components.support.base.log.logger.Logger
|
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.R
|
|
|
|
import org.mozilla.fenix.ext.getPreferenceKey
|
|
|
|
import org.mozilla.fenix.ext.getPreferenceKey
|
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
|
|
|
import org.mozilla.fenix.ext.requireComponents
|
|
|
@ -27,6 +33,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
|
|
private lateinit var job: Job
|
|
|
|
private lateinit var job: Job
|
|
|
|
override val coroutineContext: CoroutineContext
|
|
|
|
override val coroutineContext: CoroutineContext
|
|
|
|
get() = Dispatchers.Main + job
|
|
|
|
get() = Dispatchers.Main + job
|
|
|
|
|
|
|
|
private lateinit var accountManager: FxaAccountManager
|
|
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
@ -43,6 +50,8 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
setPreferencesFromResource(R.xml.account_settings_preferences, rootKey)
|
|
|
|
setPreferencesFromResource(R.xml.account_settings_preferences, rootKey)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
accountManager = requireComponents.backgroundServices.accountManager
|
|
|
|
|
|
|
|
|
|
|
|
// Sign out
|
|
|
|
// Sign out
|
|
|
|
val signOut = context!!.getPreferenceKey(R.string.pref_key_sign_out)
|
|
|
|
val signOut = context!!.getPreferenceKey(R.string.pref_key_sign_out)
|
|
|
|
val preferenceSignOut = findPreference<Preference>(signOut)
|
|
|
|
val preferenceSignOut = findPreference<Preference>(signOut)
|
|
|
@ -64,6 +73,18 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Device Name
|
|
|
|
|
|
|
|
val deviceConstellation = accountManager.authenticatedAccount()?.deviceConstellation()
|
|
|
|
|
|
|
|
val deviceNameKey = context!!.getPreferenceKey(R.string.pref_key_sync_device_name)
|
|
|
|
|
|
|
|
findPreference<Preference>(deviceNameKey)?.apply {
|
|
|
|
|
|
|
|
onPreferenceChangeListener = getChangeListenerForDeviceName()
|
|
|
|
|
|
|
|
deviceConstellation?.state()?.currentDevice?.let { device ->
|
|
|
|
|
|
|
|
summary = device.displayName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
deviceConstellation?.registerDeviceObserver(deviceConstellationObserver, owner = this, autoPause = true)
|
|
|
|
|
|
|
|
|
|
|
|
// NB: ObserverRegistry will take care of cleaning up internal references to 'observer' and
|
|
|
|
// NB: ObserverRegistry will take care of cleaning up internal references to 'observer' and
|
|
|
|
// 'owner' when appropriate.
|
|
|
|
// 'owner' when appropriate.
|
|
|
|
requireComponents.backgroundServices.syncManager.register(syncStatusObserver, owner = this, autoPause = true)
|
|
|
|
requireComponents.backgroundServices.syncManager.register(syncStatusObserver, owner = this, autoPause = true)
|
|
|
@ -72,7 +93,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
|
|
private fun getClickListenerForSignOut(): Preference.OnPreferenceClickListener {
|
|
|
|
private fun getClickListenerForSignOut(): Preference.OnPreferenceClickListener {
|
|
|
|
return Preference.OnPreferenceClickListener {
|
|
|
|
return Preference.OnPreferenceClickListener {
|
|
|
|
launch {
|
|
|
|
launch {
|
|
|
|
requireComponents.backgroundServices.accountManager.logoutAsync().await()
|
|
|
|
accountManager.logoutAsync().await()
|
|
|
|
Navigation.findNavController(view!!).popBackStack()
|
|
|
|
Navigation.findNavController(view!!).popBackStack()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
true
|
|
|
@ -85,7 +106,7 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
|
|
requireComponents.backgroundServices.syncManager.syncNow()
|
|
|
|
requireComponents.backgroundServices.syncManager.syncNow()
|
|
|
|
// Poll for device events.
|
|
|
|
// Poll for device events.
|
|
|
|
launch {
|
|
|
|
launch {
|
|
|
|
requireComponents.backgroundServices.accountManager.authenticatedAccount()
|
|
|
|
accountManager.authenticatedAccount()
|
|
|
|
?.deviceConstellation()
|
|
|
|
?.deviceConstellation()
|
|
|
|
?.refreshDeviceStateAsync()
|
|
|
|
?.refreshDeviceStateAsync()
|
|
|
|
?.await()
|
|
|
|
?.await()
|
|
|
@ -94,6 +115,30 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun getChangeListenerForDeviceName(): Preference.OnPreferenceChangeListener {
|
|
|
|
|
|
|
|
return Preference.OnPreferenceChangeListener { _, newValue ->
|
|
|
|
|
|
|
|
// Optimistically set the device name to what user requested.
|
|
|
|
|
|
|
|
val deviceNameKey = context!!.getPreferenceKey(R.string.pref_key_sync_device_name)
|
|
|
|
|
|
|
|
val preferenceDeviceName = findPreference<Preference>(deviceNameKey)
|
|
|
|
|
|
|
|
preferenceDeviceName?.summary = newValue as String
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This may fail, and we'll have a disparity in the UI until `updateDeviceName` is called.
|
|
|
|
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
accountManager.authenticatedAccount()?.let {
|
|
|
|
|
|
|
|
it.deviceConstellation().setDeviceNameAsync(newValue)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (e: FxaPanicException) {
|
|
|
|
|
|
|
|
throw e
|
|
|
|
|
|
|
|
} catch (e: FxaException) {
|
|
|
|
|
|
|
|
Logger.error("Setting device name failed.", e)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private val syncStatusObserver = object : SyncStatusObserver {
|
|
|
|
private val syncStatusObserver = object : SyncStatusObserver {
|
|
|
|
override fun onStarted() {
|
|
|
|
override fun onStarted() {
|
|
|
|
CoroutineScope(Dispatchers.Main).launch {
|
|
|
|
CoroutineScope(Dispatchers.Main).launch {
|
|
|
@ -129,6 +174,14 @@ class AccountSettingsFragment : PreferenceFragmentCompat(), CoroutineScope {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val deviceConstellationObserver = object : DeviceConstellationObserver {
|
|
|
|
|
|
|
|
override fun onDevicesUpdate(constellation: ConstellationState) {
|
|
|
|
|
|
|
|
val deviceNameKey = context!!.getPreferenceKey(R.string.pref_key_sync_device_name)
|
|
|
|
|
|
|
|
val preferenceDeviceName = findPreference<Preference>(deviceNameKey)
|
|
|
|
|
|
|
|
preferenceDeviceName?.summary = constellation.currentDevice?.displayName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun updateLastSyncedTimePref(context: Context, pref: Preference, failed: Boolean = false) {
|
|
|
|
fun updateLastSyncedTimePref(context: Context, pref: Preference, failed: Boolean = false) {
|
|
|
|
val lastSyncTime = getLastSynced(context)
|
|
|
|
val lastSyncTime = getLastSynced(context)
|
|
|
|
|
|
|
|
|
|
|
|