From 2f590871fe3144cbdd04d9bd6d7e482e99f87065 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 3 Jul 2024 18:52:15 +0200 Subject: [PATCH 1/6] 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 @@ From 93a8e3bc0e5f5500a5e99be2ae1dfd1d851fdfb1 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 10 Jul 2024 13:12:59 +0200 Subject: [PATCH 2/6] use is_muted config, migrate old preference value --- src/main/java/com/b44t/messenger/DcContext.java | 4 ++-- .../thoughtcrime/securesms/ApplicationContext.java | 13 +++++++------ .../java/org/thoughtcrime/securesms/util/Prefs.java | 3 +-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/b44t/messenger/DcContext.java b/src/main/java/com/b44t/messenger/DcContext.java index 47f64d163..7b7232155 100644 --- a/src/main/java/com/b44t/messenger/DcContext.java +++ b/src/main/java/com/b44t/messenger/DcContext.java @@ -237,11 +237,11 @@ public class DcContext { } public boolean isMuted() { - return getConfigInt("ui.muted") == 1; + return getConfigInt("is_muted") == 1; } public void setMuted(boolean muted) { - setConfigInt("ui.muted", muted? 1 : 0); + setConfigInt("is_muted", muted? 1 : 0); } /** diff --git a/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index d64ac8f62..91f032332 100644 --- a/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -131,15 +131,16 @@ public class ApplicationContext extends MultiDexApplication { rpc.start(); - // migrating chat backgrounds, added 04/10/23, can be removed after some versions - String backgroundImagePath = Prefs.getStringPreference(this, "pref_chat_background", ""); - if (!backgroundImagePath.isEmpty()) { + // migrating global notifications pref. to per-account config, added 10/July/24 + final String NOTIFICATION_PREF = "pref_key_enable_notifications"; + boolean isMuted = !Prefs.getBooleanPreference(this, NOTIFICATION_PREF, true); + if (isMuted) { for (int accId : dcAccounts.getAll()) { - Prefs.setBackgroundImagePath(this, accId, backgroundImagePath); + dcAccounts.getAccount(accId).setMuted(true); } - Prefs.setStringPreference(this, "pref_chat_background", ""); + Prefs.removePreference(this, NOTIFICATION_PREF); } - // /migrating chat backgrounds + // /migrating global notifications for (int accountId : allAccounts) { dcAccounts.getAccount(accountId).setConfig(CONFIG_VERIFIED_ONE_ON_ONE_CHATS, "1"); diff --git a/src/main/java/org/thoughtcrime/securesms/util/Prefs.java b/src/main/java/org/thoughtcrime/securesms/util/Prefs.java index 567dd16f3..f3031ae07 100644 --- a/src/main/java/org/thoughtcrime/securesms/util/Prefs.java +++ b/src/main/java/org/thoughtcrime/securesms/util/Prefs.java @@ -40,7 +40,6 @@ public class Prefs { public static final String RINGTONE_PREF = "pref_key_ringtone"; private static final String VIBRATE_PREF = "pref_key_vibrate"; private static final String CHAT_VIBRATE = "pref_chat_vibrate_"; // followed by chat-id - private static final String NOTIFICATION_PREF = "pref_key_enable_notifications"; public static final String LED_COLOR_PREF = "pref_led_color"; private static final String CHAT_RINGTONE = "pref_chat_ringtone_"; // followed by chat-id public static final String SCREEN_SECURITY_PREF = "pref_screen_security"; @@ -322,7 +321,7 @@ public class Prefs { PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(key, value).apply(); } - private static void removePreference(Context context, String key) { + public static void removePreference(Context context, String key) { PreferenceManager.getDefaultSharedPreferences(context).edit().remove(key).apply(); } From 4f3a41bd999c985197542f36646baf7f2e4f7eea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asiel=20D=C3=ADaz=20Ben=C3=ADtez?= Date: Wed, 10 Jul 2024 07:59:08 -0400 Subject: [PATCH 3/6] Update src/main/java/org/thoughtcrime/securesms/LogViewFragment.java Co-authored-by: bjoern --- src/main/java/org/thoughtcrime/securesms/LogViewFragment.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java b/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java index 994015880..ce15a5302 100644 --- a/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java +++ b/src/main/java/org/thoughtcrime/securesms/LogViewFragment.java @@ -251,7 +251,6 @@ public class LogViewFragment extends Fragment { builder.append("ignoreBatteryOptimizations=").append( powerManager.isIgnoringBatteryOptimizations(context.getPackageName())).append("\n"); } - builder.append("notifications=").append(!dcContext.isMuted()).append("\n"); builder.append("reliableService=").append( Prefs.reliableService(context)).append("\n"); From dc31b3c5ef20799e8649a59c7cb34b40c068872b Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 10 Jul 2024 14:03:32 +0200 Subject: [PATCH 4/6] rename pref_key_enable_notifications to avoid conflict with legacy --- src/main/res/xml/preferences_notifications.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/res/xml/preferences_notifications.xml b/src/main/res/xml/preferences_notifications.xml index c8c81c24e..7dce3e816 100644 --- a/src/main/res/xml/preferences_notifications.xml +++ b/src/main/res/xml/preferences_notifications.xml @@ -3,7 +3,7 @@ From 5eafe6ad498e3babc69a0ebd091d815648e66130 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 10 Jul 2024 14:35:40 +0200 Subject: [PATCH 5/6] update notifications preference key --- .../securesms/preferences/NotificationsPreferenceFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java b/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java index c2942c568..782fecbd8 100644 --- a/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java +++ b/src/main/java/org/thoughtcrime/securesms/preferences/NotificationsPreferenceFragment.java @@ -114,7 +114,7 @@ public class NotificationsPreferenceFragment extends ListSummaryPreferenceFragme return true; }); - notificationsEnabled = this.findPreference("pref_key_enable_notifications"); + notificationsEnabled = this.findPreference("pref_enable_notifications"); notificationsEnabled.setOnPreferenceChangeListener((preference, newValue) -> { boolean enabled = (Boolean) newValue; dcContext.setMuted(!enabled); From 4e404511a492a13b7322a838711ccc5b0f266226 Mon Sep 17 00:00:00 2001 From: adbenitez Date: Wed, 10 Jul 2024 15:05:07 +0200 Subject: [PATCH 6/6] update notification key in dependencies --- src/main/res/xml/preferences_notifications.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/res/xml/preferences_notifications.xml b/src/main/res/xml/preferences_notifications.xml index 7dce3e816..af0009f93 100644 --- a/src/main/res/xml/preferences_notifications.xml +++ b/src/main/res/xml/preferences_notifications.xml @@ -8,14 +8,14 @@ android:defaultValue="true" /> @@ -24,14 +24,14 @@ android:key="pref_led_color" android:defaultValue="purple" android:title="@string/pref_led_color" - android:dependency="pref_key_enable_notifications" + android:dependency="pref_enable_notifications" android:entries="@array/pref_led_color_entries" android:entryValues="@array/pref_led_color_values" /> @@ -39,7 +39,7 @@