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
 284		if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
 285			menuArchive.setVisible(false);
 286			menuMucDetails.setVisible(false);
 287			menuContactDetails.setVisible(false);
 288			menuSecure.setVisible(false);
 289			menuInviteContact.setVisible(false);
 290			menuAttach.setVisible(false);
 291			menuClearHistory.setVisible(false);
 292			menuMute.setVisible(false);
 293			menuUnmute.setVisible(false);
 294		} else {
 295			menuAdd.setVisible(!isConversationsOverviewHideable());
 296			if (this.getSelectedConversation() != null) {
 297				if (this.getSelectedConversation().getLatestMessage()
 298						.getEncryption() != Message.ENCRYPTION_NONE) {
 299					menuSecure.setIcon(R.drawable.ic_action_secure);
 300						}
 301				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
 302					menuContactDetails.setVisible(false);
 303					menuAttach.setVisible(false);
 304					menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
 305				} else {
 306					menuMucDetails.setVisible(false);
 307					final Account account = this.getSelectedConversation().getAccount();
 308				}
 309				if (this.getSelectedConversation().isMuted()) {
 310					menuMute.setVisible(false);
 311				} else {
 312					menuUnmute.setVisible(false);
 313				}
 314			}
 315		}
 316		return true;
 317	}
 318
 319	private void selectPresenceToAttachFile(final int attachmentChoice) {
 320		selectPresence(getSelectedConversation(), new OnPresenceSelected() {
 321
 322			@Override
 323			public void onPresenceSelected() {
 324				Intent intent = new Intent();
 325				boolean chooser = false;
 326				switch (attachmentChoice) {
 327					case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
 328						intent.setAction(Intent.ACTION_GET_CONTENT);
 329						intent.setType("image/*");
 330						chooser = true;
 331						break;
 332					case ATTACHMENT_CHOICE_TAKE_PHOTO:
 333						mPendingImageUri = xmppConnectionService.getFileBackend().getTakePhotoUri();
 334						intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
 335						intent.putExtra(MediaStore.EXTRA_OUTPUT,mPendingImageUri);
 336						break;
 337					case ATTACHMENT_CHOICE_CHOOSE_FILE:
 338						chooser = true;
 339						intent.setType("*/*");
 340						intent.addCategory(Intent.CATEGORY_OPENABLE);
 341						intent.setAction(Intent.ACTION_GET_CONTENT);
 342						break;
 343					case ATTACHMENT_CHOICE_RECORD_VOICE:
 344						intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
 345						break;
 346				}
 347				if (intent.resolveActivity(getPackageManager()) != null) {
 348					if (chooser) {
 349						startActivityForResult(
 350								Intent.createChooser(intent,getString(R.string.perform_action_with)),
 351								attachmentChoice);
 352					} else {
 353						startActivityForResult(intent, attachmentChoice);
 354					}
 355				}
 356			}
 357		});
 358	}
 359
 360	private void attachFile(final int attachmentChoice) {
 361		final Conversation conversation = getSelectedConversation();
 362		if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
 363			if (hasPgp()) {
 364				if (conversation.getContact().getPgpKeyId() != 0) {
 365					xmppConnectionService.getPgpEngine().hasKey(
 366							conversation.getContact(),
 367							new UiCallback<Contact>() {
 368
 369								@Override
 370								public void userInputRequried(PendingIntent pi,
 371										Contact contact) {
 372									ConversationActivity.this.runIntent(pi,
 373											attachmentChoice);
 374								}
 375
 376								@Override
 377								public void success(Contact contact) {
 378									selectPresenceToAttachFile(attachmentChoice);
 379								}
 380
 381								@Override
 382								public void error(int error, Contact contact) {
 383									displayErrorDialog(error);
 384								}
 385							});
 386				} else {
 387					final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 388						.findFragmentByTag("conversation");
 389					if (fragment != null) {
 390						fragment.showNoPGPKeyDialog(false,
 391								new OnClickListener() {
 392
 393									@Override
 394									public void onClick(DialogInterface dialog,
 395											int which) {
 396										conversation
 397											.setNextEncryption(Message.ENCRYPTION_NONE);
 398										xmppConnectionService.databaseBackend
 399											.updateConversation(conversation);
 400										selectPresenceToAttachFile(attachmentChoice);
 401									}
 402								});
 403					}
 404				}
 405			} else {
 406				showInstallPgpDialog();
 407			}
 408		} else if (getSelectedConversation().getNextEncryption(
 409					forceEncryption()) == Message.ENCRYPTION_NONE) {
 410			selectPresenceToAttachFile(attachmentChoice);
 411		} else {
 412			selectPresenceToAttachFile(attachmentChoice);
 413		}
 414	}
 415
 416	@Override
 417	public boolean onOptionsItemSelected(final MenuItem item) {
 418		if (item.getItemId() == android.R.id.home) {
 419			showConversationsOverview();
 420			return true;
 421		} else if (item.getItemId() == R.id.action_add) {
 422			startActivity(new Intent(this, StartConversationActivity.class));
 423			return true;
 424		} else if (getSelectedConversation() != null) {
 425			switch (item.getItemId()) {
 426				case R.id.action_attach_file:
 427					attachFileDialog();
 428					break;
 429				case R.id.action_archive:
 430					this.endConversation(getSelectedConversation());
 431					break;
 432				case R.id.action_contact_details:
 433					switchToContactDetails(getSelectedConversation().getContact());
 434					break;
 435				case R.id.action_muc_details:
 436					Intent intent = new Intent(this,
 437							ConferenceDetailsActivity.class);
 438					intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
 439					intent.putExtra("uuid", getSelectedConversation().getUuid());
 440					startActivity(intent);
 441					break;
 442				case R.id.action_invite:
 443					inviteToConversation(getSelectedConversation());
 444					break;
 445				case R.id.action_security:
 446					selectEncryptionDialog(getSelectedConversation());
 447					break;
 448				case R.id.action_clear_history:
 449					clearHistoryDialog(getSelectedConversation());
 450					break;
 451				case R.id.action_mute:
 452					muteConversationDialog(getSelectedConversation());
 453					break;
 454				case R.id.action_unmute:
 455					unmuteConversation(getSelectedConversation());
 456					break;
 457				case R.id.action_block:
 458					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 459					break;
 460				case R.id.action_unblock:
 461					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 462					break;
 463				default:
 464					break;
 465			}
 466			return super.onOptionsItemSelected(item);
 467		} else {
 468			return super.onOptionsItemSelected(item);
 469		}
 470	}
 471
 472	public void endConversation(Conversation conversation) {
 473		showConversationsOverview();
 474		xmppConnectionService.archiveConversation(conversation);
 475		if (conversationList.size() > 0) {
 476			setSelectedConversation(conversationList.get(0));
 477			this.mConversationFragment.reInit(getSelectedConversation());
 478		} else {
 479			setSelectedConversation(null);
 480		}
 481	}
 482
 483	@SuppressLint("InflateParams")
 484	protected void clearHistoryDialog(final Conversation conversation) {
 485		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 486		builder.setTitle(getString(R.string.clear_conversation_history));
 487		View dialogView = getLayoutInflater().inflate(
 488				R.layout.dialog_clear_history, null);
 489		final CheckBox endConversationCheckBox = (CheckBox) dialogView
 490			.findViewById(R.id.end_conversation_checkbox);
 491		builder.setView(dialogView);
 492		builder.setNegativeButton(getString(R.string.cancel), null);
 493		builder.setPositiveButton(getString(R.string.delete_messages),
 494				new OnClickListener() {
 495
 496					@Override
 497					public void onClick(DialogInterface dialog, int which) {
 498						ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
 499						if (endConversationCheckBox.isChecked()) {
 500							endConversation(conversation);
 501						} else {
 502							updateConversationList();
 503							ConversationActivity.this.mConversationFragment.updateMessages();
 504						}
 505					}
 506				});
 507		builder.create().show();
 508	}
 509
 510	protected void attachFileDialog() {
 511		View menuAttachFile = findViewById(R.id.action_attach_file);
 512		if (menuAttachFile == null) {
 513			return;
 514		}
 515		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
 516		attachFilePopup.inflate(R.menu.attachment_choices);
 517		if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
 518			attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
 519		}
 520		attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 521
 522			@Override
 523			public boolean onMenuItemClick(MenuItem item) {
 524				switch (item.getItemId()) {
 525					case R.id.attach_choose_picture:
 526						attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
 527						break;
 528					case R.id.attach_take_picture:
 529						attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
 530						break;
 531					case R.id.attach_choose_file:
 532						attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
 533						break;
 534					case R.id.attach_record_voice:
 535						attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
 536						break;
 537				}
 538				return false;
 539			}
 540		});
 541		attachFilePopup.show();
 542	}
 543
 544	public void verifyOtrSessionDialog(final Conversation conversation, View view) {
 545		if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
 546			Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
 547			return;
 548		}
 549		if (view == null) {
 550			return;
 551		}
 552		PopupMenu popup = new PopupMenu(this, view);
 553		popup.inflate(R.menu.verification_choices);
 554		popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 555			@Override
 556			public boolean onMenuItemClick(MenuItem menuItem) {
 557				Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
 558				intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
 559				intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
 560				intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
 561				switch (menuItem.getItemId()) {
 562					case R.id.scan_fingerprint:
 563						intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
 564						break;
 565					case R.id.ask_question:
 566						intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
 567						break;
 568					case R.id.manual_verification:
 569						intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
 570						break;
 571				}
 572				startActivity(intent);
 573				return true;
 574			}
 575		});
 576		popup.show();
 577	}
 578
 579	protected void selectEncryptionDialog(final Conversation conversation) {
 580		View menuItemView = findViewById(R.id.action_security);
 581		if (menuItemView == null) {
 582			return;
 583		}
 584		PopupMenu popup = new PopupMenu(this, menuItemView);
 585		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 586			.findFragmentByTag("conversation");
 587		if (fragment != null) {
 588			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 589
 590				@Override
 591				public boolean onMenuItemClick(MenuItem item) {
 592					switch (item.getItemId()) {
 593						case R.id.encryption_choice_none:
 594							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 595							item.setChecked(true);
 596							break;
 597						case R.id.encryption_choice_otr:
 598							conversation.setNextEncryption(Message.ENCRYPTION_OTR);
 599							item.setChecked(true);
 600							break;
 601						case R.id.encryption_choice_pgp:
 602							if (hasPgp()) {
 603								if (conversation.getAccount().getKeys()
 604										.has("pgp_signature")) {
 605									conversation
 606										.setNextEncryption(Message.ENCRYPTION_PGP);
 607									item.setChecked(true);
 608								} else {
 609									announcePgp(conversation.getAccount(),
 610											conversation);
 611								}
 612							} else {
 613								showInstallPgpDialog();
 614							}
 615							break;
 616						default:
 617							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 618							break;
 619					}
 620					xmppConnectionService.databaseBackend
 621						.updateConversation(conversation);
 622					fragment.updateChatMsgHint();
 623					return true;
 624				}
 625			});
 626			popup.inflate(R.menu.encryption_choices);
 627			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
 628			MenuItem none = popup.getMenu().findItem(
 629					R.id.encryption_choice_none);
 630			if (conversation.getMode() == Conversation.MODE_MULTI) {
 631				otr.setEnabled(false);
 632			} else {
 633				if (forceEncryption()) {
 634					none.setVisible(false);
 635				}
 636			}
 637			switch (conversation.getNextEncryption(forceEncryption())) {
 638				case Message.ENCRYPTION_NONE:
 639					none.setChecked(true);
 640					break;
 641				case Message.ENCRYPTION_OTR:
 642					otr.setChecked(true);
 643					break;
 644				case Message.ENCRYPTION_PGP:
 645					popup.getMenu().findItem(R.id.encryption_choice_pgp)
 646						.setChecked(true);
 647					break;
 648				default:
 649					popup.getMenu().findItem(R.id.encryption_choice_none)
 650						.setChecked(true);
 651					break;
 652			}
 653			popup.show();
 654		}
 655	}
 656
 657	protected void muteConversationDialog(final Conversation conversation) {
 658		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 659		builder.setTitle(R.string.disable_notifications);
 660		final int[] durations = getResources().getIntArray(
 661				R.array.mute_options_durations);
 662		builder.setItems(R.array.mute_options_descriptions,
 663				new OnClickListener() {
 664
 665					@Override
 666					public void onClick(final DialogInterface dialog, final int which) {
 667						final long till;
 668						if (durations[which] == -1) {
 669							till = Long.MAX_VALUE;
 670						} else {
 671							till = SystemClock.elapsedRealtime()
 672								+ (durations[which] * 1000);
 673						}
 674						conversation.setMutedTill(till);
 675						ConversationActivity.this.xmppConnectionService.databaseBackend
 676							.updateConversation(conversation);
 677						updateConversationList();
 678						ConversationActivity.this.mConversationFragment.updateMessages();
 679						invalidateOptionsMenu();
 680					}
 681				});
 682		builder.create().show();
 683	}
 684
 685	public void unmuteConversation(final Conversation conversation) {
 686		conversation.setMutedTill(0);
 687		this.xmppConnectionService.databaseBackend.updateConversation(conversation);
 688		updateConversationList();
 689		ConversationActivity.this.mConversationFragment.updateMessages();
 690		invalidateOptionsMenu();
 691	}
 692
 693	@Override
 694	public void onBackPressed() {
 695		if (!isConversationsOverviewVisable()) {
 696			showConversationsOverview();
 697		} else {
 698			moveTaskToBack(true);
 699		}
 700	}
 701
 702	@Override
 703	protected void onNewIntent(final Intent intent) {
 704		if (xmppConnectionServiceBound) {
 705			if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
 706				handleViewConversationIntent(intent);
 707			}
 708		} else {
 709			setIntent(intent);
 710		}
 711	}
 712
 713	@Override
 714	public void onStart() {
 715		super.onStart();
 716		if (this.xmppConnectionServiceBound) {
 717			this.onBackendConnected();
 718		}
 719		if (conversationList.size() >= 1) {
 720			this.onConversationUpdate();
 721		}
 722	}
 723
 724	@Override
 725	public void onPause() {
 726		super.onPause();
 727		this.mActivityPaused = true;
 728		if (this.xmppConnectionServiceBound) {
 729			this.xmppConnectionService.getNotificationService().setIsInForeground(false);
 730		}
 731	}
 732
 733	@Override
 734	public void onResume() {
 735		super.onResume();
 736		final int theme = findTheme();
 737		final boolean usingEnterKey = usingEnterKey();
 738		if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
 739			recreate();
 740		}
 741		this.mActivityPaused = false;
 742		if (this.xmppConnectionServiceBound) {
 743			this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 744		}
 745		if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
 746			sendReadMarkerIfNecessary(getSelectedConversation());
 747		}
 748	}
 749
 750	@Override
 751	public void onSaveInstanceState(final Bundle savedInstanceState) {
 752		Conversation conversation = getSelectedConversation();
 753		if (conversation != null) {
 754			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
 755					conversation.getUuid());
 756		}
 757		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
 758				isConversationsOverviewVisable());
 759		if (this.mPendingImageUri != null) {
 760			savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUri.toString());
 761		}
 762		super.onSaveInstanceState(savedInstanceState);
 763	}
 764
 765	@Override
 766	void onBackendConnected() {
 767		this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 768		updateConversationList();
 769		if (xmppConnectionService.getAccounts().size() == 0) {
 770			startActivity(new Intent(this, EditAccountActivity.class));
 771		} else if (conversationList.size() <= 0) {
 772			startActivity(new Intent(this, StartConversationActivity.class));
 773			finish();
 774		} else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
 775			handleViewConversationIntent(getIntent());
 776		} else if (selectConversationByUuid(mOpenConverstaion)) {
 777			if (mPanelOpen) {
 778				showConversationsOverview();
 779			} else {
 780				if (isConversationsOverviewHideable()) {
 781					openConversation();
 782				}
 783			}
 784			this.mConversationFragment.reInit(getSelectedConversation());
 785			mOpenConverstaion = null;
 786		} else if (getSelectedConversation() != null) {
 787			this.mConversationFragment.updateMessages();
 788		} else {
 789			showConversationsOverview();
 790			mPendingImageUri = null;
 791			mPendingFileUri = null;
 792			setSelectedConversation(conversationList.get(0));
 793			this.mConversationFragment.reInit(getSelectedConversation());
 794		}
 795
 796		if (mPendingImageUri != null) {
 797			attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 798			mPendingImageUri = null;
 799		} else if (mPendingFileUri != null) {
 800			attachFileToConversation(getSelectedConversation(),mPendingFileUri);
 801			mPendingFileUri = null;
 802		}
 803		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
 804		setIntent(new Intent());
 805	}
 806
 807	private void handleViewConversationIntent(final Intent intent) {
 808		final String uuid = (String) intent.getExtras().get(CONVERSATION);
 809		final String downloadUuid = (String) intent.getExtras().get(MESSAGE);
 810		final String text = intent.getExtras().getString(TEXT, "");
 811		final String nick = intent.getExtras().getString(NICK, null);
 812		if (selectConversationByUuid(uuid)) {
 813			this.mConversationFragment.reInit(getSelectedConversation());
 814			if (nick != null) {
 815				this.mConversationFragment.highlightInConference(nick);
 816			} else {
 817				this.mConversationFragment.appendText(text);
 818			}
 819			hideConversationsOverview();
 820			openConversation();
 821			if (mContentView instanceof SlidingPaneLayout) {
 822				updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
 823			}
 824			if (downloadUuid != null) {
 825				final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
 826				if (message != null) {
 827					mConversationFragment.messageListAdapter.startDownloadable(message);
 828				}
 829			}
 830		}
 831	}
 832
 833	private boolean selectConversationByUuid(String uuid) {
 834		if (uuid == null) {
 835			return false;
 836		}
 837		for (Conversation aConversationList : conversationList) {
 838			if (aConversationList.getUuid().equals(uuid)) {
 839				setSelectedConversation(aConversationList);
 840				return true;
 841			}
 842		}
 843		return false;
 844	}
 845
 846	@Override
 847	protected void unregisterListeners() {
 848		super.unregisterListeners();
 849		xmppConnectionService.getNotificationService().setOpenConversation(null);
 850	}
 851
 852	@Override
 853	protected void onActivityResult(int requestCode, int resultCode,
 854			final Intent data) {
 855		super.onActivityResult(requestCode, resultCode, data);
 856		if (resultCode == RESULT_OK) {
 857			if (requestCode == REQUEST_DECRYPT_PGP) {
 858				mConversationFragment.hideSnackbar();
 859				mConversationFragment.updateMessages();
 860			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
 861				mPendingImageUri = data.getData();
 862				if (xmppConnectionServiceBound) {
 863					attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 864					mPendingImageUri = null;
 865				}
 866			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
 867				mPendingFileUri = data.getData();
 868				if (xmppConnectionServiceBound) {
 869					attachFileToConversation(getSelectedConversation(),mPendingFileUri);
 870					mPendingFileUri = null;
 871				}
 872			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO && mPendingImageUri != null) {
 873				if (xmppConnectionServiceBound) {
 874					attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 875					mPendingImageUri = null;
 876				}
 877				Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 878				intent.setData(mPendingImageUri);
 879				sendBroadcast(intent);
 880			}
 881		} else {
 882			if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
 883				mPendingImageUri = null;
 884			}
 885		}
 886	}
 887
 888	private void attachFileToConversation(Conversation conversation, Uri uri) {
 889		prepareFileToast = Toast.makeText(getApplicationContext(),
 890				getText(R.string.preparing_file), Toast.LENGTH_LONG);
 891		prepareFileToast.show();
 892		xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
 893			@Override
 894			public void success(Message message) {
 895				hidePrepareFileToast();
 896				xmppConnectionService.sendMessage(message);
 897			}
 898
 899			@Override
 900			public void error(int errorCode, Message message) {
 901				displayErrorDialog(errorCode);
 902			}
 903
 904			@Override
 905			public void userInputRequried(PendingIntent pi, Message message) {
 906
 907			}
 908		});
 909	}
 910
 911	private void attachImageToConversation(Conversation conversation, Uri uri) {
 912		prepareFileToast = Toast.makeText(getApplicationContext(),
 913				getText(R.string.preparing_image), Toast.LENGTH_LONG);
 914		prepareFileToast.show();
 915		xmppConnectionService.attachImageToConversation(conversation, uri,
 916				new UiCallback<Message>() {
 917
 918					@Override
 919					public void userInputRequried(PendingIntent pi,
 920							Message object) {
 921						hidePrepareFileToast();
 922					}
 923
 924					@Override
 925					public void success(Message message) {
 926						xmppConnectionService.sendMessage(message);
 927					}
 928
 929					@Override
 930					public void error(int error, Message message) {
 931						hidePrepareFileToast();
 932						displayErrorDialog(error);
 933					}
 934				});
 935	}
 936
 937	private void hidePrepareFileToast() {
 938		if (prepareFileToast != null) {
 939			runOnUiThread(new Runnable() {
 940
 941				@Override
 942				public void run() {
 943					prepareFileToast.cancel();
 944				}
 945			});
 946		}
 947	}
 948
 949	public void updateConversationList() {
 950		xmppConnectionService
 951			.populateWithOrderedConversations(conversationList);
 952		listAdapter.notifyDataSetChanged();
 953	}
 954
 955	public void runIntent(PendingIntent pi, int requestCode) {
 956		try {
 957			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
 958					null, 0, 0, 0);
 959		} catch (final SendIntentException ignored) {
 960		}
 961	}
 962
 963	public void encryptTextMessage(Message message) {
 964		xmppConnectionService.getPgpEngine().encrypt(message,
 965				new UiCallback<Message>() {
 966
 967					@Override
 968					public void userInputRequried(PendingIntent pi,
 969							Message message) {
 970						ConversationActivity.this.runIntent(pi,
 971								ConversationActivity.REQUEST_SEND_MESSAGE);
 972					}
 973
 974					@Override
 975					public void success(Message message) {
 976						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
 977						xmppConnectionService.sendMessage(message);
 978					}
 979
 980					@Override
 981					public void error(int error, Message message) {
 982
 983					}
 984				});
 985	}
 986
 987	public boolean forceEncryption() {
 988		return getPreferences().getBoolean("force_encryption", false);
 989	}
 990
 991	public boolean useSendButtonToIndicateStatus() {
 992		return getPreferences().getBoolean("send_button_status", false);
 993	}
 994
 995	public boolean indicateReceived() {
 996		return getPreferences().getBoolean("indicate_received", false);
 997	}
 998
 999	@Override
