From a3162160047dff7742d95a699030dfb39519e0c4 Mon Sep 17 00:00:00 2001
From: Aline Bonnet <aline.bonnet@savoirfairelinux.com>
Date: Fri, 6 Jan 2017 11:11:57 -0500
Subject: [PATCH] ui: fix notifications

When receiving many text messages, as many notifications were created.
Now, there is one notification for every conversation with new
messages.

If you received messages from several senders, the ringID was always
the same. This commit fixes this behavior.

Change-Id: I8c54a7cca1ad395f3b3f9f5fba32c832cfd893bf
Tuleap: #1414
---
 .../java/cx/ring/service/LocalService.java    | 49 ++++++++++---------
 .../main/java/cx/ring/model/TextMessage.java  | 24 +++++++++
 2 files changed, 49 insertions(+), 24 deletions(-)

diff --git a/ring-android/app/src/main/java/cx/ring/service/LocalService.java b/ring-android/app/src/main/java/cx/ring/service/LocalService.java
index c78121991..f2e9a3aa8 100644
--- a/ring-android/app/src/main/java/cx/ring/service/LocalService.java
+++ b/ring-android/app/src/main/java/cx/ring/service/LocalService.java
@@ -900,7 +900,6 @@ public class LocalService extends Service implements Observer<DaemonEvent> {
 
         @Override
         protected Map<String, Conversation> doInBackground(Void... params) {
-            final Map<String, Conversation> ret = new HashMap<>();
             try {
                 final List<HistoryCall> history = mHistoryService.getAll();
                 final List<HistoryText> historyTexts = mHistoryService.getAllTextMessages();
@@ -911,7 +910,7 @@ public class LocalService extends Service implements Observer<DaemonEvent> {
                     CallContact contact = getCreateContact(call.getContactID(), call.getContactKey(), call.getNumber());
 
                     Map.Entry<String, Conversation> merge = null;
-                    for (Map.Entry<String, Conversation> ce : ret.entrySet()) {
+                    for (Map.Entry<String, Conversation> ce : conversations.entrySet()) {
                         Conversation conversation = ce.getValue();
                         if ((contact.getId() > 0 && contact.getId() == conversation.getContact().getId()) || conversation.getContact().hasNumber(call.getNumber())) {
                             merge = ce;
@@ -922,25 +921,25 @@ public class LocalService extends Service implements Observer<DaemonEvent> {
                         Conversation conversation = merge.getValue();
                         if (conversation.getContact().getId() <= 0 && contact.getId() > 0) {
                             conversation.setContact(contact);
-                            ret.remove(merge.getKey());
-                            ret.put(contact.getIds().get(0), conversation);
+                            conversations.remove(merge.getKey());
+                            conversations.put(contact.getIds().get(0), conversation);
                         }
                         conversation.addHistoryCall(call);
                         continue;
                     }
                     String key = contact.getIds().get(0);
-                    if (ret.containsKey(key)) {
-                        ret.get(key).addHistoryCall(call);
+                    if (conversations.containsKey(key)) {
+                        conversations.get(key).addHistoryCall(call);
                     } else {
                         Conversation conversation = new Conversation(contact);
                         conversation.addHistoryCall(call);
-                        ret.put(key, conversation);
+                        conversations.put(key, conversation);
                     }
                 }
 
                 for (HistoryText htext : historyTexts) {
                     CallContact contact = getCreateContact(htext.getContactID(), htext.getContactKey(), htext.getNumber());
-                    Tuple<HistoryEntry, HistoryCall> p = findHistoryByCallId(ret, htext.getCallId());
+                    Tuple<HistoryEntry, HistoryCall> p = findHistoryByCallId(conversations, htext.getCallId());
 
                     if (contact == null && p != null) {
                         contact = p.first.getContact();
@@ -960,12 +959,14 @@ public class LocalService extends Service implements Observer<DaemonEvent> {
                     }
 
                     String key = contact.getIds().get(0);
-                    if (ret.containsKey(key)) {
-                        ret.get(key).addTextMessage(msg);
+                    if (conversations.containsKey(key)) {
+                        if (!conversations.get(key).getTextMessages().contains(msg)) {
+                            conversations.get(key).addTextMessage(msg);
+                        }
                     } else {
                         Conversation c = new Conversation(contact);
                         c.addTextMessage(msg);
-                        ret.put(key, c);
+                        conversations.put(key, c);
                     }
                 }
 
@@ -993,7 +994,7 @@ public class LocalService extends Service implements Observer<DaemonEvent> {
                         Conversation conv = null;
                         ArrayList<String> ids = contact.getIds();
                         for (String id : ids) {
-                            conv = ret.get(id);
+                            conv = conversations.get(id);
                             if (conv != null) {
                                 break;
                             }
@@ -1003,24 +1004,24 @@ public class LocalService extends Service implements Observer<DaemonEvent> {
                         } else {
                             conv = new Conversation(contact);
                             conv.addConference(conf);
-                            ret.put(ids.get(0), conv);
+                            conversations.put(ids.get(0), conv);
                         }
                     }
                 }
-                for (Conversation c : ret.values()) {
+                for (Conversation c : conversations.values()) {
                     Log.w(TAG, "Conversation : " + c.getContact().getId() + " " + c.getContact().getDisplayName() + " " + c.getLastNumberUsed(c.getLastAccountUsed()) + " " + c.getLastInteraction().toString());
                 }
                 for (int i = 0; i < localContactCache.size(); i++) {
                     CallContact contact = localContactCache.valueAt(i);
                     String key = contact.getIds().get(0);
-                    if (!ret.containsKey(key)) {
-                        ret.put(key, new Conversation(contact));
+                    if (!conversations.containsKey(key)) {
+                        conversations.put(key, new Conversation(contact));
                     }
                 }
             } catch (Exception e) {
                 Log.e(TAG, "ConversationLoader doInBackground", e);
             }
-            return ret;
+            return conversations;
         }
     }
 
@@ -1089,13 +1090,13 @@ public class LocalService extends Service implements Observer<DaemonEvent> {
                 mMessageNotificationBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE)
                         .setPriority(NotificationCompat.PRIORITY_HIGH)
                         .setDefaults(NotificationCompat.DEFAULT_ALL)
-                        .setSmallIcon(R.drawable.ic_ring_logo_white)
-                        .setContentTitle(contact.getDisplayName());
-                String[] split = contact.getDisplayName().split(":");
-                if (split.length > 0) {
-                    mLastBlockchainQuery = split[1];
-                    mAccountService.lookupAddress("", "", mLastBlockchainQuery);
-                }
+                        .setSmallIcon(R.drawable.ic_ring_logo_white);
+            }
+            mMessageNotificationBuilder.setContentTitle(contact.getDisplayName());
+            String[] split = contact.getDisplayName().split(":");
+            if (split.length > 0) {
+                mLastBlockchainQuery = split[1];
+                mAccountService.lookupAddress("", "", mLastBlockchainQuery);
             }
             Intent c_intent = new Intent(Intent.ACTION_VIEW)
                     .setClass(this, ConversationActivity.class)
diff --git a/ring-android/libringclient/src/main/java/cx/ring/model/TextMessage.java b/ring-android/libringclient/src/main/java/cx/ring/model/TextMessage.java
index 466adad90..4871d30a4 100644
--- a/ring-android/libringclient/src/main/java/cx/ring/model/TextMessage.java
+++ b/ring-android/libringclient/src/main/java/cx/ring/model/TextMessage.java
@@ -212,4 +212,28 @@ public class TextMessage {
         mNotified = noti;
     }
 
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        TextMessage that = (TextMessage) o;
+
+        if (mTimestamp != that.mTimestamp) {
+            return false;
+        }
+        return mMessage.equals(that.mMessage);
+
+    }
+
+    @Override
+    public int hashCode() {
+        int result = (int) (mTimestamp ^ (mTimestamp >>> 32));
+        result = 31 * result + mMessage.hashCode();
+        return result;
+    }
 }
-- 
GitLab