diff --git a/src/main/java/com/b44t/messenger/DcContext.java b/src/main/java/com/b44t/messenger/DcContext.java index 8813385d0..e3f959c5e 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("is_muted") == 1; + } + + public void setMuted(boolean muted) { + setConfigInt("is_muted", muted? 1 : 0); + } + // Called for new profiles on chatmail servers that are "single device" initially; // to save server disk space, we make use of that delete all messages immediately after download. public void assumeSingleDevice() { 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/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..ce15a5302 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,6 @@ 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("reliableService=").append( Prefs.reliableService(context)).append("\n"); @@ -271,7 +269,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..782fecbd8 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_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..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"; @@ -168,10 +167,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); } @@ -326,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(); } diff --git a/src/main/res/xml/preferences_notifications.xml b/src/main/res/xml/preferences_notifications.xml index 5c9a9e1d0..af0009f93 100644 --- a/src/main/res/xml/preferences_notifications.xml +++ b/src/main/res/xml/preferences_notifications.xml @@ -3,19 +3,19 @@ @@ -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,35 +39,31 @@