ConversationActivity.java

   1package eu.siacs.conversations.ui;
   2
   3import android.annotation.SuppressLint;
   4import android.app.ActionBar;
   5import android.app.AlertDialog;
   6import android.app.FragmentTransaction;
   7import android.app.PendingIntent;
   8import android.content.ClipData;
   9import android.content.DialogInterface;
  10import android.content.DialogInterface.OnClickListener;
  11import android.content.Intent;
  12import android.content.IntentSender.SendIntentException;
  13import android.net.Uri;
  14import android.os.Build;
  15import android.os.Bundle;
  16import android.provider.MediaStore;
  17import android.support.v4.widget.SlidingPaneLayout;
  18import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
  19import android.util.Log;
  20import android.view.Menu;
  21import android.view.MenuItem;
  22import android.view.View;
  23import android.widget.AdapterView;
  24import android.widget.AdapterView.OnItemClickListener;
  25import android.widget.ArrayAdapter;
  26import android.widget.CheckBox;
  27import android.widget.PopupMenu;
  28import android.widget.PopupMenu.OnMenuItemClickListener;
  29import android.widget.Toast;
  30
  31import net.java.otr4j.session.SessionStatus;
  32
  33import java.util.ArrayList;
  34import java.util.Iterator;
  35import java.util.List;
  36
  37import de.timroes.android.listview.EnhancedListView;
  38import eu.siacs.conversations.Config;
  39import eu.siacs.conversations.R;
  40import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  41import eu.siacs.conversations.crypto.axolotl.SQLiteAxolotlStore.Trust;
  42import eu.siacs.conversations.entities.Account;
  43import eu.siacs.conversations.entities.Blockable;
  44import eu.siacs.conversations.entities.Contact;
  45import eu.siacs.conversations.entities.Conversation;
  46import eu.siacs.conversations.entities.Message;
  47import eu.siacs.conversations.services.XmppConnectionService;
  48import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
  49import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
  50import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
  51import eu.siacs.conversations.ui.adapter.ConversationAdapter;
  52import eu.siacs.conversations.utils.ExceptionHelper;
  53import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
  54import eu.siacs.conversations.xmpp.jid.InvalidJidException;
  55import eu.siacs.conversations.xmpp.jid.Jid;
  56
  57public class ConversationActivity extends XmppActivity
  58	implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
  59
  60	public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
  61
  62	public static final String VIEW_CONVERSATION = "viewConversation";
  63	public static final String CONVERSATION = "conversationUuid";
  64	public static final String MESSAGE = "messageUuid";
  65	public static final String TEXT = "text";
  66	public static final String NICK = "nick";
  67	public static final String PRIVATE_MESSAGE = "pm";
  68
  69	public static final int REQUEST_SEND_MESSAGE = 0x0201;
  70	public static final int REQUEST_DECRYPT_PGP = 0x0202;
  71	public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
  72	public static final int REQUEST_TRUST_KEYS_TEXT = 0x0208;
  73	public static final int REQUEST_TRUST_KEYS_MENU = 0x0209;
  74	public static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
  75	public static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
  76	public static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
  77	public static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
  78	public static final int ATTACHMENT_CHOICE_LOCATION = 0x0305;
  79	public static final int ATTACHMENT_CHOICE_INVALID = 0x0306;
  80	private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
  81	private static final String STATE_PANEL_OPEN = "state_panel_open";
  82	private static final String STATE_PENDING_URI = "state_pending_uri";
  83
  84	private String mOpenConverstaion = null;
  85	private boolean mPanelOpen = true;
  86	final private List<Uri> mPendingImageUris = new ArrayList<>();
  87	final private List<Uri> mPendingFileUris = new ArrayList<>();
  88	private Uri mPendingGeoUri = null;
  89	private boolean forbidProcessingPendings = false;
  90
  91	private View mContentView;
  92
  93	private List<Conversation> conversationList = new ArrayList<>();
  94	private Conversation swipedConversation = null;
  95	private Conversation mSelectedConversation = null;
  96	private EnhancedListView listView;
  97	private ConversationFragment mConversationFragment;
  98
  99	private ArrayAdapter<Conversation> listAdapter;
 100
 101	private Toast prepareFileToast;
 102
 103	private boolean mActivityPaused = false;
 104	private boolean mRedirected = true;
 105
 106	public Conversation getSelectedConversation() {
 107		return this.mSelectedConversation;
 108	}
 109
 110	public void setSelectedConversation(Conversation conversation) {
 111		this.mSelectedConversation = conversation;
 112	}
 113
 114	public void showConversationsOverview() {
 115		if (mContentView instanceof SlidingPaneLayout) {
 116			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 117			mSlidingPaneLayout.openPane();
 118		}
 119	}
 120
 121	@Override
 122	protected String getShareableUri() {
 123		Conversation conversation = getSelectedConversation();
 124		if (conversation != null) {
 125			return conversation.getAccount().getShareableUri();
 126		} else {
 127			return "";
 128		}
 129	}
 130
 131	public void hideConversationsOverview() {
 132		if (mContentView instanceof SlidingPaneLayout) {
 133			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 134			mSlidingPaneLayout.closePane();
 135		}
 136	}
 137
 138	public boolean isConversationsOverviewHideable() {
 139		if (mContentView instanceof SlidingPaneLayout) {
 140			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 141			return mSlidingPaneLayout.isSlideable();
 142		} else {
 143			return false;
 144		}
 145	}
 146
 147	public boolean isConversationsOverviewVisable() {
 148		if (mContentView instanceof SlidingPaneLayout) {
 149			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 150			return mSlidingPaneLayout.isOpen();
 151		} else {
 152			return true;
 153		}
 154	}
 155
 156	@Override
 157	protected void onCreate(final Bundle savedInstanceState) {
 158		super.onCreate(savedInstanceState);
 159		if (savedInstanceState != null) {
 160			mOpenConverstaion = savedInstanceState.getString(STATE_OPEN_CONVERSATION, null);
 161			mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
 162			String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
 163			if (pending != null) {
 164				mPendingImageUris.clear();
 165				mPendingImageUris.add(Uri.parse(pending));
 166			}
 167		}
 168
 169		setContentView(R.layout.fragment_conversations_overview);
 170
 171		this.mConversationFragment = new ConversationFragment();
 172		FragmentTransaction transaction = getFragmentManager().beginTransaction();
 173		transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
 174		transaction.commit();
 175
 176		listView = (EnhancedListView) findViewById(R.id.list);
 177		this.listAdapter = new ConversationAdapter(this, conversationList);
 178		listView.setAdapter(this.listAdapter);
 179
 180		if (getActionBar() != null) {
 181			getActionBar().setDisplayHomeAsUpEnabled(false);
 182			getActionBar().setHomeButtonEnabled(false);
 183		}
 184
 185		listView.setOnItemClickListener(new OnItemClickListener() {
 186
 187			@Override
 188			public void onItemClick(AdapterView<?> arg0, View clickedView,
 189					int position, long arg3) {
 190				if (getSelectedConversation() != conversationList.get(position)) {
 191					setSelectedConversation(conversationList.get(position));
 192					ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 193				}
 194				hideConversationsOverview();
 195				openConversation();
 196			}
 197		});
 198
 199		listView.setDismissCallback(new EnhancedListView.OnDismissCallback() {
 200
 201			@Override
 202			public EnhancedListView.Undoable onDismiss(final EnhancedListView enhancedListView, final int position) {
 203
 204				final int index = listView.getFirstVisiblePosition();
 205				View v = listView.getChildAt(0);
 206				final int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
 207
 208				swipedConversation = listAdapter.getItem(position);
 209				listAdapter.remove(swipedConversation);
 210				swipedConversation.markRead();
 211				xmppConnectionService.getNotificationService().clear(swipedConversation);
 212
 213				final boolean formerlySelected = (getSelectedConversation() == swipedConversation);
 214				if (position == 0 && listAdapter.getCount() == 0) {
 215					endConversation(swipedConversation, false, true);
 216					return null;
 217				} else if (formerlySelected) {
 218					setSelectedConversation(listAdapter.getItem(0));
 219					ConversationActivity.this.mConversationFragment
 220							.reInit(getSelectedConversation());
 221				}
 222
 223				return new EnhancedListView.Undoable() {
 224
 225					@Override
 226					public void undo() {
 227						listAdapter.insert(swipedConversation, position);
 228						if (formerlySelected) {
 229							setSelectedConversation(swipedConversation);
 230							ConversationActivity.this.mConversationFragment
 231									.reInit(getSelectedConversation());
 232						}
 233						swipedConversation = null;
 234						listView.setSelectionFromTop(index + (listView.getChildCount() < position ? 1 : 0), top);
 235					}
 236
 237					@Override
 238					public void discard() {
 239						if (!swipedConversation.isRead()
 240								&& swipedConversation.getMode() == Conversation.MODE_SINGLE) {
 241							swipedConversation = null;
 242							return;
 243						}
 244						endConversation(swipedConversation, false, false);
 245						swipedConversation = null;
 246					}
 247
 248					@Override
 249					public String getTitle() {
 250						if (swipedConversation.getMode() == Conversation.MODE_MULTI) {
 251							return getResources().getString(R.string.title_undo_swipe_out_muc);
 252						} else {
 253							return getResources().getString(R.string.title_undo_swipe_out_conversation);
 254						}
 255					}
 256				};
 257			}
 258		});
 259		listView.enableSwipeToDismiss();
 260		listView.setSwipingLayout(R.id.swipeable_item);
 261		listView.setUndoStyle(EnhancedListView.UndoStyle.SINGLE_POPUP);
 262		listView.setUndoHideDelay(5000);
 263		listView.setRequireTouchBeforeDismiss(false);
 264
 265		mContentView = findViewById(R.id.content_view_spl);
 266		if (mContentView == null) {
 267			mContentView = findViewById(R.id.content_view_ll);
 268		}
 269		if (mContentView instanceof SlidingPaneLayout) {
 270			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 271			mSlidingPaneLayout.setParallaxDistance(150);
 272			mSlidingPaneLayout
 273				.setShadowResource(R.drawable.es_slidingpane_shadow);
 274			mSlidingPaneLayout.setSliderFadeColor(0);
 275			mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
 276
 277				@Override
 278				public void onPanelOpened(View arg0) {
 279					updateActionBarTitle();
 280					invalidateOptionsMenu();
 281					hideKeyboard();
 282					if (xmppConnectionServiceBound) {
 283						xmppConnectionService.getNotificationService()
 284							.setOpenConversation(null);
 285					}
 286					closeContextMenu();
 287				}
 288
 289				@Override
 290				public void onPanelClosed(View arg0) {
 291					listView.discardUndo();
 292					openConversation();
 293				}
 294
 295				@Override
 296				public void onPanelSlide(View arg0, float arg1) {
 297					// TODO Auto-generated method stub
 298
 299				}
 300			});
 301		}
 302	}
 303
 304	@Override
 305	public void switchToConversation(Conversation conversation) {
 306		setSelectedConversation(conversation);
 307		runOnUiThread(new Runnable() {
 308			@Override
 309			public void run() {
 310				ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 311				openConversation();
 312			}
 313		});
 314	}
 315
 316	private void updateActionBarTitle() {
 317		updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
 318	}
 319
 320	private void updateActionBarTitle(boolean titleShouldBeName) {
 321		final ActionBar ab = getActionBar();
 322		final Conversation conversation = getSelectedConversation();
 323		if (ab != null) {
 324			if (titleShouldBeName && conversation != null) {
 325				ab.setDisplayHomeAsUpEnabled(true);
 326				ab.setHomeButtonEnabled(true);
 327				if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
 328					ab.setTitle(conversation.getName());
 329				} else {
 330					ab.setTitle(conversation.getJid().toBareJid().toString());
 331				}
 332			} else {
 333				ab.setDisplayHomeAsUpEnabled(false);
 334				ab.setHomeButtonEnabled(false);
 335				ab.setTitle(R.string.app_name);
 336			}
 337		}
 338	}
 339
 340	private void openConversation() {
 341		this.updateActionBarTitle();
 342		this.invalidateOptionsMenu();
 343		if (xmppConnectionServiceBound) {
 344			final Conversation conversation = getSelectedConversation();
 345			xmppConnectionService.getNotificationService().setOpenConversation(conversation);
 346			sendReadMarkerIfNecessary(conversation);
 347		}
 348		listAdapter.notifyDataSetChanged();
 349	}
 350
 351	public void sendReadMarkerIfNecessary(final Conversation conversation) {
 352		if (!mActivityPaused && conversation != null) {
 353			if (!conversation.isRead()) {
 354				xmppConnectionService.sendReadMarker(conversation);
 355			} else {
 356				xmppConnectionService.markRead(conversation);
 357			}
 358		}
 359	}
 360
 361	@Override
 362	public boolean onCreateOptionsMenu(Menu menu) {
 363		getMenuInflater().inflate(R.menu.conversations, menu);
 364		final MenuItem menuSecure = menu.findItem(R.id.action_security);
 365		final MenuItem menuArchive = menu.findItem(R.id.action_archive);
 366		final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
 367		final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
 368		final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
 369		final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
 370		final MenuItem menuAdd = menu.findItem(R.id.action_add);
 371		final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
 372		final MenuItem menuMute = menu.findItem(R.id.action_mute);
 373		final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
 374
 375		if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
 376			menuArchive.setVisible(false);
 377			menuMucDetails.setVisible(false);
 378			menuContactDetails.setVisible(false);
 379			menuSecure.setVisible(false);
 380			menuInviteContact.setVisible(false);
 381			menuAttach.setVisible(false);
 382			menuClearHistory.setVisible(false);
 383			menuMute.setVisible(false);
 384			menuUnmute.setVisible(false);
 385		} else {
 386			menuAdd.setVisible(!isConversationsOverviewHideable());
 387			if (this.getSelectedConversation() != null) {
 388				if (this.getSelectedConversation().getNextEncryption(forceEncryption()) != Message.ENCRYPTION_NONE) {
 389					if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 390						menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
 391					} else {
 392						menuSecure.setIcon(R.drawable.ic_action_secure);
 393					}
 394				}
 395				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
 396					menuContactDetails.setVisible(false);
 397					menuAttach.setVisible(getSelectedConversation().getAccount().httpUploadAvailable());
 398					menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
 399				} else {
 400					menuMucDetails.setVisible(false);
 401				}
 402				if (this.getSelectedConversation().isMuted()) {
 403					menuMute.setVisible(false);
 404				} else {
 405					menuUnmute.setVisible(false);
 406				}
 407			}
 408		}
 409		return true;
 410	}
 411
 412	protected void selectPresenceToAttachFile(final int attachmentChoice, final int encryption) {
 413		final Conversation conversation = getSelectedConversation();
 414		final Account account = conversation.getAccount();
 415		final OnPresenceSelected callback = new OnPresenceSelected() {
 416
 417			@Override
 418			public void onPresenceSelected() {
 419				Intent intent = new Intent();
 420				boolean chooser = false;
 421				String fallbackPackageId = null;
 422				switch (attachmentChoice) {
 423					case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
 424						intent.setAction(Intent.ACTION_GET_CONTENT);
 425						if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
 426							intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
 427						}
 428						intent.setType("image/*");
 429						chooser = true;
 430						break;
 431					case ATTACHMENT_CHOICE_TAKE_PHOTO:
 432						Uri uri = xmppConnectionService.getFileBackend().getTakePhotoUri();
 433						intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
 434						intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
 435						mPendingImageUris.clear();
 436						mPendingImageUris.add(uri);
 437						break;
 438					case ATTACHMENT_CHOICE_CHOOSE_FILE:
 439						chooser = true;
 440						intent.setType("*/*");
 441						intent.addCategory(Intent.CATEGORY_OPENABLE);
 442						intent.setAction(Intent.ACTION_GET_CONTENT);
 443						break;
 444					case ATTACHMENT_CHOICE_RECORD_VOICE:
 445						intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
 446						fallbackPackageId = "eu.siacs.conversations.voicerecorder";
 447						break;
 448					case ATTACHMENT_CHOICE_LOCATION:
 449						intent.setAction("eu.siacs.conversations.location.request");
 450						fallbackPackageId = "eu.siacs.conversations.sharelocation";
 451						break;
 452				}
 453				if (intent.resolveActivity(getPackageManager()) != null) {
 454					if (chooser) {
 455						startActivityForResult(
 456								Intent.createChooser(intent, getString(R.string.perform_action_with)),
 457								attachmentChoice);
 458					} else {
 459						startActivityForResult(intent, attachmentChoice);
 460					}
 461				} else if (fallbackPackageId != null) {
 462					startActivity(getInstallApkIntent(fallbackPackageId));
 463				}
 464			}
 465		};
 466		if ((account.httpUploadAvailable() || attachmentChoice == ATTACHMENT_CHOICE_LOCATION) && encryption != Message.ENCRYPTION_OTR) {
 467			conversation.setNextCounterpart(null);
 468			callback.onPresenceSelected();
 469		} else {
 470			selectPresence(conversation, callback);
 471		}
 472	}
 473
 474	private Intent getInstallApkIntent(final String packageId) {
 475		Intent intent = new Intent(Intent.ACTION_VIEW);
 476		intent.setData(Uri.parse("market://details?id="+packageId));
 477		if (intent.resolveActivity(getPackageManager()) != null) {
 478			return intent;
 479		} else {
 480			intent.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + packageId));
 481			return intent;
 482		}
 483	}
 484
 485	public void attachFile(final int attachmentChoice) {
 486		switch (attachmentChoice) {
 487			case ATTACHMENT_CHOICE_LOCATION:
 488				getPreferences().edit().putString("recently_used_quick_action","location").apply();
 489				break;
 490			case ATTACHMENT_CHOICE_RECORD_VOICE:
 491				getPreferences().edit().putString("recently_used_quick_action","voice").apply();
 492				break;
 493			case ATTACHMENT_CHOICE_TAKE_PHOTO:
 494				getPreferences().edit().putString("recently_used_quick_action","photo").apply();
 495				break;
 496			case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
 497				getPreferences().edit().putString("recently_used_quick_action","picture").apply();
 498				break;
 499		}
 500		final Conversation conversation = getSelectedConversation();
 501		final int encryption = conversation.getNextEncryption(forceEncryption());
 502		if (encryption == Message.ENCRYPTION_PGP) {
 503			if (hasPgp()) {
 504				if (conversation.getContact().getPgpKeyId() != 0) {
 505					xmppConnectionService.getPgpEngine().hasKey(
 506							conversation.getContact(),
 507							new UiCallback<Contact>() {
 508
 509								@Override
 510								public void userInputRequried(PendingIntent pi,
 511										Contact contact) {
 512									ConversationActivity.this.runIntent(pi,attachmentChoice);
 513								}
 514
 515								@Override
 516								public void success(Contact contact) {
 517									selectPresenceToAttachFile(attachmentChoice,encryption);
 518								}
 519
 520								@Override
 521								public void error(int error, Contact contact) {
 522									displayErrorDialog(error);
 523								}
 524							});
 525				} else {
 526					final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 527						.findFragmentByTag("conversation");
 528					if (fragment != null) {
 529						fragment.showNoPGPKeyDialog(false,
 530								new OnClickListener() {
 531
 532									@Override
 533									public void onClick(DialogInterface dialog,
 534											int which) {
 535										conversation
 536											.setNextEncryption(Message.ENCRYPTION_NONE);
 537										xmppConnectionService.databaseBackend
 538											.updateConversation(conversation);
 539										selectPresenceToAttachFile(attachmentChoice,Message.ENCRYPTION_NONE);
 540									}
 541								});
 542					}
 543				}
 544			} else {
 545				showInstallPgpDialog();
 546			}
 547		} else {
 548			if (encryption != Message.ENCRYPTION_AXOLOTL || !trustKeysIfNeeded(REQUEST_TRUST_KEYS_MENU, attachmentChoice)) {
 549				selectPresenceToAttachFile(attachmentChoice, encryption);
 550			}
 551		}
 552	}
 553
 554	@Override
 555	public boolean onOptionsItemSelected(final MenuItem item) {
 556		if (item.getItemId() == android.R.id.home) {
 557			showConversationsOverview();
 558			return true;
 559		} else if (item.getItemId() == R.id.action_add) {
 560			startActivity(new Intent(this, StartConversationActivity.class));
 561			return true;
 562		} else if (getSelectedConversation() != null) {
 563			switch (item.getItemId()) {
 564				case R.id.action_attach_file:
 565					attachFileDialog();
 566					break;
 567				case R.id.action_archive:
 568					this.endConversation(getSelectedConversation());
 569					break;
 570				case R.id.action_contact_details:
 571					switchToContactDetails(getSelectedConversation().getContact());
 572					break;
 573				case R.id.action_muc_details:
 574					Intent intent = new Intent(this,
 575							ConferenceDetailsActivity.class);
 576					intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
 577					intent.putExtra("uuid", getSelectedConversation().getUuid());
 578					startActivity(intent);
 579					break;
 580				case R.id.action_invite:
 581					inviteToConversation(getSelectedConversation());
 582					break;
 583				case R.id.action_security:
 584					selectEncryptionDialog(getSelectedConversation());
 585					break;
 586				case R.id.action_clear_history:
 587					clearHistoryDialog(getSelectedConversation());
 588					break;
 589				case R.id.action_mute:
 590					muteConversationDialog(getSelectedConversation());
 591					break;
 592				case R.id.action_unmute:
 593					unmuteConversation(getSelectedConversation());
 594					break;
 595				case R.id.action_block:
 596					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 597					break;
 598				case R.id.action_unblock:
 599					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 600					break;
 601				default:
 602					break;
 603			}
 604			return super.onOptionsItemSelected(item);
 605		} else {
 606			return super.onOptionsItemSelected(item);
 607		}
 608	}
 609
 610	public void endConversation(Conversation conversation) {
 611		endConversation(conversation, true, true);
 612	}
 613
 614	public void endConversation(Conversation conversation, boolean showOverview, boolean reinit) {
 615		if (showOverview) {
 616			showConversationsOverview();
 617		}
 618		xmppConnectionService.archiveConversation(conversation);
 619		if (reinit) {
 620			if (conversationList.size() > 0) {
 621				setSelectedConversation(conversationList.get(0));
 622				this.mConversationFragment.reInit(getSelectedConversation());
 623			} else {
 624				setSelectedConversation(null);
 625			}
 626		}
 627	}
 628
 629	@SuppressLint("InflateParams")
 630	protected void clearHistoryDialog(final Conversation conversation) {
 631		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 632		builder.setTitle(getString(R.string.clear_conversation_history));
 633		View dialogView = getLayoutInflater().inflate(
 634				R.layout.dialog_clear_history, null);
 635		final CheckBox endConversationCheckBox = (CheckBox) dialogView
 636			.findViewById(R.id.end_conversation_checkbox);
 637		builder.setView(dialogView);
 638		builder.setNegativeButton(getString(R.string.cancel), null);
 639		builder.setPositiveButton(getString(R.string.delete_messages),
 640				new OnClickListener() {
 641
 642					@Override
 643					public void onClick(DialogInterface dialog, int which) {
 644						ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
 645						if (endConversationCheckBox.isChecked()) {
 646							endConversation(conversation);
 647						} else {
 648							updateConversationList();
 649							ConversationActivity.this.mConversationFragment.updateMessages();
 650						}
 651					}
 652				});
 653		builder.create().show();
 654	}
 655
 656	protected void attachFileDialog() {
 657		View menuAttachFile = findViewById(R.id.action_attach_file);
 658		if (menuAttachFile == null) {
 659			return;
 660		}
 661		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
 662		attachFilePopup.inflate(R.menu.attachment_choices);
 663		if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
 664			attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
 665		}
 666		if (new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null) {
 667			attachFilePopup.getMenu().findItem(R.id.attach_location).setVisible(false);
 668		}
 669		attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 670
 671			@Override
 672			public boolean onMenuItemClick(MenuItem item) {
 673				switch (item.getItemId()) {
 674					case R.id.attach_choose_picture:
 675						attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
 676						break;
 677					case R.id.attach_take_picture:
 678						attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
 679						break;
 680					case R.id.attach_choose_file:
 681						attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
 682						break;
 683					case R.id.attach_record_voice:
 684						attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
 685						break;
 686					case R.id.attach_location:
 687						attachFile(ATTACHMENT_CHOICE_LOCATION);
 688						break;
 689				}
 690				return false;
 691			}
 692		});
 693		attachFilePopup.show();
 694	}
 695
 696	public void verifyOtrSessionDialog(final Conversation conversation, View view) {
 697		if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
 698			Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
 699			return;
 700		}
 701		if (view == null) {
 702			return;
 703		}
 704		PopupMenu popup = new PopupMenu(this, view);
 705		popup.inflate(R.menu.verification_choices);
 706		popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 707			@Override
 708			public boolean onMenuItemClick(MenuItem menuItem) {
 709				Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
 710				intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
 711				intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
 712				intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
 713				switch (menuItem.getItemId()) {
 714					case R.id.scan_fingerprint:
 715						intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
 716						break;
 717					case R.id.ask_question:
 718						intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
 719						break;
 720					case R.id.manual_verification:
 721						intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
 722						break;
 723				}
 724				startActivity(intent);
 725				return true;
 726			}
 727		});
 728		popup.show();
 729	}
 730
 731	protected void selectEncryptionDialog(final Conversation conversation) {
 732		View menuItemView = findViewById(R.id.action_security);
 733		if (menuItemView == null) {
 734			return;
 735		}
 736		PopupMenu popup = new PopupMenu(this, menuItemView);
 737		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 738			.findFragmentByTag("conversation");
 739		if (fragment != null) {
 740			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 741
 742				@Override
 743				public boolean onMenuItemClick(MenuItem item) {
 744					switch (item.getItemId()) {
 745						case R.id.encryption_choice_none:
 746							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 747							item.setChecked(true);
 748							break;
 749						case R.id.encryption_choice_otr:
 750							conversation.setNextEncryption(Message.ENCRYPTION_OTR);
 751							item.setChecked(true);
 752							break;
 753						case R.id.encryption_choice_pgp:
 754							if (hasPgp()) {
 755								if (conversation.getAccount().getKeys().has("pgp_signature")) {
 756									conversation.setNextEncryption(Message.ENCRYPTION_PGP);
 757									item.setChecked(true);
 758								} else {
 759									announcePgp(conversation.getAccount(),conversation);
 760								}
 761							} else {
 762								showInstallPgpDialog();
 763							}
 764							break;
 765						case R.id.encryption_choice_axolotl:
 766							Log.d(Config.LOGTAG, AxolotlService.getLogprefix(conversation.getAccount())
 767									+ "Enabled axolotl for Contact " + conversation.getContact().getJid());
 768							conversation.setNextEncryption(Message.ENCRYPTION_AXOLOTL);
 769							item.setChecked(true);
 770							break;
 771						default:
 772							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 773							break;
 774					}
 775					xmppConnectionService.databaseBackend.updateConversation(conversation);
 776					fragment.updateChatMsgHint();
 777					invalidateOptionsMenu();
 778					refreshUi();
 779					return true;
 780				}
 781			});
 782			popup.inflate(R.menu.encryption_choices);
 783			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
 784			MenuItem none = popup.getMenu().findItem(R.id.encryption_choice_none);
 785			MenuItem pgp = popup.getMenu().findItem(R.id.encryption_choice_pgp);
 786			MenuItem axolotl = popup.getMenu().findItem(R.id.encryption_choice_axolotl);
 787			if (conversation.getMode() == Conversation.MODE_MULTI) {
 788				otr.setEnabled(false);
 789				axolotl.setEnabled(false);
 790			} else {
 791				if (forceEncryption()) {
 792					none.setVisible(false);
 793				}
 794			}
 795			if (!conversation.getAccount().getAxolotlService().isContactAxolotlCapable(conversation.getContact())) {
 796				axolotl.setEnabled(false);
 797			}
 798			switch (conversation.getNextEncryption(forceEncryption())) {
 799				case Message.ENCRYPTION_NONE:
 800					none.setChecked(true);
 801					break;
 802				case Message.ENCRYPTION_OTR:
 803					otr.setChecked(true);
 804					break;
 805				case Message.ENCRYPTION_PGP:
 806					pgp.setChecked(true);
 807					break;
 808				case Message.ENCRYPTION_AXOLOTL:
 809					popup.getMenu().findItem(R.id.encryption_choice_axolotl)
 810							.setChecked(true);
 811					break;
 812				default:
 813					none.setChecked(true);
 814					break;
 815			}
 816			popup.show();
 817		}
 818	}
 819
 820	protected void muteConversationDialog(final Conversation conversation) {
 821		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 822		builder.setTitle(R.string.disable_notifications);
 823		final int[] durations = getResources().getIntArray(
 824				R.array.mute_options_durations);
 825		builder.setItems(R.array.mute_options_descriptions,
 826				new OnClickListener() {
 827
 828					@Override
 829					public void onClick(final DialogInterface dialog, final int which) {
 830						final long till;
 831						if (durations[which] == -1) {
 832							till = Long.MAX_VALUE;
 833						} else {
 834							till = System.currentTimeMillis() + (durations[which] * 1000);
 835						}
 836						conversation.setMutedTill(till);
 837						ConversationActivity.this.xmppConnectionService.databaseBackend
 838								.updateConversation(conversation);
 839						updateConversationList();
 840						ConversationActivity.this.mConversationFragment.updateMessages();
 841						invalidateOptionsMenu();
 842					}
 843				});
 844		builder.create().show();
 845	}
 846
 847	public void unmuteConversation(final Conversation conversation) {
 848		conversation.setMutedTill(0);
 849		this.xmppConnectionService.databaseBackend.updateConversation(conversation);
 850		updateConversationList();
 851		ConversationActivity.this.mConversationFragment.updateMessages();
 852		invalidateOptionsMenu();
 853	}
 854
 855	@Override
 856	public void onBackPressed() {
 857		if (!isConversationsOverviewVisable()) {
 858			showConversationsOverview();
 859		} else {
 860			moveTaskToBack(true);
 861		}
 862	}
 863
 864	@Override
 865	protected void onNewIntent(final Intent intent) {
 866		if (xmppConnectionServiceBound) {
 867			if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
 868				handleViewConversationIntent(intent);
 869			}
 870		} else {
 871			setIntent(intent);
 872		}
 873	}
 874
 875	@Override
 876	public void onStart() {
 877		super.onStart();
 878		this.mRedirected = false;
 879		if (this.xmppConnectionServiceBound) {
 880			this.onBackendConnected();
 881		}
 882		if (conversationList.size() >= 1) {
 883			this.onConversationUpdate();
 884		}
 885	}
 886
 887	@Override
 888	public void onPause() {
 889		listView.discardUndo();
 890		super.onPause();
 891		this.mActivityPaused = true;
 892		if (this.xmppConnectionServiceBound) {
 893			this.xmppConnectionService.getNotificationService().setIsInForeground(false);
 894		}
 895	}
 896
 897	@Override
 898	public void onResume() {
 899		super.onResume();
 900		final int theme = findTheme();
 901		final boolean usingEnterKey = usingEnterKey();
 902		if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
 903			recreate();
 904		}
 905		this.mActivityPaused = false;
 906		if (this.xmppConnectionServiceBound) {
 907			this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 908		}
 909
 910		if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
 911			sendReadMarkerIfNecessary(getSelectedConversation());
 912		}
 913
 914	}
 915
 916	@Override
 917	public void onSaveInstanceState(final Bundle savedInstanceState) {
 918		Conversation conversation = getSelectedConversation();
 919		if (conversation != null) {
 920			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
 921					conversation.getUuid());
 922		}
 923		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
 924				isConversationsOverviewVisable());
 925		if (this.mPendingImageUris.size() >= 1) {
 926			savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
 927		}
 928		super.onSaveInstanceState(savedInstanceState);
 929	}
 930
 931	@Override
 932	void onBackendConnected() {
 933		this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 934		updateConversationList();
 935
 936		if (mPendingConferenceInvite != null) {
 937			mPendingConferenceInvite.execute(this);
 938			mPendingConferenceInvite = null;
 939		}
 940
 941		if (xmppConnectionService.getAccounts().size() == 0) {
 942			if (!mRedirected) {
 943				this.mRedirected = true;
 944				startActivity(new Intent(this, EditAccountActivity.class));
 945				finish();
 946			}
 947		} else if (conversationList.size() <= 0) {
 948			if (!mRedirected) {
 949				this.mRedirected = true;
 950				Intent intent = new Intent(this, StartConversationActivity.class);
 951				intent.putExtra("init",true);
 952				startActivity(intent);
 953				finish();
 954			}
 955		} else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
 956			handleViewConversationIntent(getIntent());
 957		} else if (selectConversationByUuid(mOpenConverstaion)) {
 958			if (mPanelOpen) {
 959				showConversationsOverview();
 960			} else {
 961				if (isConversationsOverviewHideable()) {
 962					openConversation();
 963				}
 964			}
 965			this.mConversationFragment.reInit(getSelectedConversation());
 966			mOpenConverstaion = null;
 967		} else if (getSelectedConversation() == null) {
 968			showConversationsOverview();
 969			mPendingImageUris.clear();
 970			mPendingFileUris.clear();
 971			mPendingGeoUri = null;
 972			setSelectedConversation(conversationList.get(0));
 973			this.mConversationFragment.reInit(getSelectedConversation());
 974		}
 975
 976		if(!forbidProcessingPendings) {
 977			for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
 978				Uri foo = i.next();
 979				attachImageToConversation(getSelectedConversation(), foo);
 980			}
 981
 982			for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
 983				attachFileToConversation(getSelectedConversation(), i.next());
 984			}
 985
 986			if (mPendingGeoUri != null) {
 987				attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
 988				mPendingGeoUri = null;
 989			}
 990		}
 991		forbidProcessingPendings = false;
 992
 993		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
 994		setIntent(new Intent());
 995	}
 996
 997	private void handleViewConversationIntent(final Intent intent) {
 998		final String uuid = intent.getStringExtra(CONVERSATION);
 999		final String downloadUuid = intent.getStringExtra(MESSAGE);
1000		final String text = intent.getStringExtra(TEXT);
1001		final String nick = intent.getStringExtra(NICK);
1002		final boolean pm = intent.getBooleanExtra(PRIVATE_MESSAGE,false);
1003		if (selectConversationByUuid(uuid)) {
1004			this.mConversationFragment.reInit(getSelectedConversation());
1005			if (nick != null) {
1006				if (pm) {
1007					Jid jid = getSelectedConversation().getJid();
1008					try {
1009						Jid next = Jid.fromParts(jid.getLocalpart(),jid.getDomainpart(),nick);
1010						this.mConversationFragment.privateMessageWith(next);
1011					} catch (final InvalidJidException ignored) {
1012						//do nothing
1013					}
1014				} else {
1015					this.mConversationFragment.highlightInConference(nick);
1016				}
1017			} else {
1018				this.mConversationFragment.appendText(text);
1019			}
1020			hideConversationsOverview();
1021			openConversation();
1022			if (mContentView instanceof SlidingPaneLayout) {
1023				updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
1024			}
1025			if (downloadUuid != null) {
1026				final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
1027				if (message != null) {
1028					mConversationFragment.messageListAdapter.startDownloadable(message);
1029				}
1030			}
1031		}
1032	}
1033
1034	private boolean selectConversationByUuid(String uuid) {
1035		if (uuid == null) {
1036			return false;
1037		}
1038		for (Conversation aConversationList : conversationList) {
1039			if (aConversationList.getUuid().equals(uuid)) {
1040				setSelectedConversation(aConversationList);
1041				return true;
1042			}
1043		}
1044		return false;
1045	}
1046
1047	@Override
1048	protected void unregisterListeners() {
1049		super.unregisterListeners();
1050		xmppConnectionService.getNotificationService().setOpenConversation(null);
1051	}
1052
1053	@SuppressLint("NewApi")
1054	private static List<Uri> extractUriFromIntent(final Intent intent) {
1055		List<Uri> uris = new ArrayList<>();
1056		Uri uri = intent.getData();
1057		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && uri == null) {
1058			ClipData clipData = intent.getClipData();
1059			for(int i = 0; i < clipData.getItemCount(); ++i) {
1060				uris.add(clipData.getItemAt(i).getUri());
1061			}
1062		} else {
1063			uris.add(uri);
1064		}
1065		return uris;
1066	}
1067
1068	@Override
1069	protected void onActivityResult(int requestCode, int resultCode,
1070			final Intent data) {
1071		super.onActivityResult(requestCode, resultCode, data);
1072		if (resultCode == RESULT_OK) {
1073			if (requestCode == REQUEST_DECRYPT_PGP) {
1074				mConversationFragment.hideSnackbar();
1075				mConversationFragment.updateMessages();
1076			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
1077				mPendingImageUris.clear();
1078				mPendingImageUris.addAll(extractUriFromIntent(data));
1079				if (xmppConnectionServiceBound) {
1080					for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1081						attachImageToConversation(getSelectedConversation(),i.next());
1082					}
1083				}
1084			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
1085				mPendingFileUris.clear();
1086				mPendingFileUris.addAll(extractUriFromIntent(data));
1087				if (xmppConnectionServiceBound) {
1088					for(Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1089						attachFileToConversation(getSelectedConversation(), i.next());
1090					}
1091				}
1092			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
1093				if (mPendingImageUris.size() == 1) {
1094					Uri uri = mPendingImageUris.get(0);
1095					if (xmppConnectionServiceBound) {
1096						attachImageToConversation(getSelectedConversation(), uri);
1097						mPendingImageUris.clear();
1098					}
1099					Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
1100					intent.setData(uri);
1101					sendBroadcast(intent);
1102				} else {
1103					mPendingImageUris.clear();
1104				}
1105			} else if (requestCode == ATTACHMENT_CHOICE_LOCATION) {
1106				double latitude = data.getDoubleExtra("latitude",0);
1107				double longitude = data.getDoubleExtra("longitude",0);
1108				this.mPendingGeoUri = Uri.parse("geo:"+String.valueOf(latitude)+","+String.valueOf(longitude));
1109				if (xmppConnectionServiceBound) {
1110					attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1111					this.mPendingGeoUri = null;
1112				}
1113			} else if (requestCode == REQUEST_TRUST_KEYS_TEXT || requestCode == REQUEST_TRUST_KEYS_MENU) {
1114				this.forbidProcessingPendings = !xmppConnectionServiceBound;
1115				mConversationFragment.onActivityResult(requestCode, resultCode, data);
1116			}
1117		} else {
1118			mPendingImageUris.clear();
1119			mPendingFileUris.clear();
1120		}
1121	}
1122
1123	private void attachLocationToConversation(Conversation conversation, Uri uri) {
1124		if (conversation == null) {
1125			return;
1126		}
1127		xmppConnectionService.attachLocationToConversation(conversation,uri, new UiCallback<Message>() {
1128
1129			@Override
1130			public void success(Message message) {
1131				xmppConnectionService.sendMessage(message);
1132			}
1133
1134			@Override
1135			public void error(int errorCode, Message object) {
1136
1137			}
1138
1139			@Override
1140			public void userInputRequried(PendingIntent pi, Message object) {
1141
1142			}
1143		});
1144	}
1145
1146	private void attachFileToConversation(Conversation conversation, Uri uri) {
1147		if (conversation == null) {
1148			return;
1149		}
1150		prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_file), Toast.LENGTH_LONG);
1151		prepareFileToast.show();
1152		xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
1153			@Override
1154			public void success(Message message) {
1155				hidePrepareFileToast();
1156				xmppConnectionService.sendMessage(message);
1157			}
1158
1159			@Override
1160			public void error(int errorCode, Message message) {
1161				displayErrorDialog(errorCode);
1162			}
1163
1164			@Override
1165			public void userInputRequried(PendingIntent pi, Message message) {
1166
1167			}
1168		});
1169	}
1170
1171	private void attachImageToConversation(Conversation conversation, Uri uri) {
1172		if (conversation == null) {
1173			return;
1174		}
1175		prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_image), Toast.LENGTH_LONG);
1176		prepareFileToast.show();
1177		xmppConnectionService.attachImageToConversation(conversation, uri,
1178				new UiCallback<Message>() {
1179
1180					@Override
1181					public void userInputRequried(PendingIntent pi,
1182							Message object) {
1183						hidePrepareFileToast();
1184					}
1185
1186					@Override
1187					public void success(Message message) {
1188						xmppConnectionService.sendMessage(message);
1189					}
1190
1191					@Override
1192					public void error(int error, Message message) {
1193						hidePrepareFileToast();
1194						displayErrorDialog(error);
1195					}
1196				});
1197	}
1198
1199	private void hidePrepareFileToast() {
1200		if (prepareFileToast != null) {
1201			runOnUiThread(new Runnable() {
1202
1203				@Override
1204				public void run() {
1205					prepareFileToast.cancel();
1206				}
1207			});
1208		}
1209	}
1210
1211	public void updateConversationList() {
1212		xmppConnectionService
1213			.populateWithOrderedConversations(conversationList);
1214		if (swipedConversation != null) {
1215			if (swipedConversation.isRead()) {
1216				conversationList.remove(swipedConversation);
1217			} else {
1218				listView.discardUndo();
1219			}
1220		}
1221		listAdapter.notifyDataSetChanged();
1222	}
1223
1224	public void runIntent(PendingIntent pi, int requestCode) {
1225		try {
1226			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
1227					null, 0, 0, 0);
1228		} catch (final SendIntentException ignored) {
1229		}
1230	}
1231
1232	public void encryptTextMessage(Message message) {
1233		xmppConnectionService.getPgpEngine().encrypt(message,
1234				new UiCallback<Message>() {
1235
1236					@Override
1237					public void userInputRequried(PendingIntent pi,
1238							Message message) {
1239						ConversationActivity.this.runIntent(pi,
1240								ConversationActivity.REQUEST_SEND_MESSAGE);
1241					}
1242
1243					@Override
1244					public void success(Message message) {
1245						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1246						xmppConnectionService.sendMessage(message);
1247					}
1248
1249					@Override
1250					public void error(int error, Message message) {
1251
1252					}
1253				});
1254	}
1255
1256	public boolean forceEncryption() {
1257		return getPreferences().getBoolean("force_encryption", false);
1258	}
1259
1260	public boolean useSendButtonToIndicateStatus() {
1261		return getPreferences().getBoolean("send_button_status", false);
1262	}
1263
1264	public boolean indicateReceived() {
1265		return getPreferences().getBoolean("indicate_received", false);
1266	}
1267
1268	protected boolean trustKeysIfNeeded(int requestCode) {
1269		return trustKeysIfNeeded(requestCode, ATTACHMENT_CHOICE_INVALID);
1270	}
1271
1272	protected boolean trustKeysIfNeeded(int requestCode, int attachmentChoice) {
1273		AxolotlService axolotlService = mSelectedConversation.getAccount().getAxolotlService();
1274		boolean hasPendingKeys = !axolotlService.getKeysWithTrust(Trust.UNDECIDED,
1275				mSelectedConversation.getContact()).isEmpty()
1276				|| !axolotlService.findDevicesWithoutSession(mSelectedConversation).isEmpty();
1277		boolean hasNoTrustedKeys = axolotlService.getNumTrustedKeys(mSelectedConversation.getContact()) == 0;
1278		if( hasPendingKeys || hasNoTrustedKeys) {
1279			axolotlService.createSessionsIfNeeded(mSelectedConversation, false);
1280			Intent intent = new Intent(getApplicationContext(), TrustKeysActivity.class);
1281			intent.putExtra("contact", mSelectedConversation.getContact().getJid().toBareJid().toString());
1282			intent.putExtra("account", mSelectedConversation.getAccount().getJid().toBareJid().toString());
1283			intent.putExtra("choice", attachmentChoice);
1284			intent.putExtra("has_no_trusted", hasNoTrustedKeys);
1285			startActivityForResult(intent, requestCode);
1286			return true;
1287		} else {
1288			return false;
1289		}
1290	}
1291
1292	@Override
1293	protected void refreshUiReal() {
1294		updateConversationList();
1295		if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 0) {
1296			if (!mRedirected) {
1297				this.mRedirected = true;
1298				startActivity(new Intent(this, EditAccountActivity.class));
1299				finish();
1300			}
1301		} else if (conversationList.size() == 0) {
1302			if (!mRedirected) {
1303				this.mRedirected = true;
1304				Intent intent = new Intent(this, StartConversationActivity.class);
1305				intent.putExtra("init",true);
1306				startActivity(intent);
1307				finish();
1308			}
1309		} else {
1310			ConversationActivity.this.mConversationFragment.updateMessages();
1311			updateActionBarTitle();
1312		}
1313		invalidateOptionsMenu();
1314	}
1315
1316	@Override
1317	public void onAccountUpdate() {
1318		this.refreshUi();
1319	}
1320
1321	@Override
1322	public void onConversationUpdate() {
1323		this.refreshUi();
1324	}
1325
1326	@Override
1327	public void onRosterUpdate() {
1328		this.refreshUi();
1329	}
1330
1331	@Override
1332	public void OnUpdateBlocklist(Status status) {
1333		this.refreshUi();
1334	}
1335
1336	public void unblockConversation(final Blockable conversation) {
1337		xmppConnectionService.sendUnblockRequest(conversation);
1338	}
1339
1340	public boolean enterIsSend() {
1341		return getPreferences().getBoolean("enter_is_send",false);
1342	}
1343
1344	@Override
1345	public void onShowErrorToast(final int resId) {
1346		runOnUiThread(new Runnable() {
1347			@Override
1348			public void run() {
1349				Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
1350			}
1351		});
1352	}
1353}