ConversationInfo.java revision ae96226b7bbb4038c1aa68fce758ad6384c65889
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    public ConversationInfo(int count, int draft, String first, String firstUnread, String last) {
40        messageInfos = new ArrayList<MessageInfo>();
41        set(count, draft, first, firstUnread, last);
42    }
43
44    private ConversationInfo(Parcel in) {
45        messageCount = in.readInt();
46        draftCount = in.readInt();
47        firstSnippet = in.readString();
48        firstUnreadSnippet = in.readString();
49        lastSnippet = in.readString();
50        messageInfos = in.createTypedArrayList(MessageInfo.CREATOR);
51    }
52
53    @Override
54    public int describeContents() {
55        return 0;
56    }
57
58    @Override
59    public void writeToParcel(Parcel dest, int flags) {
60        dest.writeInt(messageCount);
61        dest.writeInt(draftCount);
62        dest.writeString(firstSnippet);
63        dest.writeString(firstUnreadSnippet);
64        dest.writeString(lastSnippet);
65        dest.writeTypedList(messageInfos);
66    }
67
68    public static ConversationInfo fromBlob(byte[] blob) {
69        if (blob == null) {
70            return null;
71        }
72        final Parcel p = Parcel.obtain();
73        p.unmarshall(blob, 0, blob.length);
74        p.setDataPosition(0);
75        final ConversationInfo result = CREATOR.createFromParcel(p);
76        p.recycle();
77        return result;
78    }
79
80    public byte[] toBlob() {
81        final Parcel p = Parcel.obtain();
82        writeToParcel(p, 0);
83        final byte[] result = p.marshall();
84        p.recycle();
85        return result;
86    }
87
88    public void set(int count, int draft, String first, String firstUnread, String last) {
89        messageInfos.clear();
90        messageCount = count;
91        draftCount = draft;
92        firstSnippet = first;
93        firstUnreadSnippet = firstUnread;
94        lastSnippet = last;
95    }
96
97    public void reset() {
98        messageInfos.clear();
99        messageCount = 0;
100        draftCount = 0;
101        firstSnippet = null;
102        firstUnreadSnippet = null;
103        lastSnippet = null;
104    }
105
106    public void addMessage(MessageInfo info) {
107        messageInfos.add(info);
108    }
109
110    public boolean markRead(boolean read) {
111        boolean changed = false;
112        for (MessageInfo msg : messageInfos) {
113            changed |= msg.markRead(read);
114        }
115        if (read) {
116            firstSnippet = lastSnippet;
117        } else {
118            firstSnippet = firstUnreadSnippet;
119        }
120        return changed;
121    }
122
123    @Override
124    public int hashCode() {
125        return Objects.hashCode(messageCount, draftCount, messageInfos, firstSnippet,
126                lastSnippet, firstUnreadSnippet);
127    }
128
129    public static final Creator<ConversationInfo> CREATOR = new Creator<ConversationInfo>() {
130
131        @Override
132        public ConversationInfo createFromParcel(Parcel source) {
133            return new ConversationInfo(source);
134        }
135
136        @Override
137        public ConversationInfo[] newArray(int size) {
138            return new ConversationInfo[size];
139        }
140
141    };
142
143}
144