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