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

Added secret debug menu preference to override the push server URL ( #24170) #26667

Merged
merged 2 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions app/src/main/java/org/mozilla/fenix/components/Push.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
package org.mozilla.fenix.components

import android.content.Context
import androidx.core.net.toUri
import mozilla.components.feature.push.AutoPushFeature
import mozilla.components.feature.push.PushConfig
import mozilla.components.feature.push.Protocol
import mozilla.components.lib.crash.CrashReporter
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.perf.lazyMonitored
import org.mozilla.fenix.push.FirebasePushService

/**
* Component group for push services. These components use services that strongly depend on
* push messaging (e.g. WebPush, SendTab).
*/
class Push(context: Context, crashReporter: CrashReporter) {
class Push(val context: Context, crashReporter: CrashReporter) {
val feature by lazyMonitored {
pushConfig?.let { config ->
AutoPushFeature(
Expand All @@ -40,7 +43,22 @@ class Push(context: Context, crashReporter: CrashReporter) {

logger.debug("Creating push configuration for autopush.")
val projectId = context.resources.getString(resId)
PushConfig(projectId)
val serverOverride = context.settings().overridePushServer
jonalmeida marked this conversation as resolved.
Show resolved Hide resolved
if (serverOverride.isEmpty()) {
PushConfig(projectId)
} else {
val uri = serverOverride.toUri()
PushConfig(
projectId,
serverHost = uri.getHost() ?: "",
protocol = if (uri.getScheme() == "http") {
Protocol.HTTP
} else {
// Treat any non "http" value as HTTPS, since those are the only 2 options.
Protocol.HTTPS
}
)
}
}

private val pushService by lazyMonitored { FirebasePushService() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
scope = lifecycleScope,
accountManager = requireComponents.backgroundServices.accountManager,
httpClient = requireComponents.core.client,
updateFxASyncOverrideMenu = ::updateFxASyncOverrideMenu,
updateFxAAllowDomesticChinaServerMenu = :: updateFxAAllowDomesticChinaServerMenu
)

Expand Down Expand Up @@ -281,6 +280,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
resources.getString(R.string.pref_key_data_choices) -> {
SettingsFragmentDirections.actionSettingsFragmentToDataChoicesFragment()
}
resources.getString(R.string.pref_key_sync_debug) -> {
SettingsFragmentDirections.actionSettingsFragmentToSyncDebugFragment()
}
resources.getString(R.string.pref_key_help) -> {
(activity as HomeActivity).openToBrowserAndLoad(
searchTermOrURL = SupportUtils.getSumoURLForTopic(
Expand Down Expand Up @@ -442,32 +444,6 @@ class SettingsFragment : PreferenceFragmentCompat() {

preferenceOpenLinksInExternalApp?.onPreferenceChangeListener = SharedPreferenceUpdater()

val preferenceFxAOverride =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_override_fxa_server))
val preferenceSyncOverride =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_override_sync_tokenserver))

val syncFxAOverrideUpdater = object : StringSharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
updateFxASyncOverrideMenu()
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
)
}
}
}
preferenceFxAOverride?.onPreferenceChangeListener = syncFxAOverrideUpdater
preferenceSyncOverride?.onPreferenceChangeListener = syncFxAOverrideUpdater

val preferenceStartProfiler =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_start_profiler))

Expand All @@ -481,6 +457,9 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<Preference>(
getPreferenceKey(R.string.pref_key_secret_debug_info)
)?.isVisible = showSecretDebugMenuThisSession
findPreference<Preference>(
getPreferenceKey(R.string.pref_key_sync_debug)
)?.isVisible = showSecretDebugMenuThisSession
preferenceStartProfiler?.isVisible = showSecretDebugMenuThisSession &&
(requireContext().components.core.engine.profiler?.isProfilerActive() != null)
}
Expand Down Expand Up @@ -537,30 +516,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}

