1 /**
2  * Copyright (c) 2012, 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 
17 package com.android.mail.providers;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 import com.google.common.base.Objects;
23 
24 import java.util.ArrayList;
25 
26 public class ConversationInfo implements Parcelable {
27 
28     public final ArrayList<ParticipantInfo> participantInfos;
29     public int messageCount;
30     public int draftCount;
31     public String firstSnippet;
32     public String firstUnreadSnippet;
33     public String lastSnippet;
34 
ConversationInfo()35     public ConversationInfo() {
36         participantInfos = new ArrayList<ParticipantInfo>();
37     }
38 
ConversationInfo(int messageCount)39     public ConversationInfo(int messageCount) {
40         this.messageCount = messageCount;
41         // message count is a decent approximation of participant count because for all incoming
42         // mailboxes we typically populate the participant list with the sender of each message
43         participantInfos = new ArrayList<ParticipantInfo>(messageCount);
44     }
45 
ConversationInfo(int messageCount, int draft, String first, String firstUnread, String last)46     public ConversationInfo(int messageCount, int draft, String first, String firstUnread,
47                             String last) {
48         // message count is a decent approximation of participant count because for all incoming
49         // mailboxes we typically populate the participant list with the sender of each message
50         participantInfos = new ArrayList<ParticipantInfo>(messageCount);
51         set(messageCount, draft, first, firstUnread, last);
52     }
53 
ConversationInfo(Parcel in)54     private ConversationInfo(Parcel in) {
55         messageCount = in.readInt();
56         draftCount = in.readInt();
57         firstSnippet = in.readString();
58         firstUnreadSnippet = in.readString();
59         lastSnippet = in.readString();
60         participantInfos = in.createTypedArrayList(ParticipantInfo.CREATOR);
61     }
62 
63     /**
64      * Sets all public fields to match the passed in ConversationInfo (does not copy objects)
65      * @param orig ConversationInfo to copy
66      */
overwriteWith(ConversationInfo orig)67     public void overwriteWith(ConversationInfo orig) {
68         participantInfos.clear();
69         participantInfos.addAll(orig.participantInfos);
70         messageCount = orig.messageCount;
71         draftCount = orig.draftCount;
72         firstSnippet = orig.firstSnippet;
73         firstUnreadSnippet = orig.firstUnreadSnippet;
74         lastSnippet = orig.lastSnippet;
75     }
76 
77     @Override
describeContents()78     public int describeContents() {
79         return 0;
80     }
81 
82     @Override
writeToParcel(Parcel dest, int flags)83     public void writeToParcel(Parcel dest, int flags) {
84         dest.writeInt(messageCount);
85         dest.writeInt(draftCount);
86         dest.writeString(firstSnippet);
87         dest.writeString(firstUnreadSnippet);
88         dest.writeString(lastSnippet);
89         dest.writeTypedList(participantInfos);
90     }
91 
fromBlob(byte[] blob)92     public static ConversationInfo fromBlob(byte[] blob) {
93         if (blob == null) {
94             return null;
95         }
96         final Parcel p = Parcel.obtain();
97         p.unmarshall(blob, 0, blob.length);
98         p.setDataPosition(0);
99         final ConversationInfo result = CREATOR.createFromParcel(p);
100         p.recycle();
101         return result;
102     }
103 
toBlob()104     public byte[] toBlob() {
105         final Parcel p = Parcel.obtain();
106         writeToParcel(p, 0);
107         final byte[] result = p.marshall();
108         p.recycle();
109         return result;
110     }
111 
set(int count, int draft, String first, String firstUnread, String last)112     public void set(int count, int draft, String first, String firstUnread, String last) {
113         participantInfos.clear();
114         messageCount = count;
115         draftCount = draft;
116         firstSnippet = first;
117         firstUnreadSnippet = firstUnread;
118         lastSnippet = last;
119     }
120 
reset()121     public void reset() {
122         participantInfos.clear();
123         messageCount = 0;
124         draftCount = 0;
125         firstSnippet = null;
126         firstUnreadSnippet = null;
127         lastSnippet = null;
128     }
129 
addParticipant(ParticipantInfo info)130     public void addParticipant(ParticipantInfo info) {
131         participantInfos.add(info);
132     }
133 
markRead(boolean read)134     public boolean markRead(boolean read) {
135         boolean changed = false;
136         for (ParticipantInfo pi : participantInfos) {
137             changed |= pi.markRead(read);
138         }
139         // Change the firstSnippet only if the conversion has messages.
140         if (messageCount > 0) {
141             if (read) {
142                 firstSnippet = lastSnippet;
143             } else {
144                 firstSnippet = firstUnreadSnippet;
145             }
146         }
147         return changed;
148     }
149 
150     @Override
hashCode()151     public int hashCode() {
152         return Objects.hashCode(messageCount, draftCount, participantInfos, firstSnippet,
153                 lastSnippet, firstUnreadSnippet);
154     }
155 
156     public static final Creator<ConversationInfo> CREATOR = new Creator<ConversationInfo>() {
157 
158         @Override
159         public ConversationInfo createFromParcel(Parcel source) {
160             return new ConversationInfo(source);
161         }
162 
163         @Override
164         public ConversationInfo[] newArray(int size) {
165             return new ConversationInfo[size];
166         }
167 
168     };
169 
170     @Override
toString()171     public String toString() {
172         StringBuilder builder = new StringBuilder();
173         builder.append("[ConversationInfo object: messageCount = ");
174         builder.append(messageCount);
175         builder.append(", draftCount = ");
176         builder.append(draftCount);
177         builder.append(", firstSnippet= ");
178         builder.append(firstSnippet);
179         builder.append(", firstUnreadSnippet = ");
180         builder.append(firstUnreadSnippet);
181         builder.append(", participants = ");
182         builder.append(participantInfos.toString());
183         builder.append("]");
184         return builder.toString();
185     }
186 }
187