ConversationActivity.java

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