2012-07-19 21:22:03 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-19 21:22:03 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-19 21:22:03 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.util.Log;
|
2012-07-20 02:23:49 +00:00
|
|
|
import android.view.LayoutInflater;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.CursorAdapter;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterCipher;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MessageDisplayHelper;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2012-10-28 23:04:24 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2012-10-28 23:04:24 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
2012-10-29 23:51:42 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord.GroupData;
|
2012-10-28 23:04:24 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.NotificationMmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.service.MessageNotifier;
|
2012-10-28 23:04:24 +00:00
|
|
|
import org.thoughtcrime.securesms.util.InvalidMessageException;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
import ws.com.google.android.mms.MmsException;
|
2012-10-28 23:04:24 +00:00
|
|
|
import ws.com.google.android.mms.pdu.MultimediaMessagePdu;
|
|
|
|
import ws.com.google.android.mms.pdu.NotificationInd;
|
|
|
|
import ws.com.google.android.mms.pdu.PduHeaders;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
|
|
|
import java.util.LinkedHashMap;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A cursor adapter for a conversation thread. Ultimately
|
|
|
|
* used by ComposeMessageActivity to display a conversation
|
|
|
|
* thread in a ListActivity.
|
2012-07-19 21:22:03 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class ConversationAdapter extends CursorAdapter {
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static final int MAX_CACHE_SIZE = 40;
|
|
|
|
|
|
|
|
private final TouchListener touchListener = new TouchListener();
|
|
|
|
private final LinkedHashMap<String,MessageRecord> messageRecordCache;
|
|
|
|
private final Handler failedIconClickHandler;
|
2012-07-19 21:22:03 +00:00
|
|
|
private final long threadId;
|
2011-12-20 18:20:44 +00:00
|
|
|
private final Context context;
|
|
|
|
private final Recipients recipients;
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
private final MasterCipher masterCipher;
|
2012-07-20 02:23:49 +00:00
|
|
|
private final LayoutInflater inflater;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private boolean dataChanged;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
public ConversationAdapter(Recipients recipients, long threadId, Context context,
|
|
|
|
MasterSecret masterSecret, Handler failedIconClickHandler)
|
|
|
|
{
|
2012-07-19 21:22:03 +00:00
|
|
|
super(context, null);
|
2011-12-20 18:20:44 +00:00
|
|
|
this.context = context;
|
|
|
|
this.recipients = recipients;
|
|
|
|
this.threadId = threadId;
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.masterCipher = new MasterCipher(masterSecret);
|
|
|
|
this.dataChanged = false;
|
|
|
|
this.failedIconClickHandler = failedIconClickHandler;
|
|
|
|
this.messageRecordCache = initializeCache();
|
2012-07-20 02:23:49 +00:00
|
|
|
this.inflater = (LayoutInflater)context
|
|
|
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
|
|
|
|
MessageNotifier.updateNotification(context, false);
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@Override
|
|
|
|
public void bindView(View view, Context context, Cursor cursor) {
|
2012-10-28 23:04:24 +00:00
|
|
|
ConversationItem item = (ConversationItem)view;
|
2011-12-20 18:20:44 +00:00
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.ID));
|
2012-10-28 23:04:24 +00:00
|
|
|
String type = cursor.getString(cursor.getColumnIndexOrThrow(MmsSmsDatabase.TRANSPORT));
|
2011-12-20 18:20:44 +00:00
|
|
|
MessageRecord messageRecord = getMessageRecord(id, cursor, type);
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
item.set(masterSecret, messageRecord, failedIconClickHandler);
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
view.setOnTouchListener(touchListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View newView(Context context, Cursor cursor, ViewGroup parent) {
|
2012-07-20 02:23:49 +00:00
|
|
|
View view;
|
|
|
|
|
|
|
|
int type = getItemViewType(cursor);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-07-20 02:23:49 +00:00
|
|
|
if (type == 0) view = inflater.inflate(R.layout.conversation_item_sent, parent, false);
|
|
|
|
else view = inflater.inflate(R.layout.conversation_item_received, parent, false);
|
|
|
|
|
|
|
|
bindView(view, context, cursor);
|
2011-12-20 18:20:44 +00:00
|
|
|
return view;
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-07-20 02:23:49 +00:00
|
|
|
@Override
|
|
|
|
public int getViewTypeCount() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
Cursor cursor = (Cursor)getItem(position);
|
|
|
|
return getItemViewType(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getItemViewType(Cursor cursor) {
|
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.ID));
|
2012-10-28 23:04:24 +00:00
|
|
|
String type = cursor.getString(cursor.getColumnIndexOrThrow(MmsSmsDatabase.TRANSPORT));
|
2012-07-20 02:23:49 +00:00
|
|
|
MessageRecord messageRecord = getMessageRecord(id, cursor, type);
|
|
|
|
|
|
|
|
if (messageRecord.isOutgoing()) return 0;
|
|
|
|
else return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
private MediaMmsMessageRecord getMediaMmsMessageRecord(long messageId, Cursor cursor) {
|
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.ID));
|
2013-01-06 21:13:14 +00:00
|
|
|
long dateSent = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsDatabase.DATE_SENT));
|
|
|
|
long dateReceived = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsDatabase.DATE_RECEIVED));
|
2012-10-28 23:04:24 +00:00
|
|
|
long box = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_BOX));
|
|
|
|
Recipient recipient = getIndividualRecipientFor(null);
|
2012-10-29 23:51:42 +00:00
|
|
|
GroupData groupData = null;
|
2012-10-28 23:04:24 +00:00
|
|
|
|
|
|
|
SlideDeck slideDeck;
|
|
|
|
|
|
|
|
try {
|
|
|
|
MultimediaMessagePdu pdu = DatabaseFactory.getEncryptingMmsDatabase(context, masterSecret).getMediaMessage(messageId);
|
|
|
|
slideDeck = new SlideDeck(context, masterSecret, pdu.getBody());
|
2012-10-29 23:51:42 +00:00
|
|
|
|
|
|
|
if (recipients != null && !recipients.isSingleRecipient()) {
|
|
|
|
int groupSize = pdu.getTo().length;
|
|
|
|
int groupSent = MmsDatabase.Types.isFailedMmsBox(box) ? 0 : groupSize;
|
|
|
|
int groupSendFailed = groupSize - groupSent;
|
|
|
|
|
|
|
|
if (groupSize <= 1) {
|
|
|
|
groupSize = cursor.getInt(cursor.getColumnIndexOrThrow(MmsSmsDatabase.GROUP_SIZE));
|
|
|
|
groupSent = cursor.getInt(cursor.getColumnIndexOrThrow(MmsSmsDatabase.MMS_GROUP_SENT_COUNT));
|
|
|
|
groupSendFailed = cursor.getInt(cursor.getColumnIndexOrThrow(MmsSmsDatabase.MMS_GROUP_SEND_FAILED_COUNT));
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.w("ConversationAdapter", "MMS GroupSize: " + groupSize + " , GroupSent: " + groupSent + " , GroupSendFailed: " + groupSendFailed);
|
|
|
|
|
|
|
|
groupData = new MessageRecord.GroupData(groupSize, groupSent, groupSendFailed);
|
|
|
|
}
|
2012-10-28 23:04:24 +00:00
|
|
|
} catch (MmsException me) {
|
|
|
|
Log.w("ConversationAdapter", me);
|
|
|
|
slideDeck = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MediaMmsMessageRecord(context, id, recipients, recipient,
|
2013-01-06 21:13:14 +00:00
|
|
|
dateSent, dateReceived, threadId,
|
|
|
|
slideDeck, box, groupData);
|
2012-10-28 23:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private NotificationMmsMessageRecord getNotificationMmsMessageRecord(long messageId, Cursor cursor) {
|
|
|
|
Recipient recipient = getIndividualRecipientFor(null);
|
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.ID));
|
2013-01-06 21:13:14 +00:00
|
|
|
long dateSent = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsDatabase.DATE_SENT));
|
|
|
|
long dateReceived = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsDatabase.DATE_RECEIVED));
|
2012-10-28 23:04:24 +00:00
|
|
|
|
|
|
|
NotificationInd notification;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2012-10-28 23:04:24 +00:00
|
|
|
notification = DatabaseFactory.getMmsDatabase(context).getNotificationMessage(messageId);
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (MmsException me) {
|
|
|
|
Log.w("ConversationAdapter", me);
|
2012-10-28 23:04:24 +00:00
|
|
|
notification = new NotificationInd(new PduHeaders());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-10-28 23:04:24 +00:00
|
|
|
|
2013-01-06 21:13:14 +00:00
|
|
|
return new NotificationMmsMessageRecord(id, recipients, recipient,
|
|
|
|
dateSent, dateReceived, threadId,
|
2012-10-28 23:04:24 +00:00
|
|
|
notification.getContentLocation(),
|
|
|
|
notification.getMessageSize(),
|
|
|
|
notification.getExpiry(),
|
|
|
|
notification.getStatus(),
|
|
|
|
notification.getTransactionId());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
private SmsMessageRecord getSmsMessageRecord(long messageId, Cursor cursor) {
|
2013-01-06 21:13:14 +00:00
|
|
|
long dateReceived = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsDatabase.DATE_RECEIVED));
|
|
|
|
long dateSent = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsDatabase.DATE_SENT));
|
2012-10-28 23:04:24 +00:00
|
|
|
long type = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.TYPE));
|
|
|
|
String body = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.BODY));
|
|
|
|
String address = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.ADDRESS));
|
|
|
|
Recipient recipient = getIndividualRecipientFor(address);
|
2012-10-29 23:51:42 +00:00
|
|
|
|
|
|
|
MessageRecord.GroupData groupData = null;
|
|
|
|
|
|
|
|
if (recipients != null && !recipients.isSingleRecipient()) {
|
|
|
|
int groupSize = cursor.getInt(cursor.getColumnIndexOrThrow(MmsSmsDatabase.GROUP_SIZE));
|
|
|
|
int groupSent = cursor.getInt(cursor.getColumnIndexOrThrow(MmsSmsDatabase.SMS_GROUP_SENT_COUNT));
|
|
|
|
int groupSendFailed = cursor.getInt(cursor.getColumnIndexOrThrow(MmsSmsDatabase.SMS_GROUP_SEND_FAILED_COUNT));
|
|
|
|
|
|
|
|
Log.w("ConversationAdapter", "GroupSize: " + groupSize + " , GroupSent: " + groupSent + " , GroupSendFailed: " + groupSendFailed);
|
|
|
|
|
|
|
|
groupData = new MessageRecord.GroupData(groupSize, groupSent, groupSendFailed);
|
|
|
|
}
|
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
SmsMessageRecord messageRecord = new SmsMessageRecord(context, messageId, recipients,
|
2013-01-06 21:13:14 +00:00
|
|
|
recipient, dateSent, dateReceived,
|
|
|
|
type, threadId, groupData);
|
2012-10-28 23:04:24 +00:00
|
|
|
|
|
|
|
if (body == null) {
|
|
|
|
body = "";
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
try {
|
|
|
|
String decryptedBody = MessageDisplayHelper.getDecryptedMessageBody(masterCipher, body);
|
|
|
|
messageRecord.setBody(decryptedBody);
|
|
|
|
} catch (InvalidMessageException ime) {
|
|
|
|
Log.w("ConversationAdapter", ime);
|
|
|
|
messageRecord.setBody(context.getString(R.string.MessageDisplayHelper_decryption_error_local_message_corrupted_mac_doesn_t_match_potential_tampering_question));
|
|
|
|
messageRecord.setEmphasis(true);
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return messageRecord;
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private MessageRecord getMessageRecord(long messageId, Cursor cursor, String type) {
|
|
|
|
if (messageRecordCache.containsKey(type + messageId))
|
|
|
|
return messageRecordCache.get(type + messageId);
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
MessageRecord messageRecord;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
if (type.equals("mms")) {
|
|
|
|
long mmsType = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_TYPE));
|
|
|
|
|
|
|
|
if (mmsType == PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND) {
|
|
|
|
messageRecord = getNotificationMmsMessageRecord(messageId, cursor);
|
|
|
|
} else {
|
|
|
|
messageRecord = getMediaMmsMessageRecord(messageId, cursor);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
messageRecord = getSmsMessageRecord(messageId, cursor);
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
|
|
|
messageRecordCache.put(type + messageId, messageRecord);
|
2011-12-20 18:20:44 +00:00
|
|
|
return messageRecord;
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
private Recipient getIndividualRecipientFor(String address) {
|
|
|
|
Recipient recipient;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
try {
|
|
|
|
if (address == null) recipient = recipients.getPrimaryRecipient();
|
2012-12-24 16:40:37 +00:00
|
|
|
else recipient = RecipientFactory.getRecipientsFromString(context, address, false).getPrimaryRecipient();
|
2012-10-28 23:04:24 +00:00
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("ConversationAdapter", e);
|
2012-12-24 16:40:37 +00:00
|
|
|
recipient = new Recipient("Unknown", "Unknown", null, null);
|
2012-10-28 23:04:24 +00:00
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
return recipient;
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
@Override
|
2011-12-20 18:20:44 +00:00
|
|
|
protected void onContentChanged() {
|
|
|
|
super.onContentChanged();
|
|
|
|
messageRecordCache.clear();
|
|
|
|
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
|
|
|
|
this.dataChanged = true;
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void close() {
|
|
|
|
this.getCursor().close();
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class TouchListener implements View.OnTouchListener {
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
if (ConversationAdapter.this.dataChanged) {
|
|
|
|
ConversationAdapter.this.dataChanged = false;
|
|
|
|
MessageNotifier.updateNotification(context, false);
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private LinkedHashMap<String,MessageRecord> initializeCache() {
|
|
|
|
return new LinkedHashMap<String,MessageRecord>() {
|
|
|
|
@Override
|
|
|
|
protected boolean removeEldestEntry(Entry<String,MessageRecord> eldest) {
|
|
|
|
return this.size() > MAX_CACHE_SIZE;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
}
|