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 VIEW_CONVERSATION = "viewConversation";
  52	public static final String CONVERSATION = "conversationUuid";
  53	public static final String TEXT = "text";
  54	public static final String NICK = "nick";
  55
  56	public static final int REQUEST_SEND_MESSAGE = 0x0201;
  57	public static final int REQUEST_DECRYPT_PGP = 0x0202;
  58	public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
  59	private static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
  60	private static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
  61	private static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
  62	private static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
  63	private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
  64	private static final String STATE_PANEL_OPEN = "state_panel_open";
  65	private static final String STATE_PENDING_URI = "state_pending_uri";
  66
  67	private String mOpenConverstaion = null;
  68	private boolean mPanelOpen = true;
  69	private Uri mPendingImageUri = null;
  70	private Uri mPendingFileUri = null;
  71
  72	private View mContentView;
  73
  74	private List<Conversation> conversationList = new ArrayList<>();
  75	private Conversation mSelectedConversation = null;
  76	private ListView listView;
  77	private ConversationFragment mConversationFragment;
  78
  79	private ArrayAdapter<Conversation> listAdapter;
  80
  81	private Toast prepareFileToast;
  82
  83	private boolean mActivityPaused = false;
  84
  85	public Conversation getSelectedConversation() {
  86		return this.mSelectedConversation;
  87	}
  88
  89	public void setSelectedConversation(Conversation conversation) {
  90		this.mSelectedConversation = conversation;
  91	}
  92
  93	public void showConversationsOverview() {
  94		if (mContentView instanceof SlidingPaneLayout) {
  95			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
  96			mSlidingPaneLayout.openPane();
  97		}
  98	}
  99
 100	@Override
 101	protected String getShareableUri() {
 102		Conversation conversation = getSelectedConversation();
 103		if (conversation != null) {
 104			return conversation.getAccount().getShareableUri();
 105		} else {
 106			return "";
 107		}
 108	}
 109
 110	public void hideConversationsOverview() {
 111		if (mContentView instanceof SlidingPaneLayout) {
 112			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 113			mSlidingPaneLayout.closePane();
 114		}
 115	}
 116
 117	public boolean isConversationsOverviewHideable() {
 118		if (mContentView instanceof SlidingPaneLayout) {
 119			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 120			return mSlidingPaneLayout.isSlideable();
 121		} else {
 122			return false;
 123		}
 124	}
 125
 126	public boolean isConversationsOverviewVisable() {
 127		if (mContentView instanceof SlidingPaneLayout) {
 128			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 129			return mSlidingPaneLayout.isOpen();
 130		} else {
 131			return true;
 132		}
 133	}
 134
 135	@Override
 136	protected void onCreate(Bundle savedInstanceState) {
 137		super.onCreate(savedInstanceState);
 138		if (savedInstanceState != null) {mOpenConverstaion = savedInstanceState.getString(
 139				STATE_OPEN_CONVERSATION, null);
 140		mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
 141		String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
 142		if (pending != null) {
 143			mPendingImageUri = Uri.parse(pending);
 144		}
 145		}
 146
 147		setContentView(R.layout.fragment_conversations_overview);
 148
 149		this.mConversationFragment = new ConversationFragment();
 150		FragmentTransaction transaction = getFragmentManager().beginTransaction();
 151		transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
 152		transaction.commit();
 153
 154		listView = (ListView) findViewById(R.id.list);
 155		this.listAdapter = new ConversationAdapter(this, conversationList);
 156		listView.setAdapter(this.listAdapter);
 157
 158		if (getActionBar() != null) {
 159			getActionBar().setDisplayHomeAsUpEnabled(false);
 160			getActionBar().setHomeButtonEnabled(false);
 161		}
 162
 163		listView.setOnItemClickListener(new OnItemClickListener() {
 164
 165			@Override
 166			public void onItemClick(AdapterView<?> arg0, View clickedView,
 167					int position, long arg3) {
 168				if (getSelectedConversation() != conversationList.get(position)) {
 169					setSelectedConversation(conversationList.get(position));
 170					ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 171				}
 172				hideConversationsOverview();
 173			}
 174		});
 175		mContentView = findViewById(R.id.content_view_spl);
 176		if (mContentView == null) {
 177			mContentView = findViewById(R.id.content_view_ll);
 178		}
 179		if (mContentView instanceof SlidingPaneLayout) {
 180			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
 181			mSlidingPaneLayout.setParallaxDistance(150);
 182			mSlidingPaneLayout
 183				.setShadowResource(R.drawable.es_slidingpane_shadow);
 184			mSlidingPaneLayout.setSliderFadeColor(0);
 185			mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
 186
 187				@Override
 188				public void onPanelOpened(View arg0) {
 189					updateActionBarTitle();
 190					invalidateOptionsMenu();
 191					hideKeyboard();
 192					if (xmppConnectionServiceBound) {
 193						xmppConnectionService.getNotificationService()
 194							.setOpenConversation(null);
 195					}
 196					closeContextMenu();
 197				}
 198
 199				@Override
 200				public void onPanelClosed(View arg0) {
 201					openConversation();
 202				}
 203
 204				@Override
 205				public void onPanelSlide(View arg0, float arg1) {
 206					// TODO Auto-generated method stub
 207
 208				}
 209			});
 210		}
 211	}
 212
 213	@Override
 214	public void switchToConversation(Conversation conversation) {
 215		setSelectedConversation(conversation);
 216		runOnUiThread(new Runnable() {
 217			@Override
 218			public void run() {
 219				ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
 220				openConversation();
 221			}
 222		});
 223	}
 224
 225	private void updateActionBarTitle() {
 226		updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
 227	}
 228
 229	private void updateActionBarTitle(boolean titleShouldBeName) {
 230		final ActionBar ab = getActionBar();
 231		final Conversation conversation = getSelectedConversation();
 232		if (ab != null) {
 233			if (titleShouldBeName && conversation != null) {
 234				ab.setDisplayHomeAsUpEnabled(true);
 235				ab.setHomeButtonEnabled(true);
 236				if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
 237					ab.setTitle(conversation.getName());
 238				} else {
 239					ab.setTitle(conversation.getJid().toBareJid().toString());
 240				}
 241			} else {
 242				ab.setDisplayHomeAsUpEnabled(false);
 243				ab.setHomeButtonEnabled(false);
 244				ab.setTitle(R.string.app_name);
 245			}
 246		}
 247	}
 248
 249	private void openConversation() {
 250		this.updateActionBarTitle();
 251		this.invalidateOptionsMenu();
 252		if (xmppConnectionServiceBound) {
 253			final Conversation conversation = getSelectedConversation();
 254			xmppConnectionService.getNotificationService().setOpenConversation(conversation);
 255			sendReadMarkerIfNecessary(conversation);
 256		}
 257	}
 258
 259	public void sendReadMarkerIfNecessary(final Conversation conversation) {
 260		if (!mActivityPaused && !conversation.isRead()) {
 261			xmppConnectionService.sendReadMarker(conversation);
 262		}
 263	}
 264
 265	@Override
 266	public boolean onCreateOptionsMenu(Menu menu) {
 267		getMenuInflater().inflate(R.menu.conversations, menu);
 268		final MenuItem menuSecure = menu.findItem(R.id.action_security);
 269		final MenuItem menuArchive = menu.findItem(R.id.action_archive);
 270		final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
 271		final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
 272		final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
 273		final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
 274		final MenuItem menuAdd = menu.findItem(R.id.action_add);
 275		final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
 276		final MenuItem menuMute = menu.findItem(R.id.action_mute);
 277		final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
 278		final MenuItem menuBlock = menu.findItem(R.id.action_block);
 279		final MenuItem menuUnblock = menu.findItem(R.id.action_unblock);
 280
 281		if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
 282			menuArchive.setVisible(false);
 283			menuMucDetails.setVisible(false);
 284			menuContactDetails.setVisible(false);
 285			menuSecure.setVisible(false);
 286			menuInviteContact.setVisible(false);
 287			menuAttach.setVisible(false);
 288			menuClearHistory.setVisible(false);
 289			menuMute.setVisible(false);
 290			menuUnmute.setVisible(false);
 291			menuBlock.setVisible(false);
 292			menuUnblock.setVisible(false);
 293		} else {
 294			menuAdd.setVisible(!isConversationsOverviewHideable());
 295			if (this.getSelectedConversation() != null) {
 296				if (this.getSelectedConversation().getLatestMessage()
 297						.getEncryption() != Message.ENCRYPTION_NONE) {
 298					menuSecure.setIcon(R.drawable.ic_action_secure);
 299						}
 300				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
 301					menuContactDetails.setVisible(false);
 302					menuAttach.setVisible(false);
 303					menuBlock.setVisible(false);
 304					menuUnblock.setVisible(false);
 305					menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
 306				} else {
 307					menuMucDetails.setVisible(false);
 308					if (this.getSelectedConversation().isBlocked()) {
 309						menuBlock.setVisible(false);
 310					} else {
 311						menuUnblock.setVisible(false);
 312					}
 313					final Account account = this.getSelectedConversation().getAccount();
 314					if (!(account.isOnlineAndConnected() && account.getXmppConnection().getFeatures().blocking())) {
 315						menuBlock.setVisible(false);
 316						menuUnblock.setVisible(false);
 317					}
 318				}
 319				if (this.getSelectedConversation().isMuted()) {
 320					menuMute.setVisible(false);
 321				} else {
 322					menuUnmute.setVisible(false);
 323				}
 324			}
 325		}
 326		return true;
 327	}
 328
 329	private void selectPresenceToAttachFile(final int attachmentChoice) {
 330		selectPresence(getSelectedConversation(), new OnPresenceSelected() {
 331
 332			@Override
 333			public void onPresenceSelected() {
 334				Intent intent = new Intent();
 335				boolean chooser = false;
 336				switch (attachmentChoice) {
 337					case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
 338						intent.setAction(Intent.ACTION_GET_CONTENT);
 339						intent.setType("image/*");
 340						chooser = true;
 341						break;
 342					case ATTACHMENT_CHOICE_TAKE_PHOTO:
 343						mPendingImageUri = xmppConnectionService.getFileBackend().getTakePhotoUri();
 344						intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
 345						intent.putExtra(MediaStore.EXTRA_OUTPUT,mPendingImageUri);
 346						break;
 347					case ATTACHMENT_CHOICE_CHOOSE_FILE:
 348						chooser = true;
 349						intent.setType("*/*");
 350						intent.addCategory(Intent.CATEGORY_OPENABLE);
 351						intent.setAction(Intent.ACTION_GET_CONTENT);
 352						break;
 353					case ATTACHMENT_CHOICE_RECORD_VOICE:
 354						intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
 355						break;
 356				}
 357				if (intent.resolveActivity(getPackageManager()) != null) {
 358					if (chooser) {
 359						startActivityForResult(
 360								Intent.createChooser(intent,getString(R.string.perform_action_with)),
 361								attachmentChoice);
 362					} else {
 363						startActivityForResult(intent, attachmentChoice);
 364					}
 365				}
 366			}
 367		});
 368	}
 369
 370	private void attachFile(final int attachmentChoice) {
 371		final Conversation conversation = getSelectedConversation();
 372		if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
 373			if (hasPgp()) {
 374				if (conversation.getContact().getPgpKeyId() != 0) {
 375					xmppConnectionService.getPgpEngine().hasKey(
 376							conversation.getContact(),
 377							new UiCallback<Contact>() {
 378
 379								@Override
 380								public void userInputRequried(PendingIntent pi,
 381										Contact contact) {
 382									ConversationActivity.this.runIntent(pi,
 383											attachmentChoice);
 384								}
 385
 386								@Override
 387								public void success(Contact contact) {
 388									selectPresenceToAttachFile(attachmentChoice);
 389								}
 390
 391								@Override
 392								public void error(int error, Contact contact) {
 393									displayErrorDialog(error);
 394								}
 395							});
 396				} else {
 397					final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 398						.findFragmentByTag("conversation");
 399					if (fragment != null) {
 400						fragment.showNoPGPKeyDialog(false,
 401								new OnClickListener() {
 402
 403									@Override
 404									public void onClick(DialogInterface dialog,
 405											int which) {
 406										conversation
 407											.setNextEncryption(Message.ENCRYPTION_NONE);
 408										xmppConnectionService.databaseBackend
 409											.updateConversation(conversation);
 410										selectPresenceToAttachFile(attachmentChoice);
 411									}
 412								});
 413					}
 414				}
 415			} else {
 416				showInstallPgpDialog();
 417			}
 418		} else if (getSelectedConversation().getNextEncryption(
 419					forceEncryption()) == Message.ENCRYPTION_NONE) {
 420			selectPresenceToAttachFile(attachmentChoice);
 421		} else {
 422			selectPresenceToAttachFile(attachmentChoice);
 423		}
 424	}
 425
 426	@Override
 427	public boolean onOptionsItemSelected(final MenuItem item) {
 428		if (item.getItemId() == android.R.id.home) {
 429			showConversationsOverview();
 430			return true;
 431		} else if (item.getItemId() == R.id.action_add) {
 432			startActivity(new Intent(this, StartConversationActivity.class));
 433			return true;
 434		} else if (getSelectedConversation() != null) {
 435			switch (item.getItemId()) {
 436				case R.id.action_attach_file:
 437					attachFileDialog();
 438					break;
 439				case R.id.action_archive:
 440					this.endConversation(getSelectedConversation());
 441					break;
 442				case R.id.action_contact_details:
 443					Contact contact = this.getSelectedConversation().getContact();
 444					if (contact.showInRoster()) {
 445						switchToContactDetails(contact);
 446					} else {
 447						showAddToRosterDialog(getSelectedConversation());
 448					}
 449					break;
 450				case R.id.action_muc_details:
 451					Intent intent = new Intent(this,
 452							ConferenceDetailsActivity.class);
 453					intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
 454					intent.putExtra("uuid", getSelectedConversation().getUuid());
 455					startActivity(intent);
 456					break;
 457				case R.id.action_invite:
 458					inviteToConversation(getSelectedConversation());
 459					break;
 460				case R.id.action_security:
 461					selectEncryptionDialog(getSelectedConversation());
 462					break;
 463				case R.id.action_clear_history:
 464					clearHistoryDialog(getSelectedConversation());
 465					break;
 466				case R.id.action_mute:
 467					muteConversationDialog(getSelectedConversation());
 468					break;
 469				case R.id.action_unmute:
 470					unmuteConversation(getSelectedConversation());
 471					break;
 472				case R.id.action_block:
 473					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 474					break;
 475				case R.id.action_unblock:
 476					BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
 477					break;
 478				default:
 479					break;
 480			}
 481			return super.onOptionsItemSelected(item);
 482		} else {
 483			return super.onOptionsItemSelected(item);
 484		}
 485	}
 486
 487	public void endConversation(Conversation conversation) {
 488		showConversationsOverview();
 489		xmppConnectionService.archiveConversation(conversation);
 490		if (conversationList.size() > 0) {
 491			setSelectedConversation(conversationList.get(0));
 492			this.mConversationFragment.reInit(getSelectedConversation());
 493		} else {
 494			setSelectedConversation(null);
 495		}
 496	}
 497
 498	@SuppressLint("InflateParams")
 499	protected void clearHistoryDialog(final Conversation conversation) {
 500		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 501		builder.setTitle(getString(R.string.clear_conversation_history));
 502		View dialogView = getLayoutInflater().inflate(
 503				R.layout.dialog_clear_history, null);
 504		final CheckBox endConversationCheckBox = (CheckBox) dialogView
 505			.findViewById(R.id.end_conversation_checkbox);
 506		builder.setView(dialogView);
 507		builder.setNegativeButton(getString(R.string.cancel), null);
 508		builder.setPositiveButton(getString(R.string.delete_messages),
 509				new OnClickListener() {
 510
 511					@Override
 512					public void onClick(DialogInterface dialog, int which) {
 513						ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
 514						if (endConversationCheckBox.isChecked()) {
 515							endConversation(conversation);
 516						} else {
 517							updateConversationList();
 518							ConversationActivity.this.mConversationFragment.updateMessages();
 519						}
 520					}
 521				});
 522		builder.create().show();
 523	}
 524
 525	protected void attachFileDialog() {
 526		View menuAttachFile = findViewById(R.id.action_attach_file);
 527		if (menuAttachFile == null) {
 528			return;
 529		}
 530		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
 531		attachFilePopup.inflate(R.menu.attachment_choices);
 532		if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
 533			attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
 534		}
 535		attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 536
 537				@Override
 538				public boolean onMenuItemClick(MenuItem item) {
 539					switch (item.getItemId()) {
 540						case R.id.attach_choose_picture:
 541							attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
 542							break;
 543						case R.id.attach_take_picture:
 544							attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
 545							break;
 546						case R.id.attach_choose_file:
 547							attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
 548							break;
 549						case R.id.attach_record_voice:
 550							attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
 551							break;
 552					}
 553					return false;
 554				}
 555			});
 556		attachFilePopup.show();
 557	}
 558
 559	public void verifyOtrSessionDialog(final Conversation conversation, View view) {
 560		if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
 561			Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
 562			return;
 563		}
 564		if (view == null) {
 565			return;
 566		}
 567		PopupMenu popup = new PopupMenu(this, view);
 568		popup.inflate(R.menu.verification_choices);
 569		popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 570			@Override
 571			public boolean onMenuItemClick(MenuItem menuItem) {
 572				Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
 573				intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
 574				intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
 575				intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
 576				switch (menuItem.getItemId()) {
 577					case R.id.scan_fingerprint:
 578						intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
 579						break;
 580					case R.id.ask_question:
 581						intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
 582						break;
 583					case R.id.manual_verification:
 584						intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
 585						break;
 586				}
 587				startActivity(intent);
 588				return true;
 589			}
 590		});
 591		popup.show();
 592	}
 593
 594	protected void selectEncryptionDialog(final Conversation conversation) {
 595		View menuItemView = findViewById(R.id.action_security);
 596		if (menuItemView == null) {
 597			return;
 598		}
 599		PopupMenu popup = new PopupMenu(this, menuItemView);
 600		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
 601			.findFragmentByTag("conversation");
 602		if (fragment != null) {
 603			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
 604
 605				@Override
 606				public boolean onMenuItemClick(MenuItem item) {
 607					switch (item.getItemId()) {
 608						case R.id.encryption_choice_none:
 609							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 610							item.setChecked(true);
 611							break;
 612						case R.id.encryption_choice_otr:
 613							conversation.setNextEncryption(Message.ENCRYPTION_OTR);
 614							item.setChecked(true);
 615							break;
 616						case R.id.encryption_choice_pgp:
 617							if (hasPgp()) {
 618								if (conversation.getAccount().getKeys()
 619										.has("pgp_signature")) {
 620									conversation
 621										.setNextEncryption(Message.ENCRYPTION_PGP);
 622									item.setChecked(true);
 623								} else {
 624									announcePgp(conversation.getAccount(),
 625											conversation);
 626								}
 627							} else {
 628								showInstallPgpDialog();
 629							}
 630							break;
 631						default:
 632							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
 633							break;
 634					}
 635					xmppConnectionService.databaseBackend
 636						.updateConversation(conversation);
 637					fragment.updateChatMsgHint();
 638					return true;
 639				}
 640			});
 641			popup.inflate(R.menu.encryption_choices);
 642			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
 643			MenuItem none = popup.getMenu().findItem(
 644					R.id.encryption_choice_none);
 645			if (conversation.getMode() == Conversation.MODE_MULTI) {
 646				otr.setEnabled(false);
 647			} else {
 648				if (forceEncryption()) {
 649					none.setVisible(false);
 650				}
 651			}
 652			switch (conversation.getNextEncryption(forceEncryption())) {
 653				case Message.ENCRYPTION_NONE:
 654					none.setChecked(true);
 655					break;
 656				case Message.ENCRYPTION_OTR:
 657					otr.setChecked(true);
 658					break;
 659				case Message.ENCRYPTION_PGP:
 660					popup.getMenu().findItem(R.id.encryption_choice_pgp)
 661						.setChecked(true);
 662					break;
 663				default:
 664					popup.getMenu().findItem(R.id.encryption_choice_none)
 665						.setChecked(true);
 666					break;
 667			}
 668			popup.show();
 669		}
 670	}
 671
 672	protected void muteConversationDialog(final Conversation conversation) {
 673		AlertDialog.Builder builder = new AlertDialog.Builder(this);
 674		builder.setTitle(R.string.disable_notifications);
 675		final int[] durations = getResources().getIntArray(
 676				R.array.mute_options_durations);
 677		builder.setItems(R.array.mute_options_descriptions,
 678				new OnClickListener() {
 679
 680					@Override
 681					public void onClick(final DialogInterface dialog, final int which) {
 682						final long till;
 683						if (durations[which] == -1) {
 684							till = Long.MAX_VALUE;
 685						} else {
 686							till = SystemClock.elapsedRealtime()
 687								+ (durations[which] * 1000);
 688						}
 689						conversation.setMutedTill(till);
 690						ConversationActivity.this.xmppConnectionService.databaseBackend
 691							.updateConversation(conversation);
 692						updateConversationList();
 693						ConversationActivity.this.mConversationFragment.updateMessages();
 694						invalidateOptionsMenu();
 695					}
 696				});
 697		builder.create().show();
 698	}
 699
 700	public void unmuteConversation(final Conversation conversation) {
 701		conversation.setMutedTill(0);
 702		this.xmppConnectionService.databaseBackend.updateConversation(conversation);
 703		updateConversationList();
 704		ConversationActivity.this.mConversationFragment.updateMessages();
 705		invalidateOptionsMenu();
 706	}
 707
 708	@Override
 709	public void onBackPressed() {
 710		if (!isConversationsOverviewVisable()) {
 711			showConversationsOverview();
 712		} else {
 713			moveTaskToBack(true);
 714		}
 715	}
 716
 717	@Override
 718	protected void onNewIntent(final Intent intent) {
 719		if (xmppConnectionServiceBound) {
 720			if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
 721				handleViewConversationIntent(intent);
 722			}
 723		} else {
 724			setIntent(intent);
 725		}
 726	}
 727
 728	@Override
 729	public void onStart() {
 730		super.onStart();
 731		if (this.xmppConnectionServiceBound) {
 732			this.onBackendConnected();
 733		}
 734		if (conversationList.size() >= 1) {
 735			this.onConversationUpdate();
 736		}
 737	}
 738
 739	@Override
 740	public void onPause() {
 741		super.onPause();
 742		this.mActivityPaused = true;
 743		if (this.xmppConnectionServiceBound) {
 744			this.xmppConnectionService.getNotificationService().setIsInForeground(false);
 745		}
 746	}
 747
 748	@Override
 749	public void onResume() {
 750		super.onResume();
 751		int theme = findTheme();
 752		if (this.mTheme != theme) {
 753			recreate();
 754		}
 755		this.mActivityPaused = false;
 756		if (this.xmppConnectionServiceBound) {
 757			this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 758		}
 759	}
 760
 761	@Override
 762	public void onSaveInstanceState(final Bundle savedInstanceState) {
 763		Conversation conversation = getSelectedConversation();
 764		if (conversation != null) {
 765			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
 766					conversation.getUuid());
 767		}
 768		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
 769				isConversationsOverviewVisable());
 770		if (this.mPendingImageUri != null) {
 771			savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUri.toString());
 772		}
 773		super.onSaveInstanceState(savedInstanceState);
 774	}
 775
 776	@Override
 777	void onBackendConnected() {
 778		this.xmppConnectionService.getNotificationService().setIsInForeground(true);
 779		updateConversationList();
 780		if (xmppConnectionService.getAccounts().size() == 0) {
 781			startActivity(new Intent(this, EditAccountActivity.class));
 782		} else if (conversationList.size() <= 0) {
 783			startActivity(new Intent(this, StartConversationActivity.class));
 784			finish();
 785		} else if (getIntent() != null
 786				&& VIEW_CONVERSATION.equals(getIntent().getType())) {
 787			handleViewConversationIntent(getIntent());
 788		} else if (mOpenConverstaion != null) {
 789			selectConversationByUuid(mOpenConverstaion);
 790			if (mPanelOpen) {
 791				showConversationsOverview();
 792			} else {
 793				if (isConversationsOverviewHideable()) {
 794					openConversation();
 795				}
 796			}
 797			this.mConversationFragment.reInit(getSelectedConversation());
 798			mOpenConverstaion = null;
 799		} else if (getSelectedConversation() != null) {
 800			this.mConversationFragment.updateMessages();
 801		} else {
 802			showConversationsOverview();
 803			mPendingImageUri = null;
 804			mPendingFileUri = null;
 805			setSelectedConversation(conversationList.get(0));
 806			this.mConversationFragment.reInit(getSelectedConversation());
 807		}
 808
 809		if (mPendingImageUri != null) {
 810			attachImageToConversation(getSelectedConversation(),mPendingImageUri);
 811			mPendingImageUri = null;
 812		} else if (mPendingFileUri != null) {
 813			attachFileToConversation(getSelectedConversation(),mPendingFileUri);
 814			mPendingFileUri = null;
 815		}
 816		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
 817		setIntent(new Intent());
 818	}
 819
 820	private void handleViewConversationIntent(Intent intent) {
 821		String uuid = (String) intent.getExtras().get(CONVERSATION);
 822		String text = intent.getExtras().getString(TEXT, "");
 823		String nick = intent.getExtras().getString(NICK,null);
 824		selectConversationByUuid(uuid);
 825		this.mConversationFragment.reInit(getSelectedConversation());
 826		if (nick!=null) {
 827			this.mConversationFragment.highlightInConference(nick);
 828		} else {
 829			this.mConversationFragment.appendText(text);
 830		}
 831		hideConversationsOverview();
 832		openConversation();
 833		if (mContentView instanceof SlidingPaneLayout) {
 834			updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
 835		}
 836	}
 837
 838	private void selectConversationByUuid(String uuid) {
 839		for (Conversation aConversationList : conversationList) {
 840			if (aConversationList.getUuid().equals(uuid)) {
 841				setSelectedConversation(aConversationList);
 842			}
 843		}
 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 void blockConversation(final Blockable conversation) {
1059		xmppConnectionService.sendBlockRequest(conversation);
1060	}
1061}