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