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     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     @Override
     62     public int describeContents() {
     63         return 0;
     64     }
     65 
     66     @Override
     67     public void writeToParcel(Parcel dest, int flags) {
     68         dest.writeInt(messageCount);
     69         dest.writeInt(draftCount);
     70         dest.writeString(firstSnippet);
     71         dest.writeString(firstUnreadSnippet);
     72         dest.writeString(lastSnippet);
     73         dest.writeTypedList(messageInfos);
     74     }
     75 
     76     public static ConversationInfo fromBlob(byte[] blob) {
     77         if (blob == null) {
     78             return null;
     79         }
     80         final Parcel p = Parcel.obtain();
     81         p.unmarshall(blob, 0, blob.length);
     82         p.setDataPosition(0);
     83         final ConversationInfo result = CREATOR.createFromParcel(p);
     84         p.recycle();
     85         return result;
     86     }
     87 
     88     public byte[] toBlob() {
     89         final Parcel p = Parcel.obtain();
     90         writeToParcel(p, 0);
     91         final byte[] result = p.marshall();
     92         p.recycle();
     93         return result;
     94     }
     95 
     96     public void set(int count, int draft, String first, String firstUnread, String last) {
     97         messageInfos.clear();
     98         messageCount = count;
     99         draftCount = draft;
    100         firstSnippet = first;
    101         firstUnreadSnippet = firstUnread;
    102         lastSnippet = last;
    103     }
    104 
    105     public void reset() {
    106         messageInfos.clear();
    107         messageCount = 0;
    108         draftCount = 0;
    109         firstSnippet = null;
    110         firstUnreadSnippet = null;
    111         lastSnippet = null;
    112     }
    113 
    114     public void addMessage(MessageInfo info) {
    115         messageInfos.add(info);
    116     }
    117 
    118     public boolean markRead(boolean read) {
    119         boolean changed = false;
    120         for (MessageInfo msg : messageInfos) {
    121             changed |= msg.markRead(read);
    122         }
    123         if (read) {
    124             firstSnippet = lastSnippet;
    125         } else {
    126             firstSnippet = firstUnreadSnippet;
    127         }
    128         return changed;
    129     }
    130 
    131     @Override
    132     public int hashCode() {
    133         return Objects.hashCode(messageCount, draftCount, messageInfos, firstSnippet,
    134                 lastSnippet, firstUnreadSnippet);
    135     }
    136 
    137     public static final Creator<ConversationInfo> CREATOR = new Creator<ConversationInfo>() {
    138 
    139         @Override
    140         public ConversationInfo createFromParcel(Parcel source) {
    141             return new ConversationInfo(source);
    142         }
    143 
    144         @Override
    145         public ConversationInfo[] newArray(int size) {
    146             return new ConversationInfo[size];
    147         }
    148 
    149     };
    150 
    151     @Override
    152     public String toString() {
    153         StringBuilder builder = new StringBuilder();
    154         builder.append("[ConversationInfo object: messageCount = ");
    155         builder.append(messageCount);
    156         builder.append(", draftCount = ");
    157         builder.append(draftCount);
    158         builder.append(", firstSnippet= ");
    159         builder.append(firstSnippet);
    160         builder.append(", firstUnreadSnippet = ");
    161         builder.append(firstUnreadSnippet);
    162         builder.append(", messageInfos = ");
    163         builder.append(messageInfos.toString());
    164         builder.append("]");
    165         return builder.toString();
    166     }
    167 }
    168