Conversation.java revision c8a994227b9c686d88ee05840544162711a85712
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.content.ContentValues;
20import android.content.Context;
21import android.database.Cursor;
22import android.net.Uri;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26public class Conversation implements Parcelable {
27
28    public long id;
29    public String subject;
30    public long dateMs;
31    public String snippet;
32    public boolean hasAttachments;
33    public Uri messageListUri;
34    public String senders;
35    public int numMessages;
36    public int numDrafts;
37    public int sendingState;
38    public int priority;
39    public boolean read;
40    public boolean starred;
41
42    @Override
43    public int describeContents() {
44        return 0;
45    }
46
47    @Override
48    public void writeToParcel(Parcel dest, int flags) {
49        dest.writeLong(id);
50        dest.writeString(subject);
51        dest.writeLong(dateMs);
52        dest.writeString(snippet);
53        dest.writeByte(hasAttachments ? (byte) 1 : 0);
54        dest.writeParcelable(messageListUri, flags);
55        dest.writeString(senders);
56        dest.writeInt(numMessages);
57        dest.writeInt(numDrafts);
58        dest.writeInt(sendingState);
59        dest.writeInt(priority);
60        dest.writeByte(read ? (byte) 1 : 0);
61        dest.writeByte(starred ? (byte) 1 : 0);
62    }
63
64    private Conversation(Parcel in) {
65        id = in.readLong();
66        subject = in.readString();
67        dateMs = in.readLong();
68        snippet = in.readString();
69        hasAttachments = (in.readByte() != 0);
70        messageListUri = in.readParcelable(null);
71        senders = in.readString();
72        numMessages = in.readInt();
73        numDrafts = in.readInt();
74        sendingState = in.readInt();
75        priority = in.readInt();
76        read = (in.readByte() != 0);
77        starred = (in.readByte() != 0);
78    }
79
80    @Override
81    public String toString() {
82        return "[conversation id=" + id + "]";
83    }
84
85    public static final Creator<Conversation> CREATOR = new Creator<Conversation>() {
86
87        @Override
88        public Conversation createFromParcel(Parcel source) {
89            return new Conversation(source);
90        }
91
92        @Override
93        public Conversation[] newArray(int size) {
94            return new Conversation[size];
95        }
96
97    };
98
99    public static Conversation from(Cursor cursor) {
100        return new Conversation(cursor);
101    }
102
103    private Conversation(Cursor cursor) {
104        if (cursor != null) {
105            id = cursor.getLong(UIProvider.CONVERSATION_ID_COLUMN);
106            dateMs = cursor.getLong(UIProvider.CONVERSATION_DATE_RECEIVED_MS_COLUMN);
107            subject = cursor.getString(UIProvider.CONVERSATION_SUBJECT_COLUMN);
108            snippet = cursor.getString(UIProvider.CONVERSATION_SNIPPET_COLUMN);
109            hasAttachments = cursor.getInt(UIProvider.CONVERSATION_HAS_ATTACHMENTS_COLUMN) == 1;
110            messageListUri = Uri.parse(cursor
111                    .getString(UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN));
112            senders = cursor.getString(UIProvider.CONVERSATION_SENDER_INFO_COLUMN);
113            numMessages = cursor.getInt(UIProvider.CONVERSATION_NUM_MESSAGES_COLUMN);
114            numDrafts = cursor.getInt(UIProvider.CONVERSATION_NUM_DRAFTS_COLUMN);
115            sendingState = cursor.getInt(UIProvider.CONVERSATION_SENDING_STATE_COLUMN);
116            priority = cursor.getInt(UIProvider.CONVERSATION_PRIORITY_COLUMN);
117            read = cursor.getInt(UIProvider.CONVERSATION_READ_COLUMN) == 1;
118            starred = cursor.getInt(UIProvider.CONVERSATION_STARRED_COLUMN) == 1;
119        }
120    }
121
122    // Below are methods that update Conversation data (update/delete)
123
124    public void updateBoolean(Context context, String columnName, boolean value) {
125        // For now, synchronous
126        ContentValues cv = new ContentValues();
127        cv.put(columnName, value);
128        context.getContentResolver().update(messageListUri, cv, null, null);
129    }
130
131    public void delete(Context context) {
132        // For now, synchronous
133        context.getContentResolver().delete(messageListUri, null, null);
134    }
135}
136