aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/siacs/conversations/ui/ConversationFragment.java
blob: 34c68e00ba8116b3229fb8cddfc4e62064899352 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package eu.siacs.conversations.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import net.java.otr4j.session.SessionStatus;
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;
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.EditMessage.OnEnterPressed;
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;
import eu.siacs.conversations.utils.UIHelper;
import android.app.AlertDialog;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView.OnEditorActionListener;
import android.widget.AbsListView;

import android.widget.ListView;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ConversationFragment extends Fragment {

	protected Conversation conversation;
	protected ListView messagesView;
	protected LayoutInflater inflater;
	protected List<Message> messageList = new ArrayList<Message>();
	protected MessageAdapter messageListAdapter;
	protected Contact contact;

	protected String queuedPqpMessage = null;

	private EditMessage mEditMessage;
	private ImageButton mSendButton;
	private String pastedText = null;
	private RelativeLayout snackbar;
	private TextView snackbarMessage;
	private TextView snackbarAction;

	private boolean messagesLoaded = false;

	private IntentSender askForPassphraseIntent = null;

	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);
				imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
				sendMessage();
				return true;
			} else {
				return false;
			}
		}
	};

	private OnClickListener mSendButtonListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			sendMessage();
		}
	};
	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);
				} catch (SendIntentException e) {
					//
				}
			}
		}
	};

	private OnClickListener clickToMuc = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(getActivity(),
					ConferenceDetailsActivity.class);
			intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
			intent.putExtra("uuid", conversation.getUuid());
			startActivity(intent);
		}
	};

	private OnClickListener leaveMuc = new OnClickListener() {

		@Override
		public void onClick(View v) {
			activity.endConversation(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);
				}
			});
		}
	};

	private OnScrollListener mOnScrollListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if (firstVisibleItem == 0 && messagesLoaded) {
				long timestamp = messageList.get(0).getTimeSent();
				messagesLoaded = false;
				int size = activity.xmppConnectionService.loadMoreMessages(
						conversation, timestamp);
				messageList.clear();
				messageList.addAll(conversation.getMessages());
				messageListAdapter.notifyDataSetChanged();
				if (size != 0) {
					messagesLoaded = true;
				}
				messagesView.setSelectionFromTop(size + 1, 0);
			}
		}
	};

	private ConversationActivity activity;

	private void sendMessage() {
		if (this.conversation == null) {
			return;
		}
		if (mEditMessage.getText().length() < 1) {
			if (this.conversation.getMode() == Conversation.MODE_MULTI) {
				conversation.setNextPresence(null);
				updateChatMsgHint();
			}
			return;
		}
		Message message = new Message(conversation, mEditMessage.getText()
				.toString(), conversation.getNextEncryption(activity
				.forceEncryption()));
		if (conversation.getMode() == Conversation.MODE_MULTI) {
			if (conversation.getNextPresence() != null) {
				message.setPresence(conversation.getNextPresence());
				message.setType(Message.TYPE_PRIVATE);
				conversation.setNextPresence(null);
			}
		}
		if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_OTR) {
			sendOtrMessage(message);
		} else if (conversation.getNextEncryption(activity.forceEncryption()) == Message.ENCRYPTION_PGP) {
			sendPgpMessage(message);
		} else {
			sendPlainTextMessage(message);
		}
	}

	public void updateChatMsgHint() {
		if (conversation.getMode() == Conversation.MODE_MULTI
				&& conversation.getNextPresence() != null) {
			this.mEditMessage.setHint(getString(
					R.string.send_private_message_to,
					conversation.getNextPresence()));
		} 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;
			}
		}
	}

	@Override
	public View onCreateView(final LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.fragment_conversation,
				container, false);
		mEditMessage = (EditMessage) view.findViewById(R.id.textinput);
		mEditMessage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (activity.getSlidingPaneLayout().isSlideable()) {
					activity.getSlidingPaneLayout().closePane();
				}
			}
		});
		mEditMessage.setOnEditorActionListener(mEditorActionListener);
		mEditMessage.setOnEnterPressedListener(new OnEnterPressed() {

			@Override
			public void onEnterPressed() {
				sendMessage();
			}
		});

		mSendButton = (ImageButton) view.findViewById(R.id.textSendButton);
		mSendButton.setOnClickListener(this.mSendButtonListener);

		snackbar = (RelativeLayout) view.findViewById(R.id.snackbar);
		snackbarMessage = (TextView) view.findViewById(R.id.snackbar_message);
		snackbarAction = (TextView) view.findViewById(R.id.snackbar_action);

		messagesView = (ListView) view.findViewById(R.id.messages_view);
		messagesView.setOnScrollListener(mOnScrollListener);
		messagesView.setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL);
		messageListAdapter = new MessageAdapter(
				(ConversationActivity) getActivity(), this.messageList);
		messageListAdapter
				.setOnContactPictureClicked(new OnContactPictureClicked() {

					@Override
					public void onContactPictureClicked(Message message) {
						if (message.getStatus() <= Message.STATUS_RECEIVED) {
							if (message.getConversation().getMode() == Conversation.MODE_MULTI) {
								if (message.getPresence() != null) {
									highlightInConference(message.getPresence());
								} else {
									highlightInConference(message
											.getCounterpart());
								}
							} else {
								Contact contact = message.getConversation()
										.getContact();
								if (contact.showInRoster()) {
									activity.switchToContactDetails(contact);
								} else {
									activity.showAddToRosterDialog(message
											.getConversation());
								}
							}
						}
					}
				});
		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.getPresence() != null) {
									privateMessageWith(message.getPresence());
								} else {
									privateMessageWith(message.getCounterpart());
								}
							}
						}
					}
				});
		messagesView.setAdapter(messageListAdapter);

		return view;
	}

	protected void privateMessageWith(String counterpart) {
		this.mEditMessage.setText("");
		this.conversation.setNextPresence(counterpart);
		updateChatMsgHint();
	}

	protected void highlightInConference(String nick) {
		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 + " ");
		}
	}

	@Override
	public void onStart() {
		super.onStart();
		this.activity = (ConversationActivity) getActivity();
		if (activity.xmppConnectionServiceBound) {
			this.onBackendConnected();
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		if (this.conversation != null) {
			this.conversation.setNextMessage(mEditMessage.getText().toString());
		}
	}

	public void onBackendConnected() {
		this.activity = (ConversationActivity) getActivity();
		this.conversation = activity.getSelectedConversation();
		if (this.conversation == null) {
			return;
		}
		String oldString = conversation.getNextMessage().trim();
		if (this.pastedText == null) {
			this.mEditMessage.setText(oldString);
		} else {

			if (oldString.isEmpty()) {
				mEditMessage.setText(pastedText);
			} else {
				mEditMessage.setText(oldString + " " + pastedText);
			}
			pastedText = null;
		}
		int position = mEditMessage.length();
		Editable etext = mEditMessage.getText();
		Selection.setSelection(etext, position);
		if (activity.getSlidingPaneLayout().isSlideable()) {
			if (!activity.shouldPaneBeOpen()) {
				activity.getSlidingPaneLayout().closePane();
				activity.openConversation(conversation);
			}
		}
		if (this.conversation.getMode() == Conversation.MODE_MULTI) {
			conversation.setNextPresence(null);
		}
		updateMessages();
	}

	private void decryptMessage(Message message) {
		PgpEngine engine = activity.xmppConnectionService.getPgpEngine();
		if (engine != null) {
			engine.decrypt(message, new UiCallback<Message>() {

				@Override
				public void userInputRequried(PendingIntent pi, Message message) {
					askForPassphraseIntent = pi.getIntentSender();
					showSnackbar(R.string.openpgp_messages_found,
							R.string.decrypt, clickToDecryptListener);
				}

				@Override
				public void success(Message message) {
					activity.xmppConnectionService.databaseBackend
							.updateMessage(message);
					updateMessages();
				}

				@Override
				public void error(int error, Message message) {
					message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
					// updateMessages();
				}
			});
		}
	}

	public void updateMessages() {
		if (getView() == null) {
			return;
		}
		hideSnackbar();
		final ConversationActivity activity = (ConversationActivity) getActivity();
		if (this.conversation != null) {
			final Contact contact = this.conversation.getContact();
			if (this.conversation.isMuted()) {
				showSnackbar(R.string.notifications_disabled, R.string.enable,
						new OnClickListener() {

							@Override
							public void onClick(View v) {
								conversation.setMutedTill(0);
								updateMessages();
							}
						});
			} else if (!contact.showInRoster()
					&& contact
							.getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) {
				showSnackbar(R.string.contact_added_you, R.string.add_back,
						new OnClickListener() {

							@Override
							public void onClick(View v) {
								activity.xmppConnectionService
										.createContact(contact);
								activity.switchToContactDetails(contact);
							}
						});
			}
			for (Message message : this.conversation.getMessages()) {
				if ((message.getEncryption() == Message.ENCRYPTION_PGP)
						&& ((message.getStatus() == Message.STATUS_RECEIVED) || (message
								.getStatus() == Message.STATUS_SEND))) {
					decryptMessage(message);
					break;
				}
			}
			this.messageList.clear();
			if (this.conversation.getMessages().size() == 0) {
				messagesLoaded = false;
			} else {
				this.messageList.addAll(this.conversation.getMessages());
				messagesLoaded = true;
				updateStatusMessages();
			}
			this.messageListAdapter.notifyDataSetChanged();
			if (conversation.getMode() == Conversation.MODE_SINGLE) {
				if (messageList.size() >= 1) {
					makeFingerprintWarning(conversation.getLatestEncryption());
				}
			} else {
				if (!conversation.getMucOptions().online()
						&& conversation.getAccount().getStatus() == Account.STATUS_ONLINE) {
					int error = conversation.getMucOptions().getError();
					switch (error) {
					case MucOptions.ERROR_NICK_IN_USE:
						showSnackbar(R.string.nick_in_use, R.string.edit,
								clickToMuc);
						break;
					case MucOptions.ERROR_ROOM_NOT_FOUND:
						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;
					default:
						break;
					}
				}
			}
			getActivity().invalidateOptionsMenu();
			updateChatMsgHint();
			if (!activity.shouldPaneBeOpen()) {
				activity.xmppConnectionService.markRead(conversation,true);
				activity.updateConversationList();
			}
			this.updateSendButton();
		}
	}

	private void messageSent() {
		int size = this.messageList.size();
		if (size >= 1 && this.messagesView.getLastVisiblePosition() != size - 1) {
			messagesView.setSelection(size - 1);
		}
		mEditMessage.setText("");
		updateChatMsgHint();
	}

	public void updateSendButton() {
		Conversation c = this.conversation;
		if (activity.useSendButtonToIndicateStatus() && c != null
				&& c.getAccount().getStatus() == Account.STATUS_ONLINE) {
			if (c.getMode() == Conversation.MODE_SINGLE) {
				switch (c.getContact().getMostAvailableStatus()) {
				case Presences.CHAT:
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_online);
					break;
				case Presences.ONLINE:
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_online);
					break;
				case Presences.AWAY:
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_away);
					break;
				case Presences.XA:
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_away);
					break;
				case Presences.DND:
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_dnd);
					break;
				default:
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_offline);
					break;
				}
			} else if (c.getMode() == Conversation.MODE_MULTI) {
				if (c.getMucOptions().online()) {
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_online);
				} else {
					this.mSendButton
							.setImageResource(R.drawable.ic_action_send_now_offline);
				}
			} else {
				this.mSendButton
						.setImageResource(R.drawable.ic_action_send_now_offline);
			}
		} else {
			this.mSendButton
					.setImageResource(R.drawable.ic_action_send_now_offline);
		}
	}

	protected void updateStatusMessages() {
		if (conversation.getMode() == Conversation.MODE_SINGLE) {
			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));
						return;
					}
				}
			}
		}
	}

	protected void makeFingerprintWarning(int latestEncryption) {
		Set<String> knownFingerprints = conversation.getContact()
				.getOtrFingerprints();
		if ((latestEncryption == Message.ENCRYPTION_OTR)
				&& (conversation.hasValidOtrSession()
						&& (!conversation.isMuted())
						&& (conversation.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) && (!knownFingerprints
							.contains(conversation.getOtrFingerprint())))) {
			showSnackbar(R.string.unknown_otr_fingerprint, R.string.verify,
					new OnClickListener() {

						@Override
						public void onClick(View v) {
							if (conversation.getOtrFingerprint() != null) {
								AlertDialog dialog = UIHelper
										.getVerifyFingerprintDialog(
												(ConversationActivity) getActivity(),
												conversation, snackbar);
								dialog.show();
							}
						}
					});
		}
	}

	protected void showSnackbar(int message, int action,
			OnClickListener clickListener) {
		snackbar.setVisibility(View.VISIBLE);
		snackbar.setOnClickListener(null);
		snackbarMessage.setText(message);
		snackbarMessage.setOnClickListener(null);
		snackbarAction.setText(action);
		snackbarAction.setOnClickListener(clickListener);
	}

	protected void hideSnackbar() {
		snackbar.setVisibility(View.GONE);
	}

	protected void sendPlainTextMessage(Message message) {
		ConversationActivity activity = (ConversationActivity) getActivity();
		activity.xmppConnectionService.sendMessage(message);
		messageSent();
	}

	protected void sendPgpMessage(final Message message) {
		final ConversationActivity activity = (ConversationActivity) getActivity();
		final XmppConnectionService xmppService = activity.xmppConnectionService;
		final Contact contact = message.getConversation().getContact();
		if (activity.hasPgp()) {
			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) {
									messageSent();
									activity.encryptTextMessage(message);
								}

								@Override
								public void error(int error, Contact contact) {

								}
							});

				} else {
					showNoPGPKeyDialog(false,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									conversation
											.setNextEncryption(Message.ENCRYPTION_NONE);
									xmppService.databaseBackend
											.updateConversation(conversation);
									message.setEncryption(Message.ENCRYPTION_NONE);
									xmppService.sendMessage(message);
									messageSent();
								}
							});
				}
			} 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);
									xmppService.databaseBackend
											.updateConversation(conversation);
									xmppService.sendMessage(message);
									messageSent();
								}
							});
				}
			}
		} else {
			activity.showInstallPgpDialog();
		}
	}

	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) {
		final ConversationActivity activity = (ConversationActivity) getActivity();
		final XmppConnectionService xmppService = activity.xmppConnectionService;
		if (conversation.hasValidOtrSession()) {
			activity.xmppConnectionService.sendMessage(message);
			messageSent();
		} else {
			activity.selectPresence(message.getConversation(),
					new OnPresenceSelected() {

						@Override
						public void onPresenceSelected() {
							message.setPresence(conversation.getNextPresence());
							xmppService.sendMessage(message);
							messageSent();
						}
					});
		}
	}

	public void setText(String text) {
		this.pastedText = text;
	}

	public void clearInputField() {
		this.mEditMessage.setText("");
	}
}