From dff85034939fc5823dc6cf4706d549eb12258b42 Mon Sep 17 00:00:00 2001 From: David Langley Date: Thu, 15 Jul 2021 16:06:46 +0100 Subject: [PATCH 1/6] fix discrepancies with web --- .../java/im/vector/app/core/preference/PushRulePreference.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt index c3e324b64a..db59fc23d4 100755 --- a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt +++ b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt @@ -130,10 +130,11 @@ class PushRulePreference : VectorPreference { } } else { if (NOTIFICATION_OFF_INDEX == index) { - if (safeKind == RuleSetKey.UNDERRIDE || safeRule.ruleId == RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS) { + if (safeKind == RuleSetKey.UNDERRIDE && safeRule.ruleId != RuleIds.RULE_ID_CALL) { safeRule.setNotify(false) } else { safeRule.copy(enabled = false) + .removeNotificationSound() } } else { val newRule = safeRule.copy(enabled = true) @@ -142,7 +143,7 @@ class PushRulePreference : VectorPreference { && safeRule.ruleId != RuleIds.RULE_ID_INVITE_ME && NOTIFICATION_NOISY_INDEX == index) - if (NOTIFICATION_NOISY_INDEX == index) { + if (NOTIFICATION_NOISY_INDEX == index && RuleIds.RULE_ID_ROOM_NOTIF != safeRule.ruleId) { newRule.setNotificationSound( if (safeRule.ruleId == RuleIds.RULE_ID_CALL) { Action.ACTION_OBJECT_VALUE_VALUE_RING From a29ccda68e6868925f4b827d44759c90af632871 Mon Sep 17 00:00:00 2001 From: David Langley Date: Mon, 19 Jul 2021 21:29:46 +0100 Subject: [PATCH 2/6] move mutable push rule logic to static declarations as on web --- .../sdk/api/pushrules/PushRuleService.kt | 2 +- .../sdk/api/pushrules/rest/PushRule.kt | 10 ++ .../notification/DefaultPushRuleService.kt | 4 +- .../pushers/UpdatePushRuleActionsTask.kt | 27 ++-- .../im/vector/app/core/di/FragmentModule.kt | 6 +- .../app/core/preference/PushRulePreference.kt | 149 +++--------------- .../settings/VectorSettingsActivity.kt | 1 + .../notifications/PushRuleDefinitions.kt | 92 +++++++++++ .../settings/notifications/StandardAction.kt | 31 ++++ ...sAdvancedNotificationPreferenceFragment.kt | 47 +++++- ...rSettingsNotificationPreferenceFragment.kt | 9 +- ...ttingsNotificationsTroubleshootFragment.kt | 5 +- .../res/xml/vector_settings_notifications.xml | 2 +- .../src/main/res/xml/vector_settings_root.xml | 2 +- 14 files changed, 229 insertions(+), 158 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt rename vector/src/main/java/im/vector/app/features/settings/{ => notifications}/VectorSettingsAdvancedNotificationPreferenceFragment.kt (68%) rename vector/src/main/java/im/vector/app/features/settings/{ => notifications}/VectorSettingsNotificationPreferenceFragment.kt (97%) rename vector/src/main/java/im/vector/app/features/settings/{ => notifications}/VectorSettingsNotificationsTroubleshootFragment.kt (97%) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt index d9bf5cfd13..05d40f43d3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt @@ -31,7 +31,7 @@ interface PushRuleService { suspend fun addPushRule(kind: RuleKind, pushRule: PushRule) - suspend fun updatePushRuleActions(kind: RuleKind, oldPushRule: PushRule, newPushRule: PushRule) + suspend fun updatePushRuleActions(kind: RuleKind, ruleId: String, enable: Boolean, actions: List?) suspend fun removePushRule(kind: RuleKind, pushRule: PushRule) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt index 3a9fc4fb83..bc6cead405 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt @@ -100,6 +100,14 @@ data class PushRule( ) } + /** + * Get the highlight status. As spec mentions assume false if no tweak present. + */ + fun getHighlight(): Boolean { + return (getActions().firstOrNull { it is Action.Highlight } as? Action.Highlight)?.highlight ?: false + } + + /** * Set the notification status. * @@ -133,4 +141,6 @@ data class PushRule( * @return true if the rule should not play sound */ fun shouldNotNotify() = actions.contains(Action.ACTION_DONT_NOTIFY) + + companion object { } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/notification/DefaultPushRuleService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/notification/DefaultPushRuleService.kt index 38f6b08b43..4e8abcf784 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/notification/DefaultPushRuleService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/notification/DefaultPushRuleService.kt @@ -113,8 +113,8 @@ internal class DefaultPushRuleService @Inject constructor( addPushRuleTask.execute(AddPushRuleTask.Params(kind, pushRule)) } - override suspend fun updatePushRuleActions(kind: RuleKind, oldPushRule: PushRule, newPushRule: PushRule) { - updatePushRuleActionsTask.execute(UpdatePushRuleActionsTask.Params(kind, oldPushRule, newPushRule)) + override suspend fun updatePushRuleActions(kind: RuleKind, ruleId: String, enable: Boolean, actions: List?) { + updatePushRuleActionsTask.execute(UpdatePushRuleActionsTask.Params(kind, ruleId, enable, actions)) } override suspend fun removePushRule(kind: RuleKind, pushRule: PushRule) { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt index 2a24aee892..194e614957 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt @@ -15,8 +15,9 @@ */ package org.matrix.android.sdk.internal.session.pushers +import org.matrix.android.sdk.api.pushrules.Action import org.matrix.android.sdk.api.pushrules.RuleKind -import org.matrix.android.sdk.api.pushrules.rest.PushRule +import org.matrix.android.sdk.api.pushrules.toJson import org.matrix.android.sdk.internal.network.GlobalErrorReceiver import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.task.Task @@ -25,8 +26,9 @@ import javax.inject.Inject internal interface UpdatePushRuleActionsTask : Task { data class Params( val kind: RuleKind, - val oldPushRule: PushRule, - val newPushRule: PushRule + val ruleId: String, + val enable: Boolean, + val actions: List? ) } @@ -36,20 +38,15 @@ internal class DefaultUpdatePushRuleActionsTask @Inject constructor( ) : UpdatePushRuleActionsTask { override suspend fun execute(params: UpdatePushRuleActionsTask.Params) { - if (params.oldPushRule.enabled != params.newPushRule.enabled) { - // First change enabled state executeRequest(globalErrorReceiver) { - pushRulesApi.updateEnableRuleStatus(params.kind.value, params.newPushRule.ruleId, params.newPushRule.enabled) + pushRulesApi.updateEnableRuleStatus(params.kind.value, params.ruleId, enable = params.enable) } - } - - if (params.newPushRule.enabled) { - // Also ensure the actions are up to date - val body = mapOf("actions" to params.newPushRule.actions) - - executeRequest(globalErrorReceiver) { - pushRulesApi.updateRuleActions(params.kind.value, params.newPushRule.ruleId, body) + if (params.actions != null) { + val body = mapOf("actions" to params.actions.toJson()) + executeRequest(globalErrorReceiver) { + pushRulesApi.updateRuleActions(params.kind.value, params.ruleId, body) + } } - } + } } diff --git a/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt b/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt index 8580543022..0a931868b1 100644 --- a/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt +++ b/vector/src/main/java/im/vector/app/core/di/FragmentModule.kt @@ -111,12 +111,12 @@ import im.vector.app.features.roomprofile.settings.RoomSettingsFragment import im.vector.app.features.roomprofile.uploads.RoomUploadsFragment import im.vector.app.features.roomprofile.uploads.files.RoomUploadsFilesFragment import im.vector.app.features.roomprofile.uploads.media.RoomUploadsMediaFragment -import im.vector.app.features.settings.VectorSettingsAdvancedNotificationPreferenceFragment +import im.vector.app.features.settings.notifications.VectorSettingsAdvancedNotificationPreferenceFragment import im.vector.app.features.settings.VectorSettingsGeneralFragment import im.vector.app.features.settings.VectorSettingsHelpAboutFragment import im.vector.app.features.settings.VectorSettingsLabsFragment -import im.vector.app.features.settings.VectorSettingsNotificationPreferenceFragment -import im.vector.app.features.settings.VectorSettingsNotificationsTroubleshootFragment +import im.vector.app.features.settings.notifications.VectorSettingsNotificationPreferenceFragment +import im.vector.app.features.settings.notifications.VectorSettingsNotificationsTroubleshootFragment import im.vector.app.features.settings.VectorSettingsPinFragment import im.vector.app.features.settings.VectorSettingsPreferencesFragment import im.vector.app.features.settings.VectorSettingsSecurityPrivacyFragment diff --git a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt index db59fc23d4..c9969453b5 100755 --- a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt +++ b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt @@ -22,18 +22,23 @@ import android.view.View import android.widget.RadioGroup import androidx.preference.PreferenceViewHolder import im.vector.app.R -import org.matrix.android.sdk.api.pushrules.Action -import org.matrix.android.sdk.api.pushrules.RuleIds -import org.matrix.android.sdk.api.pushrules.RuleSetKey -import org.matrix.android.sdk.api.pushrules.rest.PushRule -import org.matrix.android.sdk.api.pushrules.rest.PushRuleAndKind class PushRulePreference : VectorPreference { + enum class NotificationIndex(val index: Int) { + OFF(0), + SILENT(1), + NOISEY(2); + + companion object { + fun fromInt(index: Int) = values().first { it.index == index } + } + } + /** - * @return the selected push rule and its kind + * @return the selected push rule index */ - var ruleAndKind: PushRuleAndKind? = null + var index: NotificationIndex? = null private set constructor(context: Context) : super(context) @@ -47,44 +52,12 @@ class PushRulePreference : VectorPreference { } /** - * @return the bing rule status index - */ - private val ruleStatusIndex: Int - get() { - val safeRule = ruleAndKind?.pushRule ?: return NOTIFICATION_OFF_INDEX - - if (safeRule.ruleId == RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS) { - if (safeRule.shouldNotNotify()) { - return if (safeRule.enabled) { - NOTIFICATION_OFF_INDEX - } else { - NOTIFICATION_SILENT_INDEX - } - } else if (safeRule.shouldNotify()) { - return NOTIFICATION_NOISY_INDEX - } - } - - if (safeRule.enabled) { - return if (safeRule.shouldNotNotify()) { - NOTIFICATION_OFF_INDEX - } else if (safeRule.getNotificationSound() != null) { - NOTIFICATION_NOISY_INDEX - } else { - NOTIFICATION_SILENT_INDEX - } - } - - return NOTIFICATION_OFF_INDEX - } - - /** - * Update the push rule. + * Update the notification index. * * @param pushRule */ - fun setPushRule(pushRuleAndKind: PushRuleAndKind?) { - ruleAndKind = pushRuleAndKind + fun setIndex(notificationIndex: NotificationIndex? ) { + index = notificationIndex refreshSummary() } @@ -92,75 +65,13 @@ class PushRulePreference : VectorPreference { * Refresh the summary */ private fun refreshSummary() { - summary = context.getString(when (ruleStatusIndex) { - NOTIFICATION_OFF_INDEX -> R.string.notification_off - NOTIFICATION_SILENT_INDEX -> R.string.notification_silent - else -> R.string.notification_noisy + summary = context.getString(when (index) { + NotificationIndex.OFF -> R.string.notification_off + NotificationIndex.SILENT -> R.string.notification_silent + NotificationIndex.NOISEY, null -> R.string.notification_noisy }) } - /** - * Create a push rule with the updated required at index. - * - * @param index index - * @return a push rule with the updated flags / null if there is no update - */ - fun createNewRule(index: Int): PushRule? { - val safeRule = ruleAndKind?.pushRule ?: return null - val safeKind = ruleAndKind?.kind ?: return null - - return if (index != ruleStatusIndex) { - if (safeRule.ruleId == RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS) { - when (index) { - NOTIFICATION_OFF_INDEX -> { - safeRule.copy(enabled = true) - .setNotify(false) - .removeNotificationSound() - } - NOTIFICATION_SILENT_INDEX -> { - safeRule.copy(enabled = false) - .setNotify(false) - } - NOTIFICATION_NOISY_INDEX -> { - safeRule.copy(enabled = true) - .setNotify(true) - .setNotificationSound() - } - else -> safeRule - } - } else { - if (NOTIFICATION_OFF_INDEX == index) { - if (safeKind == RuleSetKey.UNDERRIDE && safeRule.ruleId != RuleIds.RULE_ID_CALL) { - safeRule.setNotify(false) - } else { - safeRule.copy(enabled = false) - .removeNotificationSound() - } - } else { - val newRule = safeRule.copy(enabled = true) - .setNotify(true) - .setHighlight(safeKind != RuleSetKey.UNDERRIDE - && safeRule.ruleId != RuleIds.RULE_ID_INVITE_ME - && NOTIFICATION_NOISY_INDEX == index) - - if (NOTIFICATION_NOISY_INDEX == index && RuleIds.RULE_ID_ROOM_NOTIF != safeRule.ruleId) { - newRule.setNotificationSound( - if (safeRule.ruleId == RuleIds.RULE_ID_CALL) { - Action.ACTION_OBJECT_VALUE_VALUE_RING - } else { - Action.ACTION_OBJECT_VALUE_VALUE_DEFAULT - } - ) - } else { - newRule.removeNotificationSound() - } - } - } - } else { - safeRule - } - } - override fun onBindViewHolder(holder: PreferenceViewHolder) { super.onBindViewHolder(holder) @@ -171,14 +82,14 @@ class PushRulePreference : VectorPreference { val radioGroup = holder.findViewById(R.id.bingPreferenceRadioGroup) as? RadioGroup radioGroup?.setOnCheckedChangeListener(null) - when (ruleStatusIndex) { - NOTIFICATION_OFF_INDEX -> { + when (index) { + NotificationIndex.OFF -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleOff) } - NOTIFICATION_SILENT_INDEX -> { + NotificationIndex.SILENT -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleSilent) } - else -> { + NotificationIndex.NOISEY -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleNoisy) } } @@ -186,23 +97,15 @@ class PushRulePreference : VectorPreference { radioGroup?.setOnCheckedChangeListener { _, checkedId -> when (checkedId) { R.id.bingPreferenceRadioBingRuleOff -> { - onPreferenceChangeListener?.onPreferenceChange(this, NOTIFICATION_OFF_INDEX) + onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.OFF) } R.id.bingPreferenceRadioBingRuleSilent -> { - onPreferenceChangeListener?.onPreferenceChange(this, NOTIFICATION_SILENT_INDEX) + onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.SILENT) } R.id.bingPreferenceRadioBingRuleNoisy -> { - onPreferenceChangeListener?.onPreferenceChange(this, NOTIFICATION_NOISY_INDEX) + onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.NOISEY) } } } } - - companion object { - - // index in mRuleStatuses - private const val NOTIFICATION_OFF_INDEX = 0 - private const val NOTIFICATION_SILENT_INDEX = 1 - private const val NOTIFICATION_NOISY_INDEX = 2 - } } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsActivity.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsActivity.kt index 757208796e..646709626c 100755 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsActivity.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsActivity.kt @@ -27,6 +27,7 @@ import im.vector.app.core.extensions.replaceFragment import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.databinding.ActivityVectorSettingsBinding import im.vector.app.features.settings.devices.VectorSettingsDevicesFragment +import im.vector.app.features.settings.notifications.VectorSettingsNotificationPreferenceFragment import org.matrix.android.sdk.api.failure.GlobalError import org.matrix.android.sdk.api.session.Session diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt new file mode 100644 index 0000000000..155986cd8a --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.notifications + +import im.vector.app.core.preference.PushRulePreference +import org.matrix.android.sdk.api.pushrules.RuleIds + +fun getStandardAction(ruleId: String, index: PushRulePreference.NotificationIndex): StandardAction? { + return when (ruleId) { + RuleIds.RULE_ID_CONTAIN_DISPLAY_NAME -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.HighlightDefaultSound + } + RuleIds.RULE_ID_CONTAIN_USER_NAME -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.HighlightDefaultSound + } + RuleIds.RULE_ID_ROOM_NOTIF -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.Highlight + } + RuleIds.RULE_ID_ONE_TO_ONE_ROOM -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + } + RuleIds.RULE_ID_ONE_TO_ONE_ENCRYPTED_ROOM -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + } + RuleIds.RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + } + RuleIds.RULE_ID_ENCRYPTED -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + } + RuleIds.RULE_ID_INVITE_ME -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + } + RuleIds.RULE_ID_CALL -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyRingSound + } + RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Disabled + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + } + RuleIds.RULE_ID_TOMBSTONE -> + when (index) { + PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify + PushRulePreference.NotificationIndex.NOISEY -> StandardAction.Highlight + } + else -> null + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt new file mode 100644 index 0000000000..3f482bf626 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.settings.notifications + +import org.matrix.android.sdk.api.pushrules.Action + +sealed class StandardAction( + val actions: List? +) { + object Notify : StandardAction(actions = listOf(Action.Notify)) + object NotifyDefaultSound : StandardAction(actions = listOf(Action.Notify, Action.Sound())) + object NotifyRingSound : StandardAction(actions = listOf(Action.Notify, Action.Sound(sound = Action.ACTION_OBJECT_VALUE_VALUE_RING))) + object Highlight : StandardAction(actions = listOf(Action.Notify, Action.Highlight(highlight = true))) + object HighlightDefaultSound : StandardAction(actions = listOf(Action.Notify, Action.Highlight(highlight = true), Action.Sound())) + object DontNotify : StandardAction(actions = listOf(Action.DoNotNotify)) + object Disabled : StandardAction(actions = null) +} diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsAdvancedNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt similarity index 68% rename from vector/src/main/java/im/vector/app/features/settings/VectorSettingsAdvancedNotificationPreferenceFragment.kt rename to vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt index 8d9f8d7170..08520e9ea3 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsAdvancedNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018 New Vector Ltd + * Copyright (c) 2021 New Vector Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,17 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package im.vector.app.features.settings +package im.vector.app.features.settings.notifications import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import im.vector.app.R +import im.vector.app.core.preference.PushRulePreference.NotificationIndex import im.vector.app.core.preference.PushRulePreference import im.vector.app.core.preference.VectorPreference import im.vector.app.core.utils.toast +import im.vector.app.features.settings.VectorSettingsBaseFragment import kotlinx.coroutines.launch import org.matrix.android.sdk.api.pushrules.RuleIds +import org.matrix.android.sdk.api.pushrules.rest.PushRule import org.matrix.android.sdk.api.pushrules.rest.PushRuleAndKind +import org.matrix.android.sdk.api.pushrules.toJson import javax.inject.Inject class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() @@ -45,24 +49,29 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() preference.isVisible = false } else { preference.isVisible = true - preference.setPushRule(ruleAndKind) + val initialIndex = getNotificationIndexForRule(ruleAndKind.pushRule) + preference.setIndex(initialIndex) preference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue -> - val newRule = preference.createNewRule(newValue as Int) - if (newRule != null) { + val newIndex = newValue as NotificationIndex + val standardAction = getStandardAction(ruleAndKind.pushRule.ruleId, newIndex) + if (standardAction != null) { + val enabled = standardAction != StandardAction.Disabled + val newActions = standardAction.actions displayLoadingView() lifecycleScope.launch { val result = runCatching { session.updatePushRuleActions(ruleAndKind.kind, - preference.ruleAndKind?.pushRule ?: ruleAndKind.pushRule, - newRule) + ruleAndKind.pushRule.ruleId, + enabled, + newActions) } if (!isAdded) { return@launch } hideLoadingView() result.onSuccess { - preference.setPushRule(ruleAndKind.copy(pushRule = newRule)) + preference.setIndex(newIndex) } result.onFailure { failure -> // Restore the previous value @@ -78,6 +87,28 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() } } + private fun getNotificationIndexForRule(rule: PushRule): NotificationIndex? { + return NotificationIndex.values().firstOrNull { + val standardAction = getStandardAction(rule.ruleId, it) ?: return@firstOrNull false + val indexActions = standardAction.actions ?: listOf() + val targetRule = rule.copy(enabled = standardAction != StandardAction.Disabled, actions = indexActions.toJson()) + val match = ruleMatches(rule, targetRule) + match + } + } + + + private fun ruleMatches(rule: PushRule, targetRule: PushRule): Boolean { + return (!rule.enabled && !targetRule.enabled) || + (rule.enabled + && targetRule.enabled + && rule.getHighlight() == targetRule.getHighlight() + && rule.getNotificationSound() == targetRule.getNotificationSound() + && rule.shouldNotify() == targetRule.shouldNotify() + && rule.shouldNotNotify() == targetRule.shouldNotNotify()) + } + + private fun refreshDisplay() { listView?.adapter?.notifyDataSetChanged() } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt similarity index 97% rename from vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt rename to vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt index fd1f406bcb..be989e62d3 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019 New Vector Ltd + * Copyright (c) 2021 New Vector Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -package im.vector.app.features.settings +package im.vector.app.features.settings.notifications import android.app.Activity import android.content.Context @@ -37,6 +37,11 @@ import im.vector.app.core.pushers.PushersManager import im.vector.app.core.utils.isIgnoringBatteryOptimizations import im.vector.app.core.utils.requestDisablingBatteryOptimization import im.vector.app.features.notifications.NotificationUtils +import im.vector.app.features.settings.BackgroundSyncMode +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.push.fcm.FcmHelper import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.tryOrNull diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationsTroubleshootFragment.kt similarity index 97% rename from vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt rename to vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationsTroubleshootFragment.kt index a1759ebfa7..6e47079afa 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationsTroubleshootFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationsTroubleshootFragment.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018 New Vector Ltd + * Copyright (c) 2021 New Vector Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package im.vector.app.features.settings +package im.vector.app.features.settings.notifications import android.app.Activity import android.content.BroadcastReceiver @@ -36,6 +36,7 @@ import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.databinding.FragmentSettingsNotificationsTroubleshootBinding import im.vector.app.features.notifications.NotificationUtils import im.vector.app.features.rageshake.BugReporter +import im.vector.app.features.settings.VectorSettingsFragmentInteractionListener import im.vector.app.features.settings.troubleshoot.NotificationTroubleshootTestManager import im.vector.app.features.settings.troubleshoot.TroubleshootTest import im.vector.app.push.fcm.NotificationTroubleshootTestManagerFactory diff --git a/vector/src/main/res/xml/vector_settings_notifications.xml b/vector/src/main/res/xml/vector_settings_notifications.xml index 983ddf36f2..03232dc8a4 100644 --- a/vector/src/main/res/xml/vector_settings_notifications.xml +++ b/vector/src/main/res/xml/vector_settings_notifications.xml @@ -26,7 +26,7 @@ android:persistent="false" android:summary="@string/settings_notification_advanced_summary" android:title="@string/settings_notification_advanced" - app:fragment="im.vector.app.features.settings.VectorSettingsAdvancedNotificationPreferenceFragment" /> + app:fragment="im.vector.app.features.settings.notifications.VectorSettingsAdvancedNotificationPreferenceFragment" /> diff --git a/vector/src/main/res/xml/vector_settings_root.xml b/vector/src/main/res/xml/vector_settings_root.xml index 100db7ce79..32e21b3391 100644 --- a/vector/src/main/res/xml/vector_settings_root.xml +++ b/vector/src/main/res/xml/vector_settings_root.xml @@ -16,7 +16,7 @@ + app:fragment="im.vector.app.features.settings.notifications.VectorSettingsNotificationPreferenceFragment" /> Date: Mon, 19 Jul 2021 22:07:20 +0100 Subject: [PATCH 3/6] checks --- ...SettingsAdvancedNotificationPreferenceFragment.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt index 08520e9ea3..12b1e072cb 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt @@ -89,18 +89,19 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() private fun getNotificationIndexForRule(rule: PushRule): NotificationIndex? { return NotificationIndex.values().firstOrNull { + // Get the actions for the index val standardAction = getStandardAction(rule.ruleId, it) ?: return@firstOrNull false val indexActions = standardAction.actions ?: listOf() + // Check if the input rule matches a rule generated from the static rule definitions val targetRule = rule.copy(enabled = standardAction != StandardAction.Disabled, actions = indexActions.toJson()) - val match = ruleMatches(rule, targetRule) - match + ruleMatches(rule, targetRule) } } - private fun ruleMatches(rule: PushRule, targetRule: PushRule): Boolean { - return (!rule.enabled && !targetRule.enabled) || - (rule.enabled + //Rules match if both are disabled, or if both are enabled and their highlight/sound/notify actions match up. + return (!rule.enabled && !targetRule.enabled) + || (rule.enabled && targetRule.enabled && rule.getHighlight() == targetRule.getHighlight() && rule.getNotificationSound() == targetRule.getNotificationSound() @@ -108,7 +109,6 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() && rule.shouldNotNotify() == targetRule.shouldNotNotify()) } - private fun refreshDisplay() { listView?.adapter?.notifyDataSetChanged() } From da993b5b58425faf19b6296a4703cb5bd6f81882 Mon Sep 17 00:00:00 2001 From: David Langley Date: Mon, 19 Jul 2021 22:13:23 +0100 Subject: [PATCH 4/6] lint --- .../java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt | 3 --- .../sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt | 1 - .../java/im/vector/app/core/preference/PushRulePreference.kt | 2 +- .../VectorSettingsAdvancedNotificationPreferenceFragment.kt | 2 +- 4 files changed, 2 insertions(+), 6 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt index bc6cead405..b95d5217a4 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt @@ -107,7 +107,6 @@ data class PushRule( return (getActions().firstOrNull { it is Action.Highlight } as? Action.Highlight)?.highlight ?: false } - /** * Set the notification status. * @@ -141,6 +140,4 @@ data class PushRule( * @return true if the rule should not play sound */ fun shouldNotNotify() = actions.contains(Action.ACTION_DONT_NOTIFY) - - companion object { } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt index 194e614957..b8dbabd09e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/UpdatePushRuleActionsTask.kt @@ -47,6 +47,5 @@ internal class DefaultUpdatePushRuleActionsTask @Inject constructor( pushRulesApi.updateRuleActions(params.kind.value, params.ruleId, body) } } - } } diff --git a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt index c9969453b5..d49d7c3407 100755 --- a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt +++ b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt @@ -56,7 +56,7 @@ class PushRulePreference : VectorPreference { * * @param pushRule */ - fun setIndex(notificationIndex: NotificationIndex? ) { + fun setIndex(notificationIndex: NotificationIndex?) { index = notificationIndex refreshSummary() } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt index 12b1e072cb..d6a3fea1bf 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt @@ -99,7 +99,7 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() } private fun ruleMatches(rule: PushRule, targetRule: PushRule): Boolean { - //Rules match if both are disabled, or if both are enabled and their highlight/sound/notify actions match up. + // Rules match if both are disabled, or if both are enabled and their highlight/sound/notify actions match up. return (!rule.enabled && !targetRule.enabled) || (rule.enabled && targetRule.enabled From 608a1d3f8f8d83c3359a1ca8ed6eb9d27afca960 Mon Sep 17 00:00:00 2001 From: David Langley Date: Tue, 20 Jul 2021 11:24:17 +0100 Subject: [PATCH 5/6] clarify sdk update and cleanup typos --- changelog.d/3681.removal | 1 + .../sdk/api/pushrules/PushRuleService.kt | 6 ++ .../sdk/api/pushrules/rest/PushRule.kt | 3 +- .../app/core/preference/PushRulePreference.kt | 12 ++-- .../notifications/PushRuleDefinitions.kt | 68 +++++++++---------- .../{StandardAction.kt => StandardActions.kt} | 16 ++--- ...sAdvancedNotificationPreferenceFragment.kt | 4 +- 7 files changed, 59 insertions(+), 51 deletions(-) create mode 100644 changelog.d/3681.removal rename vector/src/main/java/im/vector/app/features/settings/notifications/{StandardAction.kt => StandardActions.kt} (51%) diff --git a/changelog.d/3681.removal b/changelog.d/3681.removal new file mode 100644 index 0000000000..314b58c41d --- /dev/null +++ b/changelog.d/3681.removal @@ -0,0 +1 @@ +updatePushRuleActions signature has been updated to more explicitly enabled/disable the rule and update the actions. It's behaviour has also been changed to match the web with the enable/disable requests being sent on every invocation and actions sent when needed(not null). \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt index 05d40f43d3..4534368679 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt @@ -31,6 +31,12 @@ interface PushRuleService { suspend fun addPushRule(kind: RuleKind, pushRule: PushRule) + /** + * Enables/Disables a push rule and updates the actions if necessary + * @param enable Enables/Disables the rule + * @param actions Actions to update if not null + */ + suspend fun updatePushRuleActions(kind: RuleKind, ruleId: String, enable: Boolean, actions: List?) suspend fun removePushRule(kind: RuleKind, pushRule: PushRule) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt index b95d5217a4..154209731e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt @@ -18,6 +18,7 @@ package org.matrix.android.sdk.api.pushrules.rest import com.squareup.moshi.Json import com.squareup.moshi.JsonClass +import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.pushrules.Action import org.matrix.android.sdk.api.pushrules.getActions import org.matrix.android.sdk.api.pushrules.toJson @@ -104,7 +105,7 @@ data class PushRule( * Get the highlight status. As spec mentions assume false if no tweak present. */ fun getHighlight(): Boolean { - return (getActions().firstOrNull { it is Action.Highlight } as? Action.Highlight)?.highlight ?: false + return (getActions().firstOrNull { it is Action.Highlight } as? Action.Highlight)?.highlight.orFalse() } /** diff --git a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt index d49d7c3407..2471c5f66c 100755 --- a/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt +++ b/vector/src/main/java/im/vector/app/core/preference/PushRulePreference.kt @@ -28,7 +28,7 @@ class PushRulePreference : VectorPreference { enum class NotificationIndex(val index: Int) { OFF(0), SILENT(1), - NOISEY(2); + NOISY(2); companion object { fun fromInt(index: Int) = values().first { it.index == index } @@ -67,8 +67,8 @@ class PushRulePreference : VectorPreference { private fun refreshSummary() { summary = context.getString(when (index) { NotificationIndex.OFF -> R.string.notification_off - NotificationIndex.SILENT -> R.string.notification_silent - NotificationIndex.NOISEY, null -> R.string.notification_noisy + NotificationIndex.SILENT -> R.string.notification_silent + NotificationIndex.NOISY, null -> R.string.notification_noisy }) } @@ -86,10 +86,10 @@ class PushRulePreference : VectorPreference { NotificationIndex.OFF -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleOff) } - NotificationIndex.SILENT -> { + NotificationIndex.SILENT -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleSilent) } - NotificationIndex.NOISEY -> { + NotificationIndex.NOISY -> { radioGroup?.check(R.id.bingPreferenceRadioBingRuleNoisy) } } @@ -103,7 +103,7 @@ class PushRulePreference : VectorPreference { onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.SILENT) } R.id.bingPreferenceRadioBingRuleNoisy -> { - onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.NOISEY) + onPreferenceChangeListener?.onPreferenceChange(this, NotificationIndex.NOISY) } } } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt index 155986cd8a..2d1bd4d5e5 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/PushRuleDefinitions.kt @@ -19,73 +19,73 @@ package im.vector.app.features.settings.notifications import im.vector.app.core.preference.PushRulePreference import org.matrix.android.sdk.api.pushrules.RuleIds -fun getStandardAction(ruleId: String, index: PushRulePreference.NotificationIndex): StandardAction? { +fun getStandardAction(ruleId: String, index: PushRulePreference.NotificationIndex): StandardActions? { return when (ruleId) { RuleIds.RULE_ID_CONTAIN_DISPLAY_NAME -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.HighlightDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.HighlightDefaultSound } RuleIds.RULE_ID_CONTAIN_USER_NAME -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.HighlightDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.HighlightDefaultSound } RuleIds.RULE_ID_ROOM_NOTIF -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.Highlight + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.Highlight } RuleIds.RULE_ID_ONE_TO_ONE_ROOM -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_ONE_TO_ONE_ENCRYPTED_ROOM -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_ALL_OTHER_MESSAGES_ROOMS -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_ENCRYPTED -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_INVITE_ME -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_CALL -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyRingSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyRingSound } RuleIds.RULE_ID_SUPPRESS_BOTS_NOTIFICATIONS -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.DontNotify - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Disabled - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.NotifyDefaultSound + PushRulePreference.NotificationIndex.OFF -> StandardActions.DontNotify + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Disabled + PushRulePreference.NotificationIndex.NOISY -> StandardActions.NotifyDefaultSound } RuleIds.RULE_ID_TOMBSTONE -> when (index) { - PushRulePreference.NotificationIndex.OFF -> StandardAction.Disabled - PushRulePreference.NotificationIndex.SILENT -> StandardAction.Notify - PushRulePreference.NotificationIndex.NOISEY -> StandardAction.Highlight + PushRulePreference.NotificationIndex.OFF -> StandardActions.Disabled + PushRulePreference.NotificationIndex.SILENT -> StandardActions.Notify + PushRulePreference.NotificationIndex.NOISY -> StandardActions.Highlight } else -> null } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/StandardActions.kt similarity index 51% rename from vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt rename to vector/src/main/java/im/vector/app/features/settings/notifications/StandardActions.kt index 3f482bf626..d6b6165bd9 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/StandardAction.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/StandardActions.kt @@ -18,14 +18,14 @@ package im.vector.app.features.settings.notifications import org.matrix.android.sdk.api.pushrules.Action -sealed class StandardAction( +sealed class StandardActions( val actions: List? ) { - object Notify : StandardAction(actions = listOf(Action.Notify)) - object NotifyDefaultSound : StandardAction(actions = listOf(Action.Notify, Action.Sound())) - object NotifyRingSound : StandardAction(actions = listOf(Action.Notify, Action.Sound(sound = Action.ACTION_OBJECT_VALUE_VALUE_RING))) - object Highlight : StandardAction(actions = listOf(Action.Notify, Action.Highlight(highlight = true))) - object HighlightDefaultSound : StandardAction(actions = listOf(Action.Notify, Action.Highlight(highlight = true), Action.Sound())) - object DontNotify : StandardAction(actions = listOf(Action.DoNotNotify)) - object Disabled : StandardAction(actions = null) + object Notify : StandardActions(actions = listOf(Action.Notify)) + object NotifyDefaultSound : StandardActions(actions = listOf(Action.Notify, Action.Sound())) + object NotifyRingSound : StandardActions(actions = listOf(Action.Notify, Action.Sound(sound = Action.ACTION_OBJECT_VALUE_VALUE_RING))) + object Highlight : StandardActions(actions = listOf(Action.Notify, Action.Highlight(highlight = true))) + object HighlightDefaultSound : StandardActions(actions = listOf(Action.Notify, Action.Highlight(highlight = true), Action.Sound())) + object DontNotify : StandardActions(actions = listOf(Action.DoNotNotify)) + object Disabled : StandardActions(actions = null) } diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt index d6a3fea1bf..c6f7e5db89 100644 --- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsAdvancedNotificationPreferenceFragment.kt @@ -55,7 +55,7 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() val newIndex = newValue as NotificationIndex val standardAction = getStandardAction(ruleAndKind.pushRule.ruleId, newIndex) if (standardAction != null) { - val enabled = standardAction != StandardAction.Disabled + val enabled = standardAction != StandardActions.Disabled val newActions = standardAction.actions displayLoadingView() @@ -93,7 +93,7 @@ class VectorSettingsAdvancedNotificationPreferenceFragment @Inject constructor() val standardAction = getStandardAction(rule.ruleId, it) ?: return@firstOrNull false val indexActions = standardAction.actions ?: listOf() // Check if the input rule matches a rule generated from the static rule definitions - val targetRule = rule.copy(enabled = standardAction != StandardAction.Disabled, actions = indexActions.toJson()) + val targetRule = rule.copy(enabled = standardAction != StandardActions.Disabled, actions = indexActions.toJson()) ruleMatches(rule, targetRule) } } From 5d092ce18a293ebbf14d60ba7c491ba7371d7c0d Mon Sep 17 00:00:00 2001 From: David Langley Date: Fri, 23 Jul 2021 14:29:37 +0100 Subject: [PATCH 6/6] simplify getHighlight logic --- .../java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt index 154209731e..31d7770a9f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/rest/PushRule.kt @@ -105,7 +105,7 @@ data class PushRule( * Get the highlight status. As spec mentions assume false if no tweak present. */ fun getHighlight(): Boolean { - return (getActions().firstOrNull { it is Action.Highlight } as? Action.Highlight)?.highlight.orFalse() + return getActions().filterIsInstance().firstOrNull()?.highlight.orFalse() } /**