From adfa2b36af2f47f107f1d616fb73cc71361fe75a Mon Sep 17 00:00:00 2001 From: SpiritCroc Date: Thu, 16 Sep 2021 09:38:41 +0200 Subject: [PATCH] UP: Setting to force re-register UP Also move all notification settings to notification settings Change-Id: I8eee2d289e67e16b61542930a55706bb29b361af --- .../im/vector/app/core/pushers/UPHelper.kt | 26 ++++++++++++-- .../settings/VectorSettingsLabsFragment.kt | 10 ------ ...rSettingsNotificationPreferenceFragment.kt | 36 ++++++++++++++++--- vector/src/main/res/values/strings_sc.xml | 3 ++ .../src/main/res/xml/vector_settings_labs.xml | 10 ------ .../res/xml/vector_settings_notifications.xml | 21 +++++++++++ 6 files changed, 79 insertions(+), 27 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt b/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt index 218bb77f33..8f2fbc49a1 100644 --- a/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt +++ b/vector/src/main/java/im/vector/app/core/pushers/UPHelper.kt @@ -17,6 +17,7 @@ package im.vector.app.core.pushers import android.content.Context import android.content.pm.PackageManager +import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.core.content.edit import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -83,9 +84,15 @@ object UPHelper { } } - fun registerUnifiedPush(context: Context) { + fun registerUnifiedPush(context: Context, forceShowSelection: Boolean = false, onDoneRunnable: Runnable? = null) { val up = Registration() - if (up.getDistributor(context).isNotEmpty()) { + if (forceShowSelection) { + // Un-register first + up.unregisterApp(context) + up.saveDistributor(context, "") + storeUpEndpoint(context, null) + storePushGateway(context, null) + } else if (up.getDistributor(context).isNotEmpty()) { up.registerApp(context) return } @@ -101,10 +108,18 @@ object UPHelper { /** * Fallback with sync service */ + if (forceShowSelection) { + Toast.makeText(context.applicationContext, R.string.toast_unifiedpush_no_push_distributor, Toast.LENGTH_LONG).show() + } + onDoneRunnable?.run() } 1 -> { + if (forceShowSelection) { + Toast.makeText(context.applicationContext, context.getString(R.string.toast_unifiedpush_one_push_distributor, distributors.first()), Toast.LENGTH_LONG).show() + } up.saveDistributor(context, distributors.first()) up.registerApp(context) + onDoneRunnable?.run() } else -> { val builder: AlertDialog.Builder = MaterialAlertDialogBuilder(context) @@ -128,6 +143,13 @@ object UPHelper { up.saveDistributor(context, distributor) Timber.i("Saving distributor: $distributor") up.registerApp(context) + onDoneRunnable?.run() + } + builder.setOnDismissListener { + onDoneRunnable?.run() + } + builder.setOnCancelListener { + onDoneRunnable?.run() } val dialog: AlertDialog = builder.create() dialog.show() diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt index c6c2179a9a..b0feb63545 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsLabsFragment.kt @@ -17,15 +17,9 @@ package im.vector.app.features.settings import android.os.Build -import android.os.Handler -import android.os.Looper -import androidx.preference.Preference import im.vector.app.R import im.vector.app.core.preference.VectorSwitchPreference -import im.vector.app.core.pushers.UPHelper import im.vector.app.features.themes.ThemeUtils -import im.vector.app.features.MainActivity -import im.vector.app.features.MainActivityArgs import javax.inject.Inject class VectorSettingsLabsFragment @Inject constructor( @@ -62,9 +56,5 @@ class VectorSettingsLabsFragment @Inject constructor( findPreference(VectorPreferences.SETTINGS_VOICE_MESSAGE)?.isEnabled = Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP - findPreference(VectorPreferences.SETTINGS_UNIFIED_PUSH_FORCE_CUSTOM_GATEWAY)?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ -> - Handler(Looper.getMainLooper()).postDelayed({ context?.let { UPHelper.registerUnifiedPush(it)} } , 1000) - true - } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt index ac829be371..9c4ad682b1 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt @@ -21,6 +21,8 @@ import android.content.Context import android.content.Intent import android.media.RingtoneManager import android.net.Uri +import android.os.Handler +import android.os.Looper import android.os.Parcelable import android.widget.Toast import androidx.lifecycle.lifecycleScope @@ -43,6 +45,7 @@ import im.vector.app.features.settings.BackgroundSyncModeChooserDialog import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.VectorSettingsBaseFragment import im.vector.app.features.settings.VectorSettingsFragmentInteractionListener +import im.vector.app.features.settings.VectorSettingsLabsFragment import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.pushrules.RuleIds @@ -117,6 +120,20 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( } } + findPreference(VectorPreferences.SETTINGS_UNIFIED_PUSH_FORCE_CUSTOM_GATEWAY)?.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, _ -> + Handler(Looper.getMainLooper()).postDelayed({ context?.let { UPHelper.registerUnifiedPush(it)} } , 1000) + true + } + + findPreference(SETTINGS_UNIFIED_PUSH_RE_REGISTER)?.onPreferenceClickListener = Preference.OnPreferenceClickListener { + updateEnabledForDevice(false) + UPHelper.registerUnifiedPush(requireContext(), forceShowSelection = true) { + updateEnabledForDevice(true) + Handler(Looper.getMainLooper()).postDelayed({ refreshBackgroundSyncPrefs() } , 500) + } + true + } + refreshBackgroundSyncPrefs() handleSystemPreference() @@ -279,7 +296,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( override fun onPreferenceTreeClick(preference: Preference?): Boolean { return when (preference?.key) { VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY -> { - updateEnabledForDevice(preference) + updateEnabledForDevice(preference as SwitchPreference) true } VectorPreferences.SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY -> { @@ -292,9 +309,14 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( } } - private fun updateEnabledForDevice(preference: Preference?) { - val switchPref = preference as SwitchPreference - if (switchPref.isChecked) { + private fun updateEnabledForDevice(preference: SwitchPreference) { + updateEnabledForDevice(preference.isChecked) + } + + private fun updateEnabledForDevice(enabled: Boolean) { + val pref = findPreference(VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY) + pref?.isChecked = enabled + if (enabled) { UPHelper.registerUnifiedPush(requireContext()) } else { UPHelper.getUpEndpoint(requireContext())?.let { @@ -318,7 +340,7 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( return@fold } // revert the check box - switchPref.isChecked = !switchPref.isChecked + pref?.isChecked = true Toast.makeText(activity, R.string.unknown_error, Toast.LENGTH_SHORT).show() } ) @@ -352,4 +374,8 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( } } } + + companion object { + const val SETTINGS_UNIFIED_PUSH_RE_REGISTER = "SETTINGS_UNIFIED_PUSH_RE_REGISTER" + } } diff --git a/vector/src/main/res/values/strings_sc.xml b/vector/src/main/res/values/strings_sc.xml index cdf19a61d6..e110957f12 100644 --- a/vector/src/main/res/values/strings_sc.xml +++ b/vector/src/main/res/values/strings_sc.xml @@ -136,5 +136,8 @@ UnifiedPush Force custom push gateway Only enable this if your push endpoint does not require an external gateway. + Re-register push distributor + No push distributor found! + Registered push distributor %1$s diff --git a/vector/src/main/res/xml/vector_settings_labs.xml b/vector/src/main/res/xml/vector_settings_labs.xml index 654c1dbab6..99525f7f93 100644 --- a/vector/src/main/res/xml/vector_settings_labs.xml +++ b/vector/src/main/res/xml/vector_settings_labs.xml @@ -82,15 +82,5 @@ android:summary="@string/labs_use_restricted_join_rule_desc"/> - - - - - diff --git a/vector/src/main/res/xml/vector_settings_notifications.xml b/vector/src/main/res/xml/vector_settings_notifications.xml index 33581105e1..40fa60fa3a 100644 --- a/vector/src/main/res/xml/vector_settings_notifications.xml +++ b/vector/src/main/res/xml/vector_settings_notifications.xml @@ -130,4 +130,25 @@ android:key="SETTINGS_NOTIFICATIONS_TARGETS_PREFERENCE_KEY" android:title="@string/settings_notifications_targets" /--> + + + + + + + + + + \ No newline at end of file