2015-01-15 13:35:35 -08:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2014 Open Whisper Systems
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
import org.thoughtcrime.securesms.components.AvatarImageView;
|
|
|
|
import org.thoughtcrime.securesms.components.FromTextView;
|
2015-01-15 13:35:35 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.documents.IdentityKeyMismatch;
|
|
|
|
import org.thoughtcrime.securesms.database.documents.NetworkFailure;
|
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.sms.MessageSender;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A simple view to show the recipients of a message
|
|
|
|
*
|
|
|
|
* @author Jake McGinty
|
|
|
|
*/
|
|
|
|
public class MessageRecipientListItem extends RelativeLayout
|
|
|
|
implements Recipient.RecipientModifiedListener
|
|
|
|
{
|
|
|
|
private final static String TAG = MessageRecipientListItem.class.getSimpleName();
|
|
|
|
|
2015-05-04 11:36:18 -07:00
|
|
|
private Recipient recipient;
|
|
|
|
private FromTextView fromView;
|
|
|
|
private TextView errorDescription;
|
|
|
|
private Button conflictButton;
|
|
|
|
private Button resendButton;
|
|
|
|
private AvatarImageView contactPhotoImage;
|
2015-01-15 13:35:35 -08:00
|
|
|
|
|
|
|
private final Handler handler = new Handler();
|
|
|
|
|
|
|
|
public MessageRecipientListItem(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
public MessageRecipientListItem(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onFinishInflate() {
|
2015-05-04 11:36:18 -07:00
|
|
|
this.fromView = (FromTextView) findViewById(R.id.from);
|
|
|
|
this.errorDescription = (TextView) findViewById(R.id.error_description);
|
|
|
|
this.contactPhotoImage = (AvatarImageView) findViewById(R.id.contact_photo_image);
|
|
|
|
this.conflictButton = (Button) findViewById(R.id.conflict_button);
|
|
|
|
this.resendButton = (Button) findViewById(R.id.resend_button);
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
|
2015-02-26 18:31:22 -08:00
|
|
|
public void set(final MasterSecret masterSecret,
|
|
|
|
final MessageRecord record,
|
|
|
|
final Recipient recipient,
|
|
|
|
final boolean isPushGroup)
|
|
|
|
{
|
|
|
|
this.recipient = recipient;
|
|
|
|
|
2015-01-15 13:35:35 -08:00
|
|
|
recipient.addListener(this);
|
2015-05-04 11:36:18 -07:00
|
|
|
fromView.setText(recipient);
|
|
|
|
contactPhotoImage.setAvatar(recipient, false);
|
2015-02-26 18:31:22 -08:00
|
|
|
setIssueIndicators(masterSecret, record, isPushGroup);
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
|
2015-02-26 18:31:22 -08:00
|
|
|
private void setIssueIndicators(final MasterSecret masterSecret,
|
|
|
|
final MessageRecord record,
|
|
|
|
final boolean isPushGroup)
|
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
final NetworkFailure networkFailure = getNetworkFailure(record);
|
|
|
|
final IdentityKeyMismatch keyMismatch = networkFailure == null ? getKeyMismatch(record) : null;
|
|
|
|
|
|
|
|
String errorText = "";
|
2015-02-26 18:31:22 -08:00
|
|
|
|
|
|
|
if (keyMismatch != null) {
|
|
|
|
resendButton.setVisibility(View.GONE);
|
|
|
|
conflictButton.setVisibility(View.VISIBLE);
|
|
|
|
|
2016-11-26 21:40:25 +01:00
|
|
|
errorText = getContext().getString(R.string.MessageDetailsRecipient_new_safety_number);
|
2015-02-26 18:31:22 -08:00
|
|
|
conflictButton.setOnClickListener(new OnClickListener() {
|
2015-01-15 13:35:35 -08:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2015-02-26 18:31:22 -08:00
|
|
|
new ConfirmIdentityDialog(getContext(), masterSecret, record, keyMismatch).show();
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
});
|
2015-02-26 18:31:22 -08:00
|
|
|
} else if (networkFailure != null || (!isPushGroup && record.isFailed())) {
|
|
|
|
resendButton.setVisibility(View.VISIBLE);
|
2015-03-13 11:29:44 -07:00
|
|
|
resendButton.setEnabled(true);
|
2015-07-10 01:03:45 +10:00
|
|
|
resendButton.requestFocus();
|
2015-02-26 18:31:22 -08:00
|
|
|
conflictButton.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
errorText = getContext().getString(R.string.MessageDetailsRecipient_failed_to_send);
|
|
|
|
resendButton.setOnClickListener(new OnClickListener() {
|
2015-01-15 13:35:35 -08:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
2015-03-13 11:29:44 -07:00
|
|
|
resendButton.setEnabled(false);
|
2015-02-26 18:31:22 -08:00
|
|
|
new ResendAsyncTask(masterSecret, record, networkFailure).execute();
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
});
|
2015-02-26 18:31:22 -08:00
|
|
|
} else {
|
|
|
|
resendButton.setVisibility(View.GONE);
|
|
|
|
conflictButton.setVisibility(View.GONE);
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
errorDescription.setText(errorText);
|
|
|
|
errorDescription.setVisibility(TextUtils.isEmpty(errorText) ? View.GONE : View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private NetworkFailure getNetworkFailure(final MessageRecord record) {
|
|
|
|
if (record.hasNetworkFailures()) {
|
|
|
|
for (final NetworkFailure failure : record.getNetworkFailures()) {
|
|
|
|
if (failure.getRecipientId() == recipient.getRecipientId()) {
|
|
|
|
return failure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private IdentityKeyMismatch getKeyMismatch(final MessageRecord record) {
|
|
|
|
if (record.isIdentityMismatchFailure()) {
|
|
|
|
for (final IdentityKeyMismatch mismatch : record.getIdentityKeyMismatches()) {
|
|
|
|
if (mismatch.getRecipientId() == recipient.getRecipientId()) {
|
|
|
|
return mismatch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unbind() {
|
|
|
|
if (this.recipient != null) this.recipient.removeListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onModified(final Recipient recipient) {
|
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-05-04 11:36:18 -07:00
|
|
|
fromView.setText(recipient);
|
|
|
|
contactPhotoImage.setAvatar(recipient, false);
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ResendAsyncTask extends AsyncTask<Void,Void,Void> {
|
|
|
|
private final MasterSecret masterSecret;
|
|
|
|
private final MessageRecord record;
|
|
|
|
private final NetworkFailure failure;
|
|
|
|
|
|
|
|
public ResendAsyncTask(MasterSecret masterSecret, MessageRecord record, NetworkFailure failure) {
|
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.record = record;
|
|
|
|
this.failure = failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
MmsDatabase mmsDatabase = DatabaseFactory.getMmsDatabase(getContext());
|
|
|
|
mmsDatabase.removeFailure(record.getId(), failure);
|
|
|
|
|
|
|
|
if (record.getRecipients().isGroupRecipient()) {
|
|
|
|
MessageSender.resendGroupMessage(getContext(), masterSecret, record, failure.getRecipientId());
|
|
|
|
} else {
|
|
|
|
MessageSender.resend(getContext(), masterSecret, record);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|