1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.example.android.wearable.wear.messaging.model;
17 
18 import android.os.Parcel;
19 import android.os.Parcelable;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.UUID;
23 
24 /** Data representation of a chat. */
25 public class Chat implements Parcelable {
26     private String id;
27     private String alias;
28 
29     // Map using User's id as a hash for profile details for everyone in the conversation.
30     private Map<String, Profile> participants;
31     //TODO: remove me if not being used
32     private Map<String, String> lastReadMessages;
33 
34     private Message lastMessage;
35 
Chat()36     public Chat() {
37         id = UUID.randomUUID().toString();
38     }
39 
Chat(String id)40     public Chat(String id) {
41         this.id = id;
42     }
43 
getAlias()44     public String getAlias() {
45         return alias;
46     }
47 
setAlias(String alias)48     public void setAlias(String alias) {
49         this.alias = alias;
50     }
51 
getParticipants()52     public Map<String, Profile> getParticipants() {
53         return participants;
54     }
55 
setParticipants(Map<String, Profile> participants)56     public void setParticipants(Map<String, Profile> participants) {
57         this.participants = participants;
58     }
59 
generateAliasFromParticipants()60     private void generateAliasFromParticipants() {
61         StringBuilder participantNames = new StringBuilder();
62 
63         // Iterates through each participants then generates a comma separated string as the alias.
64         for (Profile profile : participants.values()) {
65             participantNames.append(profile.getName()).append(",");
66         }
67         participantNames.setLength(participantNames.length() - 1); // Remove last comma (,)
68 
69         this.alias = participantNames.toString();
70     }
71 
72     /**
73      * Sets the participants and adds an alias. The alias is displayed in the chat list.
74      *
75      * @param participants chat participants
76      */
setParticipantsAndAlias(Map<String, Profile> participants)77     public void setParticipantsAndAlias(Map<String, Profile> participants) {
78         setParticipants(participants);
79         generateAliasFromParticipants();
80     }
81 
82     /**
83      * Sets the participants and specified alias
84      *
85      * @param participants chat participants
86      * @param alias chat alias
87      */
setParticipantsAndAlias(Map<String, Profile> participants, String alias)88     public void setParticipantsAndAlias(Map<String, Profile> participants, String alias) {
89         setParticipants(participants);
90         this.alias = alias;
91     }
92 
getLastReadMessages()93     public Map<String, String> getLastReadMessages() {
94         return lastReadMessages;
95     }
96 
setLastReadMessages(Map<String, String> lastReadMessages)97     public void setLastReadMessages(Map<String, String> lastReadMessages) {
98         this.lastReadMessages = lastReadMessages;
99     }
100 
addParticipant(Profile participant)101     public void addParticipant(Profile participant) {
102         participants.put(participant.getId(), participant);
103     }
104 
getLastMessage()105     public Message getLastMessage() {
106         return lastMessage;
107     }
108 
setLastMessage(Message lastMessage)109     public void setLastMessage(Message lastMessage) {
110         this.lastMessage = lastMessage;
111     }
112 
getId()113     public String getId() {
114         return id;
115     }
116 
setId(String id)117     public void setId(String id) {
118         this.id = id;
119     }
120 
121     @Override
describeContents()122     public int describeContents() {
123         return 0;
124     }
125 
126     @Override
writeToParcel(Parcel dest, int flags)127     public void writeToParcel(Parcel dest, int flags) {
128         dest.writeString(this.id);
129         dest.writeString(this.alias);
130         dest.writeParcelable(this.lastMessage, 0);
131         dest.writeInt(this.participants != null ? this.participants.size() : -1);
132         for (Map.Entry<String, Profile> entry : this.participants.entrySet()) {
133             dest.writeString(entry.getKey());
134             dest.writeParcelable(entry.getValue(), flags);
135         }
136 
137         dest.writeInt(this.lastReadMessages != null ? this.lastReadMessages.size() : -1);
138         if (this.lastReadMessages != null) {
139             for (Map.Entry<String, String> entry : this.lastReadMessages.entrySet()) {
140                 dest.writeString(entry.getKey());
141                 dest.writeString(entry.getValue());
142             }
143         }
144     }
145 
Chat(Parcel in)146     protected Chat(Parcel in) {
147         this.id = in.readString();
148         this.alias = in.readString();
149         this.lastMessage = in.readParcelable(Message.class.getClassLoader());
150 
151         int participantsSize = in.readInt();
152         if (participantsSize != -1) {
153             this.participants = new HashMap<>(participantsSize);
154             for (int i = 0; i < participantsSize; i++) {
155                 String key = in.readString();
156                 Profile value = in.readParcelable(Profile.class.getClassLoader());
157                 this.participants.put(key, value);
158             }
159         }
160 
161         int lastReadMessagesSize = in.readInt();
162         if (lastReadMessagesSize != -1) {
163             this.lastReadMessages = new HashMap<>(lastReadMessagesSize);
164             for (int i = 0; i < lastReadMessagesSize; i++) {
165                 String key = in.readString();
166                 String value = in.readString();
167                 this.lastReadMessages.put(key, value);
168             }
169         }
170     }
171 
172     public static final Parcelable.Creator<Chat> CREATOR =
173             new Parcelable.Creator<Chat>() {
174                 @Override
175                 public Chat createFromParcel(Parcel source) {
176                     return new Chat(source);
177                 }
178 
179                 @Override
180                 public Chat[] newArray(int size) {
181                     return new Chat[size];
182                 }
183             };
184 
185     /**
186      * Indicates whether some other object is "equal to" this one.
187      *
188      * @param other - the reference object with which to compare.
189      * @return true/false based on all fields being equal or equally null
190      */
191     @Override
equals(Object other)192     public boolean equals(Object other) {
193         if (this == other) {
194             return true;
195         }
196         if (other == null || getClass() != other.getClass()) {
197             return false;
198         }
199 
200         Chat chat = (Chat) other;
201 
202         if (!id.equals(chat.id)) {
203             return false;
204         }
205         if (alias != null ? !alias.equals(chat.alias) : chat.alias != null) {
206             return false;
207         }
208         if (!participants.equals(chat.participants)) {
209             return false;
210         }
211         if (lastReadMessages != null
212                 ? !lastReadMessages.equals(chat.lastReadMessages)
213                 : chat.lastReadMessages != null) {
214             return false;
215         }
216         return lastMessage != null
217                 ? lastMessage.equals(chat.lastMessage)
218                 : chat.lastMessage == null;
219     }
220 
221     /**
222      * Returns a hash code value for the object.
223      *
224      * @return a hash code value for this object.
225      */
226     @Override
hashCode()227     public int hashCode() {
228         int result = id.hashCode();
229         result = 31 * result + (alias != null ? alias.hashCode() : 0);
230         result = 31 * result + participants.hashCode();
231         result = 31 * result + (lastReadMessages != null ? lastReadMessages.hashCode() : 0);
232         result = 31 * result + (lastMessage != null ? lastMessage.hashCode() : 0);
233         return result;
234     }
235 }
236