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 = (MenuItem) menu.findItem(R.id.action_security);
245		MenuItem menuArchive = (MenuItem) menu.findItem(R.id.action_archive);
246		MenuItem menuMucDetails = (MenuItem) menu
247				.findItem(R.id.action_muc_details);
248		MenuItem menuContactDetails = (MenuItem) menu
249				.findItem(R.id.action_contact_details);
250		MenuItem menuAttach = (MenuItem) menu.findItem(R.id.action_attach_file);
251		MenuItem menuClearHistory = (MenuItem) menu
252				.findItem(R.id.action_clear_history);
253		MenuItem menuAdd = (MenuItem) menu.findItem(R.id.action_add);
254		MenuItem menuInviteContact = (MenuItem) menu
255				.findItem(R.id.action_invite);
256		MenuItem menuMute = (MenuItem) menu.findItem(R.id.action_mute);
257
258		if (isConversationsOverviewVisable()
259				&& isConversationsOverviewHideable()) {
260			menuArchive.setVisible(false);
261			menuMucDetails.setVisible(false);
262			menuContactDetails.setVisible(false);
263			menuSecure.setVisible(false);
264			menuInviteContact.setVisible(false);
265			menuAttach.setVisible(false);
266			menuClearHistory.setVisible(false);
267			menuMute.setVisible(false);
268		} else {
269			menuAdd.setVisible(!isConversationsOverviewHideable());
270			if (this.getSelectedConversation() != null) {
271				if (this.getSelectedConversation().getLatestMessage()
272						.getEncryption() != Message.ENCRYPTION_NONE) {
273					menuSecure.setIcon(R.drawable.ic_action_secure);
274				}
275				if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
276					menuContactDetails.setVisible(false);
277					menuAttach.setVisible(false);
278				} else {
279					menuMucDetails.setVisible(false);
280					menuInviteContact.setVisible(false);
281				}
282			}
283		}
284		return true;
285	}
286
287	private void selectPresenceToAttachFile(final int attachmentChoice) {
288		selectPresence(getSelectedConversation(), new OnPresenceSelected() {
289
290			@Override
291			public void onPresenceSelected() {
292				if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_PHOTO) {
293					pendingImageUri = xmppConnectionService.getFileBackend()
294							.getTakePhotoUri();
295					Intent takePictureIntent = new Intent(
296							MediaStore.ACTION_IMAGE_CAPTURE);
297					takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
298							pendingImageUri);
299					if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
300						startActivityForResult(takePictureIntent,
301								REQUEST_IMAGE_CAPTURE);
302					}
303				} else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
304					Intent attachFileIntent = new Intent();
305					attachFileIntent.setType("image/*");
306					attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
307					Intent chooser = Intent.createChooser(attachFileIntent,
308							getString(R.string.attach_file));
309					startActivityForResult(chooser, REQUEST_ATTACH_FILE_DIALOG);
310				} else if (attachmentChoice == ATTACHMENT_CHOICE_RECORD_VOICE) {
311					Intent intent = new Intent(
312							MediaStore.Audio.Media.RECORD_SOUND_ACTION);
313					startActivityForResult(intent, REQUEST_RECORD_AUDIO);
314				}
315			}
316		});
317	}
318
319	private void attachFile(final int attachmentChoice) {
320		final Conversation conversation = getSelectedConversation();
321		if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
322			if (hasPgp()) {
323				if (conversation.getContact().getPgpKeyId() != 0) {
324					xmppConnectionService.getPgpEngine().hasKey(
325							conversation.getContact(),
326							new UiCallback<Contact>() {
327
328								@Override
329								public void userInputRequried(PendingIntent pi,
330										Contact contact) {
331									ConversationActivity.this.runIntent(pi,
332											attachmentChoice);
333								}
334
335								@Override
336								public void success(Contact contact) {
337									selectPresenceToAttachFile(attachmentChoice);
338								}
339
340								@Override
341								public void error(int error, Contact contact) {
342									displayErrorDialog(error);
343								}
344							});
345				} else {
346					final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
347							.findFragmentByTag("conversation");
348					if (fragment != null) {
349						fragment.showNoPGPKeyDialog(false,
350								new OnClickListener() {
351
352									@Override
353									public void onClick(DialogInterface dialog,
354											int which) {
355										conversation
356												.setNextEncryption(Message.ENCRYPTION_NONE);
357										xmppConnectionService.databaseBackend
358												.updateConversation(conversation);
359										selectPresenceToAttachFile(attachmentChoice);
360									}
361								});
362					}
363				}
364			} else {
365				showInstallPgpDialog();
366			}
367		} else if (getSelectedConversation().getNextEncryption(
368				forceEncryption()) == Message.ENCRYPTION_NONE) {
369			selectPresenceToAttachFile(attachmentChoice);
370		} else {
371			selectPresenceToAttachFile(attachmentChoice);
372		}
373	}
374
375	@Override
376	public boolean onOptionsItemSelected(MenuItem item) {
377		if (item.getItemId() == android.R.id.home) {
378			showConversationsOverview();
379			return true;
380		} else if (item.getItemId() == R.id.action_add) {
381			startActivity(new Intent(this, StartConversationActivity.class));
382			return true;
383		} else if (getSelectedConversation() != null) {
384			switch (item.getItemId()) {
385			case R.id.action_attach_file:
386				attachFileDialog();
387				break;
388			case R.id.action_archive:
389				this.endConversation(getSelectedConversation());
390				break;
391			case R.id.action_contact_details:
392				Contact contact = this.getSelectedConversation().getContact();
393				if (contact.showInRoster()) {
394					switchToContactDetails(contact);
395				} else {
396					showAddToRosterDialog(getSelectedConversation());
397				}
398				break;
399			case R.id.action_muc_details:
400				Intent intent = new Intent(this,
401						ConferenceDetailsActivity.class);
402				intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
403				intent.putExtra("uuid", getSelectedConversation().getUuid());
404				startActivity(intent);
405				break;
406			case R.id.action_invite:
407				inviteToConversation(getSelectedConversation());
408				break;
409			case R.id.action_security:
410				selectEncryptionDialog(getSelectedConversation());
411				break;
412			case R.id.action_clear_history:
413				clearHistoryDialog(getSelectedConversation());
414				break;
415			case R.id.action_mute:
416				muteConversationDialog(getSelectedConversation());
417				break;
418			default:
419				break;
420			}
421			return super.onOptionsItemSelected(item);
422		} else {
423			return super.onOptionsItemSelected(item);
424		}
425	}
426
427	public void endConversation(Conversation conversation) {
428		conversation.setStatus(Conversation.STATUS_ARCHIVED);
429		paneShouldBeOpen = true;
430		showConversationsOverview();
431		xmppConnectionService.archiveConversation(conversation);
432		if (conversationList.size() > 0) {
433			setSelectedConversation(conversationList.get(0));
434		} else {
435			setSelectedConversation(null);
436		}
437	}
438
439	@SuppressLint("InflateParams")
440	protected void clearHistoryDialog(final Conversation conversation) {
441		AlertDialog.Builder builder = new AlertDialog.Builder(this);
442		builder.setTitle(getString(R.string.clear_conversation_history));
443		View dialogView = getLayoutInflater().inflate(
444				R.layout.dialog_clear_history, null);
445		final CheckBox endConversationCheckBox = (CheckBox) dialogView
446				.findViewById(R.id.end_conversation_checkbox);
447		builder.setView(dialogView);
448		builder.setNegativeButton(getString(R.string.cancel), null);
449		builder.setPositiveButton(getString(R.string.delete_messages),
450				new OnClickListener() {
451
452					@Override
453					public void onClick(DialogInterface dialog, int which) {
454						ConversationActivity.this.xmppConnectionService
455								.clearConversationHistory(conversation);
456						if (endConversationCheckBox.isChecked()) {
457							endConversation(conversation);
458						}
459					}
460				});
461		builder.create().show();
462	}
463
464	protected void attachFileDialog() {
465		View menuAttachFile = findViewById(R.id.action_attach_file);
466		if (menuAttachFile == null) {
467			return;
468		}
469		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
470		attachFilePopup.inflate(R.menu.attachment_choices);
471		attachFilePopup
472				.setOnMenuItemClickListener(new OnMenuItemClickListener() {
473
474					@Override
475					public boolean onMenuItemClick(MenuItem item) {
476						switch (item.getItemId()) {
477						case R.id.attach_choose_picture:
478							attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
479							break;
480						case R.id.attach_take_picture:
481							attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
482							break;
483						case R.id.attach_record_voice:
484							attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
485							break;
486						}
487						return false;
488					}
489				});
490		attachFilePopup.show();
491	}
492
493	protected void selectEncryptionDialog(final Conversation conversation) {
494		View menuItemView = findViewById(R.id.action_security);
495		if (menuItemView == null) {
496			return;
497		}
498		PopupMenu popup = new PopupMenu(this, menuItemView);
499		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
500				.findFragmentByTag("conversation");
501		if (fragment != null) {
502			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
503
504				@Override
505				public boolean onMenuItemClick(MenuItem item) {
506					switch (item.getItemId()) {
507					case R.id.encryption_choice_none:
508						conversation.setNextEncryption(Message.ENCRYPTION_NONE);
509						item.setChecked(true);
510						break;
511					case R.id.encryption_choice_otr:
512						conversation.setNextEncryption(Message.ENCRYPTION_OTR);
513						item.setChecked(true);
514						break;
515					case R.id.encryption_choice_pgp:
516						if (hasPgp()) {
517							if (conversation.getAccount().getKeys()
518									.has("pgp_signature")) {
519								conversation
520										.setNextEncryption(Message.ENCRYPTION_PGP);
521								item.setChecked(true);
522							} else {
523								announcePgp(conversation.getAccount(),
524										conversation);
525							}
526						} else {
527							showInstallPgpDialog();
528						}
529						break;
530					default:
531						conversation.setNextEncryption(Message.ENCRYPTION_NONE);
532						break;
533					}
534					xmppConnectionService.databaseBackend
535							.updateConversation(conversation);
536					fragment.updateChatMsgHint();
537					return true;
538				}
539			});
540			popup.inflate(R.menu.encryption_choices);
541			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
542			MenuItem none = popup.getMenu().findItem(
543					R.id.encryption_choice_none);
544			if (conversation.getMode() == Conversation.MODE_MULTI) {
545				otr.setEnabled(false);
546			} else {
547				if (forceEncryption()) {
548					none.setVisible(false);
549				}
550			}
551			switch (conversation.getNextEncryption(forceEncryption())) {
552			case Message.ENCRYPTION_NONE:
553				none.setChecked(true);
554				break;
555			case Message.ENCRYPTION_OTR:
556				otr.setChecked(true);
557				break;
558			case Message.ENCRYPTION_PGP:
559				popup.getMenu().findItem(R.id.encryption_choice_pgp)
560						.setChecked(true);
561				break;
562			default:
563				popup.getMenu().findItem(R.id.encryption_choice_none)
564						.setChecked(true);
565				break;
566			}
567			popup.show();
568		}
569	}
570
571	protected void muteConversationDialog(final Conversation conversation) {
572		AlertDialog.Builder builder = new AlertDialog.Builder(this);
573		builder.setTitle(R.string.disable_notifications_for_this_conversation);
574		final int[] durations = getResources().getIntArray(
575				R.array.mute_options_durations);
576		builder.setItems(R.array.mute_options_descriptions,
577				new OnClickListener() {
578
579					@Override
580					public void onClick(DialogInterface dialog, int which) {
581						long till;
582						if (durations[which] == -1) {
583							till = Long.MAX_VALUE;
584						} else {
585							till = SystemClock.elapsedRealtime()
586									+ (durations[which] * 1000);
587						}
588						conversation.setMutedTill(till);
589						ConversationActivity.this.xmppConnectionService.databaseBackend
590								.updateConversation(conversation);
591						ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
592								.findFragmentByTag("conversation");
593						if (selectedFragment != null) {
594							selectedFragment.updateMessages();
595						}
596					}
597				});
598		builder.create().show();
599	}
600
601	protected ConversationFragment swapConversationFragment() {
602		ConversationFragment selectedFragment = new ConversationFragment();
603		if (!isFinishing()) {
604
605			FragmentTransaction transaction = getFragmentManager()
606					.beginTransaction();
607			transaction.replace(R.id.selected_conversation, selectedFragment,
608					"conversation");
609			try {
610				transaction.commitAllowingStateLoss();
611			} catch (IllegalStateException e) {
612				return selectedFragment;
613			}
614		}
615		return selectedFragment;
616	}
617
618	@Override
619	public boolean onKeyDown(int keyCode, KeyEvent event) {
620		if (keyCode == KeyEvent.KEYCODE_BACK) {
621			if (!isConversationsOverviewVisable()) {
622				showConversationsOverview();
623				return false;
624			}
625		}
626		return super.onKeyDown(keyCode, event);
627	}
628
629	@Override
630	protected void onNewIntent(Intent intent) {
631		if (xmppConnectionServiceBound) {
632			 if (intent != null
633						&& VIEW_CONVERSATION.equals(getIntent().getType())) {
634				handleViewConversationIntent(intent);
635			}
636		} else {
637			setIntent(intent);
638		}
639	}
640
641	@Override
642	public void onStart() {
643		super.onStart();
644		if (this.xmppConnectionServiceBound) {
645			this.onBackendConnected();
646		}
647		if (conversationList.size() >= 1) {
648			this.onConversationUpdate();
649		}
650	}
651
652	@Override
653	protected void onStop() {
654		if (xmppConnectionServiceBound) {
655			xmppConnectionService.removeOnConversationListChangedListener();
656			xmppConnectionService.removeOnAccountListChangedListener();
657			xmppConnectionService.removeOnRosterUpdateListener();
658			xmppConnectionService.getNotificationService().setOpenConversation(
659					null);
660		}
661		super.onStop();
662	}
663
664	@Override
665	public void onSaveInstanceState(Bundle savedInstanceState) {
666		Conversation conversation = getSelectedConversation();
667		if (conversation != null) {
668			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
669					conversation.getUuid());
670		}
671		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
672				isConversationsOverviewVisable());
673		super.onSaveInstanceState(savedInstanceState);
674	}
675
676	@Override
677	void onBackendConnected() {
678		this.registerListener();
679		updateConversationList();
680
681		if (xmppConnectionService.getAccounts().size() == 0) {
682			startActivity(new Intent(this, EditAccountActivity.class));
683		} else if (conversationList.size() <= 0) {
684			startActivity(new Intent(this, StartConversationActivity.class));
685			finish();
686		} else if (mOpenConverstaion != null) {
687			selectConversationByUuid(mOpenConverstaion);
688			paneShouldBeOpen = mPanelOpen;
689			if (paneShouldBeOpen) {
690				showConversationsOverview();
691			}
692			swapConversationFragment();
693			mOpenConverstaion = null;
694		} else if (getIntent() != null
695				&& VIEW_CONVERSATION.equals(getIntent().getType())) {
696			handleViewConversationIntent(getIntent());
697			setIntent(null);
698		} else {
699			showConversationsOverview();
700			ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
701					.findFragmentByTag("conversation");
702			if (selectedFragment != null) {
703				selectedFragment.onBackendConnected();
704			} else {
705				pendingImageUri = null;
706				setSelectedConversation(conversationList.get(0));
707				swapConversationFragment();
708			}
709		}
710
711		if (pendingImageUri != null) {
712			attachImageToConversation(getSelectedConversation(),
713					pendingImageUri);
714			pendingImageUri = null;
715		}
716		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
717	}
718	
719	private void handleViewConversationIntent(Intent intent) {
720		String uuid = (String) intent.getExtras().get(CONVERSATION);
721		String text = intent.getExtras().getString(TEXT, null);
722		selectConversationByUuid(uuid);
723		paneShouldBeOpen = false;
724		swapConversationFragment().setText(text);
725	}
726
727	private void selectConversationByUuid(String uuid) {
728		for (int i = 0; i < conversationList.size(); ++i) {
729			if (conversationList.get(i).getUuid().equals(uuid)) {
730				setSelectedConversation(conversationList.get(i));
731			}
732		}
733	}
734
735	public void registerListener() {
736		xmppConnectionService.setOnConversationListChangedListener(this);
737		xmppConnectionService.setOnAccountListChangedListener(this);
738		xmppConnectionService.setOnRosterUpdateListener(this);
739	}
740
741	@Override
742	protected void onActivityResult(int requestCode, int resultCode,
743			final Intent data) {
744		super.onActivityResult(requestCode, resultCode, data);
745		if (resultCode == RESULT_OK) {
746			if (requestCode == REQUEST_DECRYPT_PGP) {
747				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
748						.findFragmentByTag("conversation");
749				if (selectedFragment != null) {
750					selectedFragment.hideSnackbar();
751					selectedFragment.updateMessages();
752				}
753			} else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) {
754				pendingImageUri = data.getData();
755				if (xmppConnectionServiceBound) {
756					attachImageToConversation(getSelectedConversation(),
757							pendingImageUri);
758					pendingImageUri = null;
759				}
760			} else if (requestCode == REQUEST_SEND_PGP_IMAGE) {
761
762			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
763				attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
764			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
765				attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
766			} else if (requestCode == REQUEST_ANNOUNCE_PGP) {
767				announcePgp(getSelectedConversation().getAccount(),
768						getSelectedConversation());
769			} else if (requestCode == REQUEST_ENCRYPT_MESSAGE) {
770				// encryptTextMessage();
771			} else if (requestCode == REQUEST_IMAGE_CAPTURE) {
772				if (xmppConnectionServiceBound) {
773					attachImageToConversation(getSelectedConversation(),
774							pendingImageUri);
775					pendingImageUri = null;
776				}
777				Intent intent = new Intent(
778						Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
779				intent.setData(pendingImageUri);
780				sendBroadcast(intent);
781			} else if (requestCode == REQUEST_RECORD_AUDIO) {
782				attachAudioToConversation(getSelectedConversation(),
783						data.getData());
784			}
785		} else {
786			if (requestCode == REQUEST_IMAGE_CAPTURE) {
787				pendingImageUri = null;
788			}
789		}
790	}
791
792	private void attachAudioToConversation(Conversation conversation, Uri uri) {
793
794	}
795
796	private void attachImageToConversation(Conversation conversation, Uri uri) {
797		prepareImageToast = Toast.makeText(getApplicationContext(),
798				getText(R.string.preparing_image), Toast.LENGTH_LONG);
799		prepareImageToast.show();
800		xmppConnectionService.attachImageToConversation(conversation, uri,
801				new UiCallback<Message>() {
802
803					@Override
804					public void userInputRequried(PendingIntent pi,
805							Message object) {
806						hidePrepareImageToast();
807						ConversationActivity.this.runIntent(pi,
808								ConversationActivity.REQUEST_SEND_PGP_IMAGE);
809					}
810
811					@Override
812					public void success(Message message) {
813						xmppConnectionService.sendMessage(message);
814					}
815
816					@Override
817					public void error(int error, Message message) {
818						hidePrepareImageToast();
819						displayErrorDialog(error);
820					}
821				});
822	}
823
824	private void hidePrepareImageToast() {
825		if (prepareImageToast != null) {
826			runOnUiThread(new Runnable() {
827
828				@Override
829				public void run() {
830					prepareImageToast.cancel();
831				}
832			});
833		}
834	}
835
836	public void updateConversationList() {
837		xmppConnectionService
838				.populateWithOrderedConversations(conversationList);
839		listAdapter.notifyDataSetChanged();
840	}
841
842	public void runIntent(PendingIntent pi, int requestCode) {
843		try {
844			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
845					null, 0, 0, 0);
846		} catch (SendIntentException e1) {
847		}
848	}
849
850	public void encryptTextMessage(Message message) {
851		xmppConnectionService.getPgpEngine().encrypt(message,
852				new UiCallback<Message>() {
853
854					@Override
855					public void userInputRequried(PendingIntent pi,
856							Message message) {
857						ConversationActivity.this.runIntent(pi,
858								ConversationActivity.REQUEST_SEND_MESSAGE);
859					}
860
861					@Override
862					public void success(Message message) {
863						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
864						xmppConnectionService.sendMessage(message);
865					}
866
867					@Override
868					public void error(int error, Message message) {
869
870					}
871				});
872	}
873
874	public boolean forceEncryption() {
875		return getPreferences().getBoolean("force_encryption", false);
876	}
877
878	public boolean useSendButtonToIndicateStatus() {
879		return getPreferences().getBoolean("send_button_status", false);
880	}
881
882	public boolean indicateReceived() {
883		return getPreferences().getBoolean("indicate_received", false);
884	}
885
886	@Override
887	public void onAccountUpdate() {
888		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
889				.findFragmentByTag("conversation");
890		if (fragment != null) {
891			runOnUiThread(new Runnable() {
892
893				@Override
894				public void run() {
895					fragment.updateMessages();
896				}
897			});
898		}
899	}
900
901	@Override
902	public void onConversationUpdate() {
903		runOnUiThread(new Runnable() {
904
905			@Override
906			public void run() {
907				updateConversationList();
908				if (paneShouldBeOpen) {
909					if (conversationList.size() >= 1) {
910						swapConversationFragment();
911					} else {
912						startActivity(new Intent(getApplicationContext(),
913								StartConversationActivity.class));
914						finish();
915					}
916				}
917				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
918						.findFragmentByTag("conversation");
919				if (selectedFragment != null) {
920					selectedFragment.updateMessages();
921				}
922			}
923		});
924	}
925
926	@Override
927	public void onRosterUpdate() {
928		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
929				.findFragmentByTag("conversation");
930		if (fragment != null) {
931			runOnUiThread(new Runnable() {
932
933				@Override
934				public void run() {
935					fragment.updateMessages();
936				}
937			});
938		}
939	}
940}