From 2f590871fe3144cbdd04d9bd6d7e482e99f87065 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 3 Jul 2024 18:52:15 +0200 Subject: [PATCH] allow to enable/disable notifications per-account --- .../java/com/b44t/messenger/DcContext.java | 8 ++++++++ .../securesms/ConversationListActivity.java | 5 ++++- .../securesms/LogViewFragment.java | 6 ++---- .../accounts/AccountSelectionListAdapter.java | 10 +++++----- .../accounts/AccountSelectionListItem.java | 12 ++++++++---- .../securesms/connect/KeepAliveService.java | 2 +- .../notifications/NotificationCenter.java | 4 ++-- .../NotificationsPreferenceFragment.java | 18 +++++++----------- .../org/thoughtcrime/securesms/util/Prefs.java | 4 ---- src/main/res/xml/preferences_notifications.xml | 4 ---- 10 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/b44t/messenger/DcContext.java b/src/main/java/com/b44t/messenger/DcContext.java index cef7919a3..47f64d163 100644 --- a/src/main/java/com/b44t/messenger/DcContext.java +++ b/src/main/java/com/b44t/messenger/DcContext.java @@ -236,6 +236,14 @@ public class DcContext { return getConfigInt("is_chatmail") == 1; } + public boolean isMuted() { + return getConfigInt("ui.muted") == 1; + } + + public void setMuted(boolean muted) { + setConfigInt("ui.muted", muted? 1 : 0); + } + /** * @return true if at least one chat has location streaming enabled */ diff --git a/src/main/java/org/thoughtcrime/securesms/ConversationListActivity.java b/src/main/java/org/thoughtcrime/securesms/ConversationListActivity.java index 4c9d48d3b..807211dcf 100644 --- a/src/main/java/org/thoughtcrime/securesms/ConversationListActivity.java +++ b/src/main/java/org/thoughtcrime/securesms/ConversationListActivity.java @@ -287,7 +287,10 @@ public class ConversationListActivity extends PassphraseRequiredActionBarActivit int skipId = dcAccounts.getSelectedAccount().getAccountId(); for (int accountId : dcAccounts.getAll()) { if (accountId != skipId) { - unreadCount += dcAccounts.getAccount(accountId).getFreshMsgs().length; + DcContext dcContext = dcAccounts.getAccount(accountId); + if (!dcContext.isMuted()) { + unreadCount += dcContext.getFreshMsgs().length; + } } } diff --git a/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java b/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java index 4293353b4..994015880 100644 --- a/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java +++ b/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java @@ -218,7 +218,7 @@ public class LogViewFragment extends Fragment { PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); final PackageManager pm = context.getPackageManager(); final StringBuilder builder = new StringBuilder(); - + final DcContext dcContext = DcHelper.getContext(context); builder.append("device=") .append(Build.MANUFACTURER).append(" ") @@ -251,8 +251,7 @@ public class LogViewFragment extends Fragment { builder.append("ignoreBatteryOptimizations=").append( powerManager.isIgnoringBatteryOptimizations(context.getPackageName())).append("\n"); } - builder.append("notifications=").append( - Prefs.isNotificationsEnabled(context)).append("\n"); + builder.append("notifications=").append(!dcContext.isMuted()).append("\n"); builder.append("reliableService=").append( Prefs.reliableService(context)).append("\n"); @@ -271,7 +270,6 @@ public class LogViewFragment extends Fragment { } builder.append("\n"); - DcContext dcContext = DcHelper.getContext(context); builder.append(dcContext.getInfo()); return builder.toString(); diff --git a/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListAdapter.java b/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListAdapter.java index 66d9cb891..d70a51a2c 100644 --- a/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListAdapter.java +++ b/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListAdapter.java @@ -39,7 +39,7 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter super(itemView); } - public abstract void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected); + public abstract void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted); public abstract void unbind(@NonNull GlideRequests glideRequests); } @@ -64,8 +64,8 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter return (AccountSelectionListItem) itemView; } - public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected) { - getView().bind(glideRequests, accountId, self, name, addr, unreadCount, selected); + public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted) { + getView().bind(glideRequests, accountId, self, name, addr, unreadCount, selected, isMuted); } @Override @@ -100,11 +100,11 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter String name; String addr = null; int unreadCount = 0; + DcContext dcContext = accounts.getAccount(id); if (id == DcContact.DC_CONTACT_ID_ADD_ACCOUNT) { name = context.getString(R.string.add_account); } else { - DcContext dcContext = accounts.getAccount(id); dcContact = dcContext.getContact(DcContact.DC_CONTACT_ID_SELF); addr = dcContact.getAddr(); name = dcContext.getConfig("displayname"); @@ -116,7 +116,7 @@ public class AccountSelectionListAdapter extends RecyclerView.Adapter ViewHolder holder = (ViewHolder) viewHolder; holder.unbind(glideRequests); - holder.bind(glideRequests, id, dcContact, name, addr, unreadCount, id == selectedAccountId); + holder.bind(glideRequests, id, dcContact, name, addr, unreadCount, id == selectedAccountId, dcContext.isMuted()); } public interface ItemClickListener { diff --git a/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListItem.java b/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListItem.java index 0499cf29b..a2daa5cde 100644 --- a/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListItem.java +++ b/src/main/java/org/thoughtcrime/securesms/accounts/AccountSelectionListItem.java @@ -20,6 +20,7 @@ import org.thoughtcrime.securesms.components.AvatarImageView; import org.thoughtcrime.securesms.mms.GlideRequests; import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.util.DynamicTheme; +import org.thoughtcrime.securesms.util.ThemeUtil; import org.thoughtcrime.securesms.util.ViewUtil; public class AccountSelectionListItem extends LinearLayout { @@ -57,7 +58,7 @@ public class AccountSelectionListItem extends LinearLayout { ViewUtil.setTextViewGravityStart(this.nameView, getContext()); } - public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected) { + public void bind(@NonNull GlideRequests glideRequests, int accountId, DcContact self, String name, String addr, int unreadCount, boolean selected, boolean isMuted) { this.accountId = accountId; Recipient recipient; @@ -70,6 +71,8 @@ public class AccountSelectionListItem extends LinearLayout { } this.contactPhotoImage.setAvatar(glideRequests, recipient, false); + nameView.setCompoundDrawablesWithIntrinsicBounds(isMuted? R.drawable.ic_volume_off_grey600_18dp : 0, 0, 0, 0); + if (selected) { addrView.setTypeface(null, Typeface.BOLD); nameView.setTypeface(null, Typeface.BOLD); @@ -80,7 +83,7 @@ public class AccountSelectionListItem extends LinearLayout { checkbox.setVisibility(View.GONE); } - updateUnreadIndicator(unreadCount); + updateUnreadIndicator(unreadCount, isMuted); setText(name, addr); } @@ -88,10 +91,11 @@ public class AccountSelectionListItem extends LinearLayout { contactPhotoImage.clear(glideRequests); } - private void updateUnreadIndicator(int unreadCount) { + private void updateUnreadIndicator(int unreadCount, boolean isMuted) { if(unreadCount == 0) { unreadIndicator.setVisibility(View.GONE); } else { + final int color = getResources().getColor(isMuted ? (ThemeUtil.isDarkTheme(getContext()) ? R.color.unread_count_muted_dark : R.color.unread_count_muted) : R.color.unread_count); unreadIndicator.setImageDrawable(TextDrawable.builder() .beginConfig() .width(ViewUtil.dpToPx(getContext(), 24)) @@ -99,7 +103,7 @@ public class AccountSelectionListItem extends LinearLayout { .textColor(Color.WHITE) .bold() .endConfig() - .buildRound(String.valueOf(unreadCount), getResources().getColor(R.color.unread_count))); + .buildRound(String.valueOf(unreadCount), color)); unreadIndicator.setVisibility(View.VISIBLE); } } diff --git a/src/main/java/org/thoughtcrime/securesms/connect/KeepAliveService.java b/src/main/java/org/thoughtcrime/securesms/connect/KeepAliveService.java index 0d9b6b7cc..8a2953e20 100644 --- a/src/main/java/org/thoughtcrime/securesms/connect/KeepAliveService.java +++ b/src/main/java/org/thoughtcrime/securesms/connect/KeepAliveService.java @@ -29,7 +29,7 @@ public class KeepAliveService extends Service { // note, that unfortunately, the check for isIgnoringBatteryOptimizations() is not sufficient, // this checks only stock-android settings, several os have additional "optimizers" that ignore this setting. // therefore, the most reliable way to not get killed is a permanent-foreground-notification. - if (Prefs.isNotificationsEnabled(context) && Prefs.reliableService(context)) { + if (Prefs.reliableService(context)) { startSelf(context); } } diff --git a/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java b/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java index 3bf8ce800..ae329deba 100644 --- a/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java +++ b/src/main/java/org/thoughtcrime/securesms/notifications/NotificationCenter.java @@ -333,7 +333,7 @@ public class NotificationCenter { DcChat dcChat = dcContext.getChat(chatId); ChatData chatData = new ChatData(accountId, chatId); - if (!Prefs.isNotificationsEnabled(context) || dcChat.isMuted()) { + if (dcContext.isMuted() || dcChat.isMuted()) { return; } @@ -598,7 +598,7 @@ public class NotificationCenter { } public void maybePlaySendSound(DcChat dcChat) { - if (Prefs.isInChatNotifications(context) && Prefs.isNotificationsEnabled(context) && !dcChat.isMuted()) { + if (Prefs.isInChatNotifications(context) && !dcChat.isMuted()) { InChatSounds.getInstance(context).playSendSound(); } } diff --git a/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java b/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java index 7b0258c9c..c2942c568 100644 --- a/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java +++ b/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java @@ -29,13 +29,13 @@ import org.thoughtcrime.securesms.notifications.FcmReceiveService; import org.thoughtcrime.securesms.util.Prefs; import static android.app.Activity.RESULT_OK; -import static org.thoughtcrime.securesms.connect.DcHelper.CONFIG_ONLY_FETCH_MVBOX; public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragment { private static final int REQUEST_CODE_NOTIFICATION_SELECTED = 1; private CheckBoxPreference ignoreBattery; + private CheckBoxPreference notificationsEnabled; @Override public void onCreate(Bundle paramBundle) { @@ -106,7 +106,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme reliableService.setOnPreferenceChangeListener((preference, newValue) -> { Context context = getContext(); boolean enabled = (Boolean) newValue; // Prefs.reliableService() still has the old value - if (enabled && Prefs.isNotificationsEnabled(context)) { + if (enabled) { KeepAliveService.startSelf(context); } else { context.stopService(new Intent(context, KeepAliveService.class)); @@ -114,15 +114,10 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme return true; }); - CheckBoxPreference notificationsEnabled = this.findPreference("pref_key_enable_notifications"); + notificationsEnabled = this.findPreference("pref_key_enable_notifications"); notificationsEnabled.setOnPreferenceChangeListener((preference, newValue) -> { - Context context = getContext(); - boolean enabled = (Boolean) newValue; // Prefs.isNotificationsEnabled() still has the old value - if (enabled && Prefs.reliableService(context)) { - KeepAliveService.startSelf(context); - } else { - context.stopService(new Intent(context, KeepAliveService.class)); - } + boolean enabled = (Boolean) newValue; + dcContext.setMuted(!enabled); return true; }); } @@ -139,6 +134,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme // update ignoreBattery in onResume() to reflects changes done in the system settings ignoreBattery.setChecked(isIgnoringBatteryOptimizations()); + notificationsEnabled.setChecked(!dcContext.isMuted()); } @Override @@ -237,7 +233,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme public static CharSequence getSummary(Context context) { NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || notificationManager.areNotificationsEnabled()) { - return context.getString(Prefs.isNotificationsEnabled(context) ? R.string.on : R.string.off); + return context.getString(DcHelper.getContext(context).isMuted() ? R.string.off : R.string.on); } else { return context.getString(R.string.disabled_in_system_settings); } diff --git a/src/main/java/org/thoughtcrime/securesms/util/Prefs.java b/src/main/java/org/thoughtcrime/securesms/util/Prefs.java index 2d2758e42..567dd16f3 100644 --- a/src/main/java/org/thoughtcrime/securesms/util/Prefs.java +++ b/src/main/java/org/thoughtcrime/securesms/util/Prefs.java @@ -168,10 +168,6 @@ public class Prefs { return getIntegerPreference(context, PROMPTED_DOZE_MSG_ID_PREF, 0); } - public static boolean isNotificationsEnabled(Context context) { - return getBooleanPreference(context, NOTIFICATION_PREF, true); - } - public static boolean isPushEnabled(Context context) { return BuildConfig.USE_PLAY_SERVICES && getBooleanPreference(context, "pref_push_enabled", true); } diff --git a/src/main/res/xml/preferences_notifications.xml b/src/main/res/xml/preferences_notifications.xml index 5c9a9e1d0..c8c81c24e 100644 --- a/src/main/res/xml/preferences_notifications.xml +++ b/src/main/res/xml/preferences_notifications.xml @@ -46,28 +46,24 @@