Conversations/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java

1176 lines
38 KiB
Java
Raw Normal View History

2014-02-28 12:46:01 -05:00
package eu.siacs.conversations.ui;
2014-01-23 20:04:05 -05:00
2014-02-16 10:32:15 -05:00
import android.app.AlertDialog;
2014-01-23 20:04:05 -05:00
import android.app.Fragment;
2014-05-01 16:33:49 -04:00
import android.app.PendingIntent;
2014-07-31 07:26:05 -04:00
import android.content.Context;
2014-02-16 10:32:15 -05:00
import android.content.DialogInterface;
2014-03-03 20:51:01 -05:00
import android.content.Intent;
2014-02-27 18:22:56 -05:00
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
2014-01-23 20:04:05 -05:00
import android.os.Bundle;
2015-01-14 15:28:27 -05:00
import android.text.InputType;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
2014-07-31 07:26:05 -04:00
import android.view.KeyEvent;
2014-01-23 20:04:05 -05:00
import android.view.LayoutInflater;
import android.view.MenuItem;
2014-01-23 20:04:05 -05:00
import android.view.View;
2014-01-25 21:27:55 -05:00
import android.view.View.OnClickListener;
2014-01-23 20:04:05 -05:00
import android.view.ViewGroup;
2014-07-31 07:26:05 -04:00
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
2014-06-14 10:59:07 -04:00
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
2014-01-25 21:27:55 -05:00
import android.widget.ImageButton;
import android.widget.ListView;
2014-07-09 15:45:03 -04:00
import android.widget.RelativeLayout;
2014-01-25 21:27:55 -05:00
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
2014-03-03 22:09:15 -05:00
import android.widget.Toast;
2014-01-23 20:04:05 -05:00
import net.java.otr4j.session.SessionStatus;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
2014-12-01 05:18:55 -05:00
import java.util.NoSuchElementException;
import java.util.concurrent.ConcurrentLinkedQueue;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
2014-11-15 06:37:09 -05:00
import eu.siacs.conversations.entities.Downloadable;
2014-11-15 09:16:40 -05:00
import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.entities.DownloadablePlaceholder;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.Presences;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.XmppActivity.OnPresenceSelected;
import eu.siacs.conversations.ui.XmppActivity.OnValueEdited;
import eu.siacs.conversations.ui.adapter.MessageAdapter;
import eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureClicked;
import eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureLongClicked;
2015-03-11 10:48:43 -04:00
import eu.siacs.conversations.utils.GeoHelper;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.chatstate.ChatState;
import eu.siacs.conversations.xmpp.jid.Jid;
public class ConversationFragment extends Fragment implements EditMessage.KeyboardListener {
2014-02-16 10:32:15 -05:00
protected Conversation conversation;
private OnClickListener leaveMuc = new OnClickListener() {
@Override
public void onClick(View v) {
activity.endConversation(conversation);
}
};
2014-10-06 07:34:19 -04:00
private OnClickListener joinMuc = new OnClickListener() {
2014-10-06 07:34:19 -04:00
@Override
public void onClick(View v) {
activity.xmppConnectionService.joinMuc(conversation);
}
};
private OnClickListener enterPassword = new OnClickListener() {
@Override
public void onClick(View v) {
MucOptions muc = conversation.getMucOptions();
String password = muc.getPassword();
if (password == null) {
password = "";
}
activity.quickPasswordEdit(password, new OnValueEdited() {
@Override
public void onValueEdited(String value) {
activity.xmppConnectionService.providePasswordForMuc(
conversation, value);
}
});
}
};
2014-11-07 09:38:20 -05:00
protected ListView messagesView;
final protected List<Message> messageList = new ArrayList<>();
2014-11-07 09:38:20 -05:00
protected MessageAdapter messageListAdapter;
private EditMessage mEditMessage;
private ImageButton mSendButton;
private RelativeLayout snackbar;
private TextView snackbarMessage;
private TextView snackbarAction;
private boolean messagesLoaded = true;
2014-12-17 03:32:51 -05:00
private Toast messageLoaderToast;
2014-06-14 10:59:07 -04:00
private OnScrollListener mOnScrollListener = new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
private int getIndexOf(String uuid, List<Message> messages) {
if (uuid == null) {
return 0;
}
for(int i = 0; i < messages.size(); ++i) {
if (uuid.equals(messages.get(i).getUuid())) {
return i;
} else {
Message next = messages.get(i);
while(next != null && next.wasMergedIntoPrevious()) {
if (uuid.equals(next.getUuid())) {
return i;
}
next = next.next();
}
}
}
return 0;
}
2014-06-14 10:59:07 -04:00
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
synchronized (ConversationFragment.this.messageList) {
if (firstVisibleItem < 5 && messagesLoaded && messageList.size() > 0) {
long timestamp = ConversationFragment.this.messageList.get(0).getTimeSent();
messagesLoaded = false;
activity.xmppConnectionService.loadMoreMessages(conversation, timestamp, new XmppConnectionService.OnMoreMessagesLoaded() {
@Override
public void onMoreMessagesLoaded(final int c, Conversation conversation) {
if (ConversationFragment.this.conversation != conversation) {
return;
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
final int oldPosition = messagesView.getFirstVisiblePosition();
Message message = messageList.get(oldPosition);
String uuid = message != null ? message.getUuid() : null;
View v = messagesView.getChildAt(0);
final int pxOffset = (v == null) ? 0 : v.getTop();
ConversationFragment.this.conversation.populateWithMessages(ConversationFragment.this.messageList);
updateStatusMessages();
messageListAdapter.notifyDataSetChanged();
int pos = getIndexOf(uuid,messageList);
messagesView.setSelectionFromTop(pos, pxOffset);
messagesLoaded = true;
if (messageLoaderToast != null) {
messageLoaderToast.cancel();
}
}
});
}
2014-12-17 03:32:51 -05:00
@Override
public void informUser(final int resId) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (messageLoaderToast != null) {
messageLoaderToast.cancel();
}
if (ConversationFragment.this.conversation != conversation) {
return;
}
messageLoaderToast = Toast.makeText(activity, resId, Toast.LENGTH_LONG);
messageLoaderToast.show();
}
});
2014-12-17 03:32:51 -05:00
}
});
2014-06-14 10:59:07 -04:00
}
}
}
};
2014-11-07 09:38:20 -05:00
private IntentSender askForPassphraseIntent = null;
protected OnClickListener clickToDecryptListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (activity.hasPgp() && askForPassphraseIntent != null) {
try {
getActivity().startIntentSenderForResult(
askForPassphraseIntent,
ConversationActivity.REQUEST_DECRYPT_PGP, null, 0,
0, 0);
2015-02-07 06:17:03 -05:00
askForPassphraseIntent = null;
2014-11-07 09:38:20 -05:00
} catch (SendIntentException e) {
//
}
}
}
};
protected OnClickListener clickToVerify = new OnClickListener() {
@Override
public void onClick(View v) {
activity.verifyOtrSessionDialog(conversation, v);
}
};
private ConcurrentLinkedQueue<Message> mEncryptedMessages = new ConcurrentLinkedQueue<>();
2014-11-07 09:38:20 -05:00
private boolean mDecryptJobRunning = false;
private OnEditorActionListener mEditorActionListener = new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
InputMethodManager imm = (InputMethodManager) v.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
2014-11-07 09:38:20 -05:00
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
sendMessage();
return true;
} else {
return false;
}
}
};
private OnClickListener mSendButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
Object tag = v.getTag();
if (tag instanceof SendButtonAction) {
SendButtonAction action = (SendButtonAction) tag;
switch (action) {
case TAKE_PHOTO:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case SEND_LOCATION:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_LOCATION);
break;
case RECORD_VOICE:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE);
break;
case CHOOSE_PICTURE:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE);
break;
case CANCEL:
if (conversation != null && conversation.getMode() == Conversation.MODE_MULTI) {
conversation.setNextCounterpart(null);
updateChatMsgHint();
updateSendButton();
}
break;
default:
sendMessage();
}
} else {
sendMessage();
}
2014-11-07 09:38:20 -05:00
}
};
private OnClickListener clickToMuc = new OnClickListener() {
2014-06-14 10:59:07 -04:00
2014-11-07 09:38:20 -05:00
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ConferenceDetailsActivity.class);
2014-11-07 09:38:20 -05:00
intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
intent.putExtra("uuid", conversation.getUuid());
startActivity(intent);
}
};
private ConversationActivity activity;
private Message selectedMessage;
2014-08-25 09:43:50 -04:00
2014-07-31 07:26:05 -04:00
private void sendMessage() {
final String body = mEditMessage.getText().toString();
if (body.length() == 0 || this.conversation == null) {
2014-09-06 18:09:12 -04:00
return;
}
Message message = new Message(conversation, body, conversation.getNextEncryption(activity.forceEncryption()));
if (conversation.getMode() == Conversation.MODE_MULTI) {
if (conversation.getNextCounterpart() != null) {
message.setCounterpart(conversation.getNextCounterpart());
message.setType(Message.TYPE_PRIVATE);
}
}
if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_OTR) {
2014-07-31 07:26:05 -04:00
sendOtrMessage(message);
} else if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_PGP) {
2014-07-31 07:26:05 -04:00
sendPgpMessage(message);
} else {
sendPlainTextMessage(message);
}
}
2014-02-16 10:32:15 -05:00
public void updateChatMsgHint() {
2014-08-25 09:43:50 -04:00
if (conversation.getMode() == Conversation.MODE_MULTI
&& conversation.getNextCounterpart() != null) {
2014-08-25 09:43:50 -04:00
this.mEditMessage.setHint(getString(
R.string.send_private_message_to,
conversation.getNextCounterpart().getResourcepart()));
2014-08-22 07:22:07 -04:00
} else {
switch (conversation.getNextEncryption(activity.forceEncryption())) {
case Message.ENCRYPTION_NONE:
mEditMessage
.setHint(getString(R.string.send_plain_text_message));
break;
case Message.ENCRYPTION_OTR:
mEditMessage.setHint(getString(R.string.send_otr_message));
break;
case Message.ENCRYPTION_PGP:
mEditMessage.setHint(getString(R.string.send_pgp_message));
break;
default:
break;
2014-08-22 07:22:07 -04:00
}
getActivity().invalidateOptionsMenu();
2014-02-16 10:32:15 -05:00
}
}
2015-01-14 15:28:27 -05:00
private void setupIme() {
if (((ConversationActivity) getActivity()).usingEnterKey()) {
2015-01-14 15:28:27 -05:00
mEditMessage.setInputType(mEditMessage.getInputType() & (~InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE));
} else {
mEditMessage.setInputType(mEditMessage.getInputType() | InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE);
}
}
@Override
public View onCreateView(final LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_conversation, container, false);
view.setOnClickListener(null);
2014-07-31 07:26:05 -04:00
mEditMessage = (EditMessage) view.findViewById(R.id.textinput);
2015-01-14 15:28:27 -05:00
setupIme();
2014-07-31 07:26:05 -04:00
mEditMessage.setOnClickListener(new OnClickListener() {
2014-07-15 14:41:32 -04:00
@Override
public void onClick(View v) {
2015-02-18 07:11:41 -05:00
if (activity != null) {
activity.hideConversationsOverview();
}
2014-07-15 14:41:32 -04:00
}
});
2014-07-31 07:26:05 -04:00
mEditMessage.setOnEditorActionListener(mEditorActionListener);
2014-08-25 09:43:50 -04:00
2014-09-10 11:59:57 -04:00
mSendButton = (ImageButton) view.findViewById(R.id.textSendButton);
mSendButton.setOnClickListener(this.mSendButtonListener);
2014-07-09 15:45:03 -04:00
snackbar = (RelativeLayout) view.findViewById(R.id.snackbar);
snackbarMessage = (TextView) view.findViewById(R.id.snackbar_message);
snackbarAction = (TextView) view.findViewById(R.id.snackbar_action);
2014-02-27 18:22:56 -05:00
messagesView = (ListView) view.findViewById(R.id.messages_view);
2014-06-14 10:59:07 -04:00
messagesView.setOnScrollListener(mOnScrollListener);
2014-06-10 11:01:19 -04:00
messagesView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
messageListAdapter = new MessageAdapter((ConversationActivity) getActivity(), this.messageList);
messageListAdapter.setOnContactPictureClicked(new OnContactPictureClicked() {
2014-08-25 09:43:50 -04:00
@Override
public void onContactPictureClicked(Message message) {
if (message.getStatus() <= Message.STATUS_RECEIVED) {
if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
if (message.getCounterpart() != null) {
if (!message.getCounterpart().isBareJid()) {
highlightInConference(message.getCounterpart().getResourcepart());
2014-08-25 09:43:50 -04:00
} else {
highlightInConference(message.getCounterpart().toString());
2014-08-25 09:43:50 -04:00
}
}
} else {
activity.switchToContactDetails(message.getContact());
}
} else {
Account account = message.getConversation().getAccount();
Intent intent = new Intent(activity, EditAccountActivity.class);
intent.putExtra("jid", account.getJid().toBareJid().toString());
startActivity(intent);
}
}
});
2014-08-25 09:43:50 -04:00
messageListAdapter
.setOnContactPictureLongClicked(new OnContactPictureLongClicked() {
@Override
public void onContactPictureLongClicked(Message message) {
if (message.getStatus() <= Message.STATUS_RECEIVED) {
if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
if (message.getCounterpart() != null) {
privateMessageWith(message.getCounterpart());
}
2014-08-25 09:43:50 -04:00
}
} else {
activity.showQrCode();
2014-08-25 09:43:50 -04:00
}
}
});
messagesView.setAdapter(messageListAdapter);
2014-01-25 21:27:55 -05:00
registerForContextMenu(messagesView);
2014-01-25 21:27:55 -05:00
return view;
}
2014-08-25 09:43:50 -04:00
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
synchronized (this.messageList) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
this.selectedMessage = this.messageList.get(acmi.position);
populateContextMenu(menu);
}
}
private void populateContextMenu(ContextMenu menu) {
2015-01-02 08:27:49 -05:00
final Message m = this.selectedMessage;
if (m.getType() != Message.TYPE_STATUS) {
activity.getMenuInflater().inflate(R.menu.message_context, menu);
menu.setHeaderTitle(R.string.message_options);
MenuItem copyText = menu.findItem(R.id.copy_text);
MenuItem shareWith = menu.findItem(R.id.share_with);
MenuItem sendAgain = menu.findItem(R.id.send_again);
MenuItem copyUrl = menu.findItem(R.id.copy_url);
MenuItem downloadImage = menu.findItem(R.id.download_image);
2014-11-15 06:37:09 -05:00
MenuItem cancelTransmission = menu.findItem(R.id.cancel_transmission);
2015-02-19 08:36:52 -05:00
if ((m.getType() != Message.TYPE_TEXT && m.getType() != Message.TYPE_PRIVATE)
2015-03-11 10:48:43 -04:00
|| m.getDownloadable() != null || GeoHelper.isGeoUri(m.getBody())) {
copyText.setVisible(false);
2015-01-02 08:27:49 -05:00
}
2015-03-11 10:48:43 -04:00
if ((m.getType() == Message.TYPE_TEXT
|| m.getType() == Message.TYPE_PRIVATE
2015-03-11 10:48:43 -04:00
|| m.getDownloadable() != null)
&& (!GeoHelper.isGeoUri(m.getBody()))) {
shareWith.setVisible(false);
2015-03-11 10:48:43 -04:00
}
2015-01-02 08:27:49 -05:00
if (m.getStatus() != Message.STATUS_SEND_FAILED) {
sendAgain.setVisible(false);
}
if (!m.hasFileOnRemoteHost() && !GeoHelper.isGeoUri(m.getBody())) {
copyUrl.setVisible(false);
}
2015-01-02 08:27:49 -05:00
if (m.getType() != Message.TYPE_TEXT
|| m.getDownloadable() != null
|| !m.bodyContainsDownloadable()) {
downloadImage.setVisible(false);
}
2015-01-02 08:27:49 -05:00
if (!((m.getDownloadable() != null && !(m.getDownloadable() instanceof DownloadablePlaceholder))
|| (m.isFileOrImage() && (m.getStatus() == Message.STATUS_WAITING
|| m.getStatus() == Message.STATUS_OFFERED)))) {
2014-11-15 06:37:09 -05:00
cancelTransmission.setVisible(false);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share_with:
shareWith(selectedMessage);
return true;
case R.id.copy_text:
copyText(selectedMessage);
return true;
case R.id.send_again:
resendMessage(selectedMessage);
return true;
case R.id.copy_url:
copyUrl(selectedMessage);
return true;
case R.id.download_image:
downloadImage(selectedMessage);
return true;
2014-11-15 06:37:09 -05:00
case R.id.cancel_transmission:
cancelTransmission(selectedMessage);
return true;
default:
return super.onContextItemSelected(item);
}
}
private void shareWith(Message message) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
2015-03-11 10:48:43 -04:00
if (GeoHelper.isGeoUri(message.getBody())) {
shareIntent.putExtra(Intent.EXTRA_TEXT, message.getBody());
shareIntent.setType("text/plain");
} else {
shareIntent.putExtra(Intent.EXTRA_STREAM,
activity.xmppConnectionService.getFileBackend()
.getJingleFileUri(message));
shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String path = message.getRelativeFilePath();
String mime = path == null ? null : URLConnection.guessContentTypeFromName(path);
if (mime == null) {
mime = "image/webp";
}
shareIntent.setType(mime);
}
activity.startActivity(Intent.createChooser(shareIntent, getText(R.string.share_with)));
}
private void copyText(Message message) {
if (activity.copyTextToClipboard(message.getMergedBody(),
R.string.message_text)) {
Toast.makeText(activity, R.string.message_copied_to_clipboard,
Toast.LENGTH_SHORT).show();
}
}
private void resendMessage(Message message) {
2014-11-15 09:16:40 -05:00
if (message.getType() == Message.TYPE_FILE || message.getType() == Message.TYPE_IMAGE) {
DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message);
if (!file.exists()) {
Toast.makeText(activity, R.string.file_deleted, Toast.LENGTH_SHORT).show();
2014-11-15 09:16:40 -05:00
message.setDownloadable(new DownloadablePlaceholder(Downloadable.STATUS_DELETED));
return;
}
}
activity.xmppConnectionService.resendFailedMessages(message);
}
private void copyUrl(Message message) {
2015-03-11 10:48:43 -04:00
final String url;
final int resId;
if (GeoHelper.isGeoUri(message.getBody())) {
resId = R.string.location;
url = message.getBody();
} else {
resId = R.string.image_url;
url = message.getImageParams().url.toString();
}
if (activity.copyTextToClipboard(url, resId)) {
Toast.makeText(activity, R.string.url_copied_to_clipboard,
Toast.LENGTH_SHORT).show();
}
}
private void downloadImage(Message message) {
activity.xmppConnectionService.getHttpConnectionManager()
.createNewConnection(message);
}
2014-11-15 06:37:09 -05:00
private void cancelTransmission(Message message) {
Downloadable downloadable = message.getDownloadable();
if (downloadable != null) {
2014-11-15 06:37:09 -05:00
downloadable.cancel();
2015-01-02 08:27:49 -05:00
} else {
activity.xmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
2014-11-15 06:37:09 -05:00
}
}
protected void privateMessageWith(final Jid counterpart) {
this.mEditMessage.setText("");
this.conversation.setNextCounterpart(counterpart);
2014-08-22 07:22:07 -04:00
updateChatMsgHint();
updateSendButton();
}
protected void highlightInConference(String nick) {
2014-07-31 07:26:05 -04:00
String oldString = mEditMessage.getText().toString().trim();
if (oldString.isEmpty() || mEditMessage.getSelectionStart() == 0) {
mEditMessage.getText().insert(0, nick + ": ");
} else {
if (mEditMessage.getText().charAt(
mEditMessage.getSelectionStart() - 1) != ' ') {
nick = " " + nick;
}
mEditMessage.getText().insert(mEditMessage.getSelectionStart(),
nick + " ");
}
}
2014-05-07 06:59:15 -04:00
@Override
public void onStop() {
2014-10-14 20:09:20 -04:00
mDecryptJobRunning = false;
2014-05-07 06:59:15 -04:00
super.onStop();
if (this.conversation != null) {
final String msg = mEditMessage.getText().toString();
this.conversation.setNextMessage(msg);
updateChatState(this.conversation, msg);
}
}
private void updateChatState(final Conversation conversation, final String msg) {
ChatState state = msg.length() == 0 ? Config.DEFAULT_CHATSTATE : ChatState.PAUSED;
Account.State status = conversation.getAccount().getStatus();
if (status == Account.State.ONLINE && conversation.setOutgoingChatState(state)) {
activity.xmppConnectionService.sendChatState(conversation);
}
2014-05-07 06:59:15 -04:00
}
2014-11-07 09:38:20 -05:00
public void reInit(Conversation conversation) {
if (conversation == null) {
return;
}
this.activity = (ConversationActivity) getActivity();
2014-11-07 09:38:20 -05:00
if (this.conversation != null) {
final String msg = mEditMessage.getText().toString();
this.conversation.setNextMessage(msg);
if (this.conversation != conversation) {
updateChatState(this.conversation, msg);
}
this.conversation.trim();
2014-01-28 13:21:54 -05:00
}
this.askForPassphraseIntent = null;
2014-11-07 09:38:20 -05:00
this.conversation = conversation;
2014-12-01 05:18:55 -05:00
this.mDecryptJobRunning = false;
this.mEncryptedMessages.clear();
if (this.conversation.getMode() == Conversation.MODE_MULTI) {
this.conversation.setNextCounterpart(null);
}
this.mEditMessage.setKeyboardListener(null);
2014-11-07 09:38:20 -05:00
this.mEditMessage.setText("");
this.mEditMessage.append(this.conversation.getNextMessage());
this.mEditMessage.setKeyboardListener(this);
2015-02-17 03:53:16 -05:00
this.messagesView.setAdapter(messageListAdapter);
2014-08-22 07:22:07 -04:00
updateMessages();
this.messagesLoaded = true;
int size = this.messageList.size();
if (size > 0) {
messagesView.setSelection(size - 1);
}
}
2014-05-07 06:59:15 -04:00
2015-02-07 06:17:03 -05:00
private OnClickListener mUnblockClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
v.post(new Runnable() {
@Override
public void run() {
v.setVisibility(View.INVISIBLE);
}
});
if (conversation.isDomainBlocked()) {
BlockContactDialog.show(activity, activity.xmppConnectionService, conversation);
} else {
activity.unblockConversation(conversation);
}
}
};
private OnClickListener mAddBackClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final Contact contact = conversation == null ? null : conversation.getContact();
2015-02-07 06:17:03 -05:00
if (contact != null) {
activity.xmppConnectionService.createContact(contact);
activity.switchToContactDetails(contact);
}
}
};
private OnClickListener mUnmuteClickListener = new OnClickListener() {
@Override
public void onClick(final View v) {
activity.unmuteConversation(conversation);
}
};
private OnClickListener mAnswerSmpClickListener = new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(activity, VerifyOTRActivity.class);
intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
intent.putExtra("mode", VerifyOTRActivity.MODE_ANSWER_QUESTION);
2015-02-07 06:17:03 -05:00
startActivity(intent);
}
};
private void updateSnackBar(final Conversation conversation) {
final Account account = conversation.getAccount();
final Contact contact = conversation.getContact();
final int mode = conversation.getMode();
if (conversation.isBlocked()) {
showSnackbar(R.string.contact_blocked, R.string.unblock, this.mUnblockClickListener);
2015-02-07 06:17:03 -05:00
} else if (!contact.showInRoster() && contact.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
showSnackbar(R.string.contact_added_you, R.string.add_back, this.mAddBackClickListener);
2015-02-07 06:17:03 -05:00
} else if (mode == Conversation.MODE_MULTI
&& !conversation.getMucOptions().online()
2015-02-07 06:17:03 -05:00
&& account.getStatus() == Account.State.ONLINE) {
switch (conversation.getMucOptions().getError()) {
case MucOptions.ERROR_NICK_IN_USE:
showSnackbar(R.string.nick_in_use, R.string.edit, clickToMuc);
break;
case MucOptions.ERROR_UNKNOWN:
showSnackbar(R.string.conference_not_found, R.string.leave, leaveMuc);
break;
case MucOptions.ERROR_PASSWORD_REQUIRED:
showSnackbar(R.string.conference_requires_password, R.string.enter_password, enterPassword);
break;
case MucOptions.ERROR_BANNED:
showSnackbar(R.string.conference_banned, R.string.leave, leaveMuc);
break;
case MucOptions.ERROR_MEMBERS_ONLY:
showSnackbar(R.string.conference_members_only, R.string.leave, leaveMuc);
break;
case MucOptions.KICKED_FROM_ROOM:
showSnackbar(R.string.conference_kicked, R.string.join, joinMuc);
break;
default:
break;
}
} else if (askForPassphraseIntent != null) {
showSnackbar(R.string.openpgp_messages_found, R.string.decrypt, clickToDecryptListener);
2015-02-07 06:17:03 -05:00
} else if (mode == Conversation.MODE_SINGLE
&& conversation.smpRequested()) {
showSnackbar(R.string.smp_requested, R.string.verify, this.mAnswerSmpClickListener);
2015-02-07 06:17:03 -05:00
} else if (mode == Conversation.MODE_SINGLE
&& conversation.hasValidOtrSession()
2015-02-07 06:17:03 -05:00
&& (conversation.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED)
&& (!conversation.isOtrFingerprintVerified())) {
showSnackbar(R.string.unknown_otr_fingerprint, R.string.verify, clickToVerify);
} else if (conversation.isMuted()) {
showSnackbar(R.string.notifications_disabled, R.string.enable, this.mUnmuteClickListener);
2015-02-07 06:17:03 -05:00
} else {
hideSnackbar();
}
}
public void updateMessages() {
synchronized (this.messageList) {
if (getView() == null) {
return;
2014-07-21 10:16:09 -04:00
}
final ConversationActivity activity = (ConversationActivity) getActivity();
if (this.conversation != null) {
2015-02-07 06:17:03 -05:00
updateSnackBar(this.conversation);
conversation.populateWithMessages(ConversationFragment.this.messageList);
2015-01-11 10:22:29 -05:00
for (final Message message : this.messageList) {
if (message.getEncryption() == Message.ENCRYPTION_PGP
&& (message.getStatus() == Message.STATUS_RECEIVED || message
.getStatus() >= Message.STATUS_SEND)
&& message.getDownloadable() == null) {
if (!mEncryptedMessages.contains(message)) {
mEncryptedMessages.add(message);
}
}
}
decryptNext();
updateStatusMessages();
this.messageListAdapter.notifyDataSetChanged();
updateChatMsgHint();
if (!activity.isConversationsOverviewVisable() || !activity.isConversationsOverviewHideable()) {
activity.sendReadMarkerIfNecessary(conversation);
}
this.updateSendButton();
2014-03-02 23:01:02 -05:00
}
}
}
2014-10-14 20:09:20 -04:00
private void decryptNext() {
Message next = this.mEncryptedMessages.peek();
PgpEngine engine = activity.xmppConnectionService.getPgpEngine();
if (next != null && engine != null && !mDecryptJobRunning) {
2014-10-14 20:09:20 -04:00
mDecryptJobRunning = true;
engine.decrypt(next, new UiCallback<Message>() {
@Override
public void userInputRequried(PendingIntent pi, Message message) {
mDecryptJobRunning = false;
askForPassphraseIntent = pi.getIntentSender();
2015-02-07 06:17:03 -05:00
updateSnackBar(conversation);
2014-10-14 20:09:20 -04:00
}
@Override
public void success(Message message) {
mDecryptJobRunning = false;
2014-12-01 05:18:55 -05:00
try {
mEncryptedMessages.remove();
} catch (final NoSuchElementException ignored) {
}
askForPassphraseIntent = null;
2014-10-14 20:09:20 -04:00
activity.xmppConnectionService.updateMessage(message);
}
@Override
public void error(int error, Message message) {
message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
mDecryptJobRunning = false;
2014-12-01 05:18:55 -05:00
try {
mEncryptedMessages.remove();
} catch (final NoSuchElementException ignored) {
}
2014-10-14 20:09:20 -04:00
activity.xmppConnectionService.updateConversationUi();
}
});
}
}
2014-06-10 11:01:19 -04:00
private void messageSent() {
int size = this.messageList.size();
2014-10-06 16:03:01 -04:00
messagesView.setSelection(size - 1);
2014-07-31 07:26:05 -04:00
mEditMessage.setText("");
updateChatMsgHint();
2014-06-10 11:01:19 -04:00
}
2014-06-14 10:59:07 -04:00
enum SendButtonAction {TEXT, TAKE_PHOTO, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE}
private int getSendButtonImageResource(SendButtonAction action, int status) {
switch (action) {
case TEXT:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
return R.drawable.ic_send_text_online;
case Presences.AWAY:
return R.drawable.ic_send_text_away;
case Presences.XA:
case Presences.DND:
return R.drawable.ic_send_text_dnd;
default:
return R.drawable.ic_send_text_offline;
}
case TAKE_PHOTO:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
return R.drawable.ic_send_photo_online;
case Presences.AWAY:
return R.drawable.ic_send_photo_away;
case Presences.XA:
case Presences.DND:
return R.drawable.ic_send_photo_dnd;
default:
return R.drawable.ic_send_photo_offline;
}
case RECORD_VOICE:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
return R.drawable.ic_send_voice_online;
case Presences.AWAY:
return R.drawable.ic_send_voice_away;
case Presences.XA:
case Presences.DND:
return R.drawable.ic_send_voice_dnd;
default:
return R.drawable.ic_send_voice_offline;
}
case SEND_LOCATION:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
return R.drawable.ic_send_location_online;
case Presences.AWAY:
return R.drawable.ic_send_location_away;
case Presences.XA:
case Presences.DND:
return R.drawable.ic_send_location_dnd;
default:
return R.drawable.ic_send_location_offline;
}
case CANCEL:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
return R.drawable.ic_send_cancel_online;
case Presences.AWAY:
return R.drawable.ic_send_cancel_away;
case Presences.XA:
case Presences.DND:
return R.drawable.ic_send_cancel_dnd;
default:
return R.drawable.ic_send_cancel_offline;
}
case CHOOSE_PICTURE:
switch (status) {
case Presences.CHAT:
case Presences.ONLINE:
return R.drawable.ic_send_picture_online;
case Presences.AWAY:
return R.drawable.ic_send_picture_away;
case Presences.XA:
case Presences.DND:
return R.drawable.ic_send_picture_dnd;
default:
return R.drawable.ic_send_picture_offline;
}
}
return R.drawable.ic_send_text_offline;
}
public void updateSendButton() {
final Conversation c = this.conversation;
final SendButtonAction action;
final int status;
final boolean empty = this.mEditMessage == null || this.mEditMessage.getText().length() == 0;
final boolean conference = c.getMode() == Conversation.MODE_MULTI;
if (conference && !c.getAccount().httpUploadAvailable()) {
if (empty && c.getNextCounterpart() != null) {
action = SendButtonAction.CANCEL;
} else {
action = SendButtonAction.TEXT;
}
} else {
if (empty) {
if (conference && c.getNextCounterpart() != null) {
action = SendButtonAction.CANCEL;
} else {
String setting = activity.getPreferences().getString("quick_action", "recent");
if (!setting.equals("none") && UIHelper.receivedLocationQuestion(conversation.getLatestMessage())) {
setting = "location";
} else if (setting.equals("recent")) {
setting = activity.getPreferences().getString("recently_used_quick_action", "text");
}
switch (setting) {
case "photo":
action = SendButtonAction.TAKE_PHOTO;
break;
case "location":
action = SendButtonAction.SEND_LOCATION;
break;
case "voice":
action = SendButtonAction.RECORD_VOICE;
break;
case "picture":
action = SendButtonAction.CHOOSE_PICTURE;
break;
default:
action = SendButtonAction.TEXT;
break;
}
2014-09-10 11:59:57 -04:00
}
} else {
action = SendButtonAction.TEXT;
}
}
if (activity.useSendButtonToIndicateStatus() && c != null
&& c.getAccount().getStatus() == Account.State.ONLINE) {
if (c.getMode() == Conversation.MODE_SINGLE) {
status = c.getContact().getMostAvailableStatus();
} else {
status = c.getMucOptions().online() ? Presences.ONLINE : Presences.OFFLINE;
2014-09-10 11:59:57 -04:00
}
} else {
status = Presences.OFFLINE;
2014-09-10 11:59:57 -04:00
}
this.mSendButton.setTag(action);
this.mSendButton.setImageResource(getSendButtonImageResource(action, status));
2014-09-10 11:59:57 -04:00
}
protected void updateStatusMessages() {
synchronized (this.messageList) {
if (conversation.getMode() == Conversation.MODE_SINGLE) {
ChatState state = conversation.getIncomingChatState();
if (state == ChatState.COMPOSING) {
this.messageList.add(Message.createStatusMessage(conversation, getString(R.string.contact_is_typing, conversation.getName())));
} else if (state == ChatState.PAUSED) {
this.messageList.add(Message.createStatusMessage(conversation, getString(R.string.contact_has_stopped_typing, conversation.getName())));
} else {
for (int i = this.messageList.size() - 1; i >= 0; --i) {
if (this.messageList.get(i).getStatus() == Message.STATUS_RECEIVED) {
return;
} else {
if (this.messageList.get(i).getStatus() == Message.STATUS_SEND_DISPLAYED) {
this.messageList.add(i + 1,
Message.createStatusMessage(conversation, getString(R.string.contact_has_read_up_to_this_point, conversation.getName())));
return;
}
}
}
}
}
}
}
2014-02-16 10:32:15 -05:00
protected void showSnackbar(final int message, final int action,
final OnClickListener clickListener) {
2014-07-09 15:45:03 -04:00
snackbar.setVisibility(View.VISIBLE);
2014-08-12 17:23:25 -04:00
snackbar.setOnClickListener(null);
2014-07-09 15:45:03 -04:00
snackbarMessage.setText(message);
2014-08-12 17:23:25 -04:00
snackbarMessage.setOnClickListener(null);
snackbarAction.setVisibility(View.VISIBLE);
2014-07-09 15:45:03 -04:00
snackbarAction.setText(action);
snackbarAction.setOnClickListener(clickListener);
}
2014-07-09 15:45:03 -04:00
protected void hideSnackbar() {
snackbar.setVisibility(View.GONE);
}
protected void sendPlainTextMessage(Message message) {
ConversationActivity activity = (ConversationActivity) getActivity();
2014-06-11 15:53:25 -04:00
activity.xmppConnectionService.sendMessage(message);
2014-06-10 11:01:19 -04:00
messageSent();
}
2014-02-27 18:22:56 -05:00
protected void sendPgpMessage(final Message message) {
2014-05-06 15:34:30 -04:00
final ConversationActivity activity = (ConversationActivity) getActivity();
2014-02-27 18:22:56 -05:00
final XmppConnectionService xmppService = activity.xmppConnectionService;
2014-05-01 16:33:49 -04:00
final Contact contact = message.getConversation().getContact();
if (activity.hasPgp()) {
2014-05-22 09:36:41 -04:00
if (conversation.getMode() == Conversation.MODE_SINGLE) {
if (contact.getPgpKeyId() != 0) {
xmppService.getPgpEngine().hasKey(contact,
new UiCallback<Contact>() {
@Override
public void userInputRequried(PendingIntent pi,
Contact contact) {
activity.runIntent(
pi,
ConversationActivity.REQUEST_ENCRYPT_MESSAGE);
}
@Override
public void success(Contact contact) {
2014-06-10 11:01:19 -04:00
messageSent();
activity.encryptTextMessage(message);
}
@Override
public void error(int error, Contact contact) {
}
});
2014-05-22 09:36:41 -04:00
} else {
showNoPGPKeyDialog(false,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
conversation
.setNextEncryption(Message.ENCRYPTION_NONE);
2014-09-28 09:21:56 -04:00
xmppService.databaseBackend
.updateConversation(conversation);
message.setEncryption(Message.ENCRYPTION_NONE);
2014-06-11 15:53:25 -04:00
xmppService.sendMessage(message);
2014-06-10 11:01:19 -04:00
messageSent();
}
});
2014-05-22 09:36:41 -04:00
}
} else {
if (conversation.getMucOptions().pgpKeysInUse()) {
if (!conversation.getMucOptions().everybodyHasKeys()) {
Toast warning = Toast
.makeText(getActivity(),
R.string.missing_public_keys,
Toast.LENGTH_LONG);
warning.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
warning.show();
}
activity.encryptTextMessage(message);
messageSent();
} else {
showNoPGPKeyDialog(true,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
conversation
.setNextEncryption(Message.ENCRYPTION_NONE);
message.setEncryption(Message.ENCRYPTION_NONE);
2014-09-28 09:21:56 -04:00
xmppService.databaseBackend
.updateConversation(conversation);
2014-06-11 15:53:25 -04:00
xmppService.sendMessage(message);
2014-06-10 11:01:19 -04:00
messageSent();
}
});
}
}
} else {
activity.showInstallPgpDialog();
2014-02-27 18:22:56 -05:00
}
}
public void showNoPGPKeyDialog(boolean plural,
DialogInterface.OnClickListener listener) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIconAttribute(android.R.attr.alertDialogIcon);
if (plural) {
builder.setTitle(getString(R.string.no_pgp_keys));
builder.setMessage(getText(R.string.contacts_have_no_pgp_keys));
} else {
builder.setTitle(getString(R.string.no_pgp_key));
builder.setMessage(getText(R.string.contact_has_no_pgp_key));
}
builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.send_unencrypted),
listener);
builder.create().show();
}
protected void sendOtrMessage(final Message message) {
2014-06-12 17:04:28 -04:00
final ConversationActivity activity = (ConversationActivity) getActivity();
final XmppConnectionService xmppService = activity.xmppConnectionService;
activity.selectPresence(message.getConversation(),
new OnPresenceSelected() {
@Override
public void onPresenceSelected() {
message.setCounterpart(conversation.getNextCounterpart());
xmppService.sendMessage(message);
messageSent();
}
});
}
2014-11-07 09:38:20 -05:00
public void appendText(String text) {
if (text == null) {
return;
}
2014-11-07 09:38:20 -05:00
String previous = this.mEditMessage.getText().toString();
if (previous.length() != 0 && !previous.endsWith(" ")) {
text = " " + text;
}
this.mEditMessage.append(text);
2014-03-13 16:37:27 -04:00
}
@Override
2015-03-06 16:22:50 -05:00
public boolean onEnterPressed() {
if (activity.enterIsSend()) {
sendMessage();
return true;
} else {
return false;
}
}
@Override
public void onTypingStarted() {
Account.State status = conversation.getAccount().getStatus();
if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.COMPOSING)) {
activity.xmppConnectionService.sendChatState(conversation);
}
updateSendButton();
}
@Override
public void onTypingStopped() {
Account.State status = conversation.getAccount().getStatus();
if (status == Account.State.ONLINE && conversation.setOutgoingChatState(ChatState.PAUSED)) {
activity.xmppConnectionService.sendChatState(conversation);
}
}
@Override
public void onTextDeleted() {
Account.State status = conversation.getAccount().getStatus();
if (status == Account.State.ONLINE && conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
activity.xmppConnectionService.sendChatState(conversation);
}
updateSendButton();
}
2014-01-23 20:04:05 -05:00
}