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;
|
2012-07-20 02:23:49 +00:00
|
|
|
import android.view.LayoutInflater;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2013-04-26 18:23:43 +00:00
|
|
|
import android.widget.AbsListView;
|
2014-04-03 23:20:23 +00:00
|
|
|
import android.support.v4.widget.CursorAdapter;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
import org.thoughtcrime.securesms.database.MmsSmsColumns;
|
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.MessageRecord;
|
2013-05-06 19:22:03 +00:00
|
|
|
import org.thoughtcrime.securesms.util.LRUCache;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2013-05-06 19:22:03 +00:00
|
|
|
import java.lang.ref.SoftReference;
|
|
|
|
import java.util.Collections;
|
2014-12-14 02:10:59 +00:00
|
|
|
import java.util.HashSet;
|
2015-03-19 20:08:48 +00:00
|
|
|
import java.util.Locale;
|
2013-05-06 19:22:03 +00:00
|
|
|
import java.util.Map;
|
2014-12-14 02:10:59 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ConversationFragment.SelectionClickListener;
|
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
|
|
|
|
*
|
|
|
|
*/
|
2013-04-26 18:23:43 +00:00
|
|
|
public class ConversationAdapter extends CursorAdapter implements AbsListView.RecyclerListener {
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static final int MAX_CACHE_SIZE = 40;
|
2013-05-06 19:22:03 +00:00
|
|
|
private final Map<String,SoftReference<MessageRecord>> messageRecordCache =
|
|
|
|
Collections.synchronizedMap(new LRUCache<String, SoftReference<MessageRecord>>(MAX_CACHE_SIZE));
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-02-17 01:44:51 +00:00
|
|
|
public static final int MESSAGE_TYPE_OUTGOING = 0;
|
|
|
|
public static final int MESSAGE_TYPE_INCOMING = 1;
|
|
|
|
public static final int MESSAGE_TYPE_GROUP_ACTION = 2;
|
|
|
|
|
2014-12-14 02:10:59 +00:00
|
|
|
private final Set<MessageRecord> batchSelected = Collections.synchronizedSet(new HashSet<MessageRecord>());
|
|
|
|
|
|
|
|
private final SelectionClickListener selectionClickListener;
|
|
|
|
private final Context context;
|
|
|
|
private final MasterSecret masterSecret;
|
2015-03-19 20:08:48 +00:00
|
|
|
private final Locale locale;
|
2014-12-14 02:10:59 +00:00
|
|
|
private final boolean groupThread;
|
|
|
|
private final boolean pushDestination;
|
|
|
|
private final LayoutInflater inflater;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2015-03-19 20:08:48 +00:00
|
|
|
public ConversationAdapter(Context context, MasterSecret masterSecret, Locale locale,
|
|
|
|
SelectionClickListener selectionClickListener, boolean groupThread,
|
|
|
|
boolean pushDestination)
|
2012-10-28 23:04:24 +00:00
|
|
|
{
|
2014-09-24 20:54:56 +00:00
|
|
|
super(context, null, 0);
|
2011-12-20 18:20:44 +00:00
|
|
|
this.context = context;
|
|
|
|
this.masterSecret = masterSecret;
|
2015-03-19 20:08:48 +00:00
|
|
|
this.locale = locale;
|
2014-12-14 02:10:59 +00:00
|
|
|
this.selectionClickListener = selectionClickListener;
|
2013-05-05 19:51:36 +00:00
|
|
|
this.groupThread = groupThread;
|
2014-02-26 08:30:29 +00:00
|
|
|
this.pushDestination = pushDestination;
|
2014-04-03 23:20:23 +00:00
|
|
|
this.inflater = LayoutInflater.from(context);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
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
|
|
|
|
2015-03-19 20:08:48 +00:00
|
|
|
item.set(masterSecret, messageRecord, locale, batchSelected, selectionClickListener,
|
2015-01-15 21:35:35 +00:00
|
|
|
groupThread, pushDestination);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 20:54:56 +00:00
|
|
|
@Override
|
|
|
|
public void changeCursor(Cursor cursor) {
|
|
|
|
messageRecordCache.clear();
|
|
|
|
super.changeCursor(cursor);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
@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
|
|
|
|
2014-02-17 01:44:51 +00:00
|
|
|
switch (type) {
|
|
|
|
case ConversationAdapter.MESSAGE_TYPE_OUTGOING:
|
|
|
|
view = inflater.inflate(R.layout.conversation_item_sent, parent, false);
|
|
|
|
break;
|
|
|
|
case ConversationAdapter.MESSAGE_TYPE_INCOMING:
|
|
|
|
view = inflater.inflate(R.layout.conversation_item_received, parent, false);
|
|
|
|
break;
|
|
|
|
case ConversationAdapter.MESSAGE_TYPE_GROUP_ACTION:
|
|
|
|
view = inflater.inflate(R.layout.conversation_item_activity, parent, false);
|
|
|
|
break;
|
|
|
|
default: throw new IllegalArgumentException("unsupported item view type given to ConversationAdapter");
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
|
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() {
|
2014-02-17 01:44:51 +00:00
|
|
|
return 3;
|
2012-07-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
Cursor cursor = (Cursor)getItem(position);
|
|
|
|
return getItemViewType(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getItemViewType(Cursor cursor) {
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsColumns.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);
|
2014-02-20 05:06:54 +00:00
|
|
|
|
|
|
|
if (messageRecord.isGroupAction()) return MESSAGE_TYPE_GROUP_ACTION;
|
|
|
|
else if (messageRecord.isOutgoing()) return MESSAGE_TYPE_OUTGOING;
|
2014-02-17 01:44:51 +00:00
|
|
|
else return MESSAGE_TYPE_INCOMING;
|
2012-07-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private MessageRecord getMessageRecord(long messageId, Cursor cursor, String type) {
|
2013-05-06 19:22:03 +00:00
|
|
|
SoftReference<MessageRecord> reference = messageRecordCache.get(type + messageId);
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2013-05-06 19:22:03 +00:00
|
|
|
if (reference != null) {
|
|
|
|
MessageRecord record = reference.get();
|
|
|
|
|
|
|
|
if (record != null)
|
|
|
|
return record;
|
|
|
|
}
|
|
|
|
|
|
|
|
MmsSmsDatabase.Reader reader = DatabaseFactory.getMmsSmsDatabase(context)
|
|
|
|
.readerFor(cursor, masterSecret);
|
2012-07-19 21:22:03 +00:00
|
|
|
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 19:22:04 +00:00
|
|
|
MessageRecord messageRecord = reader.getCurrent();
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2013-05-06 19:22:03 +00:00
|
|
|
messageRecordCache.put(type + messageId, new SoftReference<MessageRecord>(messageRecord));
|
|
|
|
|
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
|
|
|
public void close() {
|
|
|
|
this.getCursor().close();
|
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2014-12-14 02:10:59 +00:00
|
|
|
public void toggleBatchSelected(MessageRecord messageRecord) {
|
|
|
|
if (batchSelected.contains(messageRecord)) {
|
|
|
|
batchSelected.remove(messageRecord);
|
|
|
|
} else {
|
|
|
|
batchSelected.add(messageRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<MessageRecord> getBatchSelected() {
|
|
|
|
return batchSelected;
|
|
|
|
}
|
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
@Override
|
|
|
|
public void onMovedToScrapHeap(View view) {
|
|
|
|
((ConversationItem)view).unbind();
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|