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" />