Additional refactoring/clean-up

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
This commit is contained in:
Dariusz Olszewski 2022-05-11 22:01:19 +02:00 committed by Andy Scherzinger
parent d4bdd88588
commit 93e0751901
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
3 changed files with 33 additions and 38 deletions

View File

@ -294,8 +294,7 @@ public class NotificationWorker extends Worker {
intent.setAction(Long.toString(System.currentTimeMillis())); intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getActivity(context, PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
0, intent, 0);
Uri uri = Uri.parse(signatureVerification.getUserEntity().getBaseUrl()); Uri uri = Uri.parse(signatureVerification.getUserEntity().getBaseUrl());
String baseUrl = uri.getHost(); String baseUrl = uri.getHost();
@ -343,44 +342,41 @@ public class NotificationWorker extends Worker {
notificationBuilder.setContentIntent(pendingIntent); notificationBuilder.setContentIntent(pendingIntent);
CRC32 crc32 = new CRC32();
String groupName = signatureVerification.getUserEntity().getId() + "@" + decryptedPushMessage.getId(); String groupName = signatureVerification.getUserEntity().getId() + "@" + decryptedPushMessage.getId();
crc32.update(groupName.getBytes()); notificationBuilder.setGroup(Long.toString(calculateCRC32(groupName)));
notificationBuilder.setGroup(Long.toString(crc32.getValue()));
// notificationId
crc32 = new CRC32();
String stringForCrc = String.valueOf(System.currentTimeMillis());
crc32.update(stringForCrc.getBytes());
StatusBarNotification activeStatusBarNotification = StatusBarNotification activeStatusBarNotification =
NotificationUtils.INSTANCE.findNotificationForRoom(context, NotificationUtils.INSTANCE.findNotificationForRoom(context,
signatureVerification.getUserEntity(), decryptedPushMessage.getId()); signatureVerification.getUserEntity(), decryptedPushMessage.getId());
int notificationId; // NOTE - systemNotificationId is an internal ID used on the device only.
// It is NOT the same as the notification ID used in communication with the server.
int systemNotificationId;
if (activeStatusBarNotification != null) { if (activeStatusBarNotification != null) {
notificationId = activeStatusBarNotification.getId(); systemNotificationId = activeStatusBarNotification.getId();
} else { } else {
notificationId = (int) crc32.getValue(); systemNotificationId = (int) calculateCRC32(String.valueOf(System.currentTimeMillis()));
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
CHAT.equals(decryptedPushMessage.getType()) && CHAT.equals(decryptedPushMessage.getType()) &&
decryptedPushMessage.getNotificationUser() != null) { decryptedPushMessage.getNotificationUser() != null) {
sendChatNotification(notificationBuilder, activeStatusBarNotification, notificationId); prepareChatNotification(notificationBuilder, activeStatusBarNotification, systemNotificationId);
} else {
sendNotificationWithId(notificationId, notificationBuilder.build());
} }
sendNotification(systemNotificationId, notificationBuilder.build());
}
private long calculateCRC32(String s) {
CRC32 crc32 = new CRC32();
crc32.update(s.getBytes());
return crc32.getValue();
} }
@RequiresApi(api = Build.VERSION_CODES.N) @RequiresApi(api = Build.VERSION_CODES.N)
private void sendChatNotification(NotificationCompat.Builder notificationBuilder, private void prepareChatNotification(NotificationCompat.Builder notificationBuilder,
StatusBarNotification activeStatusBarNotification, StatusBarNotification activeStatusBarNotification,
int notificationId) { int systemNotificationId) {
final NotificationUser notificationUser = decryptedPushMessage.getNotificationUser(); final NotificationUser notificationUser = decryptedPushMessage.getNotificationUser();
final String userType = notificationUser.getType(); final String userType = notificationUser.getType();
@ -397,7 +393,7 @@ public class NotificationWorker extends Worker {
.setBot("bot".equals(userType)); .setBot("bot".equals(userType));
notificationBuilder.setOnlyAlertOnce(true); notificationBuilder.setOnlyAlertOnce(true);
addReplyAction(notificationBuilder, notificationId); addReplyAction(notificationBuilder, systemNotificationId);
if ("user".equals(userType) || "guest".equals(userType)) { if ("user".equals(userType) || "guest".equals(userType)) {
String baseUrl = signatureVerification.getUserEntity().getBaseUrl(); String baseUrl = signatureVerification.getUserEntity().getBaseUrl();
@ -408,10 +404,10 @@ public class NotificationWorker extends Worker {
} }
notificationBuilder.setStyle(getStyle(person.build(), style)); notificationBuilder.setStyle(getStyle(person.build(), style));
sendNotificationWithId(notificationId, notificationBuilder.build());
} }
private void addReplyAction(NotificationCompat.Builder notificationBuilder, int notificationId) { @RequiresApi(api = Build.VERSION_CODES.N)
private void addReplyAction(NotificationCompat.Builder notificationBuilder, int systemNotificationId) {
String replyLabel = context.getResources().getString(R.string.nc_reply); String replyLabel = context.getResources().getString(R.string.nc_reply);
RemoteInput remoteInput = new RemoteInput.Builder(NotificationUtils.KEY_DIRECT_REPLY) RemoteInput remoteInput = new RemoteInput.Builder(NotificationUtils.KEY_DIRECT_REPLY)
@ -421,13 +417,12 @@ public class NotificationWorker extends Worker {
// Build a PendingIntent for the reply action // Build a PendingIntent for the reply action
Intent actualIntent = new Intent(context, DirectReplyReceiver.class); Intent actualIntent = new Intent(context, DirectReplyReceiver.class);
// NOTE - This notificationId is an internal ID used on the device only. // NOTE - systemNotificationId is an internal ID used on the device only.
// It is NOT the same as the notification ID used in communication with the server. // It is NOT the same as the notification ID used in communication with the server.
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_NOTIFICATION_ID(), notificationId); actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_SYSTEM_NOTIFICATION_ID(), systemNotificationId);
actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), decryptedPushMessage.getId()); actualIntent.putExtra(BundleKeys.INSTANCE.getKEY_ROOM_TOKEN(), decryptedPushMessage.getId());
PendingIntent replyPendingIntent = PendingIntent replyPendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), PendingIntent.getBroadcast(context, systemNotificationId, actualIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationId, actualIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action replyAction = NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(R.drawable.ic_reply, replyLabel, replyPendingIntent) new NotificationCompat.Action.Builder(R.drawable.ic_reply, replyLabel, replyPendingIntent)
@ -458,7 +453,7 @@ public class NotificationWorker extends Worker {
return newStyle; return newStyle;
} }
private void sendNotificationWithId(int notificationId, Notification notification) { private void sendNotification(int notificationId, Notification notification) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(notificationId, notification); notificationManager.notify(notificationId, notification);
@ -469,8 +464,7 @@ public class NotificationWorker extends Worker {
} }
if (!notification.category.equals(Notification.CATEGORY_CALL) || !muteCall) { if (!notification.category.equals(Notification.CATEGORY_CALL) || !muteCall) {
Uri soundUri = NotificationUtils.INSTANCE.getMessageRingtoneUri(getApplicationContext(), Uri soundUri = NotificationUtils.INSTANCE.getMessageRingtoneUri(context, appPreferences);
appPreferences);
if (soundUri != null && !ApplicationWideCurrentRoomHolder.getInstance().isInCall() && if (soundUri != null && !ApplicationWideCurrentRoomHolder.getInstance().isInCall() &&
(DoNotDisturbUtils.INSTANCE.shouldPlaySound() || importantConversation)) { (DoNotDisturbUtils.INSTANCE.shouldPlaySound() || importantConversation)) {
AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder().setContentType AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder().setContentType

View File

@ -38,8 +38,8 @@ import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.models.json.generic.GenericOverall import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.NotificationUtils import com.nextcloud.talk.utils.NotificationUtils
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_NOTIFICATION_ID
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_SYSTEM_NOTIFICATION_ID
import com.nextcloud.talk.utils.database.user.UserUtils import com.nextcloud.talk.utils.database.user.UserUtils
import io.reactivex.Observer import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
@ -60,7 +60,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
lateinit var context: Context lateinit var context: Context
lateinit var currentUser: UserEntity lateinit var currentUser: UserEntity
private var notificationId: Int? = null private var systemNotificationId: Int? = null
private var roomToken: String? = null private var roomToken: String? = null
private var replyMessage: CharSequence? = null private var replyMessage: CharSequence? = null
@ -72,9 +72,9 @@ class DirectReplyReceiver : BroadcastReceiver() {
context = receiveContext context = receiveContext
currentUser = userUtils!!.currentUser!! currentUser = userUtils!!.currentUser!!
// NOTE - This notificationId is an internal ID used on the device only. // NOTE - systemNotificationId is an internal ID used on the device only.
// It is NOT the same as the notification ID used in communication with the server. // It is NOT the same as the notification ID used in communication with the server.
notificationId = intent!!.getIntExtra(KEY_NOTIFICATION_ID, 0) systemNotificationId = intent!!.getIntExtra(KEY_SYSTEM_NOTIFICATION_ID, 0)
roomToken = intent.getStringExtra(KEY_ROOM_TOKEN) roomToken = intent.getStringExtra(KEY_ROOM_TOKEN)
replyMessage = getMessageText(intent) replyMessage = getMessageText(intent)
@ -130,7 +130,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
// https://developer.android.com/training/notify-user/build-notification#messaging-best-practices // https://developer.android.com/training/notify-user/build-notification#messaging-best-practices
// Find the original (active) notification // Find the original (active) notification
val previousNotification = findActiveNotification(notificationId!!) ?: return val previousNotification = findActiveNotification(systemNotificationId!!) ?: return
// Recreate builder based on the active notification // Recreate builder based on the active notification
val previousBuilder = NotificationCompat.Builder(context, previousNotification) val previousBuilder = NotificationCompat.Builder(context, previousNotification)
@ -152,6 +152,6 @@ class DirectReplyReceiver : BroadcastReceiver() {
previousBuilder.setStyle(previousStyle) previousBuilder.setStyle(previousStyle)
// Update the active notification. // Update the active notification.
NotificationManagerCompat.from(context).notify(notificationId!!, previousBuilder.build()) NotificationManagerCompat.from(context).notify(systemNotificationId!!, previousBuilder.build())
} }
} }

View File

@ -71,4 +71,5 @@ object BundleKeys {
val KEY_FORWARD_MSG_FLAG = "KEY_FORWARD_MSG_FLAG" val KEY_FORWARD_MSG_FLAG = "KEY_FORWARD_MSG_FLAG"
val KEY_FORWARD_MSG_TEXT = "KEY_FORWARD_MSG_TEXT" val KEY_FORWARD_MSG_TEXT = "KEY_FORWARD_MSG_TEXT"
val KEY_FORWARD_HIDE_SOURCE_ROOM = "KEY_FORWARD_HIDE_SOURCE_ROOM" val KEY_FORWARD_HIDE_SOURCE_ROOM = "KEY_FORWARD_HIDE_SOURCE_ROOM"
val KEY_SYSTEM_NOTIFICATION_ID = "KEY_SYSTEM_NOTIFICATION_ID"
} }