ConversationActivity.java

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