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