1000	public void onAccountUpdate() {
1001		runOnUiThread(new Runnable() {
1002
1003			@Override
1004			public void run() {
1005				updateConversationList();
1006				ConversationActivity.this.mConversationFragment.updateMessages();
1007				updateActionBarTitle();
1008			}
1009		});
1010	}
1011
1012	@Override
1013	public void onConversationUpdate() {
1014		runOnUiThread(new Runnable() {
1015
1016			@Override
1017			public void run() {
1018				updateConversationList();
1019				if (conversationList.size() == 0) {
1020					startActivity(new Intent(getApplicationContext(),
1021								StartConversationActivity.class));
1022					finish();
1023				}
1024				ConversationActivity.this.mConversationFragment.updateMessages();
1025				updateActionBarTitle();
1026			}
1027		});
1028	}
1029
1030	@Override
1031	public void onRosterUpdate() {
1032		runOnUiThread(new Runnable() {
1033
1034			@Override
1035			public void run() {
1036				updateConversationList();
1037				ConversationActivity.this.mConversationFragment.updateMessages();
1038				updateActionBarTitle();
1039			}
1040		});
1041	}
1042
1043	@Override
1044	public void OnUpdateBlocklist(Status status) {
1045		runOnUiThread(new Runnable() {
1046			@Override
1047			public void run() {
1048				invalidateOptionsMenu();
1049				ConversationActivity.this.mConversationFragment.updateMessages();
1050			}
1051		});
1052	}
1053
1054	public void unblockConversation(final Blockable conversation) {
1055		xmppConnectionService.sendUnblockRequest(conversation);
1056	}
1057
1058	public boolean enterIsSend() {
1059		return getPreferences().getBoolean("enter_is_send",false);
1060	}
1061}