ConversationActivity.java

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