1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.data;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.ContentValues;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.Cursor;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.database.sqlite.SQLiteStatement;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcel;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcelable;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.text.TextUtils;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseHelper;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseHelper.MessageColumns;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseWrapper;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.sms.MmsUtils;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.BugleGservices;
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.BugleGservicesKeys;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Dates;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.DebugUtils;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.OsUtil;
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.ArrayList;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.Arrays;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport java.util.List;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class MessageData implements Parcelable {
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String[] sProjection = {
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns._ID,
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.CONVERSATION_ID,
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.SENDER_PARTICIPANT_ID,
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.SELF_PARTICIPANT_ID,
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.SENT_TIMESTAMP,
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.RECEIVED_TIMESTAMP,
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.SEEN,
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.READ,
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.PROTOCOL,
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.STATUS,
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.SMS_MESSAGE_URI,
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.SMS_PRIORITY,
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.SMS_MESSAGE_SIZE,
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.MMS_SUBJECT,
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.MMS_TRANSACTION_ID,
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.MMS_CONTENT_LOCATION,
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.MMS_EXPIRY,
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.RAW_TELEPHONY_STATUS,
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessageColumns.RETRY_START_TIMESTAMP,
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    };
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_ID = 0;
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_CONVERSATION_ID = 1;
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_PARTICIPANT_ID = 2;
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_SELF_ID = 3;
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_SENT_TIMESTAMP = 4;
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_RECEIVED_TIMESTAMP = 5;
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_SEEN = 6;
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_READ = 7;
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_PROTOCOL = 8;
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_BUGLE_STATUS = 9;
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_SMS_MESSAGE_URI = 10;
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_SMS_PRIORITY = 11;
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_SMS_MESSAGE_SIZE = 12;
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_MMS_SUBJECT = 13;
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_MMS_TRANSACTION_ID = 14;
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_MMS_CONTENT_LOCATION = 15;
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_MMS_EXPIRY = 16;
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_RAW_TELEPHONY_STATUS = 17;
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final int INDEX_RETRY_START_TIMESTAMP = 18;
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // SQL statement to insert a "complete" message row (columns based on the projection above).
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String INSERT_MESSAGE_SQL =
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            "INSERT INTO " + DatabaseHelper.MESSAGES_TABLE + " ( "
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    + TextUtils.join(", ", Arrays.copyOfRange(sProjection, 1,
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            INDEX_RETRY_START_TIMESTAMP + 1))
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mMessageId;
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mConversationId;
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mParticipantId;
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mSelfId;
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private long mSentTimestamp;
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private long mReceivedTimestamp;
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private boolean mSeen;
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private boolean mRead;
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mProtocol;
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private Uri mSmsMessageUri;
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mSmsPriority;
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private long mSmsMessageSize;
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mMmsSubject;
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mMmsTransactionId;
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private String mMmsContentLocation;
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private long mMmsExpiry;
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mRawStatus;
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private int mStatus;
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private final ArrayList<MessagePartData> mParts;
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private long mRetryStartTimestamp;
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // PROTOCOL Values
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int PROTOCOL_UNKNOWN = -1;              // Unknown type
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int PROTOCOL_SMS = 0;                   // SMS message
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int PROTOCOL_MMS = 1;                   // MMS message
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int PROTOCOL_MMS_PUSH_NOTIFICATION = 2; // MMS WAP push notification
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Bugle STATUS Values
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_UNKNOWN = 0;
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Outgoing
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_COMPLETE                = 1;
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_DELIVERED               = 2;
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Transitions to either YET_TO_SEND or SEND_AFTER_PROCESSING depending attachments.
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_DRAFT                   = 3;
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_YET_TO_SEND             = 4;
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_SENDING                 = 5;
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_RESENDING               = 6;
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_AWAITING_RETRY          = 7;
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_FAILED                  = 8;
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_OUTGOING_FAILED_EMERGENCY_NUMBER = 9;
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Incoming
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_COMPLETE                   = 100;
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD     = 101;
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_RETRYING_MANUAL_DOWNLOAD   = 102;
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_MANUAL_DOWNLOADING         = 103;
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_RETRYING_AUTO_DOWNLOAD     = 104;
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_AUTO_DOWNLOADING           = 105;
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED            = 106;
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_INCOMING_EXPIRED_OR_NOT_AVAILABLE   = 107;
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final String getStatusDescription(int status) {
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (status) {
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_UNKNOWN:
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "UNKNOWN";
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_COMPLETE:
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_COMPLETE";
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_DELIVERED:
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_DELIVERED";
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_DRAFT:
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_DRAFT";
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_YET_TO_SEND:
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_YET_TO_SEND";
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_SENDING:
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_SENDING";
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_RESENDING:
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_RESENDING";
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_AWAITING_RETRY:
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_AWAITING_RETRY";
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_FAILED:
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_FAILED";
164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_OUTGOING_FAILED_EMERGENCY_NUMBER:
165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "OUTGOING_FAILED_EMERGENCY_NUMBER";
166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_COMPLETE:
167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_COMPLETE";
168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD:
169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_YET_TO_MANUAL_DOWNLOAD";
170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_RETRYING_MANUAL_DOWNLOAD:
171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_RETRYING_MANUAL_DOWNLOAD";
172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_MANUAL_DOWNLOADING:
173d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_MANUAL_DOWNLOADING";
174d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_RETRYING_AUTO_DOWNLOAD:
175d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_RETRYING_AUTO_DOWNLOAD";
176d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_AUTO_DOWNLOADING:
177d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_AUTO_DOWNLOADING";
178d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED:
179d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_DOWNLOAD_FAILED";
180d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case BUGLE_STATUS_INCOMING_EXPIRED_OR_NOT_AVAILABLE:
181d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return "INCOMING_EXPIRED_OR_NOT_AVAILABLE";
182d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            default:
183d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return String.valueOf(status) + " (check MessageData)";
184d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
185d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
186d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
187d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // All incoming messages expect to have status >= BUGLE_STATUS_FIRST_INCOMING
188d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int BUGLE_STATUS_FIRST_INCOMING = BUGLE_STATUS_INCOMING_COMPLETE;
189d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
190d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Detailed MMS failures. Most of the values are defined in PduHeaders. However, a few are
191d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // defined here instead. These are never returned in the MMS HTTP response, but are used
192d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // internally. The values here must not conflict with any of the existing PduHeader values.
193d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int RAW_TELEPHONY_STATUS_UNDEFINED = MmsUtils.PDU_HEADER_VALUE_UNDEFINED;
194d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int RAW_TELEPHONY_STATUS_MESSAGE_TOO_BIG = 10000;
195d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
196d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // Unknown result code for MMS sending/downloading. This is used as the default value
197d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    // for result code returned from platform MMS API.
198d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int UNKNOWN_RESULT_CODE = 0;
199d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
200d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
201d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create an "empty" message
202d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
203d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public MessageData() {
204d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParts = new ArrayList<MessagePartData>();
205d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
206d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
207d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static String[] getProjection() {
208d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return sProjection;
209d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
210d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
211d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
212d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create a draft message for a particular conversation based on supplied content
213d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
214d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MessageData createDraftMessage(final String conversationId,
215d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String selfId, final MessageData content) {
216d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MessageData message = new MessageData();
217d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mStatus = BUGLE_STATUS_OUTGOING_DRAFT;
218d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mProtocol = PROTOCOL_UNKNOWN;
219d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mConversationId = conversationId;
220d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParticipantId = selfId;
221d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mReceivedTimestamp = System.currentTimeMillis();
222d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (content == null) {
223d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            message.mParts.add(MessagePartData.createTextMessagePart(""));
224d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
225d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (!TextUtils.isEmpty(content.mParticipantId)) {
226d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                message.mParticipantId = content.mParticipantId;
227d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
228d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (!TextUtils.isEmpty(content.mMmsSubject)) {
229d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                message.mMmsSubject = content.mMmsSubject;
230d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
231d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            for (final MessagePartData part : content.getParts()) {
232d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                message.mParts.add(part);
233d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
234d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
235d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSelfId = selfId;
236d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return message;
237d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
238d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
239d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
240d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create a draft sms message for a particular conversation
241d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
242d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MessageData createDraftSmsMessage(final String conversationId,
243d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String selfId, final String messageText) {
244d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MessageData message = new MessageData();
245d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mStatus = BUGLE_STATUS_OUTGOING_DRAFT;
246d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mProtocol = PROTOCOL_SMS;
247d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mConversationId = conversationId;
248d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParticipantId = selfId;
249d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSelfId = selfId;
250d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParts.add(MessagePartData.createTextMessagePart(messageText));
251d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mReceivedTimestamp = System.currentTimeMillis();
252d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return message;
253d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
254d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
255d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
256d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create a draft mms message for a particular conversation
257d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
258d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MessageData createDraftMmsMessage(final String conversationId,
259d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String selfId, final String messageText, final String subjectText) {
260d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MessageData message = new MessageData();
261d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mStatus = BUGLE_STATUS_OUTGOING_DRAFT;
262d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mProtocol = PROTOCOL_MMS;
263d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mConversationId = conversationId;
264d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParticipantId = selfId;
265d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSelfId = selfId;
266d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mMmsSubject = subjectText;
267d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mReceivedTimestamp = System.currentTimeMillis();
268d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (!TextUtils.isEmpty(messageText)) {
269d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            message.mParts.add(MessagePartData.createTextMessagePart(messageText));
270d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
271d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return message;
272d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
273d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
274d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
275d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create a message received from a particular number in a particular conversation
276d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
277d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MessageData createReceivedSmsMessage(final Uri uri, final String conversationId,
278d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String participantId, final String selfId, final String messageText,
279d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String subject, final long sent, final long recieved,
280d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final boolean seen, final boolean read) {
281d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MessageData message = new MessageData();
282d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSmsMessageUri = uri;
283d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mConversationId = conversationId;
284d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParticipantId = participantId;
285d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSelfId = selfId;
286d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mProtocol = PROTOCOL_SMS;
287d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mStatus = BUGLE_STATUS_INCOMING_COMPLETE;
288d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mMmsSubject = subject;
289d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mReceivedTimestamp = recieved;
290d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSentTimestamp = sent;
291d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParts.add(MessagePartData.createTextMessagePart(messageText));
292d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSeen = seen;
293d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mRead = read;
294d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return message;
295d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
296d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
297d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
298d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create a message not yet associated with a particular conversation
299d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
300d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MessageData createSharedMessage(final String messageText) {
301d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MessageData message = new MessageData();
302d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mStatus = BUGLE_STATUS_OUTGOING_DRAFT;
303d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (!TextUtils.isEmpty(messageText)) {
304d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            message.mParts.add(MessagePartData.createTextMessagePart(messageText));
305d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
306d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return message;
307d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
308d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
309d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
310d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create a message from Sms table fields
311d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
312d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MessageData createSmsMessage(final String messageUri, final String participantId,
313d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String selfId, final String conversationId, final int bugleStatus,
314d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final boolean seen, final boolean read, final long sent,
315d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final long recieved, final String messageText) {
316d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MessageData message = new MessageData();
317d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParticipantId = participantId;
318d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSelfId = selfId;
319d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mConversationId = conversationId;
320d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSentTimestamp = sent;
321d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mReceivedTimestamp = recieved;
322d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSeen = seen;
323d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mRead = read;
324d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mProtocol = PROTOCOL_SMS;
325d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mStatus = bugleStatus;
326d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSmsMessageUri = Uri.parse(messageUri);
327d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParts.add(MessagePartData.createTextMessagePart(messageText));
328d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return message;
329d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
330d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
331d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
332d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Create a message from Mms table fields
333d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
334d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MessageData createMmsMessage(final String messageUri, final String participantId,
335d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String selfId, final String conversationId, final boolean isNotification,
336d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int bugleStatus, final String contentLocation, final String transactionId,
337d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int smsPriority, final String subject, final boolean seen, final boolean read,
338d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final long size, final int rawStatus, final long expiry, final long sent,
339d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final long received) {
340d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MessageData message = new MessageData();
341d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mParticipantId = participantId;
342d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSelfId = selfId;
343d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mConversationId = conversationId;
344d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSentTimestamp = sent;
345d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mReceivedTimestamp = received;
346d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mMmsContentLocation = contentLocation;
347d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mMmsTransactionId = transactionId;
348d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSeen = seen;
349d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mRead = read;
350d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mStatus = bugleStatus;
351d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mProtocol = (isNotification ? PROTOCOL_MMS_PUSH_NOTIFICATION : PROTOCOL_MMS);
352d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSmsMessageUri = Uri.parse(messageUri);
353d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSmsPriority = smsPriority;
354d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mSmsMessageSize = size;
355d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mMmsSubject = subject;
356d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mMmsExpiry = expiry;
357d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        message.mRawStatus = rawStatus;
358d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (bugleStatus == BUGLE_STATUS_INCOMING_RETRYING_AUTO_DOWNLOAD ||
359d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                bugleStatus == BUGLE_STATUS_OUTGOING_RESENDING) {
360d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Set the retry start timestamp if this message is already in process of retrying
361d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Either as autodownload is starting or sending already in progress (MMS update)
362d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            message.mRetryStartTimestamp = received;
363d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
364d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return message;
365d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
366d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
367d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void addPart(final MessagePartData part) {
368d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (part instanceof PendingAttachmentData) {
369d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Pending attachments may only be added to shared message data that's not associated
370d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // with any particular conversation, in order to store shared images.
371d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            Assert.isTrue(mConversationId == null);
372d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
373d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParts.add(part);
374d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
375d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
376d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public Iterable<MessagePartData> getParts() {
377d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mParts;
378d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
379d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
380d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void bind(final Cursor cursor) {
381d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMessageId = cursor.getString(INDEX_ID);
382d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mConversationId = cursor.getString(INDEX_CONVERSATION_ID);
383d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParticipantId = cursor.getString(INDEX_PARTICIPANT_ID);
384d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSelfId = cursor.getString(INDEX_SELF_ID);
385d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = cursor.getLong(INDEX_SENT_TIMESTAMP);
386d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mReceivedTimestamp = cursor.getLong(INDEX_RECEIVED_TIMESTAMP);
387d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSeen = (cursor.getInt(INDEX_SEEN) != 0);
388d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRead = (cursor.getInt(INDEX_READ) != 0);
389d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mProtocol = cursor.getInt(INDEX_PROTOCOL);
390d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = cursor.getInt(INDEX_BUGLE_STATUS);
391d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String smsMessageUri = cursor.getString(INDEX_SMS_MESSAGE_URI);
392d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSmsMessageUri = (smsMessageUri == null) ? null : Uri.parse(smsMessageUri);
393d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSmsPriority = cursor.getInt(INDEX_SMS_PRIORITY);
394d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSmsMessageSize = cursor.getLong(INDEX_SMS_MESSAGE_SIZE);
395d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsExpiry = cursor.getLong(INDEX_MMS_EXPIRY);
396d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRawStatus = cursor.getInt(INDEX_RAW_TELEPHONY_STATUS);
397d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsSubject = cursor.getString(INDEX_MMS_SUBJECT);
398d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsTransactionId = cursor.getString(INDEX_MMS_TRANSACTION_ID);
399d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsContentLocation = cursor.getString(INDEX_MMS_CONTENT_LOCATION);
400d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRetryStartTimestamp = cursor.getLong(INDEX_RETRY_START_TIMESTAMP);
401d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
402d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
403d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
404d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Bind to the draft message data for a conversation. The conversation's self id is used as
405d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * the draft's self id.
406d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
407d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void bindDraft(final Cursor cursor, final String conversationSelfId) {
408d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        bind(cursor);
409d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSelfId = conversationSelfId;
410d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
411d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
412d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected static String getParticipantId(final Cursor cursor) {
413d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return cursor.getString(INDEX_PARTICIPANT_ID);
414d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
415d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
416d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void populate(final ContentValues values) {
417d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.CONVERSATION_ID, mConversationId);
418d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.SENDER_PARTICIPANT_ID, mParticipantId);
419d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.SELF_PARTICIPANT_ID, mSelfId);
420d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.SENT_TIMESTAMP, mSentTimestamp);
421d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.RECEIVED_TIMESTAMP, mReceivedTimestamp);
422d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.SEEN, mSeen ? 1 : 0);
423d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.READ, mRead ? 1 : 0);
424d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.PROTOCOL, mProtocol);
425d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.STATUS, mStatus);
426d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String smsMessageUri = ((mSmsMessageUri == null) ? null : mSmsMessageUri.toString());
427d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.SMS_MESSAGE_URI, smsMessageUri);
428d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.SMS_PRIORITY, mSmsPriority);
429d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.SMS_MESSAGE_SIZE, mSmsMessageSize);
430d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.MMS_EXPIRY, mMmsExpiry);
431d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.MMS_SUBJECT, mMmsSubject);
432d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.MMS_TRANSACTION_ID, mMmsTransactionId);
433d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.MMS_CONTENT_LOCATION, mMmsContentLocation);
434d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.RAW_TELEPHONY_STATUS, mRawStatus);
435d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        values.put(MessageColumns.RETRY_START_TIMESTAMP, mRetryStartTimestamp);
436d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
437d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
438d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
439d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Note this is not thread safe so callers need to make sure they own the wrapper + statements
440d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * while they call this and use the returned value.
441d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
442d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public SQLiteStatement getInsertStatement(final DatabaseWrapper db) {
443d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final SQLiteStatement insert = db.getStatementInTransaction(
444d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                DatabaseWrapper.INDEX_INSERT_MESSAGE, INSERT_MESSAGE_SQL);
445d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.clearBindings();
446d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindString(INDEX_CONVERSATION_ID, mConversationId);
447d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindString(INDEX_PARTICIPANT_ID, mParticipantId);
448d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindString(INDEX_SELF_ID, mSelfId);
449d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_SENT_TIMESTAMP, mSentTimestamp);
450d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_RECEIVED_TIMESTAMP, mReceivedTimestamp);
451d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_SEEN, mSeen ? 1 : 0);
452d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_READ, mRead ? 1 : 0);
453d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_PROTOCOL, mProtocol);
454d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_BUGLE_STATUS, mStatus);
455d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mSmsMessageUri != null) {
456d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            insert.bindString(INDEX_SMS_MESSAGE_URI, mSmsMessageUri.toString());
457d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
458d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_SMS_PRIORITY, mSmsPriority);
459d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_SMS_MESSAGE_SIZE, mSmsMessageSize);
460d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_MMS_EXPIRY, mMmsExpiry);
461d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mMmsSubject != null) {
462d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            insert.bindString(INDEX_MMS_SUBJECT, mMmsSubject);
463d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
464d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mMmsTransactionId != null) {
465d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            insert.bindString(INDEX_MMS_TRANSACTION_ID, mMmsTransactionId);
466d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
467d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (mMmsContentLocation != null) {
468d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            insert.bindString(INDEX_MMS_CONTENT_LOCATION, mMmsContentLocation);
469d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
470d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_RAW_TELEPHONY_STATUS, mRawStatus);
471d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        insert.bindLong(INDEX_RETRY_START_TIMESTAMP, mRetryStartTimestamp);
472d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return insert;
473d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
474d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
475d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getMessageId() {
476d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mMessageId;
477d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
478d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
479d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getConversationId() {
480d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mConversationId;
481d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
482d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
483d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getParticipantId() {
484d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mParticipantId;
485d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
486d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
487d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getSelfId() {
488d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mSelfId;
489d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
490d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
491d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final long getSentTimeStamp() {
492d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mSentTimestamp;
493d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
494d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
495d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final long getReceivedTimeStamp() {
496d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mReceivedTimestamp;
497d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
498d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
499d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getFormattedReceivedTimeStamp() {
500d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return Dates.getMessageTimeString(mReceivedTimestamp).toString();
501d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
502d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
503d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final int getProtocol() {
504d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mProtocol;
505d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
506d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
507d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final int getStatus() {
508d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mStatus;
509d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
510d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
511d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final Uri getSmsMessageUri() {
512d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mSmsMessageUri;
513d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
514d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
515d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final int getSmsPriority() {
516d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mSmsPriority;
517d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
518d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
519d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final long getSmsMessageSize() {
520d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mSmsMessageSize;
521d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
522d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
523d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getMmsSubject() {
524d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mMmsSubject;
525d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
526d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
527d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void setMmsSubject(final String subject) {
528d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsSubject = subject;
529d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
530d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
531d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getMmsContentLocation() {
532d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mMmsContentLocation;
533d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
534d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
535d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getMmsTransactionId() {
536d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mMmsTransactionId;
537d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
538d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
539d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final boolean getMessageSeen() {
540d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mSeen;
541d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
542d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
543d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
544d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * For incoming MMS messages this returns the retrieve-status value
545d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * For sent MMS messages this returns the response-status value
546d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * See PduHeaders.java for possible values
547d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Otherwise (SMS etc) this is RAW_TELEPHONY_STATUS_UNDEFINED
548d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
549d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final int getRawTelephonyStatus() {
550d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mRawStatus;
551d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
552d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
553d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void setMessageSeen(final boolean hasSeen) {
554d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSeen = hasSeen;
555d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
556d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
557d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final boolean getInResendWindow(final long now) {
558d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final long maxAgeToResend = BugleGservices.get().getLong(
559d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                BugleGservicesKeys.MESSAGE_RESEND_TIMEOUT_MS,
560d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                BugleGservicesKeys.MESSAGE_RESEND_TIMEOUT_MS_DEFAULT);
561d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final long age = now - mRetryStartTimestamp;
562d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return age < maxAgeToResend;
563d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
564d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
565d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final boolean getInDownloadWindow(final long now) {
566d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final long maxAgeToRedownload = BugleGservices.get().getLong(
567d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                BugleGservicesKeys.MESSAGE_DOWNLOAD_TIMEOUT_MS,
568d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                BugleGservicesKeys.MESSAGE_DOWNLOAD_TIMEOUT_MS_DEFAULT);
569d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final long age = now - mRetryStartTimestamp;
570d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return age < maxAgeToRedownload;
571d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
572d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
573d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    static boolean getShowDownloadMessage(final int status) {
574d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (OsUtil.isSecondaryUser()) {
575d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Secondary users can't download mms's. Mms's are downloaded by bugle running as the
576d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // primary user.
577d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return false;
578d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
579d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Should show option for manual download iff status is manual download or failed
580d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (status == BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED ||
581d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                status == BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD ||
582d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // If debug is enabled, allow to download an expired or unavailable message.
583d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                (DebugUtils.isDebugEnabled()
584d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        && status == BUGLE_STATUS_INCOMING_EXPIRED_OR_NOT_AVAILABLE));
585d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
586d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
587d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean canDownloadMessage() {
588d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (OsUtil.isSecondaryUser()) {
589d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Secondary users can't download mms's. Mms's are downloaded by bugle running as the
590d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // primary user.
591d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return false;
592d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
593d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Can download iff status is retrying auto/manual downloading
594d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (mStatus == BUGLE_STATUS_INCOMING_RETRYING_MANUAL_DOWNLOAD ||
595d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mStatus == BUGLE_STATUS_INCOMING_RETRYING_AUTO_DOWNLOAD);
596d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
597d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
598d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean canRedownloadMessage() {
599d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (OsUtil.isSecondaryUser()) {
600d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Secondary users can't download mms's. Mms's are downloaded by bugle running as the
601d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // primary user.
602d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return false;
603d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
604d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Can redownload iff status is manual download not started or download failed
605d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (mStatus == BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED ||
606d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mStatus == BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD ||
607d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                // If debug is enabled, allow to download an expired or unavailable message.
608d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                (DebugUtils.isDebugEnabled()
609d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        && mStatus == BUGLE_STATUS_INCOMING_EXPIRED_OR_NOT_AVAILABLE));
610d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
611d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
612d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    static boolean getShowResendMessage(final int status) {
613d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Should show option to resend iff status is failed
614d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (status == BUGLE_STATUS_OUTGOING_FAILED);
615d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
616d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
617d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    static boolean getOneClickResendMessage(final int status, final int rawStatus) {
618d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Should show option to resend iff status is failed
619d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (status == BUGLE_STATUS_OUTGOING_FAILED
620d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                && rawStatus == RAW_TELEPHONY_STATUS_UNDEFINED);
621d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
622d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
623d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean canResendMessage() {
624d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Manual retry allowed only from failed
625d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (mStatus == BUGLE_STATUS_OUTGOING_FAILED);
626d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
627d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
628d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean canSendMessage() {
629d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Sending messages must be in yet_to_send or awaiting_retry state
630d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (mStatus == BUGLE_STATUS_OUTGOING_YET_TO_SEND ||
631d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                mStatus == BUGLE_STATUS_OUTGOING_AWAITING_RETRY);
632d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
633d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
634d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final boolean getYetToSend() {
635d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (mStatus == BUGLE_STATUS_OUTGOING_YET_TO_SEND);
636d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
637d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
638d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final boolean getIsMms() {
639d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mProtocol == MessageData.PROTOCOL_MMS
640d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                || mProtocol == MessageData.PROTOCOL_MMS_PUSH_NOTIFICATION;
641d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
642d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
643d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final boolean getIsMmsNotification(final int protocol) {
644d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (protocol == MessageData.PROTOCOL_MMS_PUSH_NOTIFICATION);
645d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
646d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
647d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final boolean getIsMmsNotification() {
648d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return getIsMmsNotification(mProtocol);
649d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
650d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
651d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final boolean getIsSms(final int protocol) {
652d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return protocol == (MessageData.PROTOCOL_SMS);
653d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
654d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
655d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final boolean getIsSms() {
656d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return getIsSms(mProtocol);
657d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
658d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
659d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static boolean getIsIncoming(final int status) {
660d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return (status >= MessageData.BUGLE_STATUS_FIRST_INCOMING);
661d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
662d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
663d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean getIsIncoming() {
664d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return getIsIncoming(mStatus);
665d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
666d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
667d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public long getRetryStartTimestamp() {
668d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return mRetryStartTimestamp;
669d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
670d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
671d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final String getMessageText() {
672d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String separator = System.getProperty("line.separator");
673d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final StringBuilder text = new StringBuilder();
674d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (final MessagePartData part : mParts) {
675d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (!part.isAttachment() && !TextUtils.isEmpty(part.getText())) {
676d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (text.length() > 0) {
677d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    text.append(separator);
678d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
679d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                text.append(part.getText());
680d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
681d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
682d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return text.toString();
683d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
684d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
685d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
686d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Takes all captions from attachments and adds them as a prefix to the first text part or
687d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * appends a text part
688d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
689d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void consolidateText() {
690d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String separator = System.getProperty("line.separator");
691d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final StringBuilder captionText = new StringBuilder();
692d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        MessagePartData firstTextPart = null;
693d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int firstTextPartIndex = -1;
694d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (int i = 0; i < mParts.size(); i++) {
695d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final MessagePartData part = mParts.get(i);
696d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (firstTextPart == null && !part.isAttachment()) {
697d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                firstTextPart = part;
698d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                firstTextPartIndex = i;
699d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
700d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (part.isAttachment() && !TextUtils.isEmpty(part.getText())) {
701d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (captionText.length() > 0) {
702d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    captionText.append(separator);
703d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
704d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                captionText.append(part.getText());
705d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
706d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
707d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
708d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (captionText.length() == 0) {
709d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            // Nothing to consolidate
710d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return;
711d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
712d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
713d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (firstTextPart == null) {
714d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            addPart(MessagePartData.createTextMessagePart(captionText.toString()));
715d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
716d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String partText = firstTextPart.getText();
717d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (partText.length() > 0) {
718d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                captionText.append(separator);
719d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                captionText.append(partText);
720d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
721d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mParts.set(firstTextPartIndex,
722d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    MessagePartData.createTextMessagePart(captionText.toString()));
723d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
724d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
725d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
726d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final MessagePartData getFirstAttachment() {
727d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (final MessagePartData part : mParts) {
728d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (part.isAttachment()) {
729d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return part;
730d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
731d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
732d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return null;
733d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
734d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
735d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
736d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Updates the messageId for this message.
737d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Can be used to reset the messageId prior to persisting (which will assign a new messageId)
738d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     *  or can be called on a message that does not yet have a valid messageId to set it.
739d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
740d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void updateMessageId(final String messageId) {
741d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        Assert.isTrue(TextUtils.isEmpty(messageId) || TextUtils.isEmpty(mMessageId));
742d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMessageId = messageId;
743d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
744d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // TODO : This should probably also call updateMessageId on the message parts. We
745d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // may also want to make messages effectively immutable once they have a valid message id.
746d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
747d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
748d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void updateSendingMessage(final String conversationId, final Uri messageUri,
749d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final long timestamp) {
750d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mConversationId = conversationId;
751d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSmsMessageUri = messageUri;
752d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRead = true;
753d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSeen = true;
754d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mReceivedTimestamp = timestamp;
755d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
756d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_YET_TO_SEND;
757d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRetryStartTimestamp = timestamp;
758d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
759d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
760d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void markMessageManualResend(final long timestamp) {
761d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Manual send updates timestamp and transitions back to initial sending status.
762d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mReceivedTimestamp = timestamp;
763d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
764d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_SENDING;
765d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
766d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
767d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void markMessageSending(final long timestamp) {
768d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Initial send
769d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_SENDING;
770d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
771d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
772d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
773d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void markMessageResending(final long timestamp) {
774d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Auto resend of message
775d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_RESENDING;
776d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
777d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
778d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
779d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void markMessageSent(final long timestamp) {
780d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
781d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_COMPLETE;
782d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
783d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
784d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void markMessageFailed(final long timestamp) {
785d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
786d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_FAILED;
787d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
788d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
789d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void markMessageFailedEmergencyNumber(final long timestamp) {
790d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
791d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_FAILED_EMERGENCY_NUMBER;
792d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
793d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
794d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void markMessageNotSent(final long timestamp) {
795d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = timestamp;
796d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = BUGLE_STATUS_OUTGOING_AWAITING_RETRY;
797d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
798d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
799d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void updateSizesForImageParts() {
800d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (final MessagePartData part : getParts()) {
801d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            part.decodeAndSaveSizeIfImage(false /* saveToStorage */);
802d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
803d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
804d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
805d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void setRetryStartTimestamp(final long timestamp) {
806d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRetryStartTimestamp = timestamp;
807d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
808d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
809d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void setRawTelephonyStatus(final int rawStatus) {
810d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRawStatus = rawStatus;
811d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
812d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
813d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public boolean hasContent() {
814d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return !TextUtils.isEmpty(mMmsSubject) ||
815d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                getFirstAttachment() != null ||
816d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                !TextUtils.isEmpty(getMessageText());
817d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
818d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
819d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void bindSelfId(final String selfId) {
820d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSelfId = selfId;
821d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
822d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
823d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public final void bindParticipantId(final String participantId) {
824d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParticipantId = participantId;
825d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
826d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
827d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected MessageData(final Parcel in) {
828d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMessageId = in.readString();
829d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mConversationId = in.readString();
830d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParticipantId = in.readString();
831d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSelfId = in.readString();
832d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSentTimestamp = in.readLong();
833d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mReceivedTimestamp = in.readLong();
834d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSeen = (in.readInt() != 0);
835d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRead = (in.readInt() != 0);
836d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mProtocol = in.readInt();
837d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mStatus = in.readInt();
838d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String smsMessageUri = in.readString();
839d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSmsMessageUri = (smsMessageUri == null ? null : Uri.parse(smsMessageUri));
840d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSmsPriority = in.readInt();
841d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mSmsMessageSize = in.readLong();
842d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsExpiry = in.readLong();
843d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsSubject = in.readString();
844d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsTransactionId = in.readString();
845d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mMmsContentLocation = in.readString();
846d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRawStatus = in.readInt();
847d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mRetryStartTimestamp = in.readLong();
848d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
849d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Read parts
850d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        mParts = new ArrayList<MessagePartData>();
851d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final int partCount = in.readInt();
852d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (int i = 0; i < partCount; i++) {
853d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            mParts.add((MessagePartData) in.readParcelable(MessagePartData.class.getClassLoader()));
854d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
855d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
856d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
857d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
858d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public int describeContents() {
859d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return 0;
860d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
861d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
862d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
863d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void writeToParcel(final Parcel dest, final int flags) {
864d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(mMessageId);
865d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(mConversationId);
866d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(mParticipantId);
867d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(mSelfId);
868d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeLong(mSentTimestamp);
869d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeLong(mReceivedTimestamp);
870d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeInt(mRead ? 1 : 0);
871d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeInt(mSeen ? 1 : 0);
872d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeInt(mProtocol);
873d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeInt(mStatus);
874d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String smsMessageUri = (mSmsMessageUri == null) ? null : mSmsMessageUri.toString();
875d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(smsMessageUri);
876d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeInt(mSmsPriority);
877d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeLong(mSmsMessageSize);
878d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeLong(mMmsExpiry);
879d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(mMmsSubject);
880d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(mMmsTransactionId);
881d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeString(mMmsContentLocation);
882d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeInt(mRawStatus);
883d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeLong(mRetryStartTimestamp);
884d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
885d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Write parts
886d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        dest.writeInt(mParts.size());
887d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (final MessagePartData messagePartData : mParts) {
888d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            dest.writeParcelable(messagePartData, flags);
889d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
890d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
891d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
892d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final Parcelable.Creator<MessageData> CREATOR
893d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            = new Parcelable.Creator<MessageData>() {
894d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        @Override
895d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public MessageData createFromParcel(final Parcel in) {
896d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return new MessageData(in);
897d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
898d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
899d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        @Override
900d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public MessageData[] newArray(final int size) {
901d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return new MessageData[size];
902d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
903d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    };
904d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
905d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
906d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public String toString() {
907d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return toString(mMessageId, mParts);
908d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
909d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
910d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static String toString(String messageId, List<MessagePartData> parts) {
911d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        StringBuilder sb = new StringBuilder();
912d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (messageId != null) {
913d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sb.append(messageId);
914d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sb.append(": ");
915d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
916d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        for (MessagePartData part : parts) {
917d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sb.append(part.toString());
918d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            sb.append(" ");
919d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
920d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return sb.toString();
921d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
922d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
923