private fun updateFxASyncOverrideMenu() {
val preferenceFxAOverride =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_override_fxa_server))
val preferenceSyncOverride =
findPreference<Preference>(getPreferenceKey(R.string.pref_key_override_sync_tokenserver))
val settings = requireContext().settings()
val show = settings.overrideFxAServer.isNotEmpty() ||
settings.overrideSyncTokenServer.isNotEmpty() ||
settings.showSecretDebugMenuThisSession
// Only enable changes to these prefs when the user isn't connected to an account.
val enabled =
requireComponents.backgroundServices.accountManager.authenticatedAccount() == null
preferenceFxAOverride?.apply {
isVisible = show
isEnabled = enabled
summary = settings.overrideFxAServer.ifEmpty { null }
}
preferenceSyncOverride?.apply {
isVisible = show
isEnabled = enabled
summary = settings.overrideSyncTokenServer.ifEmpty { null }
}
}

@VisibleForTesting
internal fun setupAmoCollectionOverridePreference(settings: Settings) {
val preferenceAmoCollectionOverride =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* 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.settings

import android.os.Bundle
import androidx.preference.EditTextPreference
import androidx.preference.Preference
import androidx.preference.Preference.OnPreferenceClickListener
import androidx.preference.PreferenceFragmentCompat
import kotlin.system.exitProcess
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.ext.showToolbar

/**
* Lets the user customize Private browsing options.
*/
class SyncDebugFragment : PreferenceFragmentCompat() {
private val logger = Logger("SyncDebugFragment")
private var hasChanges = false

private val preferenceUpdater = object : StringSharedPreferenceUpdater() {
override fun onPreferenceChange(preference: Preference, newValue: Any?): Boolean {
return super.onPreferenceChange(preference, newValue).also {
hasChanges = true
updateMenu()
}
}
}

override fun onResume() {
super.onResume()
showToolbar(getString(R.string.preferences_sync_debug))
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.sync_debug_preferences, rootKey)
requirePreference<EditTextPreference>(R.string.pref_key_override_fxa_server).let { pref ->
pref.setOnBindEditTextListener { it.setSingleLine() }
pref.onPreferenceChangeListener = preferenceUpdater
}
requirePreference<EditTextPreference>(R.string.pref_key_override_sync_tokenserver).let { pref ->
pref.setOnBindEditTextListener { it.setSingleLine() }
pref.onPreferenceChangeListener = preferenceUpdater
}
requirePreference<EditTextPreference>(R.string.pref_key_override_push_server).let { pref ->
pref.setOnBindEditTextListener { it.setSingleLine() }
pref.onPreferenceChangeListener = preferenceUpdater
}
requirePreference<Preference>(R.string.pref_key_sync_debug_quit).let { pref ->
pref.onPreferenceClickListener = OnPreferenceClickListener {
// Copied from StudiesView. This feels like a dramatic way to
// quit, is there a better way?
exitProcess(0)
}
}
updateMenu()
}

private fun updateMenu() {
val settings = requireContext().settings()
requirePreference<EditTextPreference>(R.string.pref_key_override_fxa_server).let {
it.summary = settings.overrideFxAServer.ifEmpty { null }
}
requirePreference<EditTextPreference>(R.string.pref_key_override_sync_tokenserver).let {
it.summary = settings.overrideSyncTokenServer.ifEmpty { null }
}
requirePreference<EditTextPreference>(R.string.pref_key_override_push_server).let {
it.summary = settings.overridePushServer.ifEmpty { null }
}
requirePreference<Preference>(R.string.pref_key_sync_debug_quit).let { pref ->
pref.isVisible = hasChanges
}

// val accountConnected =
// requireComponents.backgroundServices.accountManager.authenticatedAccount() == null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AccountUiView(
private val scope: CoroutineScope,
private val accountManager: FxaAccountManager,
private val httpClient: Client,
private val updateFxASyncOverrideMenu: () -> Unit,
private val updateFxAAllowDomesticChinaServerMenu: () -> Unit
) {

Expand All @@ -48,7 +47,6 @@ class AccountUiView(
fun updateAccountUIState(context: Context, profile: Profile?) {
val account = accountManager.authenticatedAccount()

updateFxASyncOverrideMenu()
updateFxAAllowDomesticChinaServerMenu()

// Signed-in, no problems.
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/org/mozilla/fenix/utils/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,11 @@ class Settings(private val appContext: Context) : PreferencesHolder {
default = ""
)

var overridePushServer by stringPreference(
jonalmeida marked this conversation as resolved.
Show resolved Hide resolved
appContext.getPreferenceKey(R.string.pref_key_override_push_server),
default = ""
)

var overrideAmoUser by stringPreference(
appContext.getPreferenceKey(R.string.pref_key_override_amo_user),
default = ""
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,13 @@
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
<action
android:id="@+id/action_settingsFragment_to_syncDebugFragment"
app:destination="@id/syncDebugFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
<dialog
android:id="@+id/profilerStartDialogFragment"
Expand Down Expand Up @@ -825,6 +832,10 @@
android:name="org.mozilla.fenix.exceptions.trackingprotection.TrackingProtectionExceptionsFragment"
android:label="@string/preference_exceptions"
tools:layout="@layout/fragment_exceptions" />
<fragment
android:id="@+id/syncDebugFragment"
android:name="org.mozilla.fenix.settings.SyncDebugFragment"
android:label="@string/preferences_sync_debug" />
<dialog
android:id="@+id/collectionCreationFragment"
android:name="org.mozilla.fenix.collections.CollectionCreationFragment"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/preference_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@
<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_sync_debug" translatable="false">pref_key_sync_debug</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_override_push_server" translatable="false">pref_key_override_push_server</string>
<string name="pref_key_sync_debug_quit" translatable="false">pref_key_sync_debug_quit</string>
<string name="pref_key_customize" translatable="false">pref_key_customize</string>
<string name="pref_key_private_browsing" translatable="false">pref_key_private_browsing</string>
<string name="pref_key_leakcanary" translatable="false">pref_key_leakcanary</string>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/static_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
<string name="preferences_debug_settings_task_continuity" translatable="false">Enable Task Continuity</string>
<!-- Label for enabling the Unified Search feature -->
<string name="preferences_debug_settings_unified_search" translatable="false">Enable Unified Search (requires restart)</string>
<!-- Title of preference for sync debugging (only shown in the when the secret debug menu is enabled) -->
<string name="preferences_sync_debug">Sync Debug</string>
<!-- Preference to override the Push server -->
<string name="preferences_override_push_server">Custom Push server</string>
<!-- Quit app button title for the Sync Debug preferences -->
<string name="preferences_sync_debug_quit_button_title">Stop Firefox</string>
<!-- Quit app button summary for the Sync Debug preferences -->
<string name="preferences_sync_debug_quit_button_summary">Custom server changes will take effect on the next Firefox run.</string>

<!-- A secret menu option in the tabs tray for making a tab inactive for testing. -->
<string name="inactive_tabs_menu_item">Make inactive</string>
Expand Down
14 changes: 3 additions & 11 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,10 @@
android:key="@string/pref_key_account_auth_error" />
</androidx.preference.PreferenceCategory>

<androidx.preference.EditTextPreference
android:key="@string/pref_key_override_fxa_server"
android:title="@string/preferences_override_fxa_server"
android:inputType="textUri"
app:iconSpaceReserved="false"
app:isPreferenceVisible="false" />

<androidx.preference.EditTextPreference
android:key="@string/pref_key_override_sync_tokenserver"
android:title="@string/preferences_override_sync_tokenserver"
android:inputType="textUri"
<androidx.preference.Preference
app:iconSpaceReserved="false"
android:key="@string/pref_key_sync_debug"
android:title="@string/preferences_sync_debug"
app:isPreferenceVisible="false" />

<androidx.preference.PreferenceCategory
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/xml/sync_debug_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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/. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.EditTextPreference
android:key="@string/pref_key_override_fxa_server"
android:title="@string/preferences_override_fxa_server"
android:inputType="textUri"
app:iconSpaceReserved="false" />
<androidx.preference.EditTextPreference
android:key="@string/pref_key_override_sync_tokenserver"
android:title="@string/preferences_override_sync_tokenserver"
android:inputType="textUri"
app:iconSpaceReserved="false" />
<androidx.preference.EditTextPreference
android:key="@string/pref_key_override_push_server"
android:title="@string/preferences_override_push_server"
android:inputType="textUri"
app:iconSpaceReserved="false" />
<androidx.preference.Preference
android:key="@string/pref_key_sync_debug_quit"
android:title="@string/preferences_sync_debug_quit_button_title"
android:summary="@string/preferences_sync_debug_quit_button_summary"
android:icon="@drawable/ic_close"
app:isPreferenceVisible="false" />
</PreferenceScreen>