From 323d9e16b13655c49b883f0a942add36739d64f3 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Tue, 25 Jun 2019 22:41:02 +0200 Subject: [PATCH] Updates to notifications handling Signed-off-by: Mario Danic --- .../talk/controllers/CallController.java | 6 +- .../talk/controllers/ChatController.java | 4 +- .../talk/jobs/NotificationWorker.java | 94 ++++++++++--------- .../talk/utils/NotificationUtils.java | 66 +++++++++++-- .../talk/utils/bundle/BundleKeys.java | 1 + 5 files changed, 110 insertions(+), 61 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/controllers/CallController.java b/app/src/main/java/com/nextcloud/talk/controllers/CallController.java index 943be80f6..83277f709 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/CallController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/CallController.java @@ -87,8 +87,6 @@ import io.reactivex.Observable; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; -import io.reactivex.functions.BooleanSupplier; -import io.reactivex.functions.Consumer; import io.reactivex.schedulers.Schedulers; import me.zhanghai.android.effortlesspermissions.AfterPermissionDenied; import me.zhanghai.android.effortlesspermissions.EffortlessPermissions; @@ -1123,9 +1121,9 @@ public class CallController extends BaseController { } if (!conversationUser.hasSpreedCapabilityWithName("no-ping") && !TextUtils.isEmpty(roomId)) { - NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomId); + NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomId); } else if (!TextUtils.isEmpty(roomToken)) { - NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomToken); + NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomToken); } if (!hasExternalSignalingServer) { diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java index a27205368..c2e983387 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/ChatController.java @@ -566,9 +566,9 @@ public class ChatController extends BaseController implements MessagesListAdapte private void cancelNotificationsForCurrentConversation() { if (!conversationUser.hasSpreedCapabilityWithName("no-ping") && !TextUtils.isEmpty(roomId)) { - NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomId); + NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomId); } else if (!TextUtils.isEmpty(roomToken)){ - NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser, roomToken); + NotificationUtils.cancelExistingNotificationsForRoom(getApplicationContext(), conversationUser, roomToken); } } diff --git a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java index 69b4e293d..c7a78733e 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java +++ b/app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java @@ -258,14 +258,12 @@ public class NotificationWorker extends Worker { } } + intent.setAction(Long.toString(System.currentTimeMillis())); + PendingIntent pendingIntent = PendingIntent.getActivity(context, - 0, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT); + 0, intent, PendingIntent.FLAG_ONE_SHOT); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); - - - CRC32 crc32 = new CRC32(); - Uri uri = Uri.parse(signatureVerification.getUserEntity().getBaseUrl()); String baseUrl = uri.getHost(); @@ -291,9 +289,12 @@ public class NotificationWorker extends Worker { notificationBuilder.setColor(context.getResources().getColor(R.color.colorPrimary)); } - String groupName = signatureVerification.getUserEntity().getId() + "@" + decryptedPushMessage.getId(); - crc32.update(groupName.getBytes()); - notificationBuilder.setGroup(Long.toString(crc32.getValue())); + Bundle notificationInfo = new Bundle(); + notificationInfo.putLong(BundleKeys.KEY_INTERNAL_USER_ID, signatureVerification.getUserEntity().getId()); + // could be an ID or a TOKEN + notificationInfo.putString(BundleKeys.KEY_ROOM_TOKEN, decryptedPushMessage.getId()); + notificationInfo.putLong(BundleKeys.KEY_NOTIFICATION_ID, decryptedPushMessage.getNotificationId()); + notificationBuilder.setExtras(notificationInfo); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -326,9 +327,13 @@ public class NotificationWorker extends Worker { notificationBuilder.setContentIntent(pendingIntent); - String stringForCrc = decryptedPushMessage.getSubject() + " " + signatureVerification - .getUserEntity().getDisplayName() + " " + signatureVerification.getUserEntity - ().getBaseUrl() + System.currentTimeMillis(); + String stringForCrc = String.valueOf(System.currentTimeMillis()); + + CRC32 crc32 = new CRC32(); + + String groupName = signatureVerification.getUserEntity().getId() + "@" + decryptedPushMessage.getId(); + crc32.update(groupName.getBytes()); + notificationBuilder.setGroup(Long.toString(crc32.getValue())); crc32 = new CRC32(); crc32.update(stringForCrc.getBytes()); @@ -350,45 +355,43 @@ public class NotificationWorker extends Worker { } - if (notificationManager != null) { - notificationManager.notify((int) crc32.getValue(), notificationBuilder.build()); + notificationManager.notify((int) crc32.getValue(), notificationBuilder.build()); - if (soundUri != null & !ApplicationWideCurrentRoomHolder.getInstance().isInCall() && - DoNotDisturbUtils.shouldPlaySound()) { - AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder().setContentType - (AudioAttributes.CONTENT_TYPE_SONIFICATION); - - if (decryptedPushMessage.getType().equals("chat") || decryptedPushMessage.getType().equals("room")) { - audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT); - } else { - audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST); - } - - MediaPlayer mediaPlayer = new MediaPlayer(); - try { - mediaPlayer.setDataSource(context, soundUri); - mediaPlayer.setAudioAttributes(audioAttributesBuilder.build()); - - mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start()); - mediaPlayer.setOnCompletionListener(MediaPlayer::release); - - mediaPlayer.prepareAsync(); - } catch (IOException e) { - Log.e(TAG, "Failed to set data source"); - } + if (soundUri != null & !ApplicationWideCurrentRoomHolder.getInstance().isInCall() && + DoNotDisturbUtils.shouldPlaySound()) { + AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder().setContentType + (AudioAttributes.CONTENT_TYPE_SONIFICATION); + if (decryptedPushMessage.getType().equals("chat") || decryptedPushMessage.getType().equals("room")) { + audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT); + } else { + audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST); } + MediaPlayer mediaPlayer = new MediaPlayer(); + try { + mediaPlayer.setDataSource(context, soundUri); + mediaPlayer.setAudioAttributes(audioAttributesBuilder.build()); - if (DoNotDisturbUtils.shouldVibrate(appPreferences.getShouldVibrateSetting())) { - Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); + mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start()); + mediaPlayer.setOnCompletionListener(MediaPlayer::release); - if (vibrator != null) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); - } else { - vibrator.vibrate(500); - } + mediaPlayer.prepareAsync(); + } catch (IOException e) { + Log.e(TAG, "Failed to set data source"); + } + + } + + + if (DoNotDisturbUtils.shouldVibrate(appPreferences.getShouldVibrateSetting())) { + Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); + + if (vibrator != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE)); + } else { + vibrator.vibrate(500); } } } @@ -444,8 +447,7 @@ public class NotificationWorker extends Worker { intent = new Intent(context, MainActivity.class); } - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); if (!signatureVerification.getUserEntity().hasSpreedCapabilityWithName ("no-ping")) { diff --git a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java index 6effccfa9..192af3078 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/NotificationUtils.java @@ -21,6 +21,7 @@ package com.nextcloud.talk.utils; import android.annotation.TargetApi; +import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationChannelGroup; import android.app.NotificationManager; @@ -28,8 +29,10 @@ import android.content.Context; import android.os.Build; import android.service.notification.StatusBarNotification; import android.text.TextUtils; + import com.nextcloud.talk.R; import com.nextcloud.talk.models.database.UserEntity; +import com.nextcloud.talk.utils.bundle.BundleKeys; import java.util.zip.CRC32; @@ -79,26 +82,71 @@ public class NotificationUtils { } } - public static void cancelExistingNotifications(Context context, UserEntity conversationUser, - String roomTokenOrId) { + public static void cancelAllNotificationsForAccount(Context context, UserEntity conversationUser) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && conversationUser.getId() != -1 && context != null) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - CRC32 crc32 = new CRC32(); - String groupName = conversationUser.getId() + "@" + roomTokenOrId; - crc32.update(groupName.getBytes()); - String crc32GroupString = Long.toString(crc32.getValue()); + if (notificationManager != null) { + StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications(); + Notification notification; + for (StatusBarNotification statusBarNotification : statusBarNotifications) { + notification = statusBarNotification.getNotification(); + + if (notification != null && !notification.extras.isEmpty()) { + if (conversationUser.getId() == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID)) { + notificationManager.cancel(statusBarNotification.getId()); + } + } + } + } + } + + } + + public static void cancelExistingNotificationWithId(Context context, UserEntity conversationUser, long notificationId) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && conversationUser.getId() != -1 && + context != null) { + + NotificationManager notificationManager = + (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (notificationManager != null) { StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications(); + Notification notification; for (StatusBarNotification statusBarNotification : statusBarNotifications) { + notification = statusBarNotification.getNotification(); - if (statusBarNotification.getNotification() != null && - !TextUtils.isEmpty(statusBarNotification.getNotification().getGroup())) { - if (statusBarNotification.getNotification().getGroup().equals(crc32GroupString)) { + if (notification != null && !notification.extras.isEmpty()) { + if (conversationUser.getId() == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) && + notificationId == notification.extras.getLong(BundleKeys.KEY_NOTIFICATION_ID)) { + notificationManager.cancel(statusBarNotification.getId()); + } + } + } + } + } + } + + public static void cancelExistingNotificationsForRoom(Context context, UserEntity conversationUser, + String roomTokenOrId) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M && conversationUser.getId() != -1 && + context != null) { + + NotificationManager notificationManager = + (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + if (notificationManager != null) { + StatusBarNotification[] statusBarNotifications = notificationManager.getActiveNotifications(); + Notification notification; + for (StatusBarNotification statusBarNotification : statusBarNotifications) { + notification = statusBarNotification.getNotification(); + + if (notification != null && !notification.extras.isEmpty()) { + if (conversationUser.getId() == notification.extras.getLong(BundleKeys.KEY_INTERNAL_USER_ID) && + roomTokenOrId.equals(statusBarNotification.getNotification().extras.getString(BundleKeys.KEY_ROOM_TOKEN))) { notificationManager.cancel(statusBarNotification.getId()); } } diff --git a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java index 925677264..faef6f195 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java +++ b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java @@ -55,4 +55,5 @@ public class BundleKeys { public static final String KEY_FILE_PATHS = "KEY_FILE_PATHS"; public static final String KEY_ACCOUNT = "KEY_ACCOUNT"; public static final String KEY_FILE_ID = "KEY_FILE_ID"; + public static final String KEY_NOTIFICATION_ID = "KEY_NOTIFICATION_ID"; }