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