2012-07-17 02:56:10 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-17 02:56:10 +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-17 02:56:10 +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;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2012-11-21 04:14:30 +00:00
|
|
|
import android.content.Intent;
|
2013-06-21 18:56:59 +00:00
|
|
|
import android.content.res.TypedArray;
|
2012-07-17 04:35:32 +00:00
|
|
|
import android.graphics.Typeface;
|
2012-11-21 04:14:30 +00:00
|
|
|
import android.net.Uri;
|
2014-02-14 23:59:57 +00:00
|
|
|
import android.os.Build;
|
2012-12-24 16:40:37 +00:00
|
|
|
import android.os.Handler;
|
2012-11-21 04:14:30 +00:00
|
|
|
import android.provider.Contacts.Intents;
|
|
|
|
import android.provider.ContactsContract.QuickContact;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.text.Spannable;
|
2012-07-17 04:35:32 +00:00
|
|
|
import android.text.SpannableStringBuilder;
|
2014-02-18 05:42:30 +00:00
|
|
|
import android.text.TextUtils;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.text.style.StyleSpan;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
2012-11-21 04:14:30 +00:00
|
|
|
import android.widget.ImageView;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
2012-11-21 04:14:30 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2012-07-17 02:56:10 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2014-01-08 22:29:05 +00:00
|
|
|
import org.thoughtcrime.securesms.util.DateUtils;
|
2013-06-28 23:56:30 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Emoji;
|
2012-07-17 02:56:10 +00:00
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
|
|
|
* A view that displays the element in a list of multiple conversation threads.
|
|
|
|
* Used by SecureSMS's ListActivity via a ConversationListAdapter.
|
|
|
|
*
|
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public class ConversationListItem extends RelativeLayout
|
|
|
|
implements Recipient.RecipientModifiedListener
|
|
|
|
{
|
2014-02-19 20:29:00 +00:00
|
|
|
private final static String TAG = ConversationListItem.class.getSimpleName();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
private Context context;
|
2012-07-17 04:35:32 +00:00
|
|
|
private Set<Long> selectedThreads;
|
2012-07-17 03:20:13 +00:00
|
|
|
private Recipients recipients;
|
|
|
|
private long threadId;
|
|
|
|
private TextView subjectView;
|
|
|
|
private TextView fromView;
|
|
|
|
private TextView dateView;
|
2012-12-24 16:40:37 +00:00
|
|
|
private long count;
|
|
|
|
private boolean read;
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
private ImageView contactPhotoImage;
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
private final Handler handler = new Handler();
|
2013-04-26 01:59:49 +00:00
|
|
|
private int distributionType;
|
2012-12-24 16:40:37 +00:00
|
|
|
|
|
|
|
public ConversationListItem(Context context) {
|
2011-12-20 18:20:44 +00:00
|
|
|
super(context);
|
2012-12-24 16:40:37 +00:00
|
|
|
this.context = context;
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public ConversationListItem(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
this.context = context;
|
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
@Override
|
|
|
|
protected void onFinishInflate() {
|
2013-05-24 01:10:15 +00:00
|
|
|
this.subjectView = (TextView) findViewById(R.id.subject);
|
|
|
|
this.fromView = (TextView) findViewById(R.id.from);
|
|
|
|
this.dateView = (TextView) findViewById(R.id.date);
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2013-05-24 01:10:15 +00:00
|
|
|
this.contactPhotoImage = (ImageView) findViewById(R.id.contact_photo_image);
|
2012-11-21 04:14:30 +00:00
|
|
|
|
|
|
|
initializeContactWidgetVisibility();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
public void set(ThreadRecord thread, Set<Long> selectedThreads, boolean batchMode) {
|
2013-04-26 01:59:49 +00:00
|
|
|
this.selectedThreads = selectedThreads;
|
|
|
|
this.recipients = thread.getRecipients();
|
|
|
|
this.threadId = thread.getThreadId();
|
|
|
|
this.count = thread.getCount();
|
|
|
|
this.read = thread.isRead();
|
|
|
|
this.distributionType = thread.getDistributionType();
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-01-06 23:46:26 +00:00
|
|
|
this.recipients.addListener(this);
|
2012-12-24 16:40:37 +00:00
|
|
|
this.fromView.setText(formatFrom(recipients, count, read));
|
2014-02-14 23:59:57 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
|
|
|
|
this.subjectView.setText(Emoji.getInstance(context).emojify(thread.getDisplayBody(),
|
|
|
|
Emoji.EMOJI_SMALL),
|
|
|
|
TextView.BufferType.SPANNABLE);
|
|
|
|
} else {
|
|
|
|
this.subjectView.setText(thread.getDisplayBody());
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
if (thread.getDate() > 0)
|
2014-01-08 22:29:05 +00:00
|
|
|
this.dateView.setText(DateUtils.getBetterRelativeTimeSpanString(getContext(), thread.getDate()));
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-02-09 18:03:38 +00:00
|
|
|
setBackground(read, batchMode);
|
2012-11-21 04:14:30 +00:00
|
|
|
setContactPhoto(this.recipients.getPrimaryRecipient());
|
2013-01-06 23:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void unbind() {
|
2013-06-25 20:20:29 +00:00
|
|
|
if (this.recipients != null)
|
|
|
|
this.recipients.removeListener(this);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
private void initializeContactWidgetVisibility() {
|
2014-01-08 22:29:05 +00:00
|
|
|
contactPhotoImage.setVisibility(View.VISIBLE);
|
2012-11-21 04:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setContactPhoto(final Recipient recipient) {
|
2012-12-01 03:32:43 +00:00
|
|
|
if (recipient == null) return;
|
|
|
|
|
2014-03-06 23:58:26 +00:00
|
|
|
contactPhotoImage.setImageBitmap(recipient.getCircleCroppedContactPhoto());
|
|
|
|
|
2014-02-19 20:29:00 +00:00
|
|
|
if (!recipient.isGroupRecipient()) {
|
|
|
|
contactPhotoImage.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (recipient.getContactUri() != null) {
|
|
|
|
QuickContact.showQuickContact(context, contactPhotoImage, recipient.getContactUri(), QuickContact.MODE_LARGE, null);
|
|
|
|
} else {
|
|
|
|
Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, Uri.fromParts("tel", recipient.getNumber(), null));
|
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
2012-11-21 04:14:30 +00:00
|
|
|
}
|
2014-02-19 20:29:00 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
contactPhotoImage.setOnClickListener(null);
|
|
|
|
}
|
2012-11-21 04:14:30 +00:00
|
|
|
}
|
|
|
|
|
2013-02-09 18:03:38 +00:00
|
|
|
private void setBackground(boolean read, boolean batch) {
|
2013-06-21 18:56:59 +00:00
|
|
|
int[] attributes = new int[]{R.attr.conversation_list_item_background_selected,
|
|
|
|
R.attr.conversation_list_item_background_read,
|
|
|
|
R.attr.conversation_list_item_background_unread};
|
|
|
|
|
|
|
|
TypedArray drawables = context.obtainStyledAttributes(attributes);
|
|
|
|
|
2013-05-24 01:10:15 +00:00
|
|
|
if (batch && selectedThreads.contains(threadId)) {
|
2013-06-21 18:56:59 +00:00
|
|
|
setBackgroundDrawable(drawables.getDrawable(0));
|
2013-02-09 18:03:38 +00:00
|
|
|
} else if (read) {
|
2013-06-21 18:56:59 +00:00
|
|
|
setBackgroundDrawable(drawables.getDrawable(1));
|
2013-02-09 18:03:38 +00:00
|
|
|
} else {
|
2013-06-21 18:56:59 +00:00
|
|
|
setBackgroundDrawable(drawables.getDrawable(2));
|
2013-02-09 18:03:38 +00:00
|
|
|
}
|
2013-06-21 18:56:59 +00:00
|
|
|
|
|
|
|
drawables.recycle();
|
2013-02-09 18:03:38 +00:00
|
|
|
}
|
|
|
|
|
2012-07-17 04:35:32 +00:00
|
|
|
private CharSequence formatFrom(Recipients from, long count, boolean read) {
|
2013-06-21 18:56:59 +00:00
|
|
|
int attributes[] = new int[] {R.attr.conversation_list_item_count_color};
|
|
|
|
TypedArray colors = context.obtainStyledAttributes(attributes);
|
|
|
|
|
2014-02-18 05:42:30 +00:00
|
|
|
final String fromString;
|
|
|
|
final boolean isUnnamedGroup = from.isGroupRecipient() && TextUtils.isEmpty(from.getPrimaryRecipient().getName());
|
|
|
|
if (isUnnamedGroup) {
|
|
|
|
fromString = context.getString(R.string.ConversationActivity_unnamed_group);
|
|
|
|
} else {
|
|
|
|
fromString = from.toShortString();
|
|
|
|
}
|
2013-02-05 06:09:17 +00:00
|
|
|
SpannableStringBuilder builder = new SpannableStringBuilder(fromString);
|
2012-07-17 04:35:32 +00:00
|
|
|
|
2014-02-18 05:42:30 +00:00
|
|
|
|
|
|
|
final int typeface;
|
|
|
|
if (isUnnamedGroup) {
|
|
|
|
if (!read) typeface = Typeface.BOLD_ITALIC;
|
|
|
|
else typeface = Typeface.ITALIC;
|
|
|
|
} else if (!read) {
|
|
|
|
typeface = Typeface.BOLD;
|
|
|
|
} else {
|
|
|
|
typeface = Typeface.NORMAL;
|
2012-07-17 04:35:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 05:42:30 +00:00
|
|
|
builder.setSpan(new StyleSpan(typeface), 0, builder.length(),
|
|
|
|
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
|
|
|
2013-06-21 18:56:59 +00:00
|
|
|
colors.recycle();
|
2012-07-17 04:35:32 +00:00
|
|
|
return builder;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public Recipients getRecipients() {
|
|
|
|
return recipients;
|
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public long getThreadId() {
|
|
|
|
return threadId;
|
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2013-04-26 01:59:49 +00:00
|
|
|
public int getDistributionType() {
|
|
|
|
return distributionType;
|
|
|
|
}
|
|
|
|
|
2012-12-24 16:40:37 +00:00
|
|
|
@Override
|
|
|
|
public void onModified(Recipient recipient) {
|
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ConversationListItem.this.fromView.setText(formatFrom(recipients, count, read));
|
|
|
|
setContactPhoto(ConversationListItem.this.recipients.getPrimaryRecipient());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|