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