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

542 lines
22 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.ActivityNotFoundException;
2011-12-20 18:20:44 +00:00
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.AttributeSet;
2011-12-20 18:20:44 +00:00
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
2011-12-20 18:20:44 +00:00
import com.afollestad.materialdialogs.AlertDialogWrapper;
import org.thoughtcrime.securesms.ConversationFragment.SelectionClickListener;
import org.thoughtcrime.securesms.components.AvatarImageView;
import org.thoughtcrime.securesms.components.BubbleContainer;
import org.thoughtcrime.securesms.components.ThumbnailView;
import org.thoughtcrime.securesms.crypto.MasterSecret;
2014-03-01 22:17:55 +00:00
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
2014-03-01 22:17:55 +00:00
import org.thoughtcrime.securesms.database.SmsDatabase;
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatch;
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
import org.thoughtcrime.securesms.database.model.MessageRecord;
import org.thoughtcrime.securesms.database.model.NotificationMmsMessageRecord;
import org.thoughtcrime.securesms.jobs.MmsDownloadJob;
import org.thoughtcrime.securesms.jobs.MmsSendJob;
import org.thoughtcrime.securesms.jobs.SmsSendJob;
2014-12-12 09:03:24 +00:00
import org.thoughtcrime.securesms.mms.PartAuthority;
import org.thoughtcrime.securesms.mms.Slide;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.DateUtils;
import java.util.List;
import java.util.Locale;
import java.util.Set;
2011-12-20 18:20:44 +00:00
/**
* A view that displays an individual conversation item within a conversation
* thread. Used by ComposeMessageActivity's ListActivity via a ConversationAdapter.
*
2011-12-20 18:20:44 +00:00
* @author Moxie Marlinspike
*
*/
public class ConversationItem extends LinearLayout {
private final static String TAG = ConversationItem.class.getSimpleName();
2011-12-20 18:20:44 +00:00
private MessageRecord messageRecord;
private MasterSecret masterSecret;
private Locale locale;
private boolean groupThread;
private boolean pushDestination;
2011-12-20 18:20:44 +00:00
private View bodyBubble;
private TextView bodyText;
private TextView dateText;
private TextView indicatorText;
private TextView groupStatusText;
private ImageView secureImage;
private ImageView failedImage;
private AvatarImageView contactPhoto;
private ImageView deliveryImage;
private ImageView pendingIndicator;
private BubbleContainer bubbleContainer;
private Set<MessageRecord> batchSelected;
private SelectionClickListener selectionClickListener;
private ThumbnailView mediaThumbnail;
private Button mmsDownloadButton;
private TextView mmsDownloadingLabel;
private final MmsDownloadClickListener mmsDownloadClickListener = new MmsDownloadClickListener();
private final MmsPreferencesClickListener mmsPreferencesClickListener = new MmsPreferencesClickListener();
private final ClickListener clickListener = new ClickListener();
private final Context context;
2011-12-20 18:20:44 +00:00
public ConversationItem(Context context) {
super(context);
this.context = context;
}
public ConversationItem(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
2011-12-20 18:20:44 +00:00
this.bodyText = (TextView) findViewById(R.id.conversation_item_body);
this.dateText = (TextView) findViewById(R.id.conversation_item_date);
2014-03-01 22:17:55 +00:00
this.indicatorText = (TextView) findViewById(R.id.indicator_text);
this.groupStatusText = (TextView) findViewById(R.id.group_message_status);
2011-12-20 18:20:44 +00:00
this.secureImage = (ImageView)findViewById(R.id.sms_secure_indicator);
this.failedImage = (ImageView)findViewById(R.id.sms_failed_indicator);
this.mmsDownloadButton = (Button) findViewById(R.id.mms_download_button);
this.mmsDownloadingLabel = (TextView) findViewById(R.id.mms_label_downloading);
this.contactPhoto = (AvatarImageView) findViewById(R.id.contact_photo);
this.deliveryImage = (ImageView)findViewById(R.id.delivered_indicator);
this.bodyBubble = findViewById(R.id.body_bubble);
2014-03-01 22:17:55 +00:00
this.pendingIndicator = (ImageView)findViewById(R.id.pending_approval_indicator);
this.bubbleContainer = (BubbleContainer)findViewById(R.id.bubble);
this.mediaThumbnail = (ThumbnailView)findViewById(R.id.image_view);
2011-12-20 18:20:44 +00:00
setOnClickListener(clickListener);
if (mmsDownloadButton != null) mmsDownloadButton.setOnClickListener(mmsDownloadClickListener);
if (mediaThumbnail != null) {
mediaThumbnail.setThumbnailClickListener(new ThumbnailClickListener());
mediaThumbnail.setOnLongClickListener(new MultiSelectLongClickListener());
}
2011-12-20 18:20:44 +00:00
}
public void set(@NonNull MasterSecret masterSecret,
@NonNull MessageRecord messageRecord,
@NonNull Locale locale,
@NonNull Set<MessageRecord> batchSelected,
@NonNull SelectionClickListener selectionClickListener,
boolean groupThread, boolean pushDestination)
2011-12-20 18:20:44 +00:00
{
this.masterSecret = masterSecret;
this.messageRecord = messageRecord;
this.locale = locale;
this.batchSelected = batchSelected;
this.selectionClickListener = selectionClickListener;
this.groupThread = groupThread;
this.pushDestination = pushDestination;
setSelectionBackgroundDrawables(messageRecord);
setBodyText(messageRecord);
2014-02-17 19:54:39 +00:00
if (hasConversationBubble(messageRecord)) {
setBubbleState(messageRecord);
setStatusIcons(messageRecord);
setContactPhoto(messageRecord);
setGroupMessageStatus(messageRecord);
setEvents(messageRecord);
2014-03-01 22:17:55 +00:00
setMinimumWidth();
setMediaAttributes(messageRecord);
}
2011-12-20 18:20:44 +00:00
}
public void unbind() {
}
2011-12-20 18:20:44 +00:00
public MessageRecord getMessageRecord() {
return messageRecord;
}
/// MessageRecord Attribute Parsers
private void setBubbleState(MessageRecord messageRecord) {
final int transportationState;
if ((messageRecord.isPending() || messageRecord.isFailed()) &&
pushDestination &&
!messageRecord.isForcedSms())
{
transportationState = BubbleContainer.TRANSPORT_STATE_PUSH_PENDING;
} else if (messageRecord.isPending() ||
messageRecord.isFailed() ||
messageRecord.isPendingInsecureSmsFallback())
{
transportationState = BubbleContainer.TRANSPORT_STATE_SMS_PENDING;
} else if (messageRecord.isPush()) {
transportationState = BubbleContainer.TRANSPORT_STATE_PUSH_SENT;
} else {
transportationState = BubbleContainer.TRANSPORT_STATE_SMS_SENT;
}
final int mediaCaptionState;
if (!hasMedia(messageRecord)) {
mediaCaptionState = BubbleContainer.MEDIA_STATE_NO_MEDIA;
} else if (isCaptionlessMms(messageRecord)) {
mediaCaptionState = BubbleContainer.MEDIA_STATE_CAPTIONLESS;
} else {
mediaCaptionState = BubbleContainer.MEDIA_STATE_CAPTIONED;
}
bubbleContainer.setState(transportationState, mediaCaptionState);
}
private void setSelectionBackgroundDrawables(MessageRecord messageRecord) {
int[] attributes = new int[]{R.attr.conversation_list_item_background_selected,
R.attr.conversation_item_background};
TypedArray drawables = context.obtainStyledAttributes(attributes);
if (batchSelected.contains(messageRecord)) {
setBackgroundDrawable(drawables.getDrawable(0));
} else {
setBackgroundDrawable(drawables.getDrawable(1));
}
drawables.recycle();
}
private boolean hasConversationBubble(MessageRecord messageRecord) {
return !messageRecord.isGroupAction();
}
private boolean isCaptionlessMms(MessageRecord messageRecord) {
return TextUtils.isEmpty(messageRecord.getDisplayBody()) && messageRecord.isMms();
}
private boolean hasMedia(MessageRecord messageRecord) {
return messageRecord.isMms() &&
!messageRecord.isMmsNotification() &&
((MediaMmsMessageRecord)messageRecord).getPartCount() > 0;
}
private void setBodyText(MessageRecord messageRecord) {
bodyText.setClickable(false);
bodyText.setFocusable(false);
if (isCaptionlessMms(messageRecord)) {
bodyText.setVisibility(View.GONE);
} else {
bodyText.setText(messageRecord.getDisplayBody());
bodyText.setVisibility(View.VISIBLE);
}
if (bodyText.isClickable() && bodyText.isFocusable()) {
bodyText.setOnLongClickListener(new MultiSelectLongClickListener());
bodyText.setOnClickListener(new MultiSelectLongClickListener());
}
2011-12-20 18:20:44 +00:00
}
private void setMediaAttributes(MessageRecord messageRecord) {
if (messageRecord.isMmsNotification()) {
setNotificationMmsAttributes((NotificationMmsMessageRecord) messageRecord);
} else if (messageRecord.isMms()) {
resolveMedia((MediaMmsMessageRecord) messageRecord);
}
}
private void setContactPhoto(MessageRecord messageRecord) {
if (! messageRecord.isOutgoing()) {
setContactPhotoForRecipient(messageRecord.getIndividualRecipient());
}
}
private void setStatusIcons(MessageRecord messageRecord) {
failedImage.setVisibility(messageRecord.isFailed() ? View.VISIBLE : View.GONE);
if (messageRecord.isOutgoing()) pendingIndicator.setVisibility(View.GONE);
if (messageRecord.isOutgoing()) indicatorText.setVisibility(View.GONE);
secureImage.setVisibility(messageRecord.isSecure() ? View.VISIBLE : View.GONE);
2014-03-28 02:46:27 +00:00
bodyText.setCompoundDrawablesWithIntrinsicBounds(0, 0, messageRecord.isKeyExchange() ? R.drawable.ic_menu_login : 0, 0);
deliveryImage.setVisibility(!messageRecord.isKeyExchange() && messageRecord.isDelivered() ? View.VISIBLE : View.GONE);
mmsDownloadButton.setVisibility(View.GONE);
mmsDownloadingLabel.setVisibility(View.GONE);
if (messageRecord.isFailed()) setFailedStatusIcons();
else if (messageRecord.isPendingInsecureSmsFallback()) setFallbackStatusIcons();
else if (messageRecord.isPending()) dateText.setText(" ··· ");
else setSentStatusIcons();
}
private void setSentStatusIcons() {
final long timestamp;
if (messageRecord.isPush()) timestamp = messageRecord.getDateSent();
else timestamp = messageRecord.getDateReceived();
dateText.setText(DateUtils.getExtendedRelativeTimeSpanString(getContext(), locale, timestamp));
}
private void setFailedStatusIcons() {
dateText.setText(R.string.ConversationItem_error_not_delivered);
if (indicatorText != null) {
indicatorText.setText(R.string.ConversationItem_click_for_details);
indicatorText.setVisibility(View.VISIBLE);
}
}
private void setFallbackStatusIcons() {
pendingIndicator.setVisibility(View.VISIBLE);
indicatorText.setVisibility(View.VISIBLE);
indicatorText.setText(R.string.ConversationItem_click_to_approve_unencrypted);
}
2014-03-01 22:17:55 +00:00
private void setMinimumWidth() {
if (indicatorText != null && indicatorText.getVisibility() == View.VISIBLE && indicatorText.getText() != null) {
2014-04-01 01:47:24 +00:00
final float density = getResources().getDisplayMetrics().density;
bodyBubble.setMinimumWidth(indicatorText.getText().length() * (int) (6.5 * density));
2014-03-01 22:17:55 +00:00
} else {
bodyBubble.setMinimumWidth(0);
2014-03-01 22:17:55 +00:00
}
}
private void setEvents(MessageRecord messageRecord) {
setClickable(batchSelected.isEmpty() &&
(messageRecord.isFailed() ||
messageRecord.isPendingInsecureSmsFallback() ||
messageRecord.isBundleKeyExchange()));
if (messageRecord.isFailed()) {
setOnLongClickListener(new MultiSelectLongClickListener());
}
}
private void setGroupMessageStatus(MessageRecord messageRecord) {
if (groupThread && !messageRecord.isOutgoing()) {
this.groupStatusText.setText(messageRecord.getIndividualRecipient().toShortString());
this.groupStatusText.setVisibility(View.VISIBLE);
} else {
this.groupStatusText.setVisibility(View.GONE);
}
}
private void setNotificationMmsAttributes(NotificationMmsMessageRecord messageRecord) {
String messageSize = String.format(context.getString(R.string.ConversationItem_message_size_d_kb),
messageRecord.getMessageSize());
String expires = String.format(context.getString(R.string.ConversationItem_expires_s),
DateUtils.getRelativeTimeSpanString(getContext(),
messageRecord.getExpiration(),
false));
2011-12-20 18:20:44 +00:00
dateText.setText(messageSize + "\n" + expires);
if (MmsDatabase.Status.isDisplayDownloadButton(messageRecord.getStatus())) {
2011-12-20 18:20:44 +00:00
mmsDownloadButton.setVisibility(View.VISIBLE);
mmsDownloadingLabel.setVisibility(View.GONE);
} else {
mmsDownloadingLabel.setText(MmsDatabase.Status.getLabelForStatus(context, messageRecord.getStatus()));
2011-12-20 18:20:44 +00:00
mmsDownloadButton.setVisibility(View.GONE);
mmsDownloadingLabel.setVisibility(View.VISIBLE);
if (MmsDatabase.Status.isHardError(messageRecord.getStatus()) && !messageRecord.isOutgoing())
setOnClickListener(mmsDownloadClickListener);
else if (MmsDatabase.Status.DOWNLOAD_APN_UNAVAILABLE == messageRecord.getStatus() && !messageRecord.isOutgoing())
setOnClickListener(mmsPreferencesClickListener);
2011-12-20 18:20:44 +00:00
}
}
private void resolveMedia(MediaMmsMessageRecord messageRecord) {
if (hasMedia(messageRecord)) {
mediaThumbnail.setImageResource(masterSecret, messageRecord.getId(),
messageRecord.getDateReceived(),
messageRecord.getSlideDeckFuture());
}
2011-12-20 18:20:44 +00:00
}
/// Helper Methods
private void setContactPhotoForRecipient(final Recipient recipient) {
2014-03-28 02:46:27 +00:00
if (contactPhoto == null) return;
contactPhoto.setAvatar(recipient, true);
contactPhoto.setVisibility(View.VISIBLE);
2011-12-20 18:20:44 +00:00
}
/// Event handlers
private void handleApproveIdentity() {
List<IdentityKeyMismatch> mismatches = messageRecord.getIdentityKeyMismatches();
if (mismatches.size() != 1) {
throw new AssertionError("Identity mismatch count: " + mismatches.size());
}
new ConfirmIdentityDialog(context, masterSecret, messageRecord, mismatches.get(0)).show();
2011-12-20 18:20:44 +00:00
}
private class ThumbnailClickListener implements ThumbnailView.ThumbnailClickListener {
private void fireIntent(Slide slide) {
2014-03-28 02:46:27 +00:00
Log.w(TAG, "Clicked: " + slide.getUri() + " , " + slide.getContentType());
2011-12-20 18:20:44 +00:00
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
2014-12-12 09:03:24 +00:00
intent.setDataAndType(PartAuthority.getPublicPartUri(slide.getUri()), slide.getContentType());
try {
context.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
Log.w(TAG, "No activity existed to view the media.");
Toast.makeText(context, R.string.ConversationItem_unable_to_open_media, Toast.LENGTH_LONG).show();
}
2011-12-20 18:20:44 +00:00
}
public void onClick(final View v, final Slide slide) {
if (!batchSelected.isEmpty()) {
selectionClickListener.onItemClick(null, ConversationItem.this, -1, -1);
} else if (MediaPreviewActivity.isContentTypeSupported(slide.getContentType()) &&
slide.getThumbnailUri() != null)
{
2014-08-12 19:11:23 +00:00
Intent intent = new Intent(context, MediaPreviewActivity.class);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(slide.getUri(), slide.getContentType());
if (!messageRecord.isOutgoing()) intent.putExtra(MediaPreviewActivity.RECIPIENT_EXTRA, messageRecord.getIndividualRecipient().getRecipientId());
2014-08-12 19:11:23 +00:00
intent.putExtra(MediaPreviewActivity.DATE_EXTRA, messageRecord.getDateReceived());
context.startActivity(intent);
} else {
AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(context);
2014-08-12 19:11:23 +00:00
builder.setTitle(R.string.ConversationItem_view_secure_media_question);
builder.setIconAttribute(R.attr.dialog_alert_icon);
2014-08-12 19:11:23 +00:00
builder.setCancelable(true);
builder.setMessage(R.string.ConversationItem_this_media_has_been_stored_in_an_encrypted_database_external_viewer_warning);
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
fireIntent(slide);
2014-08-12 19:11:23 +00:00
}
});
builder.setNegativeButton(R.string.no, null);
builder.show();
}
}
2011-12-20 18:20:44 +00:00
}
2011-12-20 18:20:44 +00:00
private class MmsDownloadClickListener implements View.OnClickListener {
public void onClick(View v) {
NotificationMmsMessageRecord notificationRecord = (NotificationMmsMessageRecord)messageRecord;
Log.w(TAG, "Content location: " + new String(notificationRecord.getContentLocation()));
2011-12-20 18:20:44 +00:00
mmsDownloadButton.setVisibility(View.GONE);
mmsDownloadingLabel.setVisibility(View.VISIBLE);
ApplicationContext.getInstance(context)
.getJobManager()
.add(new MmsDownloadJob(context, messageRecord.getId(),
messageRecord.getThreadId(), false));
2011-12-20 18:20:44 +00:00
}
}
private class MmsPreferencesClickListener implements View.OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(context, PromptMmsActivity.class);
intent.putExtra("message_id", messageRecord.getId());
intent.putExtra("thread_id", messageRecord.getThreadId());
intent.putExtra("automatic", true);
context.startActivity(intent);
}
}
2011-12-20 18:20:44 +00:00
private class ClickListener implements View.OnClickListener {
public void onClick(View v) {
if (messageRecord.isFailed() && !batchSelected.isEmpty()) {
selectionClickListener.onItemClick(null, ConversationItem.this, -1, -1);
} else if(messageRecord.isFailed()) {
Intent intent = new Intent(context, MessageDetailsActivity.class);
intent.putExtra(MessageDetailsActivity.MASTER_SECRET_EXTRA, masterSecret);
intent.putExtra(MessageDetailsActivity.MESSAGE_ID_EXTRA, messageRecord.getId());
intent.putExtra(MessageDetailsActivity.TYPE_EXTRA, messageRecord.isMms() ? MmsSmsDatabase.MMS_TRANSPORT : MmsSmsDatabase.SMS_TRANSPORT);
intent.putExtra(MessageDetailsActivity.IS_PUSH_GROUP_EXTRA, groupThread && pushDestination);
context.startActivity(intent);
} else if (!messageRecord.isOutgoing() && messageRecord.isIdentityMismatchFailure()) {
handleApproveIdentity();
} else if (messageRecord.isPendingInsecureSmsFallback()) {
2014-03-01 22:17:55 +00:00
handleMessageApproval();
}
}
2011-12-20 18:20:44 +00:00
}
private class MultiSelectLongClickListener implements OnLongClickListener, OnClickListener {
@Override
public boolean onLongClick(View view) {
selectionClickListener.onItemLongClick(null, ConversationItem.this, -1, -1);
return true;
}
@Override
public void onClick(View view) {
selectionClickListener.onItemClick(null, ConversationItem.this, -1, -1);
}
}
2014-03-01 22:17:55 +00:00
private void handleMessageApproval() {
2014-04-01 01:47:24 +00:00
final int title;
final int message;
if (messageRecord.isMms()) title = R.string.ConversationItem_click_to_approve_unencrypted_mms_dialog_title;
else title = R.string.ConversationItem_click_to_approve_unencrypted_sms_dialog_title;
message = R.string.ConversationItem_click_to_approve_unencrypted_dialog_message;
2014-04-01 01:47:24 +00:00
AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(context);
2014-04-01 01:47:24 +00:00
builder.setTitle(title);
2014-04-01 01:47:24 +00:00
if (message > -1) builder.setMessage(message);
2014-03-01 22:17:55 +00:00
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (messageRecord.isMms()) {
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
database.markAsInsecure(messageRecord.getId());
2014-03-01 22:17:55 +00:00
database.markAsOutbox(messageRecord.getId());
database.markAsForcedSms(messageRecord.getId());
ApplicationContext.getInstance(context)
.getJobManager()
.add(new MmsSendJob(context, messageRecord.getId()));
2014-03-01 22:17:55 +00:00
} else {
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
database.markAsInsecure(messageRecord.getId());
2014-03-01 22:17:55 +00:00
database.markAsOutbox(messageRecord.getId());
database.markAsForcedSms(messageRecord.getId());
ApplicationContext.getInstance(context)
.getJobManager()
.add(new SmsSendJob(context, messageRecord.getId(),
messageRecord.getIndividualRecipient().getNumber()));
}
2014-03-01 22:17:55 +00:00
}
});
2014-03-01 22:17:55 +00:00
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
2014-04-01 01:47:24 +00:00
if (messageRecord.isMms()) {
DatabaseFactory.getMmsDatabase(context).markAsSentFailed(messageRecord.getId());
} else {
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageRecord.getId());
}
2014-03-01 22:17:55 +00:00
}
});
builder.show();
}
2011-12-20 18:20:44 +00:00
}