summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/datamodel/action/SyncMessageBatch.java
blob: 972d691e312a238f1083780501e7a2dcffb2121e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.datamodel.action;

import android.database.Cursor;
import android.database.sqlite.SQLiteConstraintException;
import android.provider.Telephony;
import android.provider.Telephony.Mms;
import android.provider.Telephony.Sms;
import android.text.TextUtils;

import com.android.messaging.datamodel.BugleDatabaseOperations;
import com.android.messaging.datamodel.DataModel;
import com.android.messaging.datamodel.DatabaseHelper;
import com.android.messaging.datamodel.DatabaseHelper.ConversationColumns;
import com.android.messaging.datamodel.DatabaseHelper.MessageColumns;
import com.android.messaging.datamodel.DatabaseWrapper;
import com.android.messaging.datamodel.SyncManager.ThreadInfoCache;
import com.android.messaging.datamodel.data.MessageData;
import com.android.messaging.datamodel.data.ParticipantData;
import com.android.messaging.mmslib.pdu.PduHeaders;
import com.android.messaging.sms.DatabaseMessages.LocalDatabaseMessage;
import com.android.messaging.sms.DatabaseMessages.MmsMessage;
import com.android.messaging.sms.DatabaseMessages.SmsMessage;
import com.android.messaging.sms.MmsUtils;
import com.android.messaging.util.Assert;
import com.android.messaging.util.LogUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

/**
 * Update local database with a batch of messages to add/delete in one transaction
 */
class SyncMessageBatch {
    private static final String TAG = LogUtil.BUGLE_TAG;

    // Variables used during executeAction
    private final HashSet<String> mConversationsToUpdate;
    // Cache of thread->conversationId map
    private final ThreadInfoCache mCache;

    // Set of SMS messages to add
    private final ArrayList<SmsMessage> mSmsToAdd;
    // Set of MMS messages to add
    private final ArrayList<MmsMessage> mMmsToAdd;
    // Set of local messages to delete
    private final ArrayList<LocalDatabaseMessage> mMessagesToDelete;

    SyncMessageBatch(final ArrayList<SmsMessage> smsToAdd,
            final ArrayList<MmsMessage> mmsToAdd,
            final ArrayList<LocalDatabaseMessage> messagesToDelete,
            final ThreadInfoCache cache) {
        mSmsToAdd = smsToAdd;
        mMmsToAdd = mmsToAdd;
        mMessagesToDelete = messagesToDelete;
        mCache = cache;
        mConversationsToUpdate = new HashSet<String>();
    }

    void updateLocalDatabase() {
        // Perform local database changes in one transaction
        final DatabaseWrapper db = DataModel.get().getDatabase();
        db.beginTransaction();
        try {
            // Store all the SMS messages
            for (final SmsMessage sms : mSmsToAdd) {
                storeSms(db, sms);
            }
            // Store all the MMS messages
            for (final MmsMessage mms : mMmsToAdd) {
                storeMms(db, mms);
            }
            // Keep track of conversations with messages deleted
            for (final LocalDatabaseMessage message : mMessagesToDelete) {
                mConversationsToUpdate.add(message.getConversationId());
            }
            // Batch delete local messages
            batchDelete(db, DatabaseHelper.MESSAGES_TABLE, MessageColumns._ID,
                    messageListToIds(mMessagesToDelete));

            for (final LocalDatabaseMessage message : mMessagesToDelete) {
                if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                    LogUtil.v(TAG, "SyncMessageBatch: Deleted message " + message.getLocalId()
                            + " for SMS/MMS " + message.getUri() + " with timestamp "
                            + message.getTimestampInMillis());
                }
            }

            // Update conversation state for imported messages, like snippet,
            updateConversations(db);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    private static String[] messageListToIds(final List<LocalDatabaseMessage> messagesToDelete) {
        final String[] ids = new String[messagesToDelete.size()];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = Long.toString(messagesToDelete.get(i).getLocalId());
        }
        return ids;
    }

    /**
     * Store the SMS message into local database.
     *
     * @param sms
     */
    private void storeSms(final DatabaseWrapper db, final SmsMessage sms) {
        if (sms.mBody == null) {
            LogUtil.w(TAG, "SyncMessageBatch: SMS " + sms.mUri + " has no body; adding empty one");
            // try to fix it
            sms.mBody = "";
        }

        if (TextUtils.isEmpty(sms.mAddress)) {
            LogUtil.e(TAG, "SyncMessageBatch: SMS has no address; using unknown sender");
            // try to fix it
            sms.mAddress = ParticipantData.getUnknownSenderDestination();
        }

        // TODO : We need to also deal with messages in a failed/retry state
        final boolean isOutgoing = sms.mType != Sms.MESSAGE_TYPE_INBOX;

        final String otherPhoneNumber = sms.mAddress;

        // A forced resync of all messages should still keep the archived states.
        // The database upgrade code notifies sync manager of this. We need to
        // honor the original customization to this conversation if created.
        final String conversationId = mCache.getOrCreateConversation(db, sms.mThreadId, sms.mSubId,
                DataModel.get().getSyncManager().getCustomizationForThread(sms.mThreadId));
        if (conversationId == null) {
            // Cannot create conversation for this message? This should not happen.
            LogUtil.e(TAG, "SyncMessageBatch: Failed to create conversation for SMS thread "
                    + sms.mThreadId);
            return;
        }
        final ParticipantData self = ParticipantData.getSelfParticipant(sms.getSubId());
        final String selfId =
                BugleDatabaseOperations.getOrCreateParticipantInTransaction(db, self);
        final ParticipantData sender = isOutgoing ?
                self :
                ParticipantData.getFromRawPhoneBySimLocale(otherPhoneNumber, sms.getSubId());
        final String participantId = (isOutgoing ? selfId :
                BugleDatabaseOperations.getOrCreateParticipantInTransaction(db, sender));

        final int bugleStatus = bugleStatusForSms(isOutgoing, sms.mType, sms.mStatus);

        final MessageData message = MessageData.createSmsMessage(
                sms.mUri,
                participantId,
                selfId,
                conversationId,
                bugleStatus,
                sms.mSeen,
                sms.mRead,
                sms.mTimestampSentInMillis,
                sms.mTimestampInMillis,
                sms.mBody);

        // Inserting sms content into messages table
        try {
            BugleDatabaseOperations.insertNewMessageInTransaction(db, message);
        } catch (SQLiteConstraintException e) {
            rethrowSQLiteConstraintExceptionWithDetails(e, db, sms.mUri, sms.mThreadId,
                    conversationId, selfId, participantId);
        }

        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "SyncMessageBatch: Inserted new message " + message.getMessageId()
                    + " for SMS " + message.getSmsMessageUri() + " received at "
                    + message.getReceivedTimeStamp());
        }

        // Keep track of updated conversation for later updating the conversation snippet, etc.
        mConversationsToUpdate.add(conversationId);
    }

    public static int bugleStatusForSms(final boolean isOutgoing, final int type,
            final int status) {
        int bugleStatus = MessageData.BUGLE_STATUS_UNKNOWN;
        // For a message we sync either
        if (isOutgoing) {
            // Outgoing message not yet been sent
            if (type == Telephony.Sms.MESSAGE_TYPE_FAILED ||
                    type == Telephony.Sms.MESSAGE_TYPE_OUTBOX ||
                    type == Telephony.Sms.MESSAGE_TYPE_QUEUED ||
                    (type == Telephony.Sms.MESSAGE_TYPE_SENT &&
                     status == Telephony.Sms.STATUS_FAILED)) {
                // Not sent counts as failed and available for manual resend
                bugleStatus = MessageData.BUGLE_STATUS_OUTGOING_FAILED;
            } else if (status == Sms.STATUS_COMPLETE) {
                bugleStatus = MessageData.BUGLE_STATUS_OUTGOING_DELIVERED;
            } else {
                // Otherwise outgoing message is complete
                bugleStatus = MessageData.BUGLE_STATUS_OUTGOING_COMPLETE;
            }
        } else {
            // All incoming SMS messages are complete
            bugleStatus = MessageData.BUGLE_STATUS_INCOMING_COMPLETE;
        }
        return bugleStatus;
    }

    /**
     * Store the MMS message into local database
     *
     * @param mms
     */
    private void storeMms(final DatabaseWrapper db, final MmsMessage mms) {
        if (mms.mParts.size() < 1) {
            LogUtil.w(TAG, "SyncMessageBatch: MMS " + mms.mUri + " has no parts");
        }

        // TODO : We need to also deal with messages in a failed/retry state
        final boolean isOutgoing = mms.mType != Mms.MESSAGE_BOX_INBOX;
        final boolean isNotification = (mms.mMmsMessageType ==
                PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND);

        final String senderId = mms.mSender;

        // A forced resync of all messages should still keep the archived states.
        // The database upgrade code notifies sync manager of this. We need to
        // honor the original customization to this conversation if created.
        final String conversationId = mCache.getOrCreateConversation(db, mms.mThreadId, mms.mSubId,
                DataModel.get().getSyncManager().getCustomizationForThread(mms.mThreadId));
        if (conversationId == null) {
            LogUtil.e(TAG, "SyncMessageBatch: Failed to create conversation for MMS thread "
                    + mms.mThreadId);
            return;
        }
        final ParticipantData self = ParticipantData.getSelfParticipant(mms.getSubId());
        final String selfId =
                BugleDatabaseOperations.getOrCreateParticipantInTransaction(db, self);
        final ParticipantData sender = isOutgoing ?
                self : ParticipantData.getFromRawPhoneBySimLocale(senderId, mms.getSubId());
        final String participantId = (isOutgoing ? selfId :
                BugleDatabaseOperations.getOrCreateParticipantInTransaction(db, sender));

        final int bugleStatus = MmsUtils.bugleStatusForMms(isOutgoing, isNotification, mms.mType);

        // Import message and all of the parts.
        // TODO : For now we are importing these in the order we found them in the MMS
        // database. Ideally we would load and parse the SMIL which describes how the parts relate
        // to one another.

        // TODO: Need to set correct status on message
        final MessageData message = MmsUtils.createMmsMessage(mms, conversationId, participantId,
                selfId, bugleStatus);

        // Inserting mms content into messages table
        try {
            BugleDatabaseOperations.insertNewMessageInTransaction(db, message);
        } catch (SQLiteConstraintException e) {
            rethrowSQLiteConstraintExceptionWithDetails(e, db, mms.mUri, mms.mThreadId,
                    conversationId, selfId, participantId);
        }

        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "SyncMessageBatch: Inserted new message " + message.getMessageId()
                    + " for MMS " + message.getSmsMessageUri() + " received at "
                    + message.getReceivedTimeStamp());
        }

        // Keep track of updated conversation for later updating the conversation snippet, etc.
        mConversationsToUpdate.add(conversationId);
    }

    // TODO: Remove this after we no longer see this crash (b/18375758)
    private static void rethrowSQLiteConstraintExceptionWithDetails(SQLiteConstraintException e,
            DatabaseWrapper db, String messageUri, long threadId, String conversationId,
            String selfId, String senderId) {
        // Add some extra debug information to the exception for tracking down b/18375758.
        // The default detail message for SQLiteConstraintException tells us that a foreign
        // key constraint failed, but not which one! Messages have foreign keys to 3 tables:
        // conversations, participants (self), participants (sender). We'll query each one
        // to determine which one(s) violated the constraint, and then throw a new exception
        // with those details.

        String foundConversationId = null;
        Cursor cursor = null;
        try {
            // Look for an existing conversation in the db with the conversation id
            cursor = db.rawQuery("SELECT " + ConversationColumns._ID
                    + " FROM " + DatabaseHelper.CONVERSATIONS_TABLE
                    + " WHERE " + ConversationColumns._ID + "=" + conversationId,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                Assert.isTrue(cursor.getCount() == 1);
                foundConversationId = cursor.getString(0);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }

        ParticipantData foundSelfParticipant =
                BugleDatabaseOperations.getExistingParticipant(db, selfId);
        ParticipantData foundSenderParticipant =
                BugleDatabaseOperations.getExistingParticipant(db, senderId);

        String errorMsg = "SQLiteConstraintException while inserting message for " + messageUri
                + "; conversation id from getOrCreateConversation = " + conversationId
                + " (lookup thread = " + threadId + "), found conversation id = "
                + foundConversationId + ", found self participant = "
                + LogUtil.sanitizePII(foundSelfParticipant.getNormalizedDestination())
                + " (lookup id = " + selfId + "), found sender participant = "
                + LogUtil.sanitizePII(foundSenderParticipant.getNormalizedDestination())
                + " (lookup id = " + senderId + ")";
        throw new RuntimeException(errorMsg, e);
    }

    /**
     * Use the tracked latest message info to update conversations, including
     * latest chat message and sort timestamp.
     */
    private void updateConversations(final DatabaseWrapper db) {
        for (final String conversationId : mConversationsToUpdate) {
            if (BugleDatabaseOperations.deleteConversationIfEmptyInTransaction(db,
                    conversationId)) {
                continue;
            }

            final boolean archived = mCache.isArchived(conversationId);
            // Always attempt to auto-switch conversation self id for sync/import case.
            BugleDatabaseOperations.maybeRefreshConversationMetadataInTransaction(db,
                    conversationId, true /*shouldAutoSwitchSelfId*/, archived /*keepArchived*/);
        }
    }


    /**
     * Batch delete database rows by matching a column with a list of values, usually some
     * kind of IDs.
     *
     * @param table
     * @param column
     * @param ids
     * @return Total number of deleted messages
     */
    private static int batchDelete(final DatabaseWrapper db, final String table,
            final String column, final String[] ids) {
        int totalDeleted = 0;
        final int totalIds = ids.length;
        for (int start = 0; start < totalIds; start += MmsUtils.MAX_IDS_PER_QUERY) {
            final int end = Math.min(start + MmsUtils.MAX_IDS_PER_QUERY, totalIds); //excluding
            final int count = end - start;
            final String batchSelection = String.format(
                    Locale.US,
                    "%s IN %s",
                    column,
                    MmsUtils.getSqlInOperand(count));
            final String[] batchSelectionArgs = Arrays.copyOfRange(ids, start, end);
            final int deleted = db.delete(
                    table,
                    batchSelection,
                    batchSelectionArgs);
            totalDeleted += deleted;
        }
        return totalDeleted;
    }
}