Browse Source

Updates to notifications handling

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 5 years ago
parent
commit
323d9e16b1

+ 2 - 4
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) {

+ 2 - 2
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);
         }
     }
 

+ 45 - 43
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 (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 (decryptedPushMessage.getType().equals("chat") || decryptedPushMessage.getType().equals("room")) {
+                audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT);
+            } else {
+                audioAttributesBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_REQUEST);
+            }
 
-                    mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start());
-                    mediaPlayer.setOnCompletionListener(MediaPlayer::release);
+            MediaPlayer mediaPlayer = new MediaPlayer();
+            try {
+                mediaPlayer.setDataSource(context, soundUri);
+                mediaPlayer.setAudioAttributes(audioAttributesBuilder.build());
 
-                    mediaPlayer.prepareAsync();
-                } catch (IOException e) {
-                    Log.e(TAG, "Failed to set data source");
-                }
+                mediaPlayer.setOnPreparedListener(mp -> mediaPlayer.start());
+                mediaPlayer.setOnCompletionListener(MediaPlayer::release);
 
+                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);
-                    }
+        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")) {

+ 57 - 9
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 (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 (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) &&
+                                roomTokenOrId.equals(statusBarNotification.getNotification().extras.getString(BundleKeys.KEY_ROOM_TOKEN))) {
                             notificationManager.cancel(statusBarNotification.getId());
                         }
                     }

+ 1 - 0
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";
 }