adding UI toggle for email notifications in /settings/notifications

- will display a _no emails_ when the matrix account has no emails set and tapping will navigate to the emails and phone numbers screen where the user can add an email
- toggling the email notification with register and unregister push notification for the given email address
This commit is contained in:
Adam Brown 2021-09-21 10:47:03 +01:00
parent ee3f2877e9
commit 410cf5c062
7 changed files with 77 additions and 0 deletions

View File

@ -61,6 +61,22 @@ class PushersManager @Inject constructor(
)
}
fun registerEmailForPush(email: String) {
val currentSession = activeSessionHolder.getActiveSession()
currentSession.addEmailPusher(
email = email,
lang = localeProvider.current().language,
appDisplayName = appNameProvider.getAppName(),
deviceDisplayName = currentSession.sessionParams.deviceId ?: "MOBILE",
append = true,
)
}
suspend fun unregisterEmailPusher(email: String) {
val currentSession = activeSessionHolder.getSafeActiveSession() ?: return
currentSession.removeEmailPusher(email)
}
suspend fun unregisterPusher(pushKey: String) {
val currentSession = activeSessionHolder.getSafeActiveSession() ?: return
currentSession.removeHttpPusher(pushKey, stringProvider.getString(R.string.pusher_app_id))

View File

@ -117,6 +117,7 @@ class VectorPreferences @Inject constructor(private val context: Context) {
// notifications
const val SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY = "SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY"
const val SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY = "SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY"
const val SETTINGS_EMAIL_NOTIFICATION_CATEGORY_PREFERENCE_KEY = "SETTINGS_EMAIL_NOTIFICATION_CATEGORY_PREFERENCE_KEY"
// public static final String SETTINGS_TURN_SCREEN_ON_PREFERENCE_KEY = "SETTINGS_TURN_SCREEN_ON_PREFERENCE_KEY";
const val SETTINGS_SYSTEM_CALL_NOTIFICATION_PREFERENCE_KEY = "SETTINGS_SYSTEM_CALL_NOTIFICATION_PREFERENCE_KEY"

View File

@ -29,6 +29,7 @@ import im.vector.app.databinding.ActivityVectorSettingsBinding
import im.vector.app.features.discovery.DiscoverySettingsFragment
import im.vector.app.features.settings.devices.VectorSettingsDevicesFragment
import im.vector.app.features.settings.notifications.VectorSettingsNotificationPreferenceFragment
import im.vector.app.features.settings.threepids.ThreePidsSettingsFragment
import org.matrix.android.sdk.api.failure.GlobalError
import org.matrix.android.sdk.api.session.Session
@ -136,6 +137,10 @@ class VectorSettingsActivity : VectorBaseActivity<ActivityVectorSettingsBinding>
return keyToHighlight
}
override fun navigateToEmailAndPhoneNumbers() {
navigateTo(ThreePidsSettingsFragment::class.java)
}
override fun handleInvalidToken(globalError: GlobalError.InvalidToken) {
if (ignoreInvalidTokenError) {
Timber.w("Ignoring invalid token global error")

View File

@ -20,4 +20,6 @@ interface VectorSettingsFragmentInteractionListener {
fun requestHighlightPreferenceKeyOnResume(key: String?)
fun requestedKeyToHighlight(): String?
fun navigateToEmailAndPhoneNumbers()
}

View File

@ -47,6 +47,9 @@ import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.pushrules.RuleIds
import org.matrix.android.sdk.api.pushrules.RuleKind
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.pushers.Pusher
import javax.inject.Inject
// Referenced in vector_settings_preferences_root.xml
@ -116,11 +119,51 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
}
}
bindEmailNotifications()
refreshBackgroundSyncPrefs()
handleSystemPreference()
}
private fun bindEmailNotifications() {
val emails = session.getEmailsWithPushInformation()
findPreference<VectorPreferenceCategory>(VectorPreferences.SETTINGS_EMAIL_NOTIFICATION_CATEGORY_PREFERENCE_KEY)?.let { category ->
if (emails.isEmpty()) {
val vectorPreference = VectorPreference(requireContext())
vectorPreference.title = resources.getString(R.string.settings_notification_emails_no_emails)
category.addPreference(vectorPreference)
vectorPreference.setOnPreferenceClickListener {
interactionListener?.navigateToEmailAndPhoneNumbers()
true
}
} else {
emails.forEach { (emailPid, pusher) ->
val pref = VectorSwitchPreference(requireContext())
pref.title = resources.getString(R.string.settings_notification_emails_enable_for_email, emailPid.email)
pref.isChecked = pusher != null
pref.setOnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean) {
pushManager.registerEmailForPush(emailPid.email)
} else {
lifecycleScope.launch {
pushManager.unregisterEmailPusher(emailPid.email)
}
}
true
}
category.addPreference(pref)
}
}
}
}
private fun Session.getEmailsWithPushInformation(): List<Pair<ThreePid.Email, Pusher?>> {
val emailPushers = this.getPushers().filter { it.kind == "email" }
return this.getThreePids()
.filterIsInstance<ThreePid.Email>()
.map { it to emailPushers.firstOrNull { pusher -> pusher.pushKey == it.email } }
}
private val batteryStartForActivityResult = registerStartForActivityResult {
// Noop
}

View File

@ -1112,6 +1112,10 @@
<string name="settings_notification_advanced">Advanced Notification Settings</string>
<string name="settings_notification_by_event">Notification importance by event</string>
<string name="settings_notification_emails_category">Email notification</string>
<string name="settings_notification_emails_no_emails">To receive email with notification, please associate an email to your Matrix account</string>
<string name="settings_notification_emails_enable_for_email">Enable email notifications for %s</string>
<string name="settings_notification_default">Default Notifications</string>
<string name="settings_notification_mentions_and_keywords">Mentions and Keywords</string>
<string name="settings_notification_other">Other</string>

View File

@ -55,6 +55,12 @@
</im.vector.app.core.preference.VectorPreferenceCategory>
<im.vector.app.core.preference.VectorPreferenceCategory
android:key="SETTINGS_EMAIL_NOTIFICATION_CATEGORY_PREFERENCE_KEY"
android:title="@string/settings_notification_emails_category">
</im.vector.app.core.preference.VectorPreferenceCategory>
<im.vector.app.core.preference.VectorPreferenceCategory
android:persistent="false"
android:title="@string/settings_notification_configuration">