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