use ChatData class instead of Pair, fix notifications preferences

This commit is contained in:
adbenitez
2023-02-03 15:51:59 -05:00
parent 1d6a2350f0
commit 93a17e99e9
4 changed files with 85 additions and 57 deletions
@@ -350,7 +350,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
doReinitializeDraft = true;
}
DcHelper.getNotificationCenter(this).updateVisibleChat(0, 0);
DcHelper.getNotificationCenter(this).clearVisibleChat();
if (isFinishing()) overridePendingTransition(R.anim.fade_scale_in, R.anim.slide_to_right);
quickAttachmentDrawer.onPause();
inputPanel.onPause();
@@ -455,7 +455,7 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity
}
private void onSoundSettings() {
Uri current = Prefs.getChatRingtone(this, chatId);
Uri current = Prefs.getChatRingtone(this, dcContext.getAccountId(), chatId);
Uri defaultUri = Prefs.getNotificationRingtone(this);
if (current == null) current = Settings.System.DEFAULT_NOTIFICATION_URI;
@@ -472,14 +472,14 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity
}
private void onVibrateSettings() {
int checkedItem = Prefs.getChatVibrate(this, chatId).getId();
int checkedItem = Prefs.getChatVibrate(this, dcContext.getAccountId(), chatId).getId();
int[] selectedChoice = new int[]{checkedItem};
new AlertDialog.Builder(this)
.setTitle(R.string.pref_vibrate)
.setSingleChoiceItems(R.array.recipient_vibrate_entries, checkedItem,
(dialog, which) -> selectedChoice[0] = which)
.setPositiveButton(R.string.ok,
(dialog, which) -> Prefs.setChatVibrate(this, chatId, Prefs.VibrateState.fromId(selectedChoice[0])))
(dialog, which) -> Prefs.setChatVibrate(this, dcContext.getAccountId(), chatId, Prefs.VibrateState.fromId(selectedChoice[0])))
.setNegativeButton(R.string.cancel, null)
.show();
}
@@ -588,7 +588,7 @@ public class ProfileActivity extends PassphraseRequiredActionBarActivity
if (defaultValue.equals(value)) value = null;
else if (value == null) value = Uri.EMPTY;
Prefs.setChatRingtone(this, chatId, value);
Prefs.setChatRingtone(this, dcContext.getAccountId(), chatId, value);
}
}
@@ -15,7 +15,6 @@ import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -52,7 +51,7 @@ import java.util.concurrent.TimeUnit;
public class NotificationCenter {
private static final String TAG = NotificationCenter.class.getSimpleName();
@NonNull private final ApplicationContext context;
private volatile Pair<Integer, Integer> visibleChat = new Pair<>(0, 0);
private volatile ChatData visibleChat = null;
private volatile long lastAudibleNotification = 0;
private static final long MIN_AUDIBLE_PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(2);
@@ -63,8 +62,11 @@ public class NotificationCenter {
this.context = ApplicationContext.getInstance(context);
}
private @Nullable Uri effectiveSound(int chatId) { // chatId=0: return app-global setting
@Nullable Uri chatRingtone = Prefs.getChatRingtone(context, chatId);
private @Nullable Uri effectiveSound(ChatData chatData) { // chatData=null: return app-global setting
if (chatData == null) {
chatData = new ChatData(0, 0);
}
@Nullable Uri chatRingtone = Prefs.getChatRingtone(context, chatData.accountId, chatData.chatId);
if (chatRingtone!=null) {
return chatRingtone;
} else {
@@ -76,8 +78,11 @@ public class NotificationCenter {
return null;
}
private boolean effectiveVibrate(int chatId) { // chatId=0: return app-global setting
Prefs.VibrateState vibrate = Prefs.getChatVibrate(context, chatId);
private boolean effectiveVibrate(ChatData chatData) { // chatData=null: return app-global setting
if (chatData == null) {
chatData = new ChatData(0, 0);
}
Prefs.VibrateState vibrate = Prefs.getChatVibrate(context, chatData.accountId, chatData.chatId);
if (vibrate == Prefs.VibrateState.ENABLED) {
return true;
} else if (vibrate == Prefs.VibrateState.DISABLED) {
@@ -86,9 +91,12 @@ public class NotificationCenter {
return Prefs.isNotificationVibrateEnabled(context);
}
private boolean requiresIndependentChannel(int chatId) {
return Prefs.getChatRingtone(context, chatId) != null
|| Prefs.getChatVibrate(context, chatId) != Prefs.VibrateState.DEFAULT;
private boolean requiresIndependentChannel(ChatData chatData) {
if (chatData == null) {
chatData = new ChatData(0, 0);
}
return Prefs.getChatRingtone(context, chatData.accountId, chatData.chatId) != null
|| Prefs.getChatVibrate(context, chatData.accountId, chatData.chatId) != Prefs.VibrateState.DEFAULT;
}
private int getLedArgb(String ledColor) {
@@ -109,32 +117,32 @@ public class NotificationCenter {
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE());
}
private PendingIntent getOpenChatIntent(int accountId, int chatId) {
private PendingIntent getOpenChatIntent(ChatData chatData) {
Intent intent = new Intent(context, ConversationActivity.class);
intent.putExtra(ConversationActivity.ACCOUNT_ID_EXTRA, accountId);
intent.putExtra(ConversationActivity.CHAT_ID_EXTRA, chatId);
intent.putExtra(ConversationActivity.ACCOUNT_ID_EXTRA, chatData.accountId);
intent.putExtra(ConversationActivity.CHAT_ID_EXTRA, chatData.chatId);
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
return TaskStackBuilder.create(context)
.addNextIntentWithParentStack(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE());
}
private PendingIntent getRemoteReplyIntent(int accountId, int chatId) {
private PendingIntent getRemoteReplyIntent(ChatData chatData) {
Intent intent = new Intent(RemoteReplyReceiver.REPLY_ACTION);
intent.setClass(context, RemoteReplyReceiver.class);
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
intent.putExtra(RemoteReplyReceiver.ACCOUNT_ID_EXTRA, accountId);
intent.putExtra(RemoteReplyReceiver.CHAT_ID_EXTRA, chatId);
intent.putExtra(RemoteReplyReceiver.ACCOUNT_ID_EXTRA, chatData.accountId);
intent.putExtra(RemoteReplyReceiver.CHAT_ID_EXTRA, chatData.chatId);
intent.setPackage(context.getPackageName());
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE());
}
private PendingIntent getMarkAsReadIntent(int accountId, int chatId, boolean markNoticed) {
private PendingIntent getMarkAsReadIntent(ChatData chatData, boolean markNoticed) {
Intent intent = new Intent(markNoticed? MarkReadReceiver.MARK_NOTICED_ACTION : MarkReadReceiver.CANCEL_ACTION);
intent.setClass(context, MarkReadReceiver.class);
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
intent.putExtra(MarkReadReceiver.ACCOUNT_ID_EXTRA, accountId);
intent.putExtra(MarkReadReceiver.CHAT_ID_EXTRA, chatId);
intent.putExtra(MarkReadReceiver.ACCOUNT_ID_EXTRA, chatData.accountId);
intent.putExtra(MarkReadReceiver.CHAT_ID_EXTRA, chatData.chatId);
intent.setPackage(context.getPackageName());
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | IntentUtils.FLAG_MUTABLE());
}
@@ -188,7 +196,7 @@ public class NotificationCenter {
}
// full name is "ch_msgV_HASH" or "ch_msgV_HASH.ACCOUNTID.CHATID"
private String computeChannelId(String ledColor, boolean vibrate, @Nullable Uri ringtone, int accountId, int chatId) {
private String computeChannelId(String ledColor, boolean vibrate, @Nullable Uri ringtone, ChatData chatData) {
String channelId = CH_MSG_PREFIX;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
@@ -198,8 +206,8 @@ public class NotificationCenter {
String hash = String.format("%X", new BigInteger(1, md.digest())).substring(0, 16);
channelId = CH_MSG_PREFIX + CH_MSG_VERSION + "_" + hash;
if (chatId!=0) {
channelId += String.format(".%d.%d", accountId, chatId);
if (chatData != null) {
channelId += String.format(".%d.%d", chatData.accountId, chatData.chatId);
}
} catch(Exception e) {
@@ -208,8 +216,8 @@ public class NotificationCenter {
return channelId;
}
// return Pair<accountId, chatId> from "ch_msgV_HASH.ACCOUNTID.CHATID" or Pair<0, 0>
private Pair<Integer, Integer> parseNotificationChannelChat(String channelId) {
// return ChatData(ACCOUNTID, CHATID) from "ch_msgV_HASH.ACCOUNTID.CHATID" or null
private ChatData parseNotificationChannelChat(String channelId) {
try {
int point = channelId.lastIndexOf(".");
if (point>0) {
@@ -218,11 +226,11 @@ public class NotificationCenter {
point = channelId.lastIndexOf(".");
if (point>0) {
int accountId = Integer.parseInt(channelId.substring(point + 1));
return new Pair<>(accountId, chatId);
return new ChatData(accountId, chatId);
}
}
} catch(Exception e) { }
return new Pair<>(0, 0);
return null;
}
private String getNotificationChannelGroup(NotificationManagerCompat notificationManager) {
@@ -233,20 +241,19 @@ public class NotificationCenter {
return CH_GRP_MSG;
}
private String getNotificationChannel(NotificationManagerCompat notificationManager, int accountId, DcChat dcChat) {
int chatId = dcChat.getId();
private String getNotificationChannel(NotificationManagerCompat notificationManager, ChatData chatData, DcChat dcChat) {
String channelId = CH_MSG_PREFIX;
if (notificationChannelsSupported()) {
try {
// get all values we'll use as settings for the NotificationChannel
String ledColor = Prefs.getNotificationLedColor(context);
boolean defaultVibrate = effectiveVibrate(chatId);
@Nullable Uri ringtone = effectiveSound(chatId);
boolean isIndependent = requiresIndependentChannel(chatId);
boolean defaultVibrate = effectiveVibrate(chatData);
@Nullable Uri ringtone = effectiveSound(chatData);
boolean isIndependent = requiresIndependentChannel(chatData);
// get channel id from these settings
channelId = computeChannelId(ledColor, defaultVibrate, ringtone, accountId, isIndependent? chatId : 0);
channelId = computeChannelId(ledColor, defaultVibrate, ringtone, isIndependent? chatData : null);
// user-visible name of the channel -
// we just use the name of the chat or "Default"
@@ -270,8 +277,8 @@ public class NotificationCenter {
channels.get(i).setName(name);
} else {
// ... another message channel, delete if it is not in use.
Pair<Integer, Integer> currChat = parseNotificationChannelChat(currChannelId);
if (!currChannelId.equals(computeChannelId(ledColor, effectiveVibrate(currChat.second), effectiveSound(currChat.second), currChat.first, currChat.second))) {
ChatData currChat = parseNotificationChannelChat(currChannelId);
if (!currChannelId.equals(computeChannelId(ledColor, effectiveVibrate(currChat), effectiveSound(currChat), currChat))) {
notificationManager.deleteNotificationChannel(currChannelId);
}
}
@@ -323,12 +330,13 @@ public class NotificationCenter {
DcContext dcContext = context.dcAccounts.getAccount(accountId);
DcChat dcChat = dcContext.getChat(chatId);
ChatData chatData = new ChatData(accountId, chatId);
if (!Prefs.isNotificationsEnabled(context) || dcChat.isMuted()) {
return;
}
if (visibleChat.equals(Pair.create(accountId, chatId))) {
if (Util.equals(visibleChat, chatData)) {
if (Prefs.isInChatNotifications(context)) {
InChatSounds.getInstance(context).playIncomingSound();
}
@@ -357,7 +365,7 @@ public class NotificationCenter {
// even without a name or message displayed,
// it makes sense to use separate notification channels and to open the respective chat directly -
// the user may eg. have chosen a different sound
String notificationChannel = getNotificationChannel(notificationManager, accountId, dcChat);
String notificationChannel = getNotificationChannel(notificationManager, chatData, dcChat);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, notificationChannel)
.setSmallIcon(R.drawable.icon_notification)
.setColor(context.getResources().getColor(R.color.delta_primary))
@@ -365,8 +373,8 @@ public class NotificationCenter {
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOnlyAlertOnce(!signal)
.setContentText(line)
.setDeleteIntent(getMarkAsReadIntent(accountId, chatId, false))
.setContentIntent(getOpenChatIntent(accountId, chatId));
.setDeleteIntent(getMarkAsReadIntent(chatData, false))
.setContentIntent(getOpenChatIntent(chatData));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
builder.setGroup(GRP_MSG + "." + accountId);
@@ -389,11 +397,11 @@ public class NotificationCenter {
// set sound, vibrate, led for systems that do not have notification channels
if (!notificationChannelsSupported()) {
if (signal) {
Uri sound = effectiveSound(chatId);
Uri sound = effectiveSound(chatData);
if (sound != null && !TextUtils.isEmpty(sound.toString())) {
builder.setSound(sound);
}
boolean vibrate = effectiveVibrate(chatId);
boolean vibrate = effectiveVibrate(chatData);
if (vibrate) {
builder.setDefaults(Notification.DEFAULT_VIBRATE);
}
@@ -437,8 +445,8 @@ public class NotificationCenter {
if (privacy.isDisplayContact() && privacy.isDisplayMessage()
&& !Prefs.isScreenLockEnabled(context)) {
try {
PendingIntent inNotificationReplyIntent = getRemoteReplyIntent(accountId, chatId);
PendingIntent markReadIntent = getMarkAsReadIntent(accountId, chatId, true);
PendingIntent inNotificationReplyIntent = getRemoteReplyIntent(chatData);
PendingIntent markReadIntent = getMarkAsReadIntent(chatData, true);
NotificationCompat.Action markAsReadAction = new NotificationCompat.Action(R.drawable.check,
context.getString(R.string.notify_dismiss),
@@ -572,17 +580,33 @@ public class NotificationCenter {
public void updateVisibleChat(int accountId, int chatId) {
Util.runOnAnyBackgroundThread(() -> {
visibleChat = new Pair<>(accountId, chatId);
if (accountId != 0 && chatId != 0) {
visibleChat = new ChatData(accountId, chatId);
removeNotifications(accountId, chatId);
} else {
visibleChat = null;
}
});
}
public void clearVisibleChat() {
visibleChat = null;
}
public void maybePlaySendSound(DcChat dcChat) {
if (Prefs.isInChatNotifications(context) && Prefs.isNotificationsEnabled(context) && !dcChat.isMuted()) {
InChatSounds.getInstance(context).playSendSound();
}
}
private class ChatData {
public int accountId;
public int chatId;
public ChatData(int accountId, int chatId) {
this.accountId = accountId;
this.chatId = chatId;
}
}
}
+14 -10
View File
@@ -225,17 +225,19 @@ public class Prefs {
setStringPreference(context, RINGTONE_PREF, ringtone.toString());
}
public static void setChatRingtone(Context context, int chatId, Uri ringtone) {
public static void setChatRingtone(Context context, int accountId, int chatId, Uri ringtone) {
final String KEY = (accountId != 0 && chatId != 0)? CHAT_RINGTONE+accountId+"."+chatId : CHAT_RINGTONE;
if(ringtone!=null) {
setStringPreference(context, CHAT_RINGTONE+chatId, ringtone.toString());
setStringPreference(context, KEY, ringtone.toString());
}
else {
removePreference(context, CHAT_RINGTONE+chatId);
removePreference(context, KEY);
}
}
public static @Nullable Uri getChatRingtone(Context context, int chatId) {
String result = getStringPreference(context, CHAT_RINGTONE+chatId, null);
public static @Nullable Uri getChatRingtone(Context context, int accountId, int chatId) {
final String KEY = (accountId != 0 && chatId != 0)? CHAT_RINGTONE+accountId+"."+chatId : CHAT_RINGTONE;
String result = getStringPreference(context, KEY, null);
return result==null? null : Uri.parse(result);
}
@@ -254,17 +256,19 @@ public class Prefs {
return getBooleanPreference(context, VIBRATE_PREF, true);
}
public static void setChatVibrate(Context context, int chatId, VibrateState vibrateState) {
public static void setChatVibrate(Context context, int accountId, int chatId, VibrateState vibrateState) {
final String KEY = (accountId != 0 && chatId != 0)? CHAT_VIBRATE+accountId+"."+chatId : CHAT_VIBRATE;
if(vibrateState!=VibrateState.DEFAULT) {
setIntegerPrefrence(context, CHAT_VIBRATE+chatId, vibrateState.getId());
setIntegerPrefrence(context, KEY, vibrateState.getId());
}
else {
removePreference(context, CHAT_VIBRATE+chatId);
removePreference(context, KEY);
}
}
public static VibrateState getChatVibrate(Context context, int chatId) {
return VibrateState.fromId(getIntegerPreference(context, CHAT_VIBRATE+chatId, VibrateState.DEFAULT.getId()));
public static VibrateState getChatVibrate(Context context, int accountId, int chatId) {
final String KEY = (accountId != 0 && chatId != 0)? CHAT_VIBRATE+accountId+"."+chatId : CHAT_VIBRATE;
return VibrateState.fromId(getIntegerPreference(context, KEY, VibrateState.DEFAULT.getId()));
}
// led