From ba46f10e3f886c8b62640da4dac439ab440cf85f Mon Sep 17 00:00:00 2001 From: ganfra Date: Tue, 26 May 2020 19:44:14 +0200 Subject: [PATCH] Add integration entry to settings --- .../features/settings/VectorPreferences.kt | 3 +- .../settings/VectorSettingsGeneralFragment.kt | 53 ++++++++++++++++++- .../main/res/xml/vector_settings_general.xml | 19 +++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt index 86a9ee678e..beae0a623e 100755 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorPreferences.kt @@ -69,7 +69,8 @@ class VectorPreferences @Inject constructor(private val context: Context) { const val SETTINGS_ENCRYPTION_IMPORT_E2E_ROOM_KEYS_PREFERENCE_KEY = "SETTINGS_ENCRYPTION_IMPORT_E2E_ROOM_KEYS_PREFERENCE_KEY" const val SETTINGS_ENCRYPTION_NEVER_SENT_TO_PREFERENCE_KEY = "SETTINGS_ENCRYPTION_NEVER_SENT_TO_PREFERENCE_KEY" const val SETTINGS_SHOW_DEVICES_LIST_PREFERENCE_KEY = "SETTINGS_SHOW_DEVICES_LIST_PREFERENCE_KEY" - + const val SETTINGS_ALLOW_INTEGRATIONS_KEY = "SETTINGS_ALLOW_INTEGRATIONS_KEY" + const val SETTINGS_INTEGRATION_MANAGER_UI_URL_KEY = "SETTINGS_INTEGRATION_MANAGER_UI_URL_KEY" const val SETTINGS_SECURE_MESSAGE_RECOVERY_PREFERENCE_KEY = "SETTINGS_SECURE_MESSAGE_RECOVERY_PREFERENCE_KEY" // user diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt index dfa88d9b87..35fa3c4cbf 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt @@ -31,18 +31,23 @@ import androidx.core.view.isVisible import androidx.preference.EditTextPreference import androidx.preference.Preference import androidx.preference.PreferenceCategory +import androidx.preference.SwitchPreference import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.cache.DiskCache import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout import im.vector.matrix.android.api.MatrixCallback +import im.vector.matrix.android.api.NoOpMatrixCallback import im.vector.matrix.android.api.failure.isInvalidPassword +import im.vector.matrix.android.api.session.integrationmanager.IntegrationManagerConfig +import im.vector.matrix.android.api.session.integrationmanager.IntegrationManagerService import im.vector.riotx.R import im.vector.riotx.core.extensions.hideKeyboard import im.vector.riotx.core.extensions.showPassword import im.vector.riotx.core.platform.SimpleTextWatcher import im.vector.riotx.core.preference.UserAvatarPreference import im.vector.riotx.core.preference.VectorPreference +import im.vector.riotx.core.preference.VectorSwitchPreference import im.vector.riotx.core.utils.PERMISSION_REQUEST_CODE_LAUNCH_CAMERA import im.vector.riotx.core.utils.TextUtils import im.vector.riotx.core.utils.allGranted @@ -92,6 +97,18 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { findPreference(VectorPreferences.SETTINGS_CONTACTS_PHONEBOOK_COUNTRY_PREFERENCE_KEY)!! } + private val integrationServiceListener = object : IntegrationManagerService.Listener { + override fun onConfigurationChanged(config: IntegrationManagerConfig) { + super.onConfigurationChanged(config) + refreshIntegrationManagerSettings() + } + + override fun onIsEnabledChanged(enabled: Boolean) { + super.onIsEnabledChanged(enabled) + refreshIntegrationManagerSettings() + } + } + override fun bindPref() { // Avatar mUserAvatarPreference.let { @@ -196,6 +213,15 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { } } + (findPreference(VectorPreferences.SETTINGS_ALLOW_INTEGRATIONS_KEY) as? VectorSwitchPreference)?.let { + it.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> + //Disable it while updating the state, will be re-enabled by the account data listener. + it.isEnabled = false + session.integrationManagerService().setIntegrationEnabled(newValue as Boolean, NoOpMatrixCallback()) + true + } + } + // clear medias cache findPreference(VectorPreferences.SETTINGS_CLEAR_MEDIA_CACHE_PREFERENCE_KEY)!!.let { val size = getSizeOfFiles(File(requireContext().cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_DIR)) @@ -241,9 +267,15 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { override fun onResume() { super.onResume() - // Refresh identity server summary mIdentityServerPreference.summary = session.identityService().getCurrentIdentityServerUrl() ?: getString(R.string.identity_server_not_defined) + refreshIntegrationManagerSettings() + session.integrationManagerService().addListener(integrationServiceListener) + } + + override fun onPause() { + super.onPause() + session.integrationManagerService().removeListener(integrationServiceListener) } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { @@ -315,6 +347,25 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { } } + private fun refreshIntegrationManagerSettings() { + val integrationAllowed = session.integrationManagerService().isIntegrationEnabled() + (findPreference(VectorPreferences.SETTINGS_ALLOW_INTEGRATIONS_KEY))!!.let { + val savedListener = it.onPreferenceChangeListener + it.onPreferenceChangeListener = null + it.isChecked = integrationAllowed + it.isEnabled = true + it.onPreferenceChangeListener = savedListener + } + findPreference(VectorPreferences.SETTINGS_INTEGRATION_MANAGER_UI_URL_KEY)!!.let { + if (integrationAllowed) { + it.summary = session.integrationManagerService().getPreferredConfig().uiUrl + it.isVisible = true + } else { + it.isVisible = false + } + } + } + /** * Update the avatar. */ diff --git a/vector/src/main/res/xml/vector_settings_general.xml b/vector/src/main/res/xml/vector_settings_general.xml index 15d43ddc34..5ab5ab6c4f 100644 --- a/vector/src/main/res/xml/vector_settings_general.xml +++ b/vector/src/main/res/xml/vector_settings_general.xml @@ -96,6 +96,25 @@ + + + + + + + + + +