2017-10-16 20:11:42 +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;
|
2015-09-15 22:28:27 +00:00
|
|
|
import android.support.annotation.LayoutRes;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2016-11-03 16:36:45 +00:00
|
|
|
import android.support.annotation.VisibleForTesting;
|
2015-09-15 22:28:27 +00:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
2017-01-20 23:26:17 +00:00
|
|
|
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.View;
|
2015-09-15 22:28:27 +00:00
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.View.OnLongClickListener;
|
2012-07-19 21:22:03 +00:00
|
|
|
import android.view.ViewGroup;
|
2017-01-24 00:44:38 +00:00
|
|
|
import android.widget.TextView;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-02-17 19:57:51 +00:00
|
|
|
import org.thoughtcrime.securesms.ConversationAdapter.HeaderViewHolder;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2017-04-22 23:29:26 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2017-04-22 23:29:26 +00:00
|
|
|
import org.thoughtcrime.securesms.database.FastCursorRecyclerViewAdapter;
|
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;
|
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
2017-01-20 23:26:17 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.MmsMessageRecord;
|
2017-10-16 20:11:42 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
2017-04-22 23:29:26 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.SlideDeck;
|
2017-08-01 15:56:00 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-03-14 20:24:24 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Conversions;
|
2017-01-24 00:44:38 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DateUtils;
|
2013-05-06 19:22:03 +00:00
|
|
|
import org.thoughtcrime.securesms.util.LRUCache;
|
2017-01-24 00:44:38 +00:00
|
|
|
import org.thoughtcrime.securesms.util.StickyHeaderDecoration;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2016-08-16 03:23:56 +00:00
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2013-05-06 19:22:03 +00:00
|
|
|
import java.lang.ref.SoftReference;
|
2017-02-17 19:57:51 +00:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2017-01-24 00:44:38 +00:00
|
|
|
import java.util.Calendar;
|
2013-05-06 19:22:03 +00:00
|
|
|
import java.util.Collections;
|
2017-01-24 00:44:38 +00:00
|
|
|
import java.util.Date;
|
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;
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
*/
|
2015-09-15 22:28:27 +00:00
|
|
|
public class ConversationAdapter <V extends View & BindableConversationItem>
|
2017-04-22 23:29:26 +00:00
|
|
|
extends FastCursorRecyclerViewAdapter<ConversationAdapter.ViewHolder, MessageRecord>
|
2017-01-24 00:44:38 +00:00
|
|
|
implements StickyHeaderDecoration.StickyHeaderAdapter<HeaderViewHolder>
|
2015-09-15 22:28:27 +00:00
|
|
|
{
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private static final int MAX_CACHE_SIZE = 40;
|
2017-02-09 23:43:53 +00:00
|
|
|
private static final String TAG = ConversationAdapter.class.getSimpleName();
|
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
|
|
|
|
2017-01-20 17:30:08 +00:00
|
|
|
private static final int MESSAGE_TYPE_OUTGOING = 0;
|
|
|
|
private static final int MESSAGE_TYPE_INCOMING = 1;
|
|
|
|
private static final int MESSAGE_TYPE_UPDATE = 2;
|
|
|
|
private static final int MESSAGE_TYPE_AUDIO_OUTGOING = 3;
|
|
|
|
private static final int MESSAGE_TYPE_AUDIO_INCOMING = 4;
|
|
|
|
private static final int MESSAGE_TYPE_THUMBNAIL_OUTGOING = 5;
|
|
|
|
private static final int MESSAGE_TYPE_THUMBNAIL_INCOMING = 6;
|
2017-03-28 19:05:30 +00:00
|
|
|
private static final int MESSAGE_TYPE_DOCUMENT_OUTGOING = 7;
|
|
|
|
private static final int MESSAGE_TYPE_DOCUMENT_INCOMING = 8;
|
2014-02-17 01:44:51 +00:00
|
|
|
|
2014-12-14 02:10:59 +00:00
|
|
|
private final Set<MessageRecord> batchSelected = Collections.synchronizedSet(new HashSet<MessageRecord>());
|
|
|
|
|
2015-11-13 21:20:16 +00:00
|
|
|
private final @Nullable ItemClickListener clickListener;
|
|
|
|
private final @NonNull MasterSecret masterSecret;
|
2017-10-16 20:11:42 +00:00
|
|
|
private final @NonNull GlideRequests glideRequests;
|
2015-11-13 21:20:16 +00:00
|
|
|
private final @NonNull Locale locale;
|
2017-08-01 15:56:00 +00:00
|
|
|
private final @NonNull Recipient recipient;
|
2015-11-13 21:20:16 +00:00
|
|
|
private final @NonNull MmsSmsDatabase db;
|
|
|
|
private final @NonNull LayoutInflater inflater;
|
2017-01-24 00:44:38 +00:00
|
|
|
private final @NonNull Calendar calendar;
|
2017-02-17 19:57:51 +00:00
|
|
|
private final @NonNull MessageDigest digest;
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
protected static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
public <V extends View & BindableConversationItem> ViewHolder(final @NonNull V itemView) {
|
|
|
|
super(itemView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <V extends View & BindableConversationItem> V getView() {
|
|
|
|
return (V)itemView;
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2017-01-24 00:44:38 +00:00
|
|
|
|
2017-02-14 06:35:47 +00:00
|
|
|
static class HeaderViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
TextView textView;
|
2017-01-24 00:44:38 +00:00
|
|
|
|
2017-02-14 06:35:47 +00:00
|
|
|
HeaderViewHolder(View itemView) {
|
2017-01-24 00:44:38 +00:00
|
|
|
super(itemView);
|
|
|
|
textView = ViewUtil.findById(itemView, R.id.text);
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:35:47 +00:00
|
|
|
HeaderViewHolder(TextView textView) {
|
2017-01-26 00:38:36 +00:00
|
|
|
super(textView);
|
|
|
|
this.textView = textView;
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:44:38 +00:00
|
|
|
public void setText(CharSequence text) {
|
|
|
|
textView.setText(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-16 20:11:42 +00:00
|
|
|
interface ItemClickListener {
|
2016-08-16 03:23:56 +00:00
|
|
|
void onItemClick(MessageRecord item);
|
|
|
|
void onItemLongClick(MessageRecord item);
|
2015-09-15 22:28:27 +00:00
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2015-11-13 21:20:16 +00:00
|
|
|
@SuppressWarnings("ConstantConditions")
|
|
|
|
@VisibleForTesting
|
|
|
|
ConversationAdapter(Context context, Cursor cursor) {
|
|
|
|
super(context, cursor);
|
2017-02-17 19:57:51 +00:00
|
|
|
try {
|
|
|
|
this.masterSecret = null;
|
2017-10-16 20:11:42 +00:00
|
|
|
this.glideRequests = null;
|
2017-02-17 19:57:51 +00:00
|
|
|
this.locale = null;
|
|
|
|
this.clickListener = null;
|
2017-08-01 15:56:00 +00:00
|
|
|
this.recipient = null;
|
2017-02-17 19:57:51 +00:00
|
|
|
this.inflater = null;
|
|
|
|
this.db = null;
|
|
|
|
this.calendar = null;
|
|
|
|
this.digest = MessageDigest.getInstance("SHA1");
|
|
|
|
} catch (NoSuchAlgorithmException nsae) {
|
|
|
|
throw new AssertionError("SHA1 isn't supported!");
|
|
|
|
}
|
2015-11-13 21:20:16 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
public ConversationAdapter(@NonNull Context context,
|
|
|
|
@NonNull MasterSecret masterSecret,
|
2017-10-16 20:11:42 +00:00
|
|
|
@NonNull GlideRequests glideRequests,
|
2015-09-15 22:28:27 +00:00
|
|
|
@NonNull Locale locale,
|
|
|
|
@Nullable ItemClickListener clickListener,
|
|
|
|
@Nullable Cursor cursor,
|
2017-08-01 15:56:00 +00:00
|
|
|
@NonNull Recipient recipient)
|
2015-09-15 22:28:27 +00:00
|
|
|
{
|
|
|
|
super(context, cursor);
|
2017-02-17 19:57:51 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.masterSecret = masterSecret;
|
2017-10-16 20:11:42 +00:00
|
|
|
this.glideRequests = glideRequests;
|
2017-02-17 19:57:51 +00:00
|
|
|
this.locale = locale;
|
|
|
|
this.clickListener = clickListener;
|
2017-08-01 15:56:00 +00:00
|
|
|
this.recipient = recipient;
|
2017-02-17 19:57:51 +00:00
|
|
|
this.inflater = LayoutInflater.from(context);
|
|
|
|
this.db = DatabaseFactory.getMmsSmsDatabase(context);
|
|
|
|
this.calendar = Calendar.getInstance();
|
|
|
|
this.digest = MessageDigest.getInstance("SHA1");
|
|
|
|
|
|
|
|
setHasStableIds(true);
|
|
|
|
} catch (NoSuchAlgorithmException nsae) {
|
|
|
|
throw new AssertionError("SHA1 isn't supported!");
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
2014-09-24 20:54:56 +00:00
|
|
|
@Override
|
|
|
|
public void changeCursor(Cursor cursor) {
|
|
|
|
messageRecordCache.clear();
|
2017-04-22 23:29:26 +00:00
|
|
|
super.cleanFastRecords();
|
2014-09-24 20:54:56 +00:00
|
|
|
super.changeCursor(cursor);
|
|
|
|
}
|
|
|
|
|
2015-11-13 21:20:16 +00:00
|
|
|
@Override
|
2017-04-22 23:29:26 +00:00
|
|
|
protected void onBindItemViewHolder(ViewHolder viewHolder, @NonNull MessageRecord messageRecord) {
|
|
|
|
long start = System.currentTimeMillis();
|
2017-10-16 20:11:42 +00:00
|
|
|
viewHolder.getView().bind(masterSecret, messageRecord, glideRequests, locale, batchSelected, recipient);
|
2017-01-20 23:26:17 +00:00
|
|
|
Log.w(TAG, "Bind time: " + (System.currentTimeMillis() - start));
|
2015-09-15 22:28:27 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 21:20:16 +00:00
|
|
|
@Override
|
|
|
|
public ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
|
2017-01-20 23:26:17 +00:00
|
|
|
long start = System.currentTimeMillis();
|
2015-09-15 22:28:27 +00:00
|
|
|
final V itemView = ViewUtil.inflate(inflater, parent, getLayoutForViewType(viewType));
|
2017-10-16 20:11:42 +00:00
|
|
|
itemView.setOnClickListener(view -> {
|
|
|
|
if (clickListener != null) {
|
|
|
|
clickListener.onItemClick(itemView.getMessageRecord());
|
2016-08-16 03:23:56 +00:00
|
|
|
}
|
|
|
|
});
|
2017-10-16 20:11:42 +00:00
|
|
|
itemView.setOnLongClickListener(view -> {
|
|
|
|
if (clickListener != null) {
|
|
|
|
clickListener.onItemLongClick(itemView.getMessageRecord());
|
2016-08-16 03:23:56 +00:00
|
|
|
}
|
2017-10-16 20:11:42 +00:00
|
|
|
return true;
|
2016-08-16 03:23:56 +00:00
|
|
|
});
|
2017-01-20 23:26:17 +00:00
|
|
|
Log.w(TAG, "Inflate time: " + (System.currentTimeMillis() - start));
|
2015-09-15 22:28:27 +00:00
|
|
|
return new ViewHolder(itemView);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2015-11-13 21:20:16 +00:00
|
|
|
@Override
|
|
|
|
public void onItemViewRecycled(ViewHolder holder) {
|
2015-09-15 22:28:27 +00:00
|
|
|
holder.getView().unbind();
|
2012-07-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
private @LayoutRes int getLayoutForViewType(int viewType) {
|
|
|
|
switch (viewType) {
|
2017-01-20 17:11:40 +00:00
|
|
|
case MESSAGE_TYPE_AUDIO_OUTGOING:
|
2017-01-20 17:30:08 +00:00
|
|
|
case MESSAGE_TYPE_THUMBNAIL_OUTGOING:
|
2017-03-28 19:05:30 +00:00
|
|
|
case MESSAGE_TYPE_DOCUMENT_OUTGOING:
|
2017-01-20 17:11:40 +00:00
|
|
|
case MESSAGE_TYPE_OUTGOING: return R.layout.conversation_item_sent;
|
|
|
|
case MESSAGE_TYPE_AUDIO_INCOMING:
|
2017-01-20 17:30:08 +00:00
|
|
|
case MESSAGE_TYPE_THUMBNAIL_INCOMING:
|
2017-03-28 19:05:30 +00:00
|
|
|
case MESSAGE_TYPE_DOCUMENT_INCOMING:
|
2017-01-20 17:11:40 +00:00
|
|
|
case MESSAGE_TYPE_INCOMING: return R.layout.conversation_item_received;
|
|
|
|
case MESSAGE_TYPE_UPDATE: return R.layout.conversation_item_update;
|
|
|
|
default: throw new IllegalArgumentException("unsupported item view type given to ConversationAdapter");
|
2015-09-15 22:28:27 +00:00
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
@Override
|
2017-04-22 23:29:26 +00:00
|
|
|
public int getItemViewType(@NonNull MessageRecord messageRecord) {
|
2017-01-24 00:44:38 +00:00
|
|
|
if (messageRecord.isGroupAction() || messageRecord.isCallLog() || messageRecord.isJoined() ||
|
2017-06-07 01:03:09 +00:00
|
|
|
messageRecord.isExpirationTimerUpdate() || messageRecord.isEndSession() ||
|
|
|
|
messageRecord.isIdentityUpdate() || messageRecord.isIdentityVerified() ||
|
|
|
|
messageRecord.isIdentityDefault())
|
|
|
|
{
|
2015-10-27 19:18:02 +00:00
|
|
|
return MESSAGE_TYPE_UPDATE;
|
2017-01-20 17:11:40 +00:00
|
|
|
} else if (hasAudio(messageRecord)) {
|
|
|
|
if (messageRecord.isOutgoing()) return MESSAGE_TYPE_AUDIO_OUTGOING;
|
|
|
|
else return MESSAGE_TYPE_AUDIO_INCOMING;
|
2017-03-28 19:05:30 +00:00
|
|
|
} else if (hasDocument(messageRecord)) {
|
|
|
|
if (messageRecord.isOutgoing()) return MESSAGE_TYPE_DOCUMENT_OUTGOING;
|
|
|
|
else return MESSAGE_TYPE_DOCUMENT_INCOMING;
|
2017-01-20 17:30:08 +00:00
|
|
|
} else if (hasThumbnail(messageRecord)) {
|
|
|
|
if (messageRecord.isOutgoing()) return MESSAGE_TYPE_THUMBNAIL_OUTGOING;
|
|
|
|
else return MESSAGE_TYPE_THUMBNAIL_INCOMING;
|
2015-10-27 19:18:02 +00:00
|
|
|
} else if (messageRecord.isOutgoing()) {
|
|
|
|
return MESSAGE_TYPE_OUTGOING;
|
|
|
|
} else {
|
|
|
|
return MESSAGE_TYPE_INCOMING;
|
|
|
|
}
|
2012-07-20 02:23:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
@Override
|
|
|
|
protected boolean isRecordForId(@NonNull MessageRecord record, long id) {
|
|
|
|
return record.getId() == id;
|
|
|
|
}
|
|
|
|
|
2015-11-13 21:20:16 +00:00
|
|
|
@Override
|
|
|
|
public long getItemId(@NonNull Cursor cursor) {
|
2017-04-22 23:29:26 +00:00
|
|
|
String fastPreflightId = cursor.getString(cursor.getColumnIndexOrThrow(AttachmentDatabase.FAST_PREFLIGHT_ID));
|
|
|
|
|
|
|
|
if (fastPreflightId != null) {
|
|
|
|
return Long.valueOf(fastPreflightId);
|
|
|
|
}
|
|
|
|
|
2017-02-17 19:57:51 +00:00
|
|
|
final String unique = cursor.getString(cursor.getColumnIndexOrThrow(MmsSmsColumns.UNIQUE_ROW_ID));
|
|
|
|
final byte[] bytes = digest.digest(unique.getBytes());
|
|
|
|
return Conversions.byteArrayToLong(bytes);
|
2015-11-13 21:20:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
@Override
|
|
|
|
protected long getItemId(@NonNull MessageRecord record) {
|
|
|
|
if (record.isOutgoing() && record.isMms()) {
|
|
|
|
SlideDeck slideDeck = ((MmsMessageRecord)record).getSlideDeck();
|
|
|
|
|
|
|
|
if (slideDeck.getThumbnailSlide() != null && slideDeck.getThumbnailSlide().getFastPreflightId() != null) {
|
|
|
|
return Long.valueOf(slideDeck.getThumbnailSlide().getFastPreflightId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return record.getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected MessageRecord getRecordFromCursor(@NonNull Cursor cursor) {
|
2017-01-24 00:44:38 +00:00
|
|
|
long messageId = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsColumns.ID));
|
|
|
|
String type = cursor.getString(cursor.getColumnIndexOrThrow(MmsSmsDatabase.TRANSPORT));
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
final SoftReference<MessageRecord> reference = messageRecordCache.get(type + messageId);
|
2013-05-06 19:22:03 +00:00
|
|
|
if (reference != null) {
|
2015-09-15 22:28:27 +00:00
|
|
|
final MessageRecord record = reference.get();
|
|
|
|
if (record != null) return record;
|
2013-05-06 19:22:03 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
final MessageRecord messageRecord = db.readerFor(cursor, masterSecret).getCurrent();
|
2015-07-20 22:05:56 +00:00
|
|
|
messageRecordCache.put(type + messageId, new SoftReference<>(messageRecord));
|
2013-05-06 19: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
|
|
|
public void close() {
|
2015-09-15 22:28:27 +00:00
|
|
|
getCursor().close();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-19 21:22:03 +00:00
|
|
|
|
2017-02-14 06:35:47 +00:00
|
|
|
public int findLastSeenPosition(long lastSeen) {
|
|
|
|
if (lastSeen <= 0) return -1;
|
|
|
|
if (!isActiveCursor()) return -1;
|
|
|
|
|
2017-09-06 22:49:10 +00:00
|
|
|
int count = getItemCount() - (hasFooterView() ? 1 : 0);
|
2017-02-14 06:35:47 +00:00
|
|
|
|
2017-09-06 22:49:10 +00:00
|
|
|
for (int i=(hasHeaderView() ? 1 : 0);i<count;i++) {
|
2017-04-22 23:29:26 +00:00
|
|
|
MessageRecord messageRecord = getRecordForPositionOrThrow(i);
|
2017-02-14 06:35:47 +00:00
|
|
|
|
2017-02-26 18:49:48 +00:00
|
|
|
if (messageRecord.isOutgoing() || messageRecord.getDateReceived() <= lastSeen) {
|
2017-02-14 06:35:47 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
public void toggleSelection(MessageRecord messageRecord) {
|
|
|
|
if (!batchSelected.remove(messageRecord)) {
|
2014-12-14 02:10:59 +00:00
|
|
|
batchSelected.add(messageRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
public void clearSelection() {
|
|
|
|
batchSelected.clear();
|
2014-12-14 02:10:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 22:28:27 +00:00
|
|
|
public Set<MessageRecord> getSelectedItems() {
|
|
|
|
return Collections.unmodifiableSet(new HashSet<>(batchSelected));
|
2013-04-26 18:23:43 +00:00
|
|
|
}
|
2017-01-20 17:11:40 +00:00
|
|
|
|
|
|
|
private boolean hasAudio(MessageRecord messageRecord) {
|
2017-01-20 23:26:17 +00:00
|
|
|
return messageRecord.isMms() && ((MmsMessageRecord)messageRecord).getSlideDeck().getAudioSlide() != null;
|
2017-01-20 17:11:40 +00:00
|
|
|
}
|
2017-01-20 17:30:08 +00:00
|
|
|
|
2017-03-28 19:05:30 +00:00
|
|
|
private boolean hasDocument(MessageRecord messageRecord) {
|
|
|
|
return messageRecord.isMms() && ((MmsMessageRecord)messageRecord).getSlideDeck().getDocumentSlide() != null;
|
|
|
|
}
|
|
|
|
|
2017-01-20 17:30:08 +00:00
|
|
|
private boolean hasThumbnail(MessageRecord messageRecord) {
|
2017-01-20 23:26:17 +00:00
|
|
|
return messageRecord.isMms() && ((MmsMessageRecord)messageRecord).getSlideDeck().getThumbnailSlide() != null;
|
2017-01-20 17:30:08 +00:00
|
|
|
}
|
2017-01-24 00:44:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getHeaderId(int position) {
|
2017-01-26 02:00:26 +00:00
|
|
|
if (!isActiveCursor()) return -1;
|
|
|
|
if (isHeaderPosition(position)) return -1;
|
|
|
|
if (isFooterPosition(position)) return -1;
|
|
|
|
if (position >= getItemCount()) return -1;
|
2017-01-29 21:02:59 +00:00
|
|
|
if (position < 0) return -1;
|
2017-01-26 00:38:36 +00:00
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
MessageRecord record = getRecordForPositionOrThrow(position);
|
2017-01-24 00:44:38 +00:00
|
|
|
|
|
|
|
calendar.setTime(new Date(record.getDateSent()));
|
|
|
|
return Util.hashCode(calendar.get(Calendar.YEAR), calendar.get(Calendar.DAY_OF_YEAR));
|
|
|
|
}
|
|
|
|
|
2017-02-25 15:13:28 +00:00
|
|
|
public long getReceivedTimestamp(int position) {
|
2017-02-14 06:35:47 +00:00
|
|
|
if (!isActiveCursor()) return 0;
|
|
|
|
if (isHeaderPosition(position)) return 0;
|
|
|
|
if (isFooterPosition(position)) return 0;
|
|
|
|
if (position >= getItemCount()) return 0;
|
|
|
|
if (position < 0) return 0;
|
|
|
|
|
2017-04-22 23:29:26 +00:00
|
|
|
MessageRecord messageRecord = getRecordForPositionOrThrow(position);
|
2017-02-14 06:35:47 +00:00
|
|
|
|
2017-02-26 18:49:48 +00:00
|
|
|
if (messageRecord.isOutgoing()) return 0;
|
|
|
|
else return messageRecord.getDateReceived();
|
2017-02-14 06:35:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:44:38 +00:00
|
|
|
@Override
|
|
|
|
public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
|
|
|
|
return new HeaderViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.conversation_item_header, parent, false));
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:35:47 +00:00
|
|
|
public HeaderViewHolder onCreateLastSeenViewHolder(ViewGroup parent) {
|
|
|
|
return new HeaderViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.conversation_item_last_seen, parent, false));
|
|
|
|
}
|
|
|
|
|
2017-01-24 00:44:38 +00:00
|
|
|
@Override
|
|
|
|
public void onBindHeaderViewHolder(HeaderViewHolder viewHolder, int position) {
|
2017-04-22 23:29:26 +00:00
|
|
|
MessageRecord messageRecord = getRecordForPositionOrThrow(position);
|
|
|
|
viewHolder.setText(DateUtils.getRelativeDate(getContext(), locale, messageRecord.getDateReceived()));
|
2017-01-24 00:44:38 +00:00
|
|
|
}
|
2017-02-14 06:35:47 +00:00
|
|
|
|
|
|
|
public void onBindLastSeenViewHolder(HeaderViewHolder viewHolder, int position) {
|
|
|
|
viewHolder.setText(getContext().getResources().getQuantityString(R.plurals.ConversationAdapter_n_unread_messages, (position + 1), (position + 1)));
|
|
|
|
}
|
|
|
|
|
2017-02-16 06:39:53 +00:00
|
|
|
static class LastSeenHeader extends StickyHeaderDecoration {
|
2017-02-14 06:35:47 +00:00
|
|
|
|
|
|
|
private final ConversationAdapter adapter;
|
|
|
|
private final long lastSeenTimestamp;
|
|
|
|
|
2017-02-16 06:39:53 +00:00
|
|
|
LastSeenHeader(ConversationAdapter adapter, long lastSeenTimestamp) {
|
2017-02-14 06:35:47 +00:00
|
|
|
super(adapter, false, false);
|
|
|
|
this.adapter = adapter;
|
|
|
|
this.lastSeenTimestamp = lastSeenTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean hasHeader(RecyclerView parent, StickyHeaderAdapter stickyAdapter, int position) {
|
|
|
|
if (!adapter.isActiveCursor()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastSeenTimestamp <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-25 15:13:28 +00:00
|
|
|
long currentRecordTimestamp = adapter.getReceivedTimestamp(position);
|
|
|
|
long previousRecordTimestamp = adapter.getReceivedTimestamp(position + 1);
|
2017-02-14 06:35:47 +00:00
|
|
|
|
|
|
|
return currentRecordTimestamp > lastSeenTimestamp && previousRecordTimestamp < lastSeenTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-02-16 06:39:53 +00:00
|
|
|
protected int getHeaderTop(RecyclerView parent, View child, View header, int adapterPos, int layoutPos) {
|
|
|
|
return parent.getLayoutManager().getDecoratedTop(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-02-14 06:35:47 +00:00
|
|
|
protected HeaderViewHolder getHeader(RecyclerView parent, StickyHeaderAdapter stickyAdapter, int position) {
|
|
|
|
HeaderViewHolder viewHolder = adapter.onCreateLastSeenViewHolder(parent);
|
|
|
|
adapter.onBindLastSeenViewHolder(viewHolder, position);
|
|
|
|
|
|
|
|
int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
|
|
|
|
int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.UNSPECIFIED);
|
|
|
|
|
|
|
|
int childWidth = ViewGroup.getChildMeasureSpec(widthSpec, parent.getPaddingLeft() + parent.getPaddingRight(), viewHolder.itemView.getLayoutParams().width);
|
|
|
|
int childHeight = ViewGroup.getChildMeasureSpec(heightSpec, parent.getPaddingTop() + parent.getPaddingBottom(), viewHolder.itemView.getLayoutParams().height);
|
|
|
|
|
|
|
|
viewHolder.itemView.measure(childWidth, childHeight);
|
|
|
|
viewHolder.itemView.layout(0, 0, viewHolder.itemView.getMeasuredWidth(), viewHolder.itemView.getMeasuredHeight());
|
|
|
|
|
|
|
|
return viewHolder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2017-01-24 00:44:38 +00:00
|
|
|
|