Add domestic China FxA service for Mozilla Online builds

China fxa related string

Add switch preference in settings to use local/global fxa server

inherit fennec FxA settings

Present or hide fxa switch according to isMozillaonline

Allow China fxa server auto login during migration
upstream-sync
rxu 4 years ago committed by Grisha Kruglov
parent cb62f98717
commit efdb30483a

@ -6,6 +6,7 @@ package org.mozilla.fenix.components
import android.content.Context
import mozilla.components.service.fxa.ServerConfig
import mozilla.components.service.fxa.ServerConfig.Server
import org.mozilla.fenix.Config
import org.mozilla.fenix.ext.settings
/**
@ -17,10 +18,18 @@ object FxaServer {
const val REDIRECT_URL = "urn:ietf:wg:oauth:2.0:oob:oauth-redirect-webchannel"
fun config(context: Context): ServerConfig {
// If a server override is configured, use that. Otherwise:
// - for all channels other than Mozilla Online, use Server.Release.
// - for Mozilla Online channel, if domestic server is allowed, use Server.CHINA; otherwise, use Server.RELEASE
val serverOverride = context.settings().overrideFxAServer
val tokenServerOverride = context.settings().overrideSyncTokenServer.ifEmpty { null }
if (serverOverride.isEmpty()) {
return ServerConfig(Server.RELEASE, CLIENT_ID, REDIRECT_URL, tokenServerOverride)
val releaseServer = if (Config.channel.isMozillaOnline && context.settings().allowDomesticChinaFxaServer) {
Server.CHINA
} else {
Server.RELEASE
}
return ServerConfig(releaseServer, CLIENT_ID, REDIRECT_URL, tokenServerOverride)
}
return ServerConfig(serverOverride, CLIENT_ID, REDIRECT_URL, tokenServerOverride)
}

@ -13,13 +13,13 @@ import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.NavHostFragment.findNavController
import androidx.navigation.fragment.findNavController
import mozilla.components.feature.qr.QrFeature
import mozilla.components.support.base.feature.UserInteractionHandler
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.requireComponents
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar
class PairFragment : Fragment(R.layout.fragment_pair), UserInteractionHandler {
@ -67,7 +67,11 @@ class PairFragment : Fragment(R.layout.fragment_pair), UserInteractionHandler {
false
)
},
scanMessage = R.string.pair_instructions_2
scanMessage =
if (requireContext().settings().allowDomesticChinaFxaServer &&
org.mozilla.fenix.Config.channel.isMozillaOnline)
R.string.pair_instructions_2_cn
else R.string.pair_instructions_2
),
owner = this,
view = view

@ -26,6 +26,7 @@ import androidx.navigation.findNavController
import androidx.navigation.fragment.navArgs
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.amo_collection_override_dialog.view.*
import kotlinx.coroutines.CoroutineScope
@ -92,7 +93,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
scope = lifecycleScope,
accountManager = requireComponents.backgroundServices.accountManager,
httpClient = requireComponents.core.client,
updateFxASyncOverrideMenu = ::updateFxASyncOverrideMenu
updateFxASyncOverrideMenu = ::updateFxASyncOverrideMenu,
updateFxAAllowDomesticChinaServerMenu = :: updateFxAAllowDomesticChinaServerMenu
)
// Observe account changes to keep the UI up-to-date.
@ -447,6 +449,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
setupAmoCollectionOverridePreference(requireContext().settings())
setupAllowDomesticChinaFxaServerPreference()
}
/**
@ -539,6 +542,22 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}
private fun updateFxAAllowDomesticChinaServerMenu() {
val settings = requireContext().settings()
val preferenceAllowDomesticChinaServer =
findPreference<SwitchPreference>(getPreferenceKey(R.string.pref_key_allow_domestic_china_fxa_server))
// Only enable changes to these prefs when the user isn't connected to an account.
val enabled =
requireComponents.backgroundServices.accountManager.authenticatedAccount() == null
val checked = settings.allowDomesticChinaFxaServer
val visible = Config.channel.isMozillaOnline
preferenceAllowDomesticChinaServer?.apply {
isEnabled = enabled
isChecked = checked
isVisible = visible
}
}
private fun updateFxASyncOverrideMenu() {
val preferenceFxAOverride =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_override_fxa_server))
@ -577,6 +596,33 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}
private fun setupAllowDomesticChinaFxaServerPreference() {
val allowDomesticChinaFxAServer = getPreferenceKey(R.string.pref_key_allow_domestic_china_fxa_server)
val preferenceAllowDomesticChinaFxAServer = findPreference<SwitchPreference>(allowDomesticChinaFxAServer)
val visible = Config.channel.isMozillaOnline
preferenceAllowDomesticChinaFxAServer?.apply {
isVisible = visible
}
if (visible) {
preferenceAllowDomesticChinaFxAServer?.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { preference, newValue ->
preference.context.settings().preferences.edit()
.putBoolean(preference.key, newValue as Boolean).apply()
updateFxAAllowDomesticChinaServerMenu()
Toast.makeText(
context,
getString(R.string.toast_override_fxa_sync_server_done),
Toast.LENGTH_LONG
).show()
Handler(Looper.getMainLooper()).postDelayed({
exitProcess(0)
}, FXA_SYNC_OVERRIDE_EXIT_DELAY)
}
}
}
companion object {
private const val REQUEST_CODE_BROWSER_ROLE = 1
private const val SCROLL_INDICATOR_DELAY = 10L

@ -26,7 +26,8 @@ class AccountUiView(
private val scope: CoroutineScope,
private val accountManager: FxaAccountManager,
private val httpClient: Client,
private val updateFxASyncOverrideMenu: () -> Unit
private val updateFxASyncOverrideMenu: () -> Unit,
private val updateFxAAllowDomesticChinaServerMenu: () -> Unit
) {
private val preferenceSignIn =
@ -48,6 +49,7 @@ class AccountUiView(
val account = accountManager.authenticatedAccount()
updateFxASyncOverrideMenu()
updateFxAAllowDomesticChinaServerMenu()
// Signed-in, no problems.
if (account != null && !accountManager.accountNeedsReauth()) {

@ -21,6 +21,7 @@ import mozilla.components.concept.sync.OAuthAccount
import mozilla.components.support.ktx.android.content.hasCamera
import mozilla.components.support.ktx.android.content.isPermissionGranted
import mozilla.components.support.ktx.android.view.hideKeyboard
import org.mozilla.fenix.Config
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.components.FenixSnackbar
@ -114,7 +115,9 @@ class TurnOnSyncFragment : Fragment(), AccountObserver {
view.signInScanButton.setOnClickListener(paringClickListener)
view.signInEmailButton.setOnClickListener(signInClickListener)
view.signInInstructions.text = HtmlCompat.fromHtml(
getString(R.string.sign_in_instructions),
if (requireContext().settings().allowDomesticChinaFxaServer && Config.channel.isMozillaOnline)
getString(R.string.sign_in_instructions_cn)
else getString(R.string.sign_in_instructions),
HtmlCompat.FROM_HTML_MODE_LEGACY
)

@ -887,6 +887,11 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = false
)
var allowDomesticChinaFxaServer by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_allow_domestic_china_fxa_server),
default = true
)
var overrideFxAServer by stringPreference(
appContext.getPreferenceKey(R.string.pref_key_override_fxa_server),
default = ""

@ -27,4 +27,13 @@
<string name="default_top_site_jd">京东</string>
<!-- Default title for pinned JD top site that links to PDD home page -->
<string name="default_top_site_pdd">拼多多</string>
<!-- FxA server switch-->
<!-- Switch Preference for domestic China/global fxa server -->
<string name="preferences_allow_domestic_china_fxa_server">使用本地服务</string>
<!-- Instructions on how to access pairing -->
<string name="pair_instructions_2_cn"><![CDATA[打开 <b>firefox.com.cn/pair</b> 并扫描网站上的二维码]]></string>
<!-- Instructions on how to access pairing -->
<string name="sign_in_instructions_cn"><![CDATA[在计算机上使用 Firefox 打开 <b>https://firefox.com.cn/pair</b>]]></string>
</resources>

@ -27,4 +27,13 @@
<string name="default_top_site_jd">JD</string>
<!-- Default title for pinned JD top site that links to PDD home page -->
<string name="default_top_site_pdd">PDD</string>
<!-- FxA server switch-->
<!-- Switch Preference for domestic China/global fxa server -->
<string name="preferences_allow_domestic_china_fxa_server">Use domestic China service</string>
<!-- Instructions on how to access pairing -->
<string name="pair_instructions_2_cn"><![CDATA[Scan the QR code shown at <b>firefox.com.cn/pair</b>]]></string>
<!-- Instructions on how to access pairing -->
<string name="sign_in_instructions_cn"><![CDATA[On your computer open Firefox and go to <b>https://firefox.com.cn/pair</b>]]></string>
</resources>

@ -47,6 +47,8 @@
<string name="pref_key_account" translatable="false">pref_key_account</string>
<string name="pref_key_sign_in" translatable="false">pref_key_sign_in</string>
<string name="pref_key_account_auth_error" translatable="false">pref_key_account_auth_error</string>
<string name="pref_key_allow_domestic_china_fxa_server" translatable="false">pref_key_allow_domestic_china_fxa_server</string>
<string name="pref_key_have_read_fxa_account_json" translatable="false">pref_key_have_read_fxa_account_json</string>
<string name="pref_key_override_fxa_server" translatable="false">pref_key_override_fxa_server</string>
<string name="pref_key_override_sync_tokenserver" translatable="false">pref_key_override_sync_tokenserver</string>
<string name="pref_key_private_mode" translatable="false">pref_key_private_mode</string>

@ -13,6 +13,11 @@
android:title="@string/preferences_sync"
app:allowDividerBelow="false" />
<androidx.preference.SwitchPreference
android:key="@string/pref_key_allow_domestic_china_fxa_server"
android:title="@string/preferences_allow_domestic_china_fxa_server"
android:defaultValue="true"/>
<androidx.preference.PreferenceCategory
android:key="@string/pref_key_account_category"
android:title="@string/preferences_category_account"

@ -14,6 +14,11 @@
android:title="@string/preferences_sync"
app:allowDividerBelow="false" />
<androidx.preference.SwitchPreference
android:key="@string/pref_key_allow_domestic_china_fxa_server"
android:title="@string/preferences_allow_domestic_china_fxa_server"
android:defaultValue="true"/>
<androidx.preference.PreferenceCategory
android:key="@string/pref_key_account_category"
android:title="@string/preferences_category_account"

@ -22,6 +22,8 @@ class MigratingFenixApplication : FenixApplication() {
}
}
val fxaExpectChinaServers = Config.channel.isMozillaOnline
val migrator by lazy {
FennecMigrator.Builder(this, this.components.analytics.crashReporter)
.migrateOpenTabs(this.components.useCases.tabsUseCases)
@ -31,7 +33,7 @@ class MigratingFenixApplication : FenixApplication() {
this.components.core.pinnedSiteStorage
)
.migrateLogins(this.components.core.lazyPasswordsStorage)
.migrateFxa(lazy { this.components.backgroundServices.accountManager })
.migrateFxa(lazy { this.components.backgroundServices.accountManager }, fxaExpectChinaServers)
.migrateAddons(
this.components.core.engine,
this.components.addonCollectionProvider,

Loading…
Cancel
Save