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