session-android/src/org/thoughtcrime/securesms/ConversationAdapter.java

228 lines
8.2 KiB
Java
Raw Normal View History

/**
2011-12-20 18:20:44 +00:00
* Copyright (C) 2011 Whisper Systems
*
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.
*
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;
import android.content.Context;
import android.database.Cursor;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
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.MessageRecord;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.SmsDatabase;
import org.thoughtcrime.securesms.mms.MmsFactory;
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;
import ws.com.google.android.mms.MmsException;
import java.util.LinkedHashMap;
2012-09-28 23:11:11 +00:00
import java.util.Map.Entry;
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.
*
2011-12-20 18:20:44 +00:00
* @author Moxie Marlinspike
*
*/
public class ConversationAdapter extends CursorAdapter {
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;
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;
private final LayoutInflater inflater;
2011-12-20 18:20:44 +00:00
private boolean dataChanged;
public ConversationAdapter(Recipients recipients, long threadId, Context context, MasterSecret masterSecret, Handler failedIconClickHandler) {
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();
this.inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2011-12-20 18:20:44 +00:00
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
MessageNotifier.updateNotification(context, false);
}
2011-12-20 18:20:44 +00:00
private Recipient buildRecipient(String address) {
Recipient recipient;
2011-12-20 18:20:44 +00:00
try {
if (address == null) recipient = recipients.getPrimaryRecipient();
else recipient = RecipientFactory.getRecipientsFromString(context, address).getPrimaryRecipient();
} catch (RecipientFormattingException e) {
Log.w("ConversationAdapter", e);
recipient = new Recipient("Unknown", "Unknown", null);
}
2011-12-20 18:20:44 +00:00
return recipient;
}
2011-12-20 18:20:44 +00:00
@Override
public void bindView(View view, Context context, Cursor cursor) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.ID));
String type = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.TRANSPORT));
MessageRecord messageRecord = getMessageRecord(id, cursor, type);
((ConversationItem)view).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) {
View view;
int type = getItemViewType(cursor);
2011-12-20 18:20:44 +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;
}
@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));
String type = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.TRANSPORT));
MessageRecord messageRecord = getMessageRecord(id, cursor, type);
if (messageRecord.isOutgoing()) return 0;
else return 1;
}
2011-12-20 18:20:44 +00:00
private MessageRecord getNewMmsMessageRecord(long messageId, Cursor cursor) {
MessageRecord messageRecord = getNewSmsMessageRecord(messageId, cursor);
long mmsType = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_TYPE));
long mmsBox = cursor.getLong(cursor.getColumnIndexOrThrow(MmsDatabase.MESSAGE_BOX));
2011-12-20 18:20:44 +00:00
try {
return MmsFactory.getMms(context, masterSecret, messageRecord, mmsType, mmsBox);
} catch (MmsException me) {
Log.w("ConversationAdapter", me);
return messageRecord;
}
}
2011-12-20 18:20:44 +00:00
private MessageRecord getNewSmsMessageRecord(long messageId, Cursor cursor) {
long date = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.DATE));
long type = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.TYPE));
String address = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.ADDRESS));
Recipient recipient = buildRecipient(address);
MessageRecord messageRecord = new MessageRecord(messageId, recipients, date, type, threadId);
2011-12-20 18:20:44 +00:00
messageRecord.setMessageRecipient(recipient);
setBody(cursor, messageRecord);
2011-12-20 18:20:44 +00:00
return messageRecord;
}
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);
2011-12-20 18:20:44 +00:00
MessageRecord messageRecord;
2011-12-20 18:20:44 +00:00
if (type.equals("mms")) messageRecord = getNewMmsMessageRecord(messageId, cursor);
else messageRecord = getNewSmsMessageRecord(messageId, cursor);
messageRecordCache.put(type + messageId, messageRecord);
2011-12-20 18:20:44 +00:00
return messageRecord;
}
2011-12-20 18:20:44 +00:00
protected void setBody(Cursor cursor, MessageRecord message) {
String body = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.BODY));
2011-12-20 18:20:44 +00:00
if (body == null)
message.setBody("");
else
MessageDisplayHelper.setDecryptedMessageBody(context, body, message, masterCipher);
2011-12-20 18:20:44 +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;
}
2011-12-20 18:20:44 +00:00
public void close() {
this.getCursor().close();
}
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);
}
2011-12-20 18:20:44 +00:00
return false;
}
}
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;
}
};
}
2011-12-20 18:20:44 +00:00
}