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.DialogInterface;
   9import android.content.DialogInterface.OnClickListener;
  10import android.content.Intent;
  11import android.content.IntentSender.SendIntentException;
  12import android.net.Uri;
  13import android.os.Bundle;
  14import android.os.SystemClock;
  15import android.provider.MediaStore;
  16import android.support.v4.widget.SlidingPaneLayout;
  17import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
  18import android.view.Menu;
  19import android.view.MenuItem;
  20import android.view.View;
  21import android.widget.AdapterView;
  22import android.widget.AdapterView.OnItemClickListener;
  23import android.widget.ArrayAdapter;
  24import android.widget.CheckBox;
  25import android.widget.ListView;
  26import android.widget.PopupMenu;
  27import android.widget.PopupMenu.OnMenuItemClickListener;
  28import android.widget.Toast;
  29
  30import net.java.otr4j.session.SessionStatus;
  31
  32import java.util.ArrayList;
  33import java.util.List;
  34
  35import eu.siacs.conversations.R;
  36import eu.siacs.conversations.entities.Account;
  37import eu.siacs.conversations.entities.Blockable;
  38import eu.siacs.conversations.entities.Contact;
  39import eu.siacs.conversations.entities.Conversation;
  40import eu.siacs.conversations.entities.Message;
  41import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
  42import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
  43import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
  44import eu.siacs.conversations.ui.adapter.ConversationAdapter;
  45import eu.siacs.conversations.utils.ExceptionHelper;
  46import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
  47
  48public class ConversationActivity extends XmppActivity
  49	implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist {
  50
  51	public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
  52
  53	public static final String VIEW_CONVERSATION = "viewConversation";
  54	public static final String CONVERSATION = "conversationUuid";
  55	public static final String MESSAGE = "messageUuid";
  56	public static final String TEXT = "text";
  57	public static final String NICK = "nick";
  58
  59	public static final int REQUEST_SEND_MESSAGE = 0x0201;
  60	public static final int REQUEST_DECRYPT_PGP = 0x0202;
  61	public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
  62	private static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
  63	private static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
  64	private static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
  65	private static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
  66	private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
  67	private static final String STATE_PANEL_OPEN = "state_panel_open";
  68	private static final String STATE_PENDING_URI = "state_pending_uri";
  69
  70	private String mOpenConverstaion = null;
  71	private boolean mPanelOpen = true;
  72	private Uri mPendingImageUri = null;
  73	private Uri mPendingFileUri = null;
  74
  75	private View mContentView;
  76
  77	private List<Conversation> conversationList = new ArrayList<>();
  78	private Conversation mSelectedConversation = null;
  79	private ListView listView;
  80	private ConversationFragment mConversationFragment;
  81
  82	private ArrayAdapter<Conversation> listAdapter;
  83
  84	private Toast prepareFileToast;
  85
  86	private boolean mActivityPaused = false;
  87
  88	public Conversation getSelectedConversation() {
  89		return this.mSelectedConversation;
  90	}
  91
  92	public void setSelectedConversation(Conversation conversation) {
  93		this.mSelectedConversation = conversation;
  94	}
  95
  96	public void showConversationsOverview() {
  97		if (mContentView instanceof SlidingPaneLayout) {
  98			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
  99			mSlidingPaneLayout.openPane();
 100		}
 101	}
 102
 103	@Override
 104	protected String getShareableUri() {
 105		Conversation conversation = getSelectedConversation();
 106		if (conversation != null) {
 107			return conversation.getAccount().getShareableUri();
 108		} else {
 109			return "";
 110		}
 111	}
 112
 113	public void hideConversationsOverview() {
 114		if (mContentView instanceof SlidingPaneLayout) {
 115			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 116			mSlidingPaneLayout.closePane();
 117		}
 118	}
 119
 120	public boolean isConversationsOverviewHideable() {
 121		if (mContentView instanceof SlidingPaneLayout) {
 122			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 123			return mSlidingPaneLayout.isSlideable();
 124		} else {
 125			return false;
 126		}
 127	}
 128
 129	public boolean isConversationsOverviewVisable() {
 130		if (mContentView instanceof SlidingPaneLayout) {
 131			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 132			return mSlidingPaneLayout.isOpen();
 133		} else {
 134			return true;
 135		}
 136	}
 137
 138	@Override
 139	protected void onCreate(final Bundle savedInstanceState) {
 140		super.onCreate(savedInstanceState);
 141		if (savedInstanceState != null) {mOpenConverstaion = savedInstanceState.getString(
 142				STATE_OPEN_CONVERSATION, null);
 143		mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
 144		String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
 145		if (pending != null) {
 146			mPendingImageUri = Uri.parse(pending);
 147		}
 148		}
 149
 150		setContentView(R.layout.fragment_conversations_overview);
 151
 152		this.mConversationFragment = new ConversationFragment();
 153		FragmentTransaction transaction = getFragmentManager().beginTransaction();
 154		transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
 155		transaction.commit();
 156
 157		listView = (ListView) findViewById(R.id.list);
 158		this.listAdapter = new ConversationAdapter(this, conversationList);
 159		listView.setAdapter(this.listAdapter);
 160
 161		if (getActionBar() != null) {
 162			getActionBar().setDisplayHomeAsUpEnabled(false);
 163			getActionBar().setHomeButtonEnabled(false);
 164		}
 165
 166		listView.setOnItemClickListener(new OnItemClickListener() {
 167
 168			@Override
 169			public void onItemClick(AdapterView<?> arg0, View clickedView,
 170					int position, long arg3) {
 171				if (getSelectedConversation() != conversationList.get(position)) {
 172					setSelectedConversation(conversationList.get(position));
 173					ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 174				}
 175				hideConversationsOverview();
 176				openConversation();
 177			}
 178		});
 179		mContentView = findViewById(R.id.content_view_spl);
 180		if (mContentView == null) {
 181			mContentView = findViewById(R.id.content_view_ll);
 182		}
 183		if (mContentView instanceof SlidingPaneLayout) {
 184			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 185			mSlidingPaneLayout.setParallaxDistance(150);
 186			mSlidingPaneLayout
 187				.setShadowResource(R.drawable.es_slidingpane_shadow);
 188			mSlidingPaneLayout.setSliderFadeColor(0);
 189			mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
 190
 191				@Override
 192				public void onPanelOpened(View arg0) {
 193					updateActionBarTitle();
 194					invalidateOptionsMenu();
 195					hideKeyboard();
 196					if (xmppConnectionServiceBound) {
 197						xmppConnectionService.getNotificationService()
 198							.setOpenConversation(null);
 199					}
 200					closeContextMenu();
 201				}
 202
 203				@Override
 204				public void onPanelClosed(View arg0) {
 205					openConversation();
 206				}
 207
 208				@Override
 209				public void onPanelSlide(View arg0, float arg1) {
 210					// TODO Auto-generated method stub
 211
 212				}
 213			});
 214		}
 215	}
 216
 217	@Override
 218	public void switchToConversation(Conversation conversation) {
 219		setSelectedConversation(conversation);
 220		runOnUiThread(new Runnable() {
 221			@Override
 222			public void run() {
 223				ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 224				openConversation();
 225			}
 226		});
 227	}
 228
 229	private void updateActionBarTitle() {
 230		updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
 231	}
 232
 233	private void updateActionBarTitle(boolean titleShouldBeName) {
 234		final ActionBar ab = getActionBar();
 235		final Conversation conversation = getSelectedConversation();
 236		if (ab != null) {
 237			if (titleShouldBeName && conversation != null) {
 238				ab.setDisplayHomeAsUpEnabled(true);
 239				ab.setHomeButtonEnabled(true);
 240				if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
 241					ab.setTitle(conversation.getName());
 242				} else {
 243					ab.setTitle(conversation.getJid().toBareJid().toString());
 244				}
 245			} else {
 246				ab.setDisplayHomeAsUpEnabled(false);
 247				ab.setHomeButtonEnabled(false);
 248				ab.setTitle(R.string.app_name);
 249			}
 250		}
 251	}
 252
 253	private void openConversation() {
 254		this.updateActionBarTitle();
 255		this.invalidateOptionsMenu();
 256		if (xmppConnectionServiceBound) {
 257			final Conversation conversation = getSelectedConversation();
 258			xmppConnectionService.getNotificationService().setOpenConversation(conversation);
 259			sendReadMarkerIfNecessary(conversation);
 260		}
 261		listAdapter.notifyDataSetChanged();
 262	}
 263
 264	public void sendReadMarkerIfNecessary(final Conversation conversation) {
 265		if (!mActivityPaused && conversation != null && !conversation.isRead()) {
 266			xmppConnectionService.sendReadMarker(conversation);
 267		}
 268	}
 269
 270	@Override
 271	public boolean onCreateOptionsMenu(Menu menu) {
 272		getMenuInflater().inflate(R.menu.conversations, menu);
 273		final MenuItem menuSecure = menu.findItem(R.id.action_security);
 274		final MenuItem menuArchive = menu.findItem(R.id.action_archive);
 275		final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
 276		final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
 277		final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
 278		final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
 279		final MenuItem menuAdd = menu.findItem(R.id.action_add);
 280		final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
 281		final MenuItem menuMute = menu.findItem(R.id.action_mute);
 282		final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
 283		final MenuItem menuBlock = menu.findItem(R.id.action_block);
 284		final MenuItem menuUnblock = menu.findItem(R.id.action_unblock);
 285
 286		if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
 287			menuArchive.setVisible(false);
 288			menuMucDetails.setVisible(false);
 289			menuContactDetails.setVisible(false);
 290			menuSecure.setVisible(false);
 291			menuInviteContact.setVisible(false);
 292			menuAttach.setVisible(false);
 293			menuClearHistory.setVisible(false);
 294			menuMute.setVisible(false);
 295			menuUnmute.setVisible(false);
 296			menuBlock.setVisible(false);
 297			menuUnblock.setVisible(false);
 298		} else {
 299			menuAdd.setVisible(!isConversationsOverviewHideable());
 300			if (this.getSelectedConversation() != null) {
 301				if (this.getSelectedConversation().getLatestMessage()
 302						.getEncryption() != Message.ENCRYPTION_NONE) {
 303					menuSecure.setIcon(R.drawable.ic_action_secure);
 304						}
 305				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
 306					menuContactDetails.setVisible(false);
 307					menuAttach.setVisible(false);
 308					menuBlock.setVisible(false);
 309					menuUnblock.setVisible(false);
 310					menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
 311				} else {
 312					menuMucDetails.setVisible(false);
 313					if (this.getSelectedConversation().isBlocked()) {
 314						menuBlock.setVisible(false);
 315					} else {
 316						menuUnblock.setVisible(false);
 317					}
 318					final Account account = this.getSelectedConversation().getAccount();
 319					if (!(account.isOnlineAndConnected() && account.getXmppConnection().getFeatures().blocking())) {
 320						menuBlock.setVisible(false);
 321						menuUnblock.setVisible(false);
 322					}
 323				}
 324				if (this.getSelectedConversation().isMuted()) {
 325					menuMute.setVisible(false);
 326				} else {
 327					menuUnmute.setVisible(false);
 328				}
 329			}
 330		}
 331		return true;
 332	}
 333
 334	private void selectPresenceToAttachFile(final int attachmentChoice) {
 335		selectPresence(getSelectedConversation(), new OnPresenceSelected() {
 336
 337			@Override
 338			public void onPresenceSelected() {
 339				Intent intent = new Intent();
 340				boolean chooser = false;
 341				switch (attachmentChoice) {
 342					case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
 343						intent.setAction(Intent.ACTION_GET_CONTENT);
 344						intent.setType("image/*");
 345						chooser = true;
 346						break;
 347					case ATTACHMENT_CHOICE_TAKE_PHOTO:
 348						mPendingImageUri = xmppConnectionService.getFileBackend().getTakePhotoUri();
 349						intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
 350						intent.putExtra(MediaStore.EXTRA_OUTPUT,mPendingImageUri);
 351						break;
 352					case ATTACHMENT_CHOICE_CHOOSE_FILE:
 353						chooser = true;
 354						intent.setType("*/*");
 355						intent.addCategory(Intent.CATEGORY_OPENABLE);
 356						intent.setAction(Intent.ACTION_GET_CONTENT);
 357						break;
 358					case ATTACHMENT_CHOICE_RECORD_VOICE:
 359						intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
 360						break;
 361				}
 362				if (intent.resolveActivity(getPackageManager()) != null) {
 363					if (chooser) {
 364						startActivityForResult(
 365								Intent.createChooser(intent,getString(R.string.perform_action_with)),
 366								attachmentChoice);
 367					} else {
 368						startActivityForResult(intent, attachmentChoice);
 369					}
 370				}
 371			}
 372		});
 373	}
 374
 375	private void attachFile(final int attachmentChoice) {
 376		final Conversation conversation = getSelectedConversation();
 377		if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
 378			if (hasPgp()) {
 379				if (conversation.getContact().getPgpKeyId() != 0) {
 380					xmppConnectionService.getPgpEngine().hasKey(
 381							conversation.getContact(),
 382							new UiCallback<Contact>() {
 383
 384								@Override
 385								public void userInputRequried(PendingIntent pi,
 386										Contact contact) {
 387									ConversationActivity.this.runIntent(pi,
 388											attachmentChoice);
 389								}
 390
 391								@Override
 392								public void success(Contact contact) {
 393									selectPresenceToAttachFile(attachmentChoice);
 394								}
 395
 396								@Override
 397								public void error(int error, Contact contact) {
 398									displayErrorDialog(error);
 399								}
 400							});
 401				} else {
 402					final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 403						.findFragmentByTag("conversation");
 404					if (fragment != null) {
 405						fragment.showNoPGPKeyDialog(false,
 406								new OnClickListener() {
 407
 408									@Override
 409									public void onClick(DialogInterface dialog,
 410											int which) {
 411										conversation
 412											.setNextEncryption(Message.ENCRYPTION_NONE);
 413										xmppConnectionService.databaseBackend
 414											.updateConversation(conversation);
 415										selectPresenceToAttachFile(attachmentChoice);
 416									}
 417								});
 418					}
 419				}
 420			} else {
 421				showInstallPgpDialog();
 422			}
 423		} else if (getSelectedConversation().getNextEncryption(
 424					forceEncryption()) == Message.ENCRYPTION_NONE) {
 425			selectPresenceToAttachFile(attachmentChoice);
 426		} else {
 427			selectPresenceToAttachFile(attachmentChoice);
 428		}
 429	}
 430
 431	@Override
 432	public boolean onOptionsItemSelected(final MenuItem item) {
 433		if (item.getItemId() == android.R.id.home) {
 434			showConversationsOverview();
 435			return true;
 436		} else if (item.getItemId() == R.id.action_add) {
 437			startActivity(new Intent(this, StartConversationActivity.class));
 438			return true;
 439		} else if (getSelectedConversation() != null) {
 440			switch (item.getItemId()) {
 441				case R.id.action_attach_file:
 442					attachFileDialog();
 443					break;
 444				case R.id.action_archive:
 445					this.endConversation(getSelectedConversation());
 446					break;
 447				case R.id.action_contact_details:
 448					Contact contact = this.getSelectedConversation().getContact();
 449					if (contact.showInRoster()) {
 450						switchToContactDetails(contact);
 451					} else {
 452						showAddToRosterDialog(getSelectedConversation());
 453					}
 454					break;
 455				case R.id.action_muc_details:
 456					Intent intent = new Intent(this,
 457							ConferenceDetailsActivity.class);
 458					intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
 459					intent.putExtra("uuid", getSelectedConversation().getUuid());
 460					startActivity(intent);
 461					break;
 462				case R.id.action_invite:
 463					inviteToConversation(getSelectedConversation());
 464					break;
 465				case R.id.action_security:
 466					selectEncryptionDialog(getSelectedConversation());
 467					break;
 468				case R.id.action_clear_history:
 469					clearHistoryDialog(getSelectedConversation());
 470					break;
 471				case R.id.action_mute:
 472					muteConversationDialog(getSelectedConversation());
 473					break;
 474				case R.id.action_unmute:
 475					unmuteConversation(getSelectedConversation());
 476					break;
 477				case R.id.action_block:
 478					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 479					break;
 480				case R.id.action_unblock:
 481					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 482					break;
 483				default:
 484					break;
 485			}
 486			return super.onOptionsItemSelected(item);
 487		} else {
 488			return super.onOptionsItemSelected(item);
 489		}
 490	}
 491
 492	public void endConversation(Conversation conversation) {
 493		showConversationsOverview();
 494		xmppConnectionService.archiveConversation(conversation);
 495		if (conversationList.size() > 0) {
 496			setSelectedConversation(conversationList.get(0));
 497			this.mConversationFragment.reInit(getSelectedConversation());
 498		} else {
 499			setSelectedConversation(null);
 500		}
 501	}
 502
 503	@SuppressLint("InflateParams")
 504	protected void clearHistoryDialog(final Conversation conversation) {
 505		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 506		builder.setTitle(getString(R.string.clear_conversation_history));
 507		View dialogView = getLayoutInflater().inflate(
 508				R.layout.dialog_clear_history, null);
 509		final CheckBox endConversationCheckBox = (CheckBox) dialogView
 510			.findViewById(R.id.end_conversation_checkbox);
 511		builder.setView(dialogView);
 512		builder.setNegativeButton(getString(R.string.cancel), null);
 513		builder.setPositiveButton(getString(R.string.delete_messages),
 514				new OnClickListener() {
 515
 516					@Override
 517					public void onClick(DialogInterface dialog, int which) {
 518						ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
 519						if (endConversationCheckBox.isChecked()) {
 520							endConversation(conversation);
 521						} else {
 522							updateConversationList();
 523							ConversationActivity.this.mConversationFragment.updateMessages();
 524						}
 525					}
 526				});
 527		builder.create().show();
 528	}
 529
 530	protected void attachFileDialog() {
 531		View menuAttachFile = findViewById(R.id.action_attach_file);
 532		if (menuAttachFile == null) {
 533			return;
 534		}
 535		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
 536		attachFilePopup.inflate(R.menu.attachment_choices);
 537		if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
 538			attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
 539		}
 540		attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 541
 542			@Override
 543			public boolean onMenuItemClick(MenuItem item) {
 544				switch (item.getItemId()) {
 545					case R.id.attach_choose_picture:
 546						attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
 547						break;
 548					case R.id.attach_take_picture:
 549						attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
 550						break;
 551					case R.id.attach_choose_file:
 552						attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
 553						break;
 554					case R.id.attach_record_voice:
 555						attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
 556						break;
 557				}
 558				return false;
 559			}
 560		});
 561		attachFilePopup.show();
 562	}
 563
 564	public void verifyOtrSessionDialog(final Conversation conversation, View view) {
 565		if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
 566			Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
 567			return;
 568		}
 569		if (view == null) {
 570			return;
 571		}
 572		PopupMenu popup = new PopupMenu(this, view);
 573		popup.inflate(R.menu.verification_choices);
 574		popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 575			@Override
 576			public boolean onMenuItemClick(MenuItem menuItem) {
 577				Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
 578				intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
 579				intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
 580				intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
 581				switch (menuItem.getItemId()) {
 582					case R.id.scan_fingerprint:
 583						intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
 584						break;
 585					case R.id.ask_question:
 586						intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
 587						break;
 588					case R.id.manual_verification:
 589						intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
 590						break;
 591				}
 592				startActivity(intent);
 593				return true;
 594			}
 595		});
 596		popup.show();
 597	}
 598
 599	protected void selectEncryptionDialog(final Conversation conversation) {
 600		View menuItemView = findViewById(R.id.action_security);
 601		if (menuItemView == null) {
 602			return;
 603		}
 604		PopupMenu popup = new PopupMenu(this, menuItemView);
 605		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 606			.findFragmentByTag("conversation");
 607		if (fragment != null) {
 608			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 609
 610				@Override
 611				public boolean onMenuItemClick(MenuItem item) {
 612					switch (item.getItemId()) {
 613						case R.id.encryption_choice_none:
 614							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 615							item.setChecked(true);
 616							break;
 617						case R.id.encryption_choice_otr:
 618							conversation.setNextEncryption(Message.ENCRYPTION_OTR);
 619							item.setChecked(true);
 620							break;
 621						case R.id.encryption_choice_pgp:
 622							if (hasPgp()) {
 623								if (conversation.getAccount().getKeys()
 624										.has("pgp_signature")) {
 625									conversation
 626										.setNextEncryption(Message.ENCRYPTION_PGP);
 627									item.setChecked(true);
 628								} else {
 629									announcePgp(conversation.getAccount(),
 630											conversation);
 631								}
 632							} else {
 633								showInstallPgpDialog();
 634							}
 635							break;
 636						default:
 637							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 638							break;
 639					}
 640					xmppConnectionService.databaseBackend
 641						.updateConversation(conversation);
 642					fragment.updateChatMsgHint();
 643					return true;
 644				}
 645			});
 646			popup.inflate(R.menu.encryption_choices);
 647			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
 648			MenuItem none = popup.getMenu().findItem(
 649					R.id.encryption_choice_none);
 650			if (conversation.getMode() == Conversation.MODE_MULTI) {
 651				otr.setEnabled(false);
 652			} else {
 653				if (forceEncryption()) {
 654					none.setVisible(false);
 655				}
 656			}
 657			switch (conversation.getNextEncryption(forceEncryption())) {
 658				case Message.ENCRYPTION_NONE:
 659					none.setChecked(true);
 660					break;
 661				case Message.ENCRYPTION_OTR:
 662					otr.setChecked(true);
 663					break;
 664				case Message.ENCRYPTION_PGP:
 665					popup.getMenu().findItem(R.id.encryption_choice_pgp)
 666						.setChecked(true);
 667					break;
 668				default:
 669					popup.getMenu().findItem(R.id.encryption_choice_none)
 670						.setChecked(true);
 671					break;
 672			}
 673			popup.show();
 674		}
 675	}
 676
 677	protected void muteConversationDialog(final Conversation conversation) {
 678		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 679		builder.setTitle(R.string.disable_notifications);
 680		final int[] durations = getResources().getIntArray(
 681				R.array.mute_options_durations);
 682		builder.setItems(R.array.mute_options_descriptions,
 683				new OnClickListener() {
 684
 685					@Override
 686					public void onClick(final DialogInterface dialog, final int which) {
 687						final long till;
 688						if (durations[which] == -1) {
 689							till = Long.MAX_VALUE;
 690						} else {
 691							till = SystemClock.elapsedRealtime()
 692								+ (durations[which] * 1000);
 693						}
 694						conversation.setMutedTill(till);
 695						ConversationActivity.this.xmppConnectionService.databaseBackend
 696							.updateConversation(conversation);
 697						updateConversationList();
 698						ConversationActivity.this.mConversationFragment.updateMessages();
 699						invalidateOptionsMenu();
 700					}
 701				});
 702		builder.create().show();
 703	}
 704
 705	public void unmuteConversation(final Conversation conversation) {
 706		conversation.setMutedTill(0);
 707		this.xmppConnectionService.databaseBackend.updateConversation(conversation);
 708		updateConversationList();
 709		ConversationActivity.this.mConversationFragment.updateMessages();
 710		invalidateOptionsMenu();
 711	}
 712
 713	@Override
 714	public void onBackPressed() {
 715		if (!isConversationsOverviewVisable()) {
 716			showConversationsOverview();
 717		} else {
 718			moveTaskToBack(true);
 719		}
 720	}
 721
 722	@Override
 723	protected void onNewIntent(final Intent intent) {
 724		if (xmppConnectionServiceBound) {
 725			if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
 726				handleViewConversationIntent(intent);
 727			}
 728		} else {
 729			setIntent(intent);
 730		}
 731	}
 732
 733	@Override
 734	public void onStart() {
 735		super.onStart();
 736		if (this.xmppConnectionServiceBound) {
 737			this.onBackendConnected();
 738		}
 739		if (conversationList.size() >= 1) {
 740			this.onConversationUpdate();
 741		}
 742	}
 743
 744	@Override
 745	public void onPause() {
 746		super.onPause();
 747		this.mActivityPaused = true;
 748		if (this.xmppConnectionServiceBound) {
 749			this.xmppConnectionService.getNotificationService().setIsInForeground(false);
 750		}
 751	}
 752
 753	@Override
 754	public void onResume() {
 755		super.onResume();
 756		final int theme = findTheme();
 757		final boolean usingEnterKey = usingEnterKey();
 758		if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
 759			recreate();
 760		}
 761		this.mActivityPaused = false;
 762		if (this.xmppConnectionServiceBound) {
 763			this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 764		}
 765		if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
 766			sendReadMarkerIfNecessary(getSelectedConversation());
 767		}
 768	}
 769
 770	@Override
 771	public void onSaveInstanceState(final Bundle savedInstanceState) {
 772		Conversation conversation = getSelectedConversation();
 773		if (conversation != null) {
 774			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
 775					conversation.getUuid());
 776		}
 777		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
 778				isConversationsOverviewVisable());
 779		if (this.mPendingImageUri != null) {
 780			savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUri.toString());
 781		}
 782		super.onSaveInstanceState(savedInstanceState);
 783	}
 784
 785	@Override
 786	void onBackendConnected() {
 787		this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 788		updateConversationList();
 789		if (xmppConnectionService.getAccounts().size() == 0) {
 790			startActivity(new Intent(this, EditAccountActivity.class));
 791		} else if (conversationList.size() <= 0) {
 792			startActivity(new Intent(this, StartConversationActivity.class));
 793			finish();
 794		} else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
 795			handleViewConversationIntent(getIntent());
 796		} else if (selectConversationByUuid(mOpenConverstaion)) {
 797			if (mPanelOpen) {
 798				showConversationsOverview();
 799			} else {
 800				if (isConversationsOverviewHideable()) {
 801					openConversation();
 802				}
 803			}
 804			this.mConversationFragment.reInit(getSelectedConversation());
 805			mOpenConverstaion = null;
 806		} else if (getSelectedConversation() != null) {
 807			this.mConversationFragment.updateMessages();
 808		} else {
 809			showConversationsOverview();
 810			mPendingImageUri = null;
 811			mPendingFileUri = null;
 812			setSelectedConversation(conversationList.get(0));
 813			this.mConversationFragment.reInit(getSelectedConversation());
 814		}
 815
 816		if (mPendingImageUri != null) {
 817			attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 818			mPendingImageUri = null;
 819		} else if (mPendingFileUri != null) {
 820			attachFileToConversation(getSelectedConversation(),mPendingFileUri);
 821			mPendingFileUri = null;
 822		}
 823		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
 824		setIntent(new Intent());
 825	}
 826
 827	private void handleViewConversationIntent(final Intent intent) {
 828		final String uuid = (String) intent.getExtras().get(CONVERSATION);
 829		final String downloadUuid = (String) intent.getExtras().get(MESSAGE);
 830		final String text = intent.getExtras().getString(TEXT, "");
 831		final String nick = intent.getExtras().getString(NICK, null);
 832		if (selectConversationByUuid(uuid)) {
 833			this.mConversationFragment.reInit(getSelectedConversation());
 834			if (nick != null) {
 835				this.mConversationFragment.highlightInConference(nick);
 836			} else {
 837				this.mConversationFragment.appendText(text);
 838			}
 839			hideConversationsOverview();
 840			openConversation();
 841			if (mContentView instanceof SlidingPaneLayout) {
 842				updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
 843			}
 844			if (downloadUuid != null) {
 845				final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
 846				if (message != null) {
 847					mConversationFragment.messageListAdapter.startDownloadable(message);
 848				}
 849			}
 850		}
 851	}
 852
 853	private boolean selectConversationByUuid(String uuid) {
 854		if (uuid == null) {
 855			return false;
 856		}
 857		for (Conversation aConversationList : conversationList) {
 858			if (aConversationList.getUuid().equals(uuid)) {
 859				setSelectedConversation(aConversationList);
 860				return true;
 861			}
 862		}
 863		return false;
 864	}
 865
 866	@Override
 867	protected void unregisterListeners() {
 868		super.unregisterListeners();
 869		xmppConnectionService.getNotificationService().setOpenConversation(null);
 870	}
 871
 872	@Override
 873	protected void onActivityResult(int requestCode, int resultCode,
 874			final Intent data) {
 875		super.onActivityResult(requestCode, resultCode, data);
 876		if (resultCode == RESULT_OK) {
 877			if (requestCode == REQUEST_DECRYPT_PGP) {
 878				mConversationFragment.hideSnackbar();
 879				mConversationFragment.updateMessages();
 880			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
 881				mPendingImageUri = data.getData();
 882				if (xmppConnectionServiceBound) {
 883					attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 884					mPendingImageUri = null;
 885				}
 886			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
 887				mPendingFileUri = data.getData();
 888				if (xmppConnectionServiceBound) {
 889					attachFileToConversation(getSelectedConversation(),mPendingFileUri);
 890					mPendingFileUri = null;
 891				}
 892			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO && mPendingImageUri != null) {
 893				if (xmppConnectionServiceBound) {
 894					attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 895					mPendingImageUri = null;
 896				}
 897				Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 898				intent.setData(mPendingImageUri);
 899				sendBroadcast(intent);
 900			}
 901		} else {
 902			if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
 903				mPendingImageUri = null;
 904			}
 905		}
 906	}
 907
 908	private void attachFileToConversation(Conversation conversation, Uri uri) {
 909		prepareFileToast = Toast.makeText(getApplicationContext(),
 910				getText(R.string.preparing_file), Toast.LENGTH_LONG);
 911		prepareFileToast.show();
 912		xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
 913			@Override
 914			public void success(Message message) {
 915				hidePrepareFileToast();
 916				xmppConnectionService.sendMessage(message);
 917			}
 918
 919			@Override
 920			public void error(int errorCode, Message message) {
 921				displayErrorDialog(errorCode);
 922			}
 923
 924			@Override
 925			public void userInputRequried(PendingIntent pi, Message message) {
 926
 927			}
 928		});
 929	}
 930
 931	private void attachImageToConversation(Conversation conversation, Uri uri) {
 932		prepareFileToast = Toast.makeText(getApplicationContext(),
 933				getText(R.string.preparing_image), Toast.LENGTH_LONG);
 934		prepareFileToast.show();
 935		xmppConnectionService.attachImageToConversation(conversation, uri,
 936				new UiCallback<Message>() {
 937
 938					@Override
 939					public void userInputRequried(PendingIntent pi,
 940							Message object) {
 941						hidePrepareFileToast();
 942					}
 943
 944					@Override
 945					public void success(Message message) {
 946						xmppConnectionService.sendMessage(message);
 947					}
 948
 949					@Override
 950					public void error(int error, Message message) {
 951						hidePrepareFileToast();
 952						displayErrorDialog(error);
 953					}
 954				});
 955	}
 956
 957	private void hidePrepareFileToast() {
 958		if (prepareFileToast != null) {
 959			runOnUiThread(new Runnable() {
 960
 961				@Override
 962				public void run() {
 963					prepareFileToast.cancel();
 964				}
 965			});
 966		}
 967	}
 968
 969	public void updateConversationList() {
 970		xmppConnectionService
 971			.populateWithOrderedConversations(conversationList);
 972		listAdapter.notifyDataSetChanged();
 973	}
 974
 975	public void runIntent(PendingIntent pi, int requestCode) {
 976		try {
 977			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
 978					null, 0, 0, 0);
 979		} catch (final SendIntentException ignored) {
 980		}
 981	}
 982
 983	public void encryptTextMessage(Message message) {
 984		xmppConnectionService.getPgpEngine().encrypt(message,
 985				new UiCallback<Message>() {
 986
 987					@Override
 988					public void userInputRequried(PendingIntent pi,
 989							Message message) {
 990						ConversationActivity.this.runIntent(pi,
 991								ConversationActivity.REQUEST_SEND_MESSAGE);
 992					}
 993
 994					@Override
 995					public void success(Message message) {
 996						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
 997						xmppConnectionService.sendMessage(message);
 998					}
 999
1000					@Override
1001					public void error(int error, Message message) {
1002
1003					}
1004				});
1005	}
1006
1007	public boolean forceEncryption() {
1008		return getPreferences().getBoolean("force_encryption", false);
1009	}
1010
1011	public boolean useSendButtonToIndicateStatus() {
1012		return getPreferences().getBoolean("send_button_status", false);
1013	}
1014
1015	public boolean indicateReceived() {
1016		return getPreferences().getBoolean("indicate_received", false);
1017	}
1018
1019	@Override
1020	public void onAccountUpdate() {
1021		runOnUiThread(new Runnable() {
1022
1023			@Override
1024			public void run() {
1025				updateConversationList();
1026				ConversationActivity.this.mConversationFragment.updateMessages();
1027				updateActionBarTitle();
1028			}
1029		});
1030	}
1031
1032	@Override
1033	public void onConversationUpdate() {
1034		runOnUiThread(new Runnable() {
1035
1036			@Override
1037			public void run() {
1038				updateConversationList();
1039				if (conversationList.size() == 0) {
1040					startActivity(new Intent(getApplicationContext(),
1041								StartConversationActivity.class));
1042					finish();
1043				}
1044				ConversationActivity.this.mConversationFragment.updateMessages();
1045				updateActionBarTitle();
1046			}
1047		});
1048	}
1049
1050	@Override
1051	public void onRosterUpdate() {
1052		runOnUiThread(new Runnable() {
1053
1054			@Override
1055			public void run() {
1056				updateConversationList();
1057				ConversationActivity.this.mConversationFragment.updateMessages();
1058				updateActionBarTitle();
1059			}
1060		});
1061	}
1062
1063	@Override
1064	public void OnUpdateBlocklist(Status status) {
1065		runOnUiThread(new Runnable() {
1066			@Override
1067			public void run() {
1068				invalidateOptionsMenu();
1069				ConversationActivity.this.mConversationFragment.updateMessages();
1070			}
1071		});
1072	}
1073
1074	public void unblockConversation(final Blockable conversation) {
1075		xmppConnectionService.sendUnblockRequest(conversation);
1076	}
1077
1078	public boolean enterIsSend() {
1079		return getPreferences().getBoolean("enter_is_send",false);
1080	}
1081}