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.ActivityNotFoundException;
   9import android.content.ClipData;
  10import android.content.DialogInterface;
  11import android.content.DialogInterface.OnClickListener;
  12import android.content.Intent;
  13import android.content.IntentSender.SendIntentException;
  14import android.content.pm.PackageManager;
  15import android.net.Uri;
  16import android.os.Build;
  17import android.os.Bundle;
  18import android.os.Handler;
  19import android.provider.MediaStore;
  20import android.provider.Settings;
  21import android.support.v4.widget.SlidingPaneLayout;
  22import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
  23import android.util.Log;
  24import android.util.Pair;
  25import android.view.Gravity;
  26import android.view.KeyEvent;
  27import android.view.Menu;
  28import android.view.MenuItem;
  29import android.view.Surface;
  30import android.view.View;
  31import android.widget.AdapterView;
  32import android.widget.AdapterView.OnItemClickListener;
  33import android.widget.ArrayAdapter;
  34import android.widget.CheckBox;
  35import android.widget.PopupMenu;
  36import android.widget.PopupMenu.OnMenuItemClickListener;
  37import android.widget.Toast;
  38
  39import net.java.otr4j.session.SessionStatus;
  40
  41import org.openintents.openpgp.util.OpenPgpApi;
  42
  43import java.util.ArrayList;
  44import java.util.Iterator;
  45import java.util.List;
  46import java.util.concurrent.atomic.AtomicBoolean;
  47
  48import de.timroes.android.listview.EnhancedListView;
  49import eu.siacs.conversations.Config;
  50import eu.siacs.conversations.R;
  51import eu.siacs.conversations.crypto.axolotl.AxolotlService;
  52import eu.siacs.conversations.crypto.axolotl.FingerprintStatus;
  53import eu.siacs.conversations.entities.Account;
  54import eu.siacs.conversations.entities.Blockable;
  55import eu.siacs.conversations.entities.Contact;
  56import eu.siacs.conversations.entities.Conversation;
  57import eu.siacs.conversations.entities.Message;
  58import eu.siacs.conversations.entities.Transferable;
  59import eu.siacs.conversations.persistance.FileBackend;
  60import eu.siacs.conversations.services.XmppConnectionService;
  61import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
  62import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
  63import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
  64import eu.siacs.conversations.ui.adapter.ConversationAdapter;
  65import eu.siacs.conversations.ui.service.EmojiService;
  66import eu.siacs.conversations.utils.ExceptionHelper;
  67import eu.siacs.conversations.utils.UIHelper;
  68import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
  69import eu.siacs.conversations.xmpp.XmppConnection;
  70import eu.siacs.conversations.xmpp.jid.InvalidJidException;
  71import eu.siacs.conversations.xmpp.jid.Jid;
  72
  73public class ConversationActivity extends XmppActivity
  74	implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
  75
  76	public static final String RECENTLY_USED_QUICK_ACTION = "recently_used_quick_action";
  77
  78	public static final String ACTION_VIEW_CONVERSATION = "eu.siacs.conversations.action.VIEW";
  79	public static final String CONVERSATION = "conversationUuid";
  80	public static final String EXTRA_DOWNLOAD_UUID = "eu.siacs.conversations.download_uuid";
  81	public static final String TEXT = "text";
  82	public static final String NICK = "nick";
  83	public static final String PRIVATE_MESSAGE = "pm";
  84
  85	public static final int REQUEST_SEND_MESSAGE = 0x0201;
  86	public static final int REQUEST_DECRYPT_PGP = 0x0202;
  87	public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
  88	public static final int REQUEST_TRUST_KEYS_TEXT = 0x0208;
  89	public static final int REQUEST_TRUST_KEYS_MENU = 0x0209;
  90	public static final int REQUEST_START_DOWNLOAD = 0x0210;
  91	public static final int REQUEST_ADD_EDITOR_CONTENT = 0x0211;
  92	public static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
  93	public static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
  94	public static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
  95	public static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
  96	public static final int ATTACHMENT_CHOICE_LOCATION = 0x0305;
  97	public static final int ATTACHMENT_CHOICE_INVALID = 0x0306;
  98	public static final int ATTACHMENT_CHOICE_RECORD_VIDEO = 0x0307;
  99	private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
 100	private static final String STATE_PANEL_OPEN = "state_panel_open";
 101	private static final String STATE_PENDING_URI = "state_pending_uri";
 102	private static final String STATE_FIRST_VISIBLE = "first_visible";
 103	private static final String STATE_OFFSET_FROM_TOP = "offset_from_top";
 104
 105	private String mOpenConversation = null;
 106	private boolean mPanelOpen = true;
 107	private AtomicBoolean mShouldPanelBeOpen = new AtomicBoolean(false);
 108	private Pair<Integer,Integer> mScrollPosition = null;
 109	final private List<Uri> mPendingImageUris = new ArrayList<>();
 110	final private List<Uri> mPendingFileUris = new ArrayList<>();
 111	private Uri mPendingGeoUri = null;
 112	private boolean forbidProcessingPendings = false;
 113	private Message mPendingDownloadableMessage = null;
 114
 115	private boolean conversationWasSelectedByKeyboard = false;
 116
 117	private View mContentView;
 118
 119	private List<Conversation> conversationList = new ArrayList<>();
 120	private Conversation swipedConversation = null;
 121	private Conversation mSelectedConversation = null;
 122	private EnhancedListView listView;
 123	private ConversationFragment mConversationFragment;
 124
 125	private ArrayAdapter<Conversation> listAdapter;
 126
 127	private boolean mActivityPaused = false;
 128	private AtomicBoolean mRedirected = new AtomicBoolean(false);
 129	private Pair<Integer, Intent> mPostponedActivityResult;
 130	private boolean mUnprocessedNewIntent = false;
 131	public Uri mPendingEditorContent = null;
 132
 133	public Conversation getSelectedConversation() {
 134		return this.mSelectedConversation;
 135	}
 136
 137	public void setSelectedConversation(Conversation conversation) {
 138		this.mSelectedConversation = conversation;
 139	}
 140
 141	public void showConversationsOverview() {
 142		if (mContentView instanceof SlidingPaneLayout) {
 143			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 144			mShouldPanelBeOpen.set(true);
 145			mSlidingPaneLayout.openPane();
 146		}
 147	}
 148
 149	@Override
 150	protected String getShareableUri() {
 151		Conversation conversation = getSelectedConversation();
 152		if (conversation != null) {
 153			return conversation.getAccount().getShareableUri();
 154		} else {
 155			return "";
 156		}
 157	}
 158
 159	public void hideConversationsOverview() {
 160		if (mContentView instanceof SlidingPaneLayout) {
 161			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 162			mShouldPanelBeOpen.set(false);
 163			mSlidingPaneLayout.closePane();
 164		}
 165	}
 166
 167	public boolean isConversationsOverviewHideable() {
 168		return mContentView instanceof SlidingPaneLayout;
 169	}
 170
 171	public boolean isConversationsOverviewVisable() {
 172		if (mContentView instanceof SlidingPaneLayout) {
 173			return mShouldPanelBeOpen.get();
 174		} else {
 175			return true;
 176		}
 177	}
 178
 179	@Override
 180	protected void onCreate(final Bundle savedInstanceState) {
 181		super.onCreate(savedInstanceState);
 182		new EmojiService(this).init();
 183		if (savedInstanceState != null) {
 184			mOpenConversation = savedInstanceState.getString(STATE_OPEN_CONVERSATION, null);
 185			mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
 186			int pos = savedInstanceState.getInt(STATE_FIRST_VISIBLE, -1);
 187			int offset = savedInstanceState.getInt(STATE_OFFSET_FROM_TOP, 1);
 188			if (pos >= 0 && offset <= 0) {
 189				Log.d(Config.LOGTAG,"retrieved scroll position from instanceState "+pos+":"+offset);
 190				mScrollPosition = new Pair<>(pos,offset);
 191			} else {
 192				mScrollPosition = null;
 193			}
 194			String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
 195			if (pending != null) {
 196				Log.d(Config.LOGTAG,"ConversationsActivity.onCreate() - restoring pending image uri");
 197				mPendingImageUris.clear();
 198				mPendingImageUris.add(Uri.parse(pending));
 199			}
 200		}
 201
 202		setContentView(R.layout.fragment_conversations_overview);
 203
 204		this.mConversationFragment = new ConversationFragment();
 205		FragmentTransaction transaction = getFragmentManager().beginTransaction();
 206		transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
 207		transaction.commit();
 208
 209		this.listView = findViewById(R.id.list);
 210		this.listAdapter = new ConversationAdapter(this, conversationList);
 211		this.listView.setAdapter(this.listAdapter);
 212		this.listView.setSwipeDirection(EnhancedListView.SwipeDirection.END);
 213
 214		final ActionBar actionBar = getActionBar();
 215		if (actionBar != null) {
 216			actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
 217		}
 218
 219		listView.setOnItemClickListener(new OnItemClickListener() {
 220
 221			@Override
 222			public void onItemClick(AdapterView<?> arg0, View clickedView,
 223									int position, long arg3) {
 224				if (getSelectedConversation() != conversationList.get(position)) {
 225					setSelectedConversation(conversationList.get(position));
 226					ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 227					conversationWasSelectedByKeyboard = false;
 228				}
 229				hideConversationsOverview();
 230				openConversation();
 231			}
 232		});
 233
 234		listView.setDismissCallback(new EnhancedListView.OnDismissCallback() {
 235
 236			@Override
 237			public EnhancedListView.Undoable onDismiss(final EnhancedListView enhancedListView, final int position) {
 238
 239				final int index = listView.getFirstVisiblePosition();
 240				View v = listView.getChildAt(0);
 241				final int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
 242
 243				try {
 244					swipedConversation = listAdapter.getItem(position);
 245				} catch (IndexOutOfBoundsException e) {
 246					return null;
 247				}
 248				listAdapter.remove(swipedConversation);
 249				xmppConnectionService.markRead(swipedConversation);
 250
 251				final boolean formerlySelected = (getSelectedConversation() == swipedConversation);
 252				if (position == 0 && listAdapter.getCount() == 0) {
 253					endConversation(swipedConversation, false, true);
 254					return null;
 255				} else if (formerlySelected) {
 256					setSelectedConversation(listAdapter.getItem(0));
 257					ConversationActivity.this.mConversationFragment
 258							.reInit(getSelectedConversation());
 259				}
 260
 261				return new EnhancedListView.Undoable() {
 262
 263					@Override
 264					public void undo() {
 265						listAdapter.insert(swipedConversation, position);
 266						if (formerlySelected) {
 267							setSelectedConversation(swipedConversation);
 268							ConversationActivity.this.mConversationFragment
 269									.reInit(getSelectedConversation());
 270						}
 271						swipedConversation = null;
 272						listView.setSelectionFromTop(index + (listView.getChildCount() < position ? 1 : 0), top);
 273					}
 274
 275					@Override
 276					public void discard() {
 277						if (!swipedConversation.isRead()
 278								&& swipedConversation.getMode() == Conversation.MODE_SINGLE) {
 279							swipedConversation = null;
 280							return;
 281						}
 282						endConversation(swipedConversation, false, false);
 283						swipedConversation = null;
 284					}
 285
 286					@Override
 287					public String getTitle() {
 288						if (swipedConversation.getMode() == Conversation.MODE_MULTI) {
 289							return getResources().getString(R.string.title_undo_swipe_out_muc);
 290						} else {
 291							return getResources().getString(R.string.title_undo_swipe_out_conversation);
 292						}
 293					}
 294				};
 295			}
 296		});
 297		listView.enableSwipeToDismiss();
 298		listView.setSwipingLayout(R.id.swipeable_item);
 299		listView.setUndoStyle(EnhancedListView.UndoStyle.SINGLE_POPUP);
 300		listView.setUndoHideDelay(5000);
 301		listView.setRequireTouchBeforeDismiss(false);
 302
 303		mContentView = findViewById(R.id.content_view_spl);
 304		if (mContentView == null) {
 305			mContentView = findViewById(R.id.content_view_ll);
 306		}
 307		if (mContentView instanceof SlidingPaneLayout) {
 308			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 309			mSlidingPaneLayout.setShadowResource(R.drawable.es_slidingpane_shadow);
 310			mSlidingPaneLayout.setSliderFadeColor(0);
 311			mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
 312
 313				@Override
 314				public void onPanelOpened(View arg0) {
 315					mShouldPanelBeOpen.set(true);
 316					updateActionBarTitle();
 317					invalidateOptionsMenu();
 318					hideKeyboard();
 319					if (xmppConnectionServiceBound) {
 320						xmppConnectionService.getNotificationService().setOpenConversation(null);
 321					}
 322					closeContextMenu();
 323				}
 324
 325				@Override
 326				public void onPanelClosed(View arg0) {
 327					mShouldPanelBeOpen.set(false);
 328					listView.discardUndo();
 329					openConversation();
 330				}
 331
 332				@Override
 333				public void onPanelSlide(View arg0, float arg1) {
 334					// TODO Auto-generated method stub
 335
 336				}
 337			});
 338		}
 339	}
 340
 341	@Override
 342	public void switchToConversation(Conversation conversation) {
 343		setSelectedConversation(conversation);
 344		runOnUiThread(new Runnable() {
 345			@Override
 346			public void run() {
 347				ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 348				openConversation();
 349			}
 350		});
 351	}
 352
 353	private void updateActionBarTitle() {
 354		updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
 355	}
 356
 357	private void updateActionBarTitle(boolean titleShouldBeName) {
 358		final ActionBar ab = getActionBar();
 359		final Conversation conversation = getSelectedConversation();
 360		if (ab != null) {
 361			if (titleShouldBeName && conversation != null) {
 362				if ((ab.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != ActionBar.DISPLAY_HOME_AS_UP) {
 363					ab.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
 364				}
 365				if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
 366					ab.setTitle(conversation.getName());
 367				} else {
 368					ab.setTitle(conversation.getJid().toBareJid().toString());
 369				}
 370			} else {
 371				if ((ab.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) == ActionBar.DISPLAY_HOME_AS_UP) {
 372					ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
 373				}
 374				ab.setTitle(R.string.app_name);
 375			}
 376		}
 377	}
 378
 379	private void openConversation() {
 380		this.updateActionBarTitle();
 381		this.invalidateOptionsMenu();
 382		if (xmppConnectionServiceBound) {
 383			final Conversation conversation = getSelectedConversation();
 384			xmppConnectionService.getNotificationService().setOpenConversation(conversation);
 385			sendReadMarkerIfNecessary(conversation);
 386		}
 387		listAdapter.notifyDataSetChanged();
 388	}
 389
 390	public void sendReadMarkerIfNecessary(final Conversation conversation) {
 391		if (!mActivityPaused && !mUnprocessedNewIntent && conversation != null) {
 392			xmppConnectionService.sendReadMarker(conversation);
 393		}
 394	}
 395
 396	@Override
 397	public boolean onCreateOptionsMenu(Menu menu) {
 398		getMenuInflater().inflate(R.menu.conversations, menu);
 399		final MenuItem menuSecure = menu.findItem(R.id.action_security);
 400		final MenuItem menuArchive = menu.findItem(R.id.action_archive);
 401		final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
 402		final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
 403		final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
 404		final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
 405		final MenuItem menuAdd = menu.findItem(R.id.action_add);
 406		final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
 407		final MenuItem menuMute = menu.findItem(R.id.action_mute);
 408		final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
 409
 410		if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
 411			menuArchive.setVisible(false);
 412			menuMucDetails.setVisible(false);
 413			menuContactDetails.setVisible(false);
 414			menuSecure.setVisible(false);
 415			menuInviteContact.setVisible(false);
 416			menuAttach.setVisible(false);
 417			menuClearHistory.setVisible(false);
 418			menuMute.setVisible(false);
 419			menuUnmute.setVisible(false);
 420		} else {
 421			menuAdd.setVisible(!isConversationsOverviewHideable());
 422			if (this.getSelectedConversation() != null) {
 423				if (this.getSelectedConversation().getNextEncryption() != Message.ENCRYPTION_NONE) {
 424					if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
 425						menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
 426					} else {
 427						menuSecure.setIcon(R.drawable.ic_action_secure);
 428					}
 429				}
 430				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
 431					menuContactDetails.setVisible(false);
 432					menuAttach.setVisible(getSelectedConversation().getAccount().httpUploadAvailable() && getSelectedConversation().getMucOptions().participating());
 433					menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
 434					menuSecure.setVisible((Config.supportOpenPgp() || Config.supportOmemo()) && Config.multipleEncryptionChoices()); //only if pgp is supported we have a choice
 435				} else {
 436					menuContactDetails.setVisible(!this.getSelectedConversation().withSelf());
 437					menuMucDetails.setVisible(false);
 438					menuSecure.setVisible(Config.multipleEncryptionChoices());
 439					menuInviteContact.setVisible(xmppConnectionService != null && xmppConnectionService.findConferenceServer(getSelectedConversation().getAccount()) != null);
 440				}
 441				if (this.getSelectedConversation().isMuted()) {
 442					menuMute.setVisible(false);
 443				} else {
 444					menuUnmute.setVisible(false);
 445				}
 446			}
 447		}
 448		if (Config.supportOmemo()) {
 449			new Handler().post(addOmemoDebuggerRunnable);
 450		}
 451		return super.onCreateOptionsMenu(menu);
 452	}
 453
 454	private Runnable addOmemoDebuggerRunnable = new Runnable() {
 455		@Override
 456		public void run() {
 457			View view = findViewById(R.id.action_security);
 458			if (view != null) {
 459				view.setOnLongClickListener(new View.OnLongClickListener() {
 460					@Override
 461					public boolean onLongClick(View v) {
 462						return v.getId() == R.id.action_security && quickOmemoDebugger(getSelectedConversation());
 463					}
 464				});
 465			}
 466		}
 467	};
 468
 469	private boolean quickOmemoDebugger(Conversation c) {
 470		if (c != null) {
 471			boolean single = c.getMode() == Conversation.MODE_SINGLE;
 472			AxolotlService axolotlService = c.getAccount().getAxolotlService();
 473			Pair<AxolotlService.AxolotlCapability,Jid> capabilityJidPair = axolotlService.isConversationAxolotlCapableDetailed(c);
 474			switch (capabilityJidPair.first) {
 475				case MISSING_PRESENCE:
 476					Toast.makeText(ConversationActivity.this,single ? getString(R.string.missing_presence_subscription) : getString(R.string.missing_presence_subscription_with_x,capabilityJidPair.second.toBareJid().toString()),Toast.LENGTH_SHORT).show();
 477					return true;
 478				case MISSING_KEYS:
 479					Toast.makeText(ConversationActivity.this,single ? getString(R.string.missing_omemo_keys) : getString(R.string.missing_keys_from_x,capabilityJidPair.second.toBareJid().toString()),Toast.LENGTH_SHORT).show();
 480					return true;
 481				case WRONG_CONFIGURATION:
 482					Toast.makeText(ConversationActivity.this,R.string.wrong_conference_configuration, Toast.LENGTH_SHORT).show();
 483					return true;
 484				case NO_MEMBERS:
 485					Toast.makeText(ConversationActivity.this,R.string.this_conference_has_no_members, Toast.LENGTH_SHORT).show();
 486					return true;
 487			}
 488		}
 489		return false;
 490	}
 491
 492	protected void selectPresenceToAttachFile(final int attachmentChoice, final int encryption) {
 493		final Conversation conversation = getSelectedConversation();
 494		final Account account = conversation.getAccount();
 495		final OnPresenceSelected callback = new OnPresenceSelected() {
 496
 497			@Override
 498			public void onPresenceSelected() {
 499				Intent intent = new Intent();
 500				boolean chooser = false;
 501				String fallbackPackageId = null;
 502				switch (attachmentChoice) {
 503					case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
 504						intent.setAction(Intent.ACTION_GET_CONTENT);
 505						if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
 506							intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
 507						}
 508						intent.setType("image/*");
 509						chooser = true;
 510						break;
 511					case ATTACHMENT_CHOICE_RECORD_VIDEO:
 512						intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
 513						break;
 514					case ATTACHMENT_CHOICE_TAKE_PHOTO:
 515						Uri uri = xmppConnectionService.getFileBackend().getTakePhotoUri();
 516						mPendingImageUris.clear();
 517						mPendingImageUris.add(uri);
 518						intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
 519						intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
 520						intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
 521						intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
 522						break;
 523					case ATTACHMENT_CHOICE_CHOOSE_FILE:
 524						chooser = true;
 525						intent.setType("*/*");
 526						intent.addCategory(Intent.CATEGORY_OPENABLE);
 527						intent.setAction(Intent.ACTION_GET_CONTENT);
 528						break;
 529					case ATTACHMENT_CHOICE_RECORD_VOICE:
 530						intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
 531						fallbackPackageId = "eu.siacs.conversations.voicerecorder";
 532						break;
 533					case ATTACHMENT_CHOICE_LOCATION:
 534						intent.setAction("eu.siacs.conversations.location.request");
 535						fallbackPackageId = "eu.siacs.conversations.sharelocation";
 536						break;
 537				}
 538				if (intent.resolveActivity(getPackageManager()) != null) {
 539					if (chooser) {
 540						startActivityForResult(
 541								Intent.createChooser(intent, getString(R.string.perform_action_with)),
 542								attachmentChoice);
 543					} else {
 544						startActivityForResult(intent, attachmentChoice);
 545					}
 546				} else if (fallbackPackageId != null) {
 547					startActivity(getInstallApkIntent(fallbackPackageId));
 548				}
 549			}
 550		};
 551		if ((account.httpUploadAvailable() || attachmentChoice == ATTACHMENT_CHOICE_LOCATION) && encryption != Message.ENCRYPTION_OTR) {
 552			conversation.setNextCounterpart(null);
 553			callback.onPresenceSelected();
 554		} else {
 555			selectPresence(conversation, callback);
 556		}
 557	}
 558
 559	private Intent getInstallApkIntent(final String packageId) {
 560		Intent intent = new Intent(Intent.ACTION_VIEW);
 561		intent.setData(Uri.parse("market://details?id=" + packageId));
 562		if (intent.resolveActivity(getPackageManager()) != null) {
 563			return intent;
 564		} else {
 565			intent.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + packageId));
 566			return intent;
 567		}
 568	}
 569
 570	public void attachFile(final int attachmentChoice) {
 571		if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) {
 572			if (!Config.ONLY_INTERNAL_STORAGE && !hasStoragePermission(attachmentChoice)) {
 573				return;
 574			}
 575		}
 576		final ConversationFragment.SendButtonAction action;
 577		switch (attachmentChoice) {
 578			case ATTACHMENT_CHOICE_LOCATION:
 579				action = ConversationFragment.SendButtonAction.SEND_LOCATION;
 580				break;
 581			case ATTACHMENT_CHOICE_RECORD_VOICE:
 582				action = ConversationFragment.SendButtonAction.RECORD_VOICE;
 583				break;
 584			case ATTACHMENT_CHOICE_RECORD_VIDEO:
 585				action = ConversationFragment.SendButtonAction.RECORD_VIDEO;
 586				break;
 587			case ATTACHMENT_CHOICE_TAKE_PHOTO:
 588				action = ConversationFragment.SendButtonAction.TAKE_PHOTO;
 589				break;
 590			case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
 591				action = ConversationFragment.SendButtonAction.CHOOSE_PICTURE;
 592				break;
 593			default:
 594				action = null;
 595				break;
 596		}
 597		if (action != null) {
 598			getPreferences().edit().putString(RECENTLY_USED_QUICK_ACTION,action.toString()).apply();
 599		}
 600		final Conversation conversation = getSelectedConversation();
 601		final int encryption = conversation.getNextEncryption();
 602		final int mode = conversation.getMode();
 603		if (encryption == Message.ENCRYPTION_PGP) {
 604			if (hasPgp()) {
 605				if (mode == Conversation.MODE_SINGLE && conversation.getContact().getPgpKeyId() != 0) {
 606					xmppConnectionService.getPgpEngine().hasKey(
 607							conversation.getContact(),
 608							new UiCallback<Contact>() {
 609
 610								@Override
 611								public void userInputRequried(PendingIntent pi, Contact contact) {
 612									ConversationActivity.this.runIntent(pi, attachmentChoice);
 613								}
 614
 615								@Override
 616								public void success(Contact contact) {
 617									selectPresenceToAttachFile(attachmentChoice, encryption);
 618								}
 619
 620								@Override
 621								public void error(int error, Contact contact) {
 622									replaceToast(getString(error));
 623								}
 624							});
 625				} else if (mode == Conversation.MODE_MULTI && conversation.getMucOptions().pgpKeysInUse()) {
 626					if (!conversation.getMucOptions().everybodyHasKeys()) {
 627						Toast warning = Toast
 628								.makeText(this,
 629										R.string.missing_public_keys,
 630										Toast.LENGTH_LONG);
 631						warning.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
 632						warning.show();
 633					}
 634					selectPresenceToAttachFile(attachmentChoice, encryption);
 635				} else {
 636					final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 637							.findFragmentByTag("conversation");
 638					if (fragment != null) {
 639						fragment.showNoPGPKeyDialog(false,
 640								new OnClickListener() {
 641
 642									@Override
 643									public void onClick(DialogInterface dialog,
 644														int which) {
 645										conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 646										xmppConnectionService.updateConversation(conversation);
 647										selectPresenceToAttachFile(attachmentChoice, Message.ENCRYPTION_NONE);
 648									}
 649								});
 650					}
 651				}
 652			} else {
 653				showInstallPgpDialog();
 654			}
 655		} else {
 656			if (encryption != Message.ENCRYPTION_AXOLOTL || !trustKeysIfNeeded(REQUEST_TRUST_KEYS_MENU, attachmentChoice)) {
 657				selectPresenceToAttachFile(attachmentChoice, encryption);
 658			}
 659		}
 660	}
 661
 662	@Override
 663	public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
 664		if (grantResults.length > 0)
 665			if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
 666				if (requestCode == REQUEST_START_DOWNLOAD) {
 667					if (this.mPendingDownloadableMessage != null) {
 668						startDownloadable(this.mPendingDownloadableMessage);
 669					}
 670				} else if (requestCode == REQUEST_ADD_EDITOR_CONTENT) {
 671					if (this.mPendingEditorContent != null) {
 672						attachImageToConversation(this.mPendingEditorContent);
 673					}
 674				} else {
 675					attachFile(requestCode);
 676				}
 677			} else {
 678				Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
 679			}
 680	}
 681
 682	public void startDownloadable(Message message) {
 683		if (!Config.ONLY_INTERNAL_STORAGE && !hasStoragePermission(ConversationActivity.REQUEST_START_DOWNLOAD)) {
 684			this.mPendingDownloadableMessage = message;
 685			return;
 686		}
 687		Transferable transferable = message.getTransferable();
 688		if (transferable != null) {
 689			if (!transferable.start()) {
 690				Toast.makeText(this, R.string.not_connected_try_again, Toast.LENGTH_SHORT).show();
 691			}
 692		} else if (message.treatAsDownloadable()) {
 693			xmppConnectionService.getHttpConnectionManager().createNewDownloadConnection(message, true);
 694		}
 695	}
 696
 697	@Override
 698	public boolean onOptionsItemSelected(final MenuItem item) {
 699		if (item.getItemId() == android.R.id.home) {
 700			showConversationsOverview();
 701			return true;
 702		} else if (item.getItemId() == R.id.action_add) {
 703			startActivity(new Intent(this, StartConversationActivity.class));
 704			return true;
 705		} else if (getSelectedConversation() != null) {
 706			switch (item.getItemId()) {
 707				case R.id.action_attach_file:
 708					attachFileDialog();
 709					break;
 710				case R.id.action_archive:
 711					this.endConversation(getSelectedConversation());
 712					break;
 713				case R.id.action_contact_details:
 714					switchToContactDetails(getSelectedConversation().getContact());
 715					break;
 716				case R.id.action_muc_details:
 717					Intent intent = new Intent(this,
 718							ConferenceDetailsActivity.class);
 719					intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
 720					intent.putExtra("uuid", getSelectedConversation().getUuid());
 721					startActivity(intent);
 722					break;
 723				case R.id.action_invite:
 724					inviteToConversation(getSelectedConversation());
 725					break;
 726				case R.id.action_security:
 727					selectEncryptionDialog(getSelectedConversation());
 728					break;
 729				case R.id.action_clear_history:
 730					clearHistoryDialog(getSelectedConversation());
 731					break;
 732				case R.id.action_mute:
 733					muteConversationDialog(getSelectedConversation());
 734					break;
 735				case R.id.action_unmute:
 736					unmuteConversation(getSelectedConversation());
 737					break;
 738				case R.id.action_block:
 739					BlockContactDialog.show(this, getSelectedConversation());
 740					break;
 741				case R.id.action_unblock:
 742					BlockContactDialog.show(this, getSelectedConversation());
 743					break;
 744				default:
 745					break;
 746			}
 747			return super.onOptionsItemSelected(item);
 748		} else {
 749			return super.onOptionsItemSelected(item);
 750		}
 751	}
 752
 753	public void endConversation(Conversation conversation) {
 754		endConversation(conversation, true, true);
 755	}
 756
 757	public void endConversation(Conversation conversation, boolean showOverview, boolean reinit) {
 758		if (showOverview) {
 759			showConversationsOverview();
 760		}
 761		xmppConnectionService.archiveConversation(conversation);
 762		if (reinit) {
 763			if (conversationList.size() > 0) {
 764				setSelectedConversation(conversationList.get(0));
 765				this.mConversationFragment.reInit(getSelectedConversation());
 766			} else {
 767				setSelectedConversation(null);
 768				if (mRedirected.compareAndSet(false, true)) {
 769					Intent intent = new Intent(this, StartConversationActivity.class);
 770					intent.putExtra("init", true);
 771					startActivity(intent);
 772					finish();
 773				}
 774			}
 775		}
 776	}
 777
 778	@SuppressLint("InflateParams")
 779	protected void clearHistoryDialog(final Conversation conversation) {
 780		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 781		builder.setTitle(getString(R.string.clear_conversation_history));
 782		View dialogView = getLayoutInflater().inflate(
 783				R.layout.dialog_clear_history, null);
 784		final CheckBox endConversationCheckBox = (CheckBox) dialogView
 785				.findViewById(R.id.end_conversation_checkbox);
 786		builder.setView(dialogView);
 787		builder.setNegativeButton(getString(R.string.cancel), null);
 788		builder.setPositiveButton(getString(R.string.delete_messages),
 789				new OnClickListener() {
 790
 791					@Override
 792					public void onClick(DialogInterface dialog, int which) {
 793						ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
 794						if (endConversationCheckBox.isChecked()) {
 795							endConversation(conversation);
 796						} else {
 797							updateConversationList();
 798							ConversationActivity.this.mConversationFragment.updateMessages();
 799						}
 800					}
 801				});
 802		builder.create().show();
 803	}
 804
 805	protected void attachFileDialog() {
 806		View menuAttachFile = findViewById(R.id.action_attach_file);
 807		if (menuAttachFile == null) {
 808			return;
 809		}
 810		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
 811		attachFilePopup.inflate(R.menu.attachment_choices);
 812		if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
 813			attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
 814		}
 815		if (new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null) {
 816			attachFilePopup.getMenu().findItem(R.id.attach_location).setVisible(false);
 817		}
 818		attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 819
 820			@Override
 821			public boolean onMenuItemClick(MenuItem item) {
 822				switch (item.getItemId()) {
 823					case R.id.attach_choose_picture:
 824						attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
 825						break;
 826					case R.id.attach_take_picture:
 827						attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
 828						break;
 829					case R.id.attach_record_video:
 830						attachFile(ATTACHMENT_CHOICE_RECORD_VIDEO);
 831						break;
 832					case R.id.attach_choose_file:
 833						attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
 834						break;
 835					case R.id.attach_record_voice:
 836						attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
 837						break;
 838					case R.id.attach_location:
 839						attachFile(ATTACHMENT_CHOICE_LOCATION);
 840						break;
 841				}
 842				return false;
 843			}
 844		});
 845		UIHelper.showIconsInPopup(attachFilePopup);
 846		attachFilePopup.show();
 847	}
 848
 849	public void verifyOtrSessionDialog(final Conversation conversation, View view) {
 850		if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
 851			Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
 852			return;
 853		}
 854		if (view == null) {
 855			return;
 856		}
 857		PopupMenu popup = new PopupMenu(this, view);
 858		popup.inflate(R.menu.verification_choices);
 859		popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 860			@Override
 861			public boolean onMenuItemClick(MenuItem menuItem) {
 862				Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
 863				intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
 864				intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
 865				intent.putExtra(EXTRA_ACCOUNT, conversation.getAccount().getJid().toBareJid().toString());
 866				switch (menuItem.getItemId()) {
 867					case R.id.scan_fingerprint:
 868						intent.putExtra("mode", VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
 869						break;
 870					case R.id.ask_question:
 871						intent.putExtra("mode", VerifyOTRActivity.MODE_ASK_QUESTION);
 872						break;
 873					case R.id.manual_verification:
 874						intent.putExtra("mode", VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
 875						break;
 876				}
 877				startActivity(intent);
 878				return true;
 879			}
 880		});
 881		popup.show();
 882	}
 883
 884	protected void selectEncryptionDialog(final Conversation conversation) {
 885		View menuItemView = findViewById(R.id.action_security);
 886		if (menuItemView == null) {
 887			return;
 888		}
 889		PopupMenu popup = new PopupMenu(this, menuItemView);
 890		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 891				.findFragmentByTag("conversation");
 892		if (fragment != null) {
 893			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 894
 895				@Override
 896				public boolean onMenuItemClick(MenuItem item) {
 897					switch (item.getItemId()) {
 898						case R.id.encryption_choice_none:
 899							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 900							item.setChecked(true);
 901							break;
 902						case R.id.encryption_choice_otr:
 903							conversation.setNextEncryption(Message.ENCRYPTION_OTR);
 904							item.setChecked(true);
 905							break;
 906						case R.id.encryption_choice_pgp:
 907							if (hasPgp()) {
 908								if (conversation.getAccount().getPgpSignature() != null) {
 909									conversation.setNextEncryption(Message.ENCRYPTION_PGP);
 910									item.setChecked(true);
 911								} else {
 912									announcePgp(conversation.getAccount(), conversation, onOpenPGPKeyPublished);
 913								}
 914							} else {
 915								showInstallPgpDialog();
 916							}
 917							break;
 918						case R.id.encryption_choice_axolotl:
 919							Log.d(Config.LOGTAG, AxolotlService.getLogprefix(conversation.getAccount())
 920									+ "Enabled axolotl for Contact " + conversation.getContact().getJid());
 921							conversation.setNextEncryption(Message.ENCRYPTION_AXOLOTL);
 922							item.setChecked(true);
 923							break;
 924						default:
 925							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 926							break;
 927					}
 928					xmppConnectionService.updateConversation(conversation);
 929					fragment.updateChatMsgHint();
 930					invalidateOptionsMenu();
 931					refreshUi();
 932					return true;
 933				}
 934			});
 935			popup.inflate(R.menu.encryption_choices);
 936			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
 937			MenuItem none = popup.getMenu().findItem(R.id.encryption_choice_none);
 938			MenuItem pgp = popup.getMenu().findItem(R.id.encryption_choice_pgp);
 939			MenuItem axolotl = popup.getMenu().findItem(R.id.encryption_choice_axolotl);
 940			pgp.setVisible(Config.supportOpenPgp());
 941			none.setVisible(Config.supportUnencrypted() || conversation.getMode() == Conversation.MODE_MULTI);
 942			otr.setVisible(Config.supportOtr());
 943			axolotl.setVisible(Config.supportOmemo());
 944			if (conversation.getMode() == Conversation.MODE_MULTI) {
 945				otr.setVisible(false);
 946			}
 947			if (!conversation.getAccount().getAxolotlService().isConversationAxolotlCapable(conversation)) {
 948				axolotl.setEnabled(false);
 949			}
 950			switch (conversation.getNextEncryption()) {
 951				case Message.ENCRYPTION_NONE:
 952					none.setChecked(true);
 953					break;
 954				case Message.ENCRYPTION_OTR:
 955					otr.setChecked(true);
 956					break;
 957				case Message.ENCRYPTION_PGP:
 958					pgp.setChecked(true);
 959					break;
 960				case Message.ENCRYPTION_AXOLOTL:
 961					axolotl.setChecked(true);
 962					break;
 963				default:
 964					none.setChecked(true);
 965					break;
 966			}
 967			popup.show();
 968		}
 969	}
 970
 971	protected void muteConversationDialog(final Conversation conversation) {
 972		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 973		builder.setTitle(R.string.disable_notifications);
 974		final int[] durations = getResources().getIntArray(R.array.mute_options_durations);
 975		builder.setItems(R.array.mute_options_descriptions,
 976				new OnClickListener() {
 977
 978					@Override
 979					public void onClick(final DialogInterface dialog, final int which) {
 980						final long till;
 981						if (durations[which] == -1) {
 982							till = Long.MAX_VALUE;
 983						} else {
 984							till = System.currentTimeMillis() + (durations[which] * 1000);
 985						}
 986						conversation.setMutedTill(till);
 987						ConversationActivity.this.xmppConnectionService.updateConversation(conversation);
 988						updateConversationList();
 989						ConversationActivity.this.mConversationFragment.updateMessages();
 990						invalidateOptionsMenu();
 991					}
 992				});
 993		builder.create().show();
 994	}
 995
 996	public void unmuteConversation(final Conversation conversation) {
 997		conversation.setMutedTill(0);
 998		this.xmppConnectionService.updateConversation(conversation);
 999		updateConversationList();
1000		ConversationActivity.this.mConversationFragment.updateMessages();
1001		invalidateOptionsMenu();
1002	}
1003
1004	@Override
1005	public void onBackPressed() {
1006		if (!isConversationsOverviewVisable()) {
1007			showConversationsOverview();
1008		} else {
1009			super.onBackPressed();
1010		}
1011	}
1012
1013	@Override
1014	public boolean onKeyUp(int key, KeyEvent event) {
1015		int rotation = getWindowManager().getDefaultDisplay().getRotation();
1016		final int upKey;
1017		final int downKey;
1018		switch (rotation) {
1019			case Surface.ROTATION_90:
1020				upKey = KeyEvent.KEYCODE_DPAD_LEFT;
1021				downKey = KeyEvent.KEYCODE_DPAD_RIGHT;
1022				break;
1023			case Surface.ROTATION_180:
1024				upKey = KeyEvent.KEYCODE_DPAD_DOWN;
1025				downKey = KeyEvent.KEYCODE_DPAD_UP;
1026				break;
1027			case Surface.ROTATION_270:
1028				upKey = KeyEvent.KEYCODE_DPAD_RIGHT;
1029				downKey = KeyEvent.KEYCODE_DPAD_LEFT;
1030				break;
1031			case Surface.ROTATION_0:
1032			default:
1033				upKey = KeyEvent.KEYCODE_DPAD_UP;
1034				downKey = KeyEvent.KEYCODE_DPAD_DOWN;
1035		}
1036		final boolean modifier = event.isCtrlPressed() || (event.getMetaState() & KeyEvent.META_ALT_LEFT_ON) != 0;
1037		if (modifier && key == KeyEvent.KEYCODE_TAB && isConversationsOverviewHideable()) {
1038			toggleConversationsOverview();
1039			return true;
1040		} else if (modifier && key == KeyEvent.KEYCODE_SPACE) {
1041			startActivity(new Intent(this, StartConversationActivity.class));
1042			return true;
1043		} else if (modifier && key == downKey) {
1044			if (isConversationsOverviewHideable() && !isConversationsOverviewVisable()) {
1045				showConversationsOverview();
1046				;
1047			}
1048			return selectDownConversation();
1049		} else if (modifier && key == upKey) {
1050			if (isConversationsOverviewHideable() && !isConversationsOverviewVisable()) {
1051				showConversationsOverview();
1052			}
1053			return selectUpConversation();
1054		} else if (modifier && key == KeyEvent.KEYCODE_1) {
1055			return openConversationByIndex(0);
1056		} else if (modifier && key == KeyEvent.KEYCODE_2) {
1057			return openConversationByIndex(1);
1058		} else if (modifier && key == KeyEvent.KEYCODE_3) {
1059			return openConversationByIndex(2);
1060		} else if (modifier && key == KeyEvent.KEYCODE_4) {
1061			return openConversationByIndex(3);
1062		} else if (modifier && key == KeyEvent.KEYCODE_5) {
1063			return openConversationByIndex(4);
1064		} else if (modifier && key == KeyEvent.KEYCODE_6) {
1065			return openConversationByIndex(5);
1066		} else if (modifier && key == KeyEvent.KEYCODE_7) {
1067			return openConversationByIndex(6);
1068		} else if (modifier && key == KeyEvent.KEYCODE_8) {
1069			return openConversationByIndex(7);
1070		} else if (modifier && key == KeyEvent.KEYCODE_9) {
1071			return openConversationByIndex(8);
1072		} else if (modifier && key == KeyEvent.KEYCODE_0) {
1073			return openConversationByIndex(9);
1074		} else {
1075			return super.onKeyUp(key, event);
1076		}
1077	}
1078
1079	private void toggleConversationsOverview() {
1080		if (isConversationsOverviewVisable()) {
1081			hideConversationsOverview();
1082			if (mConversationFragment != null) {
1083				mConversationFragment.setFocusOnInputField();
1084			}
1085		} else {
1086			showConversationsOverview();
1087		}
1088	}
1089
1090	private boolean selectUpConversation() {
1091		if (this.mSelectedConversation != null) {
1092			int index = this.conversationList.indexOf(this.mSelectedConversation);
1093			if (index > 0) {
1094				return openConversationByIndex(index - 1);
1095			}
1096		}
1097		return false;
1098	}
1099
1100	private boolean selectDownConversation() {
1101		if (this.mSelectedConversation != null) {
1102			int index = this.conversationList.indexOf(this.mSelectedConversation);
1103			if (index != -1 && index < this.conversationList.size() - 1) {
1104				return openConversationByIndex(index + 1);
1105			}
1106		}
1107		return false;
1108	}
1109
1110	private boolean openConversationByIndex(int index) {
1111		try {
1112			this.conversationWasSelectedByKeyboard = true;
1113			setSelectedConversation(this.conversationList.get(index));
1114			this.mConversationFragment.reInit(getSelectedConversation());
1115			if (index > listView.getLastVisiblePosition() - 1 || index < listView.getFirstVisiblePosition() + 1) {
1116				this.listView.setSelection(index);
1117			}
1118			openConversation();
1119			return true;
1120		} catch (IndexOutOfBoundsException e) {
1121			return false;
1122		}
1123	}
1124
1125	@Override
1126	protected void onNewIntent(final Intent intent) {
1127		if (intent != null && ACTION_VIEW_CONVERSATION.equals(intent.getAction())) {
1128			mOpenConversation = null;
1129			mUnprocessedNewIntent = true;
1130			if (xmppConnectionServiceBound) {
1131				handleViewConversationIntent(intent);
1132				intent.setAction(Intent.ACTION_MAIN);
1133			} else {
1134				setIntent(intent);
1135			}
1136		}
1137	}
1138
1139	@Override
1140	public void onStart() {
1141		super.onStart();
1142		this.mRedirected.set(false);
1143		if (this.xmppConnectionServiceBound) {
1144			this.onBackendConnected();
1145		}
1146		if (conversationList.size() >= 1) {
1147			this.onConversationUpdate();
1148		}
1149	}
1150
1151	@Override
1152	public void onPause() {
1153		listView.discardUndo();
1154		super.onPause();
1155		this.mActivityPaused = true;
1156	}
1157
1158	@Override
1159	public void onResume() {
1160		super.onResume();
1161		final int theme = findTheme();
1162		final boolean usingEnterKey = usingEnterKey();
1163		if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
1164			recreate();
1165		}
1166		this.mActivityPaused = false;
1167
1168
1169		if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
1170			sendReadMarkerIfNecessary(getSelectedConversation());
1171		}
1172
1173	}
1174
1175	@Override
1176	public void onSaveInstanceState(final Bundle savedInstanceState) {
1177		Conversation conversation = getSelectedConversation();
1178		if (conversation != null) {
1179			savedInstanceState.putString(STATE_OPEN_CONVERSATION, conversation.getUuid());
1180			Pair<Integer,Integer> scrollPosition = mConversationFragment.getScrollPosition();
1181			if (scrollPosition != null) {
1182				savedInstanceState.putInt(STATE_FIRST_VISIBLE, scrollPosition.first);
1183				savedInstanceState.putInt(STATE_OFFSET_FROM_TOP, scrollPosition.second);
1184			}
1185		} else {
1186			savedInstanceState.remove(STATE_OPEN_CONVERSATION);
1187		}
1188		savedInstanceState.putBoolean(STATE_PANEL_OPEN, isConversationsOverviewVisable());
1189		if (this.mPendingImageUris.size() >= 1) {
1190			Log.d(Config.LOGTAG,"ConversationsActivity.onSaveInstanceState() - saving pending image uri");
1191			savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
1192		} else {
1193			savedInstanceState.remove(STATE_PENDING_URI);
1194		}
1195		super.onSaveInstanceState(savedInstanceState);
1196	}
1197
1198	private void clearPending() {
1199		mPendingImageUris.clear();
1200		mPendingFileUris.clear();
1201		mPendingGeoUri = null;
1202		mPostponedActivityResult = null;
1203	}
1204
1205	private void redirectToStartConversationActivity() {
1206		Account pendingAccount = xmppConnectionService.getPendingAccount();
1207		if (pendingAccount == null) {
1208			Intent startConversationActivity = new Intent(this, StartConversationActivity.class);
1209			startConversationActivity.putExtra("init", true);
1210			startActivity(startConversationActivity);
1211		} else {
1212			switchToAccount(pendingAccount, true);
1213		}
1214		finish();
1215	}
1216
1217	@Override
1218	void onBackendConnected() {
1219		this.xmppConnectionService.getNotificationService().setIsInForeground(true);
1220		updateConversationList();
1221
1222		if (mPendingConferenceInvite != null) {
1223			if (mPendingConferenceInvite.execute(this)) {
1224				mToast = Toast.makeText(this, R.string.creating_conference, Toast.LENGTH_LONG);
1225				mToast.show();
1226			}
1227			mPendingConferenceInvite = null;
1228		}
1229
1230		final Intent intent = getIntent();
1231
1232		if (xmppConnectionService.getAccounts().size() == 0) {
1233			if (mRedirected.compareAndSet(false, true)) {
1234				if (Config.X509_VERIFICATION) {
1235					startActivity(new Intent(this, ManageAccountActivity.class));
1236				} else if (Config.MAGIC_CREATE_DOMAIN != null) {
1237					startActivity(new Intent(this, WelcomeActivity.class));
1238				} else {
1239					Intent editAccount = new Intent(this, EditAccountActivity.class);
1240					editAccount.putExtra("init",true);
1241					startActivity(editAccount);
1242				}
1243				finish();
1244			}
1245		} else if (conversationList.size() <= 0) {
1246			if (mRedirected.compareAndSet(false, true)) {
1247				redirectToStartConversationActivity();
1248			}
1249		} else if (selectConversationByUuid(mOpenConversation)) {
1250			if (mPanelOpen) {
1251				showConversationsOverview();
1252			} else {
1253				if (isConversationsOverviewHideable()) {
1254					openConversation();
1255					updateActionBarTitle(true);
1256				}
1257			}
1258			if (this.mConversationFragment.reInit(getSelectedConversation())) {
1259				Log.d(Config.LOGTAG,"setting scroll position on fragment");
1260				this.mConversationFragment.setScrollPosition(mScrollPosition);
1261			}
1262			mOpenConversation = null;
1263		} else if (intent != null && ACTION_VIEW_CONVERSATION.equals(intent.getAction())) {
1264			clearPending();
1265			handleViewConversationIntent(intent);
1266			intent.setAction(Intent.ACTION_MAIN);
1267		} else if (getSelectedConversation() == null) {
1268			reInitLatestConversation();
1269		} else {
1270			this.mConversationFragment.messageListAdapter.updatePreferences();
1271			this.mConversationFragment.messagesView.invalidateViews();
1272			this.mConversationFragment.setupIme();
1273		}
1274
1275		if (this.mPostponedActivityResult != null) {
1276			this.onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second);
1277		}
1278
1279		final boolean stopping = isStopping();
1280
1281		if (!forbidProcessingPendings) {
1282			for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1283				Uri foo = i.next();
1284				Log.d(Config.LOGTAG,"ConversationsActivity.onBackendConnected() - attaching image to conversations. stopping="+Boolean.toString(stopping));
1285				attachImageToConversation(getSelectedConversation(), foo);
1286			}
1287
1288			for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1289				Log.d(Config.LOGTAG,"ConversationsActivity.onBackendConnected() - attaching file to conversations. stopping="+Boolean.toString(stopping));
1290				attachFileToConversation(getSelectedConversation(), i.next());
1291			}
1292
1293			if (mPendingGeoUri != null) {
1294				attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1295				mPendingGeoUri = null;
1296			}
1297		}
1298		forbidProcessingPendings = false;
1299
1300		if (!ExceptionHelper.checkForCrash(this, this.xmppConnectionService) && !mRedirected.get()) {
1301			openBatteryOptimizationDialogIfNeeded();
1302		}
1303		if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
1304			xmppConnectionService.getNotificationService().setOpenConversation(null);
1305		} else {
1306			xmppConnectionService.getNotificationService().setOpenConversation(getSelectedConversation());
1307		}
1308	}
1309
1310	private boolean isStopping() {
1311		if (Build.VERSION.SDK_INT >= 17) {
1312			return isFinishing() || isDestroyed();
1313		} else {
1314			return isFinishing();
1315		}
1316	}
1317
1318	private void reInitLatestConversation() {
1319		showConversationsOverview();
1320		clearPending();
1321		setSelectedConversation(conversationList.get(0));
1322		this.mConversationFragment.reInit(getSelectedConversation());
1323	}
1324
1325	private void handleViewConversationIntent(final Intent intent) {
1326		final String uuid = intent.getStringExtra(CONVERSATION);
1327		final String downloadUuid = intent.getStringExtra(EXTRA_DOWNLOAD_UUID);
1328		final String text = intent.getStringExtra(TEXT);
1329		final String nick = intent.getStringExtra(NICK);
1330		final boolean pm = intent.getBooleanExtra(PRIVATE_MESSAGE, false);
1331		if (selectConversationByUuid(uuid)) {
1332			this.mConversationFragment.reInit(getSelectedConversation());
1333			if (nick != null) {
1334				if (pm) {
1335					Jid jid = getSelectedConversation().getJid();
1336					try {
1337						Jid next = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), nick);
1338						this.mConversationFragment.privateMessageWith(next);
1339					} catch (final InvalidJidException ignored) {
1340						//do nothing
1341					}
1342				} else {
1343					this.mConversationFragment.highlightInConference(nick);
1344				}
1345			} else {
1346				this.mConversationFragment.appendText(text);
1347			}
1348			hideConversationsOverview();
1349			mUnprocessedNewIntent = false;
1350			openConversation();
1351			if (mContentView instanceof SlidingPaneLayout) {
1352				updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
1353			}
1354			if (downloadUuid != null) {
1355				final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
1356				if (message != null) {
1357					startDownloadable(message);
1358				}
1359			}
1360		} else {
1361			mUnprocessedNewIntent = false;
1362		}
1363	}
1364
1365	private boolean selectConversationByUuid(String uuid) {
1366		if (uuid == null) {
1367			return false;
1368		}
1369		for (Conversation aConversationList : conversationList) {
1370			if (aConversationList.getUuid().equals(uuid)) {
1371				setSelectedConversation(aConversationList);
1372				return true;
1373			}
1374		}
1375		return false;
1376	}
1377
1378	@Override
1379	protected void unregisterListeners() {
1380		super.unregisterListeners();
1381		xmppConnectionService.getNotificationService().setOpenConversation(null);
1382	}
1383
1384	@SuppressLint("NewApi")
1385	private static List<Uri> extractUriFromIntent(final Intent intent) {
1386		List<Uri> uris = new ArrayList<>();
1387		if (intent == null) {
1388			return uris;
1389		}
1390		Uri uri = intent.getData();
1391		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && uri == null) {
1392			final ClipData clipData = intent.getClipData();
1393			if (clipData != null) {
1394				for (int i = 0; i < clipData.getItemCount(); ++i) {
1395					uris.add(clipData.getItemAt(i).getUri());
1396				}
1397			}
1398		} else {
1399			uris.add(uri);
1400		}
1401		return uris;
1402	}
1403
1404	@Override
1405	protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
1406		super.onActivityResult(requestCode, resultCode, data);
1407		if (resultCode == RESULT_OK) {
1408			if (requestCode == REQUEST_DECRYPT_PGP) {
1409				mConversationFragment.onActivityResult(requestCode, resultCode, data);
1410			} else if (requestCode == REQUEST_CHOOSE_PGP_ID) {
1411				// the user chose OpenPGP for encryption and selected his key in the PGP provider
1412				if (xmppConnectionServiceBound) {
1413					if (data.getExtras().containsKey(OpenPgpApi.EXTRA_SIGN_KEY_ID)) {
1414						// associate selected PGP keyId with the account
1415						mSelectedConversation.getAccount().setPgpSignId(data.getExtras().getLong(OpenPgpApi.EXTRA_SIGN_KEY_ID));
1416						// we need to announce the key as described in XEP-027
1417						announcePgp(mSelectedConversation.getAccount(), null, onOpenPGPKeyPublished);
1418					} else {
1419						choosePgpSignId(mSelectedConversation.getAccount());
1420					}
1421					this.mPostponedActivityResult = null;
1422				} else {
1423					this.mPostponedActivityResult = new Pair<>(requestCode, data);
1424				}
1425			} else if (requestCode == REQUEST_ANNOUNCE_PGP) {
1426				if (xmppConnectionServiceBound) {
1427					announcePgp(mSelectedConversation.getAccount(), mSelectedConversation, onOpenPGPKeyPublished);
1428					this.mPostponedActivityResult = null;
1429				} else {
1430					this.mPostponedActivityResult = new Pair<>(requestCode, data);
1431				}
1432			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
1433				mPendingImageUris.clear();
1434				mPendingImageUris.addAll(extractUriFromIntent(data));
1435				if (xmppConnectionServiceBound) {
1436					for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1437						Log.d(Config.LOGTAG,"ConversationsActivity.onActivityResult() - attaching image to conversations. CHOOSE_IMAGE");
1438						attachImageToConversation(getSelectedConversation(), i.next());
1439					}
1440				}
1441			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE || requestCode == ATTACHMENT_CHOICE_RECORD_VIDEO) {
1442				final List<Uri> uris = extractUriFromIntent(data);
1443				Log.d(Config.LOGTAG,"uris "+uris.toString());
1444				final Conversation c = getSelectedConversation();
1445				final OnPresenceSelected callback = new OnPresenceSelected() {
1446					@Override
1447					public void onPresenceSelected() {
1448						mPendingFileUris.clear();
1449						mPendingFileUris.addAll(uris);
1450						if (xmppConnectionServiceBound) {
1451							for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1452								Log.d(Config.LOGTAG,"ConversationsActivity.onActivityResult() - attaching file to conversations. CHOOSE_FILE/RECORD_VOICE/RECORD_VIDEO");
1453								attachFileToConversation(c, i.next());
1454							}
1455						}
1456					}
1457				};
1458				if (c == null || c.getMode() == Conversation.MODE_MULTI
1459						|| FileBackend.allFilesUnderSize(this, uris, getMaxHttpUploadSize(c))
1460						|| c.getNextEncryption() == Message.ENCRYPTION_OTR) {
1461					callback.onPresenceSelected();
1462				} else {
1463					selectPresence(c, callback);
1464				}
1465			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
1466				if (mPendingImageUris.size() == 1) {
1467					Uri uri = FileBackend.getIndexableTakePhotoUri(mPendingImageUris.get(0));
1468					mPendingImageUris.set(0, uri);
1469					if (xmppConnectionServiceBound) {
1470						Log.d(Config.LOGTAG,"ConversationsActivity.onActivityResult() - attaching image to conversations. TAKE_PHOTO");
1471						attachImageToConversation(getSelectedConversation(), uri);
1472						mPendingImageUris.clear();
1473					}
1474					if (!Config.ONLY_INTERNAL_STORAGE) {
1475						Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
1476						intent.setData(uri);
1477						sendBroadcast(intent);
1478					}
1479				} else {
1480					mPendingImageUris.clear();
1481				}
1482			} else if (requestCode == ATTACHMENT_CHOICE_LOCATION) {
1483				double latitude = data.getDoubleExtra("latitude", 0);
1484				double longitude = data.getDoubleExtra("longitude", 0);
1485				this.mPendingGeoUri = Uri.parse("geo:" + String.valueOf(latitude) + "," + String.valueOf(longitude));
1486				if (xmppConnectionServiceBound) {
1487					attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1488					this.mPendingGeoUri = null;
1489				}
1490			} else if (requestCode == REQUEST_TRUST_KEYS_TEXT || requestCode == REQUEST_TRUST_KEYS_MENU) {
1491				this.forbidProcessingPendings = true;
1492				if (xmppConnectionServiceBound) {
1493					mConversationFragment.onActivityResult(requestCode, resultCode, data);
1494					this.mPostponedActivityResult = null;
1495				} else {
1496					this.mPostponedActivityResult = new Pair<>(requestCode, data);
1497				}
1498
1499			}
1500		} else {
1501			mPendingImageUris.clear();
1502			mPendingFileUris.clear();
1503			if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) {
1504				mConversationFragment.onActivityResult(requestCode, resultCode, data);
1505			}
1506			if (requestCode == REQUEST_BATTERY_OP) {
1507				setNeverAskForBatteryOptimizationsAgain();
1508			}
1509		}
1510	}
1511
1512	private long getMaxHttpUploadSize(Conversation conversation) {
1513		final XmppConnection connection = conversation.getAccount().getXmppConnection();
1514		return connection == null ? -1 : connection.getFeatures().getMaxHttpUploadSize();
1515	}
1516
1517	private void setNeverAskForBatteryOptimizationsAgain() {
1518		getPreferences().edit().putBoolean("show_battery_optimization", false).apply();
1519	}
1520
1521	private void openBatteryOptimizationDialogIfNeeded() {
1522		if (hasAccountWithoutPush()
1523				&& isOptimizingBattery()
1524				&& getPreferences().getBoolean("show_battery_optimization", true)) {
1525			AlertDialog.Builder builder = new AlertDialog.Builder(this);
1526			builder.setTitle(R.string.battery_optimizations_enabled);
1527			builder.setMessage(R.string.battery_optimizations_enabled_dialog);
1528			builder.setPositiveButton(R.string.next, new OnClickListener() {
1529				@Override
1530				public void onClick(DialogInterface dialog, int which) {
1531					Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
1532					Uri uri = Uri.parse("package:" + getPackageName());
1533					intent.setData(uri);
1534					try {
1535						startActivityForResult(intent, REQUEST_BATTERY_OP);
1536					} catch (ActivityNotFoundException e) {
1537						Toast.makeText(ConversationActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show();
1538					}
1539				}
1540			});
1541			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
1542				builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
1543					@Override
1544					public void onDismiss(DialogInterface dialog) {
1545						setNeverAskForBatteryOptimizationsAgain();
1546					}
1547				});
1548			}
1549			AlertDialog dialog = builder.create();
1550			dialog.setCanceledOnTouchOutside(false);
1551			dialog.show();
1552		}
1553	}
1554
1555	private boolean hasAccountWithoutPush() {
1556		for(Account account : xmppConnectionService.getAccounts()) {
1557			if (account.getStatus() != Account.State.DISABLED && !xmppConnectionService.getPushManagementService().available(account)) {
1558				return true;
1559			}
1560		}
1561		return false;
1562	}
1563
1564	private void attachLocationToConversation(Conversation conversation, Uri uri) {
1565		if (conversation == null) {
1566			return;
1567		}
1568		xmppConnectionService.attachLocationToConversation(conversation,uri, new UiCallback<Message>() {
1569
1570			@Override
1571			public void success(Message message) {
1572				xmppConnectionService.sendMessage(message);
1573			}
1574
1575			@Override
1576			public void error(int errorCode, Message object) {
1577
1578			}
1579
1580			@Override
1581			public void userInputRequried(PendingIntent pi, Message object) {
1582
1583			}
1584		});
1585	}
1586
1587	private void attachFileToConversation(Conversation conversation, Uri uri) {
1588		if (conversation == null) {
1589			return;
1590		}
1591		final Toast prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_file), Toast.LENGTH_LONG);
1592		prepareFileToast.show();
1593		xmppConnectionService.attachFileToConversation(conversation, uri, new UiInformableCallback<Message>() {
1594			@Override
1595			public void inform(final String text) {
1596				hidePrepareFileToast(prepareFileToast);
1597				runOnUiThread(new Runnable() {
1598					@Override
1599					public void run() {
1600						replaceToast(text);
1601					}
1602				});
1603			}
1604
1605			@Override
1606			public void success(Message message) {
1607				runOnUiThread(new Runnable() {
1608					@Override
1609					public void run() {
1610						hideToast();
1611					}
1612				});
1613				hidePrepareFileToast(prepareFileToast);
1614				xmppConnectionService.sendMessage(message);
1615			}
1616
1617			@Override
1618			public void error(final int errorCode, Message message) {
1619				hidePrepareFileToast(prepareFileToast);
1620				runOnUiThread(new Runnable() {
1621					@Override
1622					public void run() {
1623						replaceToast(getString(errorCode));
1624					}
1625				});
1626
1627			}
1628
1629			@Override
1630			public void userInputRequried(PendingIntent pi, Message message) {
1631				hidePrepareFileToast(prepareFileToast);
1632			}
1633		});
1634	}
1635
1636	public void attachImageToConversation(Uri uri) {
1637		this.attachImageToConversation(getSelectedConversation(), uri);
1638	}
1639
1640	private void attachImageToConversation(Conversation conversation, Uri uri) {
1641		if (conversation == null) {
1642			return;
1643		}
1644		final Toast prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_image), Toast.LENGTH_LONG);
1645		prepareFileToast.show();
1646		xmppConnectionService.attachImageToConversation(conversation, uri,
1647				new UiCallback<Message>() {
1648
1649					@Override
1650					public void userInputRequried(PendingIntent pi, Message object) {
1651						hidePrepareFileToast(prepareFileToast);
1652					}
1653
1654					@Override
1655					public void success(Message message) {
1656						hidePrepareFileToast(prepareFileToast);
1657						xmppConnectionService.sendMessage(message);
1658					}
1659
1660					@Override
1661					public void error(final int error, Message message) {
1662						hidePrepareFileToast(prepareFileToast);
1663						runOnUiThread(new Runnable() {
1664							@Override
1665							public void run() {
1666								replaceToast(getString(error));
1667							}
1668						});
1669					}
1670				});
1671	}
1672
1673	private void hidePrepareFileToast(final Toast prepareFileToast) {
1674		if (prepareFileToast != null) {
1675			runOnUiThread(new Runnable() {
1676
1677				@Override
1678				public void run() {
1679					prepareFileToast.cancel();
1680				}
1681			});
1682		}
1683	}
1684
1685	public void updateConversationList() {
1686		xmppConnectionService.populateWithOrderedConversations(conversationList);
1687		if (!conversationList.contains(mSelectedConversation)) {
1688			mSelectedConversation = null;
1689		}
1690		if (swipedConversation != null) {
1691			if (swipedConversation.isRead()) {
1692				conversationList.remove(swipedConversation);
1693			} else {
1694				listView.discardUndo();
1695			}
1696		}
1697		listAdapter.notifyDataSetChanged();
1698	}
1699
1700	public void runIntent(PendingIntent pi, int requestCode) {
1701		try {
1702			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
1703					null, 0, 0, 0);
1704		} catch (final SendIntentException ignored) {
1705		}
1706	}
1707
1708	public void encryptTextMessage(Message message) {
1709		xmppConnectionService.getPgpEngine().encrypt(message,
1710				new UiCallback<Message>() {
1711
1712					@Override
1713					public void userInputRequried(PendingIntent pi,Message message) {
1714						ConversationActivity.this.runIntent(pi,ConversationActivity.REQUEST_SEND_MESSAGE);
1715					}
1716
1717					@Override
1718					public void success(Message message) {
1719						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1720						xmppConnectionService.sendMessage(message);
1721						runOnUiThread(new Runnable() {
1722							@Override
1723							public void run() {
1724								mConversationFragment.messageSent();
1725							}
1726						});
1727					}
1728
1729					@Override
1730					public void error(final int error, Message message) {
1731						runOnUiThread(new Runnable() {
1732							@Override
1733							public void run() {
1734								mConversationFragment.doneSendingPgpMessage();
1735								Toast.makeText(ConversationActivity.this,
1736										R.string.unable_to_connect_to_keychain,
1737										Toast.LENGTH_SHORT
1738								).show();
1739							}
1740						});
1741
1742					}
1743				});
1744	}
1745
1746	public boolean useSendButtonToIndicateStatus() {
1747		return getPreferences().getBoolean("send_button_status", getResources().getBoolean(R.bool.send_button_status));
1748	}
1749
1750	public boolean indicateReceived() {
1751		return getPreferences().getBoolean("indicate_received", getResources().getBoolean(R.bool.indicate_received));
1752	}
1753
1754	public boolean useGreenBackground() {
1755		return getPreferences().getBoolean("use_green_background",getResources().getBoolean(R.bool.use_green_background));
1756	}
1757
1758	protected boolean trustKeysIfNeeded(int requestCode) {
1759		return trustKeysIfNeeded(requestCode, ATTACHMENT_CHOICE_INVALID);
1760	}
1761
1762	protected boolean trustKeysIfNeeded(int requestCode, int attachmentChoice) {
1763		AxolotlService axolotlService = mSelectedConversation.getAccount().getAxolotlService();
1764		final List<Jid> targets = axolotlService.getCryptoTargets(mSelectedConversation);
1765		boolean hasUnaccepted = !mSelectedConversation.getAcceptedCryptoTargets().containsAll(targets);
1766		boolean hasUndecidedOwn = !axolotlService.getKeysWithTrust(FingerprintStatus.createActiveUndecided()).isEmpty();
1767		boolean hasUndecidedContacts = !axolotlService.getKeysWithTrust(FingerprintStatus.createActiveUndecided(), targets).isEmpty();
1768		boolean hasPendingKeys = !axolotlService.findDevicesWithoutSession(mSelectedConversation).isEmpty();
1769		boolean hasNoTrustedKeys = axolotlService.anyTargetHasNoTrustedKeys(targets);
1770		if(hasUndecidedOwn || hasUndecidedContacts || hasPendingKeys || hasNoTrustedKeys || hasUnaccepted) {
1771			axolotlService.createSessionsIfNeeded(mSelectedConversation);
1772			Intent intent = new Intent(getApplicationContext(), TrustKeysActivity.class);
1773			String[] contacts = new String[targets.size()];
1774			for(int i = 0; i < contacts.length; ++i) {
1775				contacts[i] = targets.get(i).toString();
1776			}
1777			intent.putExtra("contacts", contacts);
1778			intent.putExtra(EXTRA_ACCOUNT, mSelectedConversation.getAccount().getJid().toBareJid().toString());
1779			intent.putExtra("choice", attachmentChoice);
1780			intent.putExtra("conversation",mSelectedConversation.getUuid());
1781			startActivityForResult(intent, requestCode);
1782			return true;
1783		} else {
1784			return false;
1785		}
1786	}
1787
1788	@Override
1789	protected void refreshUiReal() {
1790		updateConversationList();
1791		if (conversationList.size() > 0) {
1792			if (!this.mConversationFragment.isAdded()) {
1793				Log.d(Config.LOGTAG,"fragment NOT added to activity. detached="+Boolean.toString(mConversationFragment.isDetached()));
1794			}
1795			if (getSelectedConversation() == null) {
1796				reInitLatestConversation();
1797			} else {
1798				ConversationActivity.this.mConversationFragment.updateMessages();
1799				updateActionBarTitle();
1800				invalidateOptionsMenu();
1801			}
1802		} else {
1803			if (!isStopping() && mRedirected.compareAndSet(false, true)) {
1804				redirectToStartConversationActivity();
1805			}
1806			Log.d(Config.LOGTAG,"not updating conversations fragment because conversations list size was 0");
1807		}
1808	}
1809
1810	@Override
1811	public void onAccountUpdate() {
1812		this.refreshUi();
1813	}
1814
1815	@Override
1816	public void onConversationUpdate() {
1817		this.refreshUi();
1818	}
1819
1820	@Override
1821	public void onRosterUpdate() {
1822		this.refreshUi();
1823	}
1824
1825	@Override
1826	public void OnUpdateBlocklist(Status status) {
1827		this.refreshUi();
1828	}
1829
1830	public void unblockConversation(final Blockable conversation) {
1831		xmppConnectionService.sendUnblockRequest(conversation);
1832	}
1833
1834	public boolean enterIsSend() {
1835		return getPreferences().getBoolean("enter_is_send",getResources().getBoolean(R.bool.enter_is_send));
1836	}
1837
1838	@Override
1839	public void onShowErrorToast(final int resId) {
1840		runOnUiThread(new Runnable() {
1841			@Override
1842			public void run() {
1843				Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
1844			}
1845		});
1846	}
1847
1848	public boolean highlightSelectedConversations() {
1849		return !isConversationsOverviewHideable() || this.conversationWasSelectedByKeyboard;
1850	}
1851}