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;
|
2012-07-17 04:35:32 +00:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Typeface;
|
2012-11-21 04:14:30 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
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;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.text.format.DateUtils;
|
2012-07-17 04:35:32 +00:00
|
|
|
import android.text.style.ForegroundColorSpan;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.text.style.StyleSpan;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
import android.widget.CompoundButton;
|
2012-11-21 04:14:30 +00:00
|
|
|
import android.widget.ImageView;
|
2012-07-17 03:20:13 +00:00
|
|
|
import android.widget.QuickContactBadge;
|
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;
|
|
|
|
|
|
|
|
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-07-26 23:21:45 +00:00
|
|
|
public class ConversationListItem extends RelativeLayout {
|
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;
|
|
|
|
private CheckBox checkbox;
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
private ImageView contactPhotoImage;
|
|
|
|
private QuickContactBadge contactPhotoBadge;
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-07-26 23:21:45 +00:00
|
|
|
public ConversationListItem(Context context, Set<Long> selectedThreads) {
|
2011-12-20 18:20:44 +00:00
|
|
|
super(context);
|
|
|
|
|
|
|
|
LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2012-08-01 00:02:50 +00:00
|
|
|
li.inflate(R.layout.conversation_list_item_view, this, true);
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
this.context = context;
|
2011-12-20 18:20:44 +00:00
|
|
|
this.selectedThreads = selectedThreads;
|
|
|
|
this.subjectView = (TextView)findViewById(R.id.subject);
|
|
|
|
this.fromView = (TextView)findViewById(R.id.from);
|
|
|
|
this.dateView = (TextView)findViewById(R.id.date);
|
|
|
|
this.checkbox = (CheckBox)findViewById(R.id.checkbox);
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
this.contactPhotoBadge = (QuickContactBadge)findViewById(R.id.contact_photo_badge);
|
|
|
|
this.contactPhotoImage = (ImageView)findViewById(R.id.contact_photo_image);
|
|
|
|
|
|
|
|
initializeContactWidgetVisibility();
|
2011-12-20 18:20:44 +00:00
|
|
|
intializeListeners();
|
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-07-26 23:21:45 +00:00
|
|
|
public ConversationListItem(Context context, AttributeSet attrs) {
|
2011-12-20 18:20:44 +00:00
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
public void set(ThreadRecord thread, boolean batchMode) {
|
|
|
|
this.recipients = thread.getRecipients();
|
|
|
|
this.threadId = thread.getThreadId();
|
|
|
|
this.fromView.setText(formatFrom(recipients, thread.getCount(), thread.isRead()));
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
if (thread.isKeyExchange())
|
2012-09-20 02:56:04 +00:00
|
|
|
this.subjectView.setText(R.string.ConversationListItem_key_exchange_message,
|
|
|
|
TextView.BufferType.SPANNABLE);
|
2011-12-20 18:20:44 +00:00
|
|
|
else
|
2012-10-28 23:04:24 +00:00
|
|
|
this.subjectView.setText(thread.getBody(), TextView.BufferType.SPANNABLE);
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
if (thread.getEmphasis())
|
2011-12-20 18:20:44 +00:00
|
|
|
((Spannable)this.subjectView.getText()).setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 0, this.subjectView.getText().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
2012-10-28 23:04:24 +00:00
|
|
|
if (thread.getDate() > 0)
|
|
|
|
this.dateView.setText(DateUtils.getRelativeTimeSpanString(getContext(), thread.getDate(), false));
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-07-17 02:56:10 +00:00
|
|
|
if (selectedThreads != null)
|
2011-12-20 18:20:44 +00:00
|
|
|
this.checkbox.setChecked(selectedThreads.contains(threadId));
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
if (batchMode) checkbox.setVisibility(View.VISIBLE);
|
|
|
|
else checkbox.setVisibility(View.GONE);
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
setContactPhoto(this.recipients.getPrimaryRecipient());
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void intializeListeners() {
|
|
|
|
checkbox.setOnCheckedChangeListener(new CheckedChangedListener());
|
|
|
|
}
|
2012-07-17 02:56:10 +00:00
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
private void initializeContactWidgetVisibility() {
|
|
|
|
if (isBadgeEnabled()) {
|
|
|
|
contactPhotoBadge.setVisibility(View.VISIBLE);
|
|
|
|
contactPhotoImage.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
contactPhotoBadge.setVisibility(View.GONE);
|
|
|
|
contactPhotoImage.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setContactPhoto(final Recipient recipient) {
|
2012-12-01 03:32:43 +00:00
|
|
|
if (recipient == null) return;
|
|
|
|
|
2012-11-21 04:14:30 +00:00
|
|
|
if (isBadgeEnabled()) {
|
|
|
|
contactPhotoBadge.setImageBitmap(recipient.getContactPhoto());
|
|
|
|
contactPhotoBadge.assignContactFromPhone(recipient.getNumber(), true);
|
|
|
|
} else {
|
|
|
|
contactPhotoImage.setImageBitmap(recipient.getContactPhoto());
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isBadgeEnabled() {
|
|
|
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
|
|
|
|
}
|
|
|
|
|
2012-07-17 04:35:32 +00:00
|
|
|
private CharSequence formatFrom(Recipients from, long count, boolean read) {
|
|
|
|
SpannableStringBuilder builder = new SpannableStringBuilder(from.toShortString());
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
builder.append(" " + count);
|
|
|
|
builder.setSpan(new ForegroundColorSpan(Color.parseColor("#66333333")),
|
|
|
|
from.toShortString().length(), builder.length(),
|
|
|
|
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!read) {
|
|
|
|
builder.setSpan(new StyleSpan(Typeface.BOLD), 0, builder.length(),
|
|
|
|
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private class CheckedChangedListener implements CompoundButton.OnCheckedChangeListener {
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
if (isChecked) selectedThreads.add(threadId);
|
|
|
|
else selectedThreads.remove(threadId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|