Home | History | Annotate | Download | only in providers
      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 
     35     public ConversationInfo() {
     36         participantInfos = new ArrayList<ParticipantInfo>();
     37     }
     38 
     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 
     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 
     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      */
     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
     78     public int describeContents() {
     79         return 0;
     80     }
     81 
     82     @Override
     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 
     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 
    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 
    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 
    121     public void reset() {
    122         participantInfos.clear();
    123         messageCount = 0;
    124         draftCount = 0;
    125         firstSnippet = null;
    126         firstUnreadSnippet = null;
    127         lastSnippet = null;
    128     }
    129 
    130     public void addParticipant(ParticipantInfo info) {
    131         participantInfos.add(info);
    132     }
    133 
    134     public boolean markRead(boolean read) {
    135         boolean changed = false;
    136         for (ParticipantInfo pi : participantInfos) {
    137             changed |= pi.markRead(read);
    138         }
    139         if (read) {
    140             firstSnippet = lastSnippet;
    141         } else {
    142             firstSnippet = firstUnreadSnippet;
    143         }
    144         return changed;
    145     }
    146 
    147     @Override
    148     public int hashCode() {
    149         return Objects.hashCode(messageCount, draftCount, participantInfos, firstSnippet,
    150                 lastSnippet, firstUnreadSnippet);
    151     }
    152 
    153     public static final Creator<ConversationInfo> CREATOR = new Creator<ConversationInfo>() {
    154 
    155         @Override
    156         public ConversationInfo createFromParcel(Parcel source) {
    157             return new ConversationInfo(source);
    158         }
    159 
    160         @Override
    161         public ConversationInfo[] newArray(int size) {
    162             return new ConversationInfo[size];
    163         }
    164 
    165     };
    166 
    167     @Override
    168     public String toString() {
    169         StringBuilder builder = new StringBuilder();
    170         builder.append("[ConversationInfo object: messageCount = ");
    171         builder.append(messageCount);
    172         builder.append(", draftCount = ");
    173         builder.append(draftCount);
    174         builder.append(", firstSnippet= ");
    175         builder.append(firstSnippet);
    176         builder.append(", firstUnreadSnippet = ");
    177         builder.append(firstUnreadSnippet);
    178         builder.append(", participants = ");
    179         builder.append(participantInfos.toString());
    180         builder.append("]");
    181         return builder.toString();
    182     }
    183 }
    184