ConversationActivity.java

  1package eu.siacs.conversations.ui;
  2
  3import android.annotation.SuppressLint;
  4import android.app.ActionBar;
  5import android.app.AlertDialog;
  6import android.app.FragmentTransaction;
  7import android.app.PendingIntent;
  8import android.content.DialogInterface;
  9import android.content.DialogInterface.OnClickListener;
 10import android.content.Intent;
 11import android.content.IntentSender.SendIntentException;
 12import android.net.Uri;
 13import android.os.Bundle;
 14import android.os.SystemClock;
 15import android.provider.MediaStore;
 16import android.support.v4.widget.SlidingPaneLayout;
 17import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
 18import android.util.Log;
 19import android.view.KeyEvent;
 20import android.view.Menu;
 21import android.view.MenuItem;
 22import android.view.View;
 23import android.widget.AdapterView;
 24import android.widget.AdapterView.OnItemClickListener;
 25import android.widget.ArrayAdapter;
 26import android.widget.CheckBox;
 27import android.widget.ListView;
 28import android.widget.PopupMenu;
 29import android.widget.PopupMenu.OnMenuItemClickListener;
 30import android.widget.Toast;
 31
 32import java.util.ArrayList;
 33import java.util.List;
 34
 35import eu.siacs.conversations.Config;
 36import eu.siacs.conversations.R;
 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;
 45
 46public class ConversationActivity extends XmppActivity implements
 47		OnAccountUpdate, OnConversationUpdate, OnRosterUpdate {
 48
 49	public static final String VIEW_CONVERSATION = "viewConversation";
 50	public static final String CONVERSATION = "conversationUuid";
 51	public static final String TEXT = "text";
 52	public static final String PRESENCE = "eu.siacs.conversations.presence";
 53
 54	public static final int REQUEST_SEND_MESSAGE = 0x0201;
 55	public static final int REQUEST_DECRYPT_PGP = 0x0202;
 56	public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
 57	private static final int REQUEST_ATTACH_IMAGE_DIALOG = 0x0203;
 58	private static final int REQUEST_IMAGE_CAPTURE = 0x0204;
 59	private static final int REQUEST_RECORD_AUDIO = 0x0205;
 60	private static final int REQUEST_SEND_PGP_IMAGE = 0x0206;
 61	private static final int REQUEST_ATTACH_FILE_DIALOG = 0x0208;
 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 String STATE_OPEN_CONVERSATION = "state_open_conversation";
 66	private static final String STATE_PANEL_OPEN = "state_panel_open";
 67	private static final String STATE_PENDING_URI = "state_pending_uri";
 68
 69	private String mOpenConverstaion = null;
 70	private boolean mPanelOpen = true;
 71	private Uri mPendingImageUri = null;
 72	private Uri mPendingFileUri = null;
 73
 74	private View mContentView;
 75
 76	private List<Conversation> conversationList = new ArrayList<>();
 77	private Conversation selectedConversation = null;
 78	private ListView listView;
 79	private ConversationFragment mConversationFragment;
 80
 81	private ArrayAdapter<Conversation> listAdapter;
 82
 83	private Toast prepareImageToast;
 84
 85
 86	public List<Conversation> getConversationList() {
 87		return this.conversationList;
 88	}
 89
 90	public Conversation getSelectedConversation() {
 91		return this.selectedConversation;
 92	}
 93
 94	public void setSelectedConversation(Conversation conversation) {
 95		this.selectedConversation = conversation;
 96	}
 97
 98	public ListView getConversationListView() {
 99		return this.listView;
100	}
101
102	public void showConversationsOverview() {
103		if (mContentView instanceof SlidingPaneLayout) {
104			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
105			mSlidingPaneLayout.openPane();
106		}
107	}
108
109	@Override
110	protected String getShareableUri() {
111		Conversation conversation = getSelectedConversation();
112		if (conversation != null) {
113			return "xmpp:" + conversation.getAccount().getJid().toBareJid();
114		} else {
115			return "";
116		}
117	}
118
119	public void hideConversationsOverview() {
120		if (mContentView instanceof SlidingPaneLayout) {
121			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
122			mSlidingPaneLayout.closePane();
123		}
124	}
125
126	public boolean isConversationsOverviewHideable() {
127		if (mContentView instanceof SlidingPaneLayout) {
128			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
129			return mSlidingPaneLayout.isSlideable();
130		} else {
131			return false;
132		}
133	}
134
135	public boolean isConversationsOverviewVisable() {
136		if (mContentView instanceof SlidingPaneLayout) {
137			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
138			return mSlidingPaneLayout.isOpen();
139		} else {
140			return true;
141		}
142	}
143
144	@Override
145	protected void onCreate(Bundle savedInstanceState) {
146		super.onCreate(savedInstanceState);
147		if (savedInstanceState != null) {mOpenConverstaion = savedInstanceState.getString(
148					STATE_OPEN_CONVERSATION, null);
149			mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
150			String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
151			if (pending != null) {
152				mPendingImageUri = Uri.parse(pending);
153			}
154		}
155
156		setContentView(R.layout.fragment_conversations_overview);
157
158		this.mConversationFragment = new ConversationFragment();
159		FragmentTransaction transaction = getFragmentManager().beginTransaction();
160		transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
161		transaction.commit();
162
163		listView = (ListView) findViewById(R.id.list);
164		this.listAdapter = new ConversationAdapter(this, conversationList);
165		listView.setAdapter(this.listAdapter);
166
167		if (getActionBar() != null) {
168			getActionBar().setDisplayHomeAsUpEnabled(false);
169			getActionBar().setHomeButtonEnabled(false);
170		}
171
172		listView.setOnItemClickListener(new OnItemClickListener() {
173
174			@Override
175			public void onItemClick(AdapterView<?> arg0, View clickedView,
176									int position, long arg3) {
177				if (getSelectedConversation() != conversationList.get(position)) {
178					setSelectedConversation(conversationList.get(position));
179					ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
180				}
181				hideConversationsOverview();
182			}
183		});
184		mContentView = findViewById(R.id.content_view_spl);
185		if (mContentView == null) {
186			mContentView = findViewById(R.id.content_view_ll);
187		}
188		if (mContentView instanceof SlidingPaneLayout) {
189			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
190			mSlidingPaneLayout.setParallaxDistance(150);
191			mSlidingPaneLayout
192					.setShadowResource(R.drawable.es_slidingpane_shadow);
193			mSlidingPaneLayout.setSliderFadeColor(0);
194			mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
195
196				@Override
197				public void onPanelOpened(View arg0) {
198					ActionBar ab = getActionBar();
199					if (ab != null) {
200						ab.setDisplayHomeAsUpEnabled(false);
201						ab.setHomeButtonEnabled(false);
202						ab.setTitle(R.string.app_name);
203					}
204					invalidateOptionsMenu();
205					hideKeyboard();
206					if (xmppConnectionServiceBound) {
207						xmppConnectionService.getNotificationService()
208								.setOpenConversation(null);
209					}
210					closeContextMenu();
211				}
212
213				@Override
214				public void onPanelClosed(View arg0) {
215					openConversation();
216				}
217
218				@Override
219				public void onPanelSlide(View arg0, float arg1) {
220					// TODO Auto-generated method stub
221
222				}
223			});
224		}
225	}
226
227	public void openConversation() {
228		ActionBar ab = getActionBar();
229		if (ab != null) {
230			ab.setDisplayHomeAsUpEnabled(true);
231			ab.setHomeButtonEnabled(true);
232			if (getSelectedConversation().getMode() == Conversation.MODE_SINGLE
233					|| ConversationActivity.this
234					.useSubjectToIdentifyConference()) {
235				ab.setTitle(getSelectedConversation().getName());
236			} else {
237				ab.setTitle(getSelectedConversation().getContactJid().toBareJid().toString());
238			}
239		}
240		invalidateOptionsMenu();
241		if (xmppConnectionServiceBound) {
242			xmppConnectionService.getNotificationService().setOpenConversation(getSelectedConversation());
243			if (!getSelectedConversation().isRead()) {
244				xmppConnectionService.markRead(getSelectedConversation(), true);
245				listView.invalidateViews();
246			}
247		}
248	}
249
250	@Override
251	public boolean onCreateOptionsMenu(Menu menu) {
252		getMenuInflater().inflate(R.menu.conversations, menu);
253		MenuItem menuSecure = menu.findItem(R.id.action_security);
254		MenuItem menuArchive = menu.findItem(R.id.action_archive);
255		MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
256		MenuItem menuContactDetails = menu
257				.findItem(R.id.action_contact_details);
258		MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
259		MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
260		MenuItem menuAdd = menu.findItem(R.id.action_add);
261		MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
262		MenuItem menuMute = menu.findItem(R.id.action_mute);
263
264		if (isConversationsOverviewVisable()
265				&& isConversationsOverviewHideable()) {
266			menuArchive.setVisible(false);
267			menuMucDetails.setVisible(false);
268			menuContactDetails.setVisible(false);
269			menuSecure.setVisible(false);
270			menuInviteContact.setVisible(false);
271			menuAttach.setVisible(false);
272			menuClearHistory.setVisible(false);
273			menuMute.setVisible(false);
274		} else {
275			menuAdd.setVisible(!isConversationsOverviewHideable());
276			if (this.getSelectedConversation() != null) {
277				if (this.getSelectedConversation().getLatestMessage()
278						.getEncryption() != Message.ENCRYPTION_NONE) {
279					menuSecure.setIcon(R.drawable.ic_action_secure);
280				}
281				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
282					menuContactDetails.setVisible(false);
283					menuAttach.setVisible(false);
284				} else {
285					menuMucDetails.setVisible(false);
286					menuInviteContact.setVisible(false);
287				}
288			}
289		}
290		return true;
291	}
292
293	private void selectPresenceToAttachFile(final int attachmentChoice) {
294		selectPresence(getSelectedConversation(), new OnPresenceSelected() {
295
296			@Override
297			public void onPresenceSelected() {
298				if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_PHOTO) {
299					mPendingImageUri = xmppConnectionService.getFileBackend()
300							.getTakePhotoUri();
301					Intent takePictureIntent = new Intent(
302							MediaStore.ACTION_IMAGE_CAPTURE);
303					takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
304							mPendingImageUri);
305					if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
306						startActivityForResult(takePictureIntent,
307								REQUEST_IMAGE_CAPTURE);
308					}
309				} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
310					Intent attachFileIntent = new Intent();
311					attachFileIntent.setType("image/*");
312					attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
313					Intent chooser = Intent.createChooser(attachFileIntent,
314							getString(R.string.attach_file));
315					startActivityForResult(chooser, REQUEST_ATTACH_IMAGE_DIALOG);
316				} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_FILE) {
317					Intent attachFileIntent = new Intent();
318					//attachFileIntent.setType("file/*");
319					attachFileIntent.setType("*/*");
320					attachFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
321					attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
322					Intent chooser = Intent.createChooser(attachFileIntent,
323							getString(R.string.attach_file));
324					startActivityForResult(chooser, REQUEST_ATTACH_FILE_DIALOG);
325				}
326			}
327		});
328	}
329
330	private void attachFile(final int attachmentChoice) {
331		final Conversation conversation = getSelectedConversation();
332		if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
333			if (hasPgp()) {
334				if (conversation.getContact().getPgpKeyId() != 0) {
335					xmppConnectionService.getPgpEngine().hasKey(
336							conversation.getContact(),
337							new UiCallback<Contact>() {
338
339								@Override
340								public void userInputRequried(PendingIntent pi,
341															  Contact contact) {
342									ConversationActivity.this.runIntent(pi,
343											attachmentChoice);
344								}
345
346								@Override
347								public void success(Contact contact) {
348									selectPresenceToAttachFile(attachmentChoice);
349								}
350
351								@Override
352								public void error(int error, Contact contact) {
353									displayErrorDialog(error);
354								}
355							});
356				} else {
357					final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
358							.findFragmentByTag("conversation");
359					if (fragment != null) {
360						fragment.showNoPGPKeyDialog(false,
361								new OnClickListener() {
362
363									@Override
364									public void onClick(DialogInterface dialog,
365														int which) {
366										conversation
367												.setNextEncryption(Message.ENCRYPTION_NONE);
368										xmppConnectionService.databaseBackend
369												.updateConversation(conversation);
370										selectPresenceToAttachFile(attachmentChoice);
371									}
372								});
373					}
374				}
375			} else {
376				showInstallPgpDialog();
377			}
378		} else if (getSelectedConversation().getNextEncryption(
379				forceEncryption()) == Message.ENCRYPTION_NONE) {
380			selectPresenceToAttachFile(attachmentChoice);
381		} else {
382			selectPresenceToAttachFile(attachmentChoice);
383		}
384	}
385
386	@Override
387	public boolean onOptionsItemSelected(MenuItem item) {
388		if (item.getItemId() == android.R.id.home) {
389			showConversationsOverview();
390			return true;
391		} else if (item.getItemId() == R.id.action_add) {
392			startActivity(new Intent(this, StartConversationActivity.class));
393			return true;
394		} else if (getSelectedConversation() != null) {
395			switch (item.getItemId()) {
396				case R.id.action_attach_file:
397					attachFileDialog();
398					break;
399				case R.id.action_archive:
400					this.endConversation(getSelectedConversation());
401					break;
402				case R.id.action_contact_details:
403					Contact contact = this.getSelectedConversation().getContact();
404					if (contact.showInRoster()) {
405						switchToContactDetails(contact);
406					} else {
407						showAddToRosterDialog(getSelectedConversation());
408					}
409					break;
410				case R.id.action_muc_details:
411					Intent intent = new Intent(this,
412							ConferenceDetailsActivity.class);
413					intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
414					intent.putExtra("uuid", getSelectedConversation().getUuid());
415					startActivity(intent);
416					break;
417				case R.id.action_invite:
418					inviteToConversation(getSelectedConversation());
419					break;
420				case R.id.action_security:
421					selectEncryptionDialog(getSelectedConversation());
422					break;
423				case R.id.action_clear_history:
424					clearHistoryDialog(getSelectedConversation());
425					break;
426				case R.id.action_mute:
427					muteConversationDialog(getSelectedConversation());
428					break;
429				default:
430					break;
431			}
432			return super.onOptionsItemSelected(item);
433		} else {
434			return super.onOptionsItemSelected(item);
435		}
436	}
437
438	public void endConversation(Conversation conversation) {
439		conversation.setStatus(Conversation.STATUS_ARCHIVED);
440		showConversationsOverview();
441		xmppConnectionService.archiveConversation(conversation);
442		if (conversationList.size() > 0) {
443			setSelectedConversation(conversationList.get(0));
444			this.mConversationFragment.reInit(getSelectedConversation());
445		} else {
446			setSelectedConversation(null);
447		}
448	}
449
450	@SuppressLint("InflateParams")
451	protected void clearHistoryDialog(final Conversation conversation) {
452		AlertDialog.Builder builder = new AlertDialog.Builder(this);
453		builder.setTitle(getString(R.string.clear_conversation_history));
454		View dialogView = getLayoutInflater().inflate(
455				R.layout.dialog_clear_history, null);
456		final CheckBox endConversationCheckBox = (CheckBox) dialogView
457				.findViewById(R.id.end_conversation_checkbox);
458		builder.setView(dialogView);
459		builder.setNegativeButton(getString(R.string.cancel), null);
460		builder.setPositiveButton(getString(R.string.delete_messages),
461				new OnClickListener() {
462
463					@Override
464					public void onClick(DialogInterface dialog, int which) {
465						ConversationActivity.this.xmppConnectionService
466								.clearConversationHistory(conversation);
467						if (endConversationCheckBox.isChecked()) {
468							endConversation(conversation);
469						}
470					}
471				});
472		builder.create().show();
473	}
474
475	protected void attachFileDialog() {
476		View menuAttachFile = findViewById(R.id.action_attach_file);
477		if (menuAttachFile == null) {
478			return;
479		}
480		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
481		attachFilePopup.inflate(R.menu.attachment_choices);
482		attachFilePopup
483				.setOnMenuItemClickListener(new OnMenuItemClickListener() {
484
485					@Override
486					public boolean onMenuItemClick(MenuItem item) {
487						switch (item.getItemId()) {
488							case R.id.attach_choose_picture:
489								attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
490								break;
491							case R.id.attach_take_picture:
492								attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
493								break;
494							case R.id.attach_record_voice:
495								attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
496								break;
497						}
498						return false;
499					}
500				});
501		attachFilePopup.show();
502	}
503
504	protected void selectEncryptionDialog(final Conversation conversation) {
505		View menuItemView = findViewById(R.id.action_security);
506		if (menuItemView == null) {
507			return;
508		}
509		PopupMenu popup = new PopupMenu(this, menuItemView);
510		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
511				.findFragmentByTag("conversation");
512		if (fragment != null) {
513			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
514
515				@Override
516				public boolean onMenuItemClick(MenuItem item) {
517					switch (item.getItemId()) {
518						case R.id.encryption_choice_none:
519							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
520							item.setChecked(true);
521							break;
522						case R.id.encryption_choice_otr:
523							conversation.setNextEncryption(Message.ENCRYPTION_OTR);
524							item.setChecked(true);
525							break;
526						case R.id.encryption_choice_pgp:
527							if (hasPgp()) {
528								if (conversation.getAccount().getKeys()
529										.has("pgp_signature")) {
530									conversation
531											.setNextEncryption(Message.ENCRYPTION_PGP);
532									item.setChecked(true);
533								} else {
534									announcePgp(conversation.getAccount(),
535											conversation);
536								}
537							} else {
538								showInstallPgpDialog();
539							}
540							break;
541						default:
542							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
543							break;
544					}
545					xmppConnectionService.databaseBackend
546							.updateConversation(conversation);
547					fragment.updateChatMsgHint();
548					return true;
549				}
550			});
551			popup.inflate(R.menu.encryption_choices);
552			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
553			MenuItem none = popup.getMenu().findItem(
554					R.id.encryption_choice_none);
555			if (conversation.getMode() == Conversation.MODE_MULTI) {
556				otr.setEnabled(false);
557			} else {
558				if (forceEncryption()) {
559					none.setVisible(false);
560				}
561			}
562			switch (conversation.getNextEncryption(forceEncryption())) {
563				case Message.ENCRYPTION_NONE:
564					none.setChecked(true);
565					break;
566				case Message.ENCRYPTION_OTR:
567					otr.setChecked(true);
568					break;
569				case Message.ENCRYPTION_PGP:
570					popup.getMenu().findItem(R.id.encryption_choice_pgp)
571							.setChecked(true);
572					break;
573				default:
574					popup.getMenu().findItem(R.id.encryption_choice_none)
575							.setChecked(true);
576					break;
577			}
578			popup.show();
579		}
580	}
581
582	protected void muteConversationDialog(final Conversation conversation) {
583		AlertDialog.Builder builder = new AlertDialog.Builder(this);
584		builder.setTitle(R.string.disable_notifications_for_this_conversation);
585		final int[] durations = getResources().getIntArray(
586				R.array.mute_options_durations);
587		builder.setItems(R.array.mute_options_descriptions,
588				new OnClickListener() {
589
590					@Override
591					public void onClick(DialogInterface dialog, int which) {
592						long till;
593						if (durations[which] == -1) {
594							till = Long.MAX_VALUE;
595						} else {
596							till = SystemClock.elapsedRealtime()
597									+ (durations[which] * 1000);
598						}
599						conversation.setMutedTill(till);
600						ConversationActivity.this.xmppConnectionService.databaseBackend
601								.updateConversation(conversation);
602						ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
603								.findFragmentByTag("conversation");
604						if (selectedFragment != null) {
605							selectedFragment.updateMessages();
606						}
607					}
608				});
609		builder.create().show();
610	}
611
612	@Override
613	public boolean onKeyDown(final int keyCode, final KeyEvent event) {
614		if (keyCode == KeyEvent.KEYCODE_BACK) {
615			if (!isConversationsOverviewVisable()) {
616				showConversationsOverview();
617				return false;
618			}
619		}
620		return super.onKeyDown(keyCode, event);
621	}
622
623	@Override
624	protected void onNewIntent(final Intent intent) {
625		if (xmppConnectionServiceBound) {
626			if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
627				handleViewConversationIntent(intent);
628			}
629		} else {
630			setIntent(intent);
631		}
632	}
633
634	@Override
635	public void onStart() {
636		super.onStart();
637		if (this.xmppConnectionServiceBound) {
638			this.onBackendConnected();
639		}
640		if (conversationList.size() >= 1) {
641			this.onConversationUpdate();
642		}
643	}
644
645	@Override
646	public void onSaveInstanceState(final Bundle savedInstanceState) {
647		Conversation conversation = getSelectedConversation();
648		if (conversation != null) {
649			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
650					conversation.getUuid());
651		}
652		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
653				isConversationsOverviewVisable());
654		if (this.mPendingImageUri != null) {
655			savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUri.toString());
656		}
657		super.onSaveInstanceState(savedInstanceState);
658	}
659
660	@Override
661	void onBackendConnected() {
662		updateConversationList();
663		if (xmppConnectionService.getAccounts().size() == 0) {
664			startActivity(new Intent(this, EditAccountActivity.class));
665		} else if (conversationList.size() <= 0) {
666			startActivity(new Intent(this, StartConversationActivity.class));
667			finish();
668		} else if (getIntent() != null
669				&& VIEW_CONVERSATION.equals(getIntent().getType())) {
670			handleViewConversationIntent(getIntent());
671		} else if (mOpenConverstaion != null) {
672			selectConversationByUuid(mOpenConverstaion);
673			if (mPanelOpen) {
674				showConversationsOverview();
675			} else {
676				if (isConversationsOverviewHideable()) {
677					openConversation();
678				}
679			}
680			this.mConversationFragment.reInit(getSelectedConversation());
681			mOpenConverstaion = null;
682		} else if (getSelectedConversation() != null) {
683			this.mConversationFragment.updateMessages();
684		} else {
685			showConversationsOverview();
686			mPendingImageUri = null;
687			mPendingFileUri = null;
688			setSelectedConversation(conversationList.get(0));
689			this.mConversationFragment.reInit(getSelectedConversation());
690		}
691
692		if (mPendingImageUri != null) {
693			attachImageToConversation(getSelectedConversation(),mPendingImageUri);
694			mPendingImageUri = null;
695		} else if (mPendingFileUri != null) {
696			attachFileToConversation(getSelectedConversation(),mPendingFileUri);
697			mPendingFileUri = null;
698		}
699		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
700		setIntent(new Intent());
701	}
702
703	private void handleViewConversationIntent(Intent intent) {
704		String uuid = (String) intent.getExtras().get(CONVERSATION);
705		String text = intent.getExtras().getString(TEXT, "");
706		selectConversationByUuid(uuid);
707		this.mConversationFragment.reInit(getSelectedConversation());
708		this.mConversationFragment.appendText(text);
709		hideConversationsOverview();
710		if (mContentView instanceof SlidingPaneLayout) {
711			openConversation();
712		}
713	}
714
715	private void selectConversationByUuid(String uuid) {
716        for (Conversation aConversationList : conversationList) {
717            if (aConversationList.getUuid().equals(uuid)) {
718                setSelectedConversation(aConversationList);
719            }
720        }
721	}
722
723	@Override
724	protected void unregisterListeners() {
725		super.unregisterListeners();
726		xmppConnectionService.getNotificationService().setOpenConversation(null);
727	}
728
729	@Override
730	protected void onActivityResult(int requestCode, int resultCode,
731									final Intent data) {
732		super.onActivityResult(requestCode, resultCode, data);
733		if (resultCode == RESULT_OK) {
734			if (requestCode == REQUEST_DECRYPT_PGP) {
735				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
736						.findFragmentByTag("conversation");
737				if (selectedFragment != null) {
738					selectedFragment.hideSnackbar();
739					selectedFragment.updateMessages();
740				}
741			} else if (requestCode == REQUEST_ATTACH_IMAGE_DIALOG) {
742				mPendingImageUri = data.getData();
743				if (xmppConnectionServiceBound) {
744					attachImageToConversation(getSelectedConversation(),
745							mPendingImageUri);
746					mPendingImageUri = null;
747				}
748			} else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) {
749				mPendingFileUri = data.getData();
750				if (xmppConnectionServiceBound) {
751					attachFileToConversation(getSelectedConversation(),
752							mPendingFileUri);
753					mPendingFileUri = null;
754				}
755			} else if (requestCode == REQUEST_SEND_PGP_IMAGE) {
756
757			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
758				attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
759			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
760				attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
761			} else if (requestCode == REQUEST_ANNOUNCE_PGP) {
762				announcePgp(getSelectedConversation().getAccount(),
763						getSelectedConversation());
764			} else if (requestCode == REQUEST_ENCRYPT_MESSAGE) {
765				// encryptTextMessage();
766			} else if (requestCode == REQUEST_IMAGE_CAPTURE && mPendingImageUri != null) {
767				if (xmppConnectionServiceBound) {
768					attachImageToConversation(getSelectedConversation(),
769							mPendingImageUri);
770					mPendingImageUri = null;
771				}
772				Intent intent = new Intent(
773						Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
774				intent.setData(mPendingImageUri);
775				sendBroadcast(intent);
776			}
777		} else {
778			if (requestCode == REQUEST_IMAGE_CAPTURE) {
779				mPendingImageUri = null;
780			}
781		}
782	}
783
784	private void attachFileToConversation(Conversation conversation, Uri uri) {
785		xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
786			@Override
787			public void success(Message message) {
788				xmppConnectionService.sendMessage(message);
789			}
790
791			@Override
792			public void error(int errorCode, Message message) {
793
794			}
795
796			@Override
797			public void userInputRequried(PendingIntent pi, Message message) {
798
799			}
800		});
801	}
802
803	private void attachImageToConversation(Conversation conversation, Uri uri) {
804		prepareImageToast = Toast.makeText(getApplicationContext(),
805				getText(R.string.preparing_image), Toast.LENGTH_LONG);
806		prepareImageToast.show();
807		xmppConnectionService.attachImageToConversation(conversation, uri,
808				new UiCallback<Message>() {
809
810					@Override
811					public void userInputRequried(PendingIntent pi,
812												  Message object) {
813						hidePrepareImageToast();
814						ConversationActivity.this.runIntent(pi,
815								ConversationActivity.REQUEST_SEND_PGP_IMAGE);
816					}
817
818					@Override
819					public void success(Message message) {
820						xmppConnectionService.sendMessage(message);
821					}
822
823					@Override
824					public void error(int error, Message message) {
825						hidePrepareImageToast();
826						displayErrorDialog(error);
827					}
828				});
829	}
830
831	private void hidePrepareImageToast() {
832		if (prepareImageToast != null) {
833			runOnUiThread(new Runnable() {
834
835				@Override
836				public void run() {
837					prepareImageToast.cancel();
838				}
839			});
840		}
841	}
842
843	public void updateConversationList() {
844		xmppConnectionService
845				.populateWithOrderedConversations(conversationList);
846		listAdapter.notifyDataSetChanged();
847	}
848
849	public void runIntent(PendingIntent pi, int requestCode) {
850		try {
851			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
852					null, 0, 0, 0);
853		} catch (final SendIntentException ignored) {
854		}
855	}
856
857	public void encryptTextMessage(Message message) {
858		xmppConnectionService.getPgpEngine().encrypt(message,
859				new UiCallback<Message>() {
860
861					@Override
862					public void userInputRequried(PendingIntent pi,
863												  Message message) {
864						ConversationActivity.this.runIntent(pi,
865								ConversationActivity.REQUEST_SEND_MESSAGE);
866					}
867
868					@Override
869					public void success(Message message) {
870						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
871						xmppConnectionService.sendMessage(message);
872					}
873
874					@Override
875					public void error(int error, Message message) {
876
877					}
878				});
879	}
880
881	public boolean forceEncryption() {
882		return getPreferences().getBoolean("force_encryption", false);
883	}
884
885	public boolean useSendButtonToIndicateStatus() {
886		return getPreferences().getBoolean("send_button_status", false);
887	}
888
889	public boolean indicateReceived() {
890		return getPreferences().getBoolean("indicate_received", false);
891	}
892
893	@Override
894	public void onAccountUpdate() {
895		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
896				.findFragmentByTag("conversation");
897		if (fragment != null) {
898			runOnUiThread(new Runnable() {
899
900				@Override
901				public void run() {
902					fragment.updateMessages();
903				}
904			});
905		}
906	}
907
908	@Override
909	public void onConversationUpdate() {
910		runOnUiThread(new Runnable() {
911
912			@Override
913			public void run() {
914				updateConversationList();
915				if (conversationList.size() == 0) {
916					startActivity(new Intent(getApplicationContext(),
917							StartConversationActivity.class));
918					finish();
919				}
920				ConversationActivity.this.mConversationFragment.updateMessages();
921			}
922		});
923	}
924
925	@Override
926	public void onRosterUpdate() {
927		runOnUiThread(new Runnable() {
928
929				@Override
930				public void run() {
931					ConversationActivity.this.mConversationFragment.updateMessages();
932				}
933			});
934	}
935}