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	protected ConversationActivity activity = this;
 78	private Toast prepareImageToast;
 79
 80	private Uri pendingImageUri = null;
 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 boolean shouldPaneBeOpen() {
 99		return paneShouldBeOpen;
100	}
101
102	public void showConversationsOverview() {
103		if (mContentView instanceof SlidingPaneLayout) {
104			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
105			mSlidingPaneLayout.openPane();
106		}
107	}
108
109	public void hideConversationsOverview() {
110		if (mContentView instanceof SlidingPaneLayout) {
111			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
112			mSlidingPaneLayout.closePane();
113		}
114	}
115
116	public boolean isConversationsOverviewHideable() {
117		if (mContentView instanceof SlidingPaneLayout) {
118			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
119			return mSlidingPaneLayout.isSlideable();
120		} else {
121			return false;
122		}
123	}
124
125	public boolean isConversationsOverviewVisable() {
126		if (mContentView instanceof SlidingPaneLayout) {
127			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
128			return mSlidingPaneLayout.isOpen();
129		} else {
130			return true;
131		}
132	}
133
134	@Override
135	protected void onCreate(Bundle savedInstanceState) {
136		super.onCreate(savedInstanceState);
137
138		if (savedInstanceState != null) {
139			mOpenConverstaion = savedInstanceState.getString(
140					STATE_OPEN_CONVERSATION, null);
141			mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
142		}
143
144		setContentView(R.layout.fragment_conversations_overview);
145
146		listView = (ListView) findViewById(R.id.list);
147
148		getActionBar().setDisplayHomeAsUpEnabled(false);
149		getActionBar().setHomeButtonEnabled(false);
150
151		this.listAdapter = new ConversationAdapter(this, conversationList);
152		listView.setAdapter(this.listAdapter);
153
154		listView.setOnItemClickListener(new OnItemClickListener() {
155
156			@Override
157			public void onItemClick(AdapterView<?> arg0, View clickedView,
158					int position, long arg3) {
159				paneShouldBeOpen = false;
160				if (getSelectedConversation() != conversationList.get(position)) {
161					setSelectedConversation(conversationList.get(position));
162					swapConversationFragment();
163				} else {
164					hideConversationsOverview();
165				}
166			}
167		});
168		mContentView = findViewById(R.id.content_view_spl);
169		if (mContentView == null) {
170			mContentView = findViewById(R.id.content_view_ll);
171		}
172		if (mContentView instanceof SlidingPaneLayout) {
173			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
174			mSlidingPaneLayout.setParallaxDistance(150);
175			mSlidingPaneLayout
176					.setShadowResource(R.drawable.es_slidingpane_shadow);
177			mSlidingPaneLayout.setSliderFadeColor(0);
178			mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
179
180				@Override
181				public void onPanelOpened(View arg0) {
182					paneShouldBeOpen = true;
183					ActionBar ab = getActionBar();
184					if (ab != null) {
185						ab.setDisplayHomeAsUpEnabled(false);
186						ab.setHomeButtonEnabled(false);
187						ab.setTitle(R.string.app_name);
188					}
189					invalidateOptionsMenu();
190					hideKeyboard();
191					if (xmppConnectionServiceBound) {
192						xmppConnectionService.getNotificationService()
193								.setOpenConversation(null);
194					}
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					|| activity.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						activity.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						activity.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
609			transaction.commitAllowingStateLoss();
610		}
611		return selectedFragment;
612	}
613
614	@Override
615	public boolean onKeyDown(int keyCode, KeyEvent event) {
616		if (keyCode == KeyEvent.KEYCODE_BACK) {
617			if (!isConversationsOverviewVisable()) {
618				showConversationsOverview();
619				return false;
620			}
621		}
622		return super.onKeyDown(keyCode, event);
623	}
624
625	@Override
626	protected void onNewIntent(Intent intent) {
627		if (xmppConnectionServiceBound) {
628			if ((Intent.ACTION_VIEW.equals(intent.getAction()) && (VIEW_CONVERSATION
629					.equals(intent.getType())))) {
630				String convToView = (String) intent.getExtras().get(
631						CONVERSATION);
632				updateConversationList();
633				for (int i = 0; i < conversationList.size(); ++i) {
634					if (conversationList.get(i).getUuid().equals(convToView)) {
635						setSelectedConversation(conversationList.get(i));
636						break;
637					}
638				}
639				paneShouldBeOpen = false;
640				String text = intent.getExtras().getString(TEXT, null);
641				swapConversationFragment().setText(text);
642			}
643		} else {
644			handledViewIntent = false;
645			setIntent(intent);
646		}
647	}
648
649	@Override
650	public void onStart() {
651		super.onStart();
652		if (this.xmppConnectionServiceBound) {
653			this.onBackendConnected();
654		}
655		if (conversationList.size() >= 1) {
656			this.onConversationUpdate();
657		}
658	}
659
660	@Override
661	protected void onStop() {
662		if (xmppConnectionServiceBound) {
663			xmppConnectionService.removeOnConversationListChangedListener();
664			xmppConnectionService.removeOnAccountListChangedListener();
665			xmppConnectionService.removeOnRosterUpdateListener();
666			xmppConnectionService.getNotificationService().setOpenConversation(
667					null);
668		}
669		super.onStop();
670	}
671
672	@Override
673	public void onSaveInstanceState(Bundle savedInstanceState) {
674		Conversation conversation = getSelectedConversation();
675		if (conversation != null) {
676			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
677					conversation.getUuid());
678		}
679		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
680				isConversationsOverviewVisable());
681		super.onSaveInstanceState(savedInstanceState);
682	}
683
684	@Override
685	void onBackendConnected() {
686		this.registerListener();
687		updateConversationList();
688
689		if (xmppConnectionService.getAccounts().size() == 0) {
690			startActivity(new Intent(this, EditAccountActivity.class));
691		} else if (conversationList.size() <= 0) {
692			startActivity(new Intent(this, StartConversationActivity.class));
693			finish();
694		} else if (mOpenConverstaion != null) {
695			selectConversationByUuid(mOpenConverstaion);
696			paneShouldBeOpen = mPanelOpen;
697			if (paneShouldBeOpen) {
698				showConversationsOverview();
699			}
700			swapConversationFragment();
701			mOpenConverstaion = null;
702		} else if (getIntent() != null
703				&& VIEW_CONVERSATION.equals(getIntent().getType())) {
704			String uuid = (String) getIntent().getExtras().get(CONVERSATION);
705			String text = getIntent().getExtras().getString(TEXT, null);
706			selectConversationByUuid(uuid);
707			paneShouldBeOpen = false;
708			swapConversationFragment().setText(text);
709			setIntent(null);
710		} else {
711			showConversationsOverview();
712			ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
713					.findFragmentByTag("conversation");
714			if (selectedFragment != null) {
715				selectedFragment.onBackendConnected();
716			} else {
717				pendingImageUri = null;
718				setSelectedConversation(conversationList.get(0));
719				swapConversationFragment();
720			}
721		}
722
723		if (pendingImageUri != null) {
724			attachImageToConversation(getSelectedConversation(),
725					pendingImageUri);
726			pendingImageUri = null;
727		}
728		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
729	}
730
731	private void selectConversationByUuid(String uuid) {
732		for (int i = 0; i < conversationList.size(); ++i) {
733			if (conversationList.get(i).getUuid().equals(uuid)) {
734				setSelectedConversation(conversationList.get(i));
735			}
736		}
737	}
738
739	public void registerListener() {
740		if (xmppConnectionServiceBound) {
741			xmppConnectionService.setOnConversationListChangedListener(this);
742			xmppConnectionService.setOnAccountListChangedListener(this);
743			xmppConnectionService.setOnRosterUpdateListener(this);
744		}
745	}
746
747	@Override
748	protected void onActivityResult(int requestCode, int resultCode,
749			final Intent data) {
750		super.onActivityResult(requestCode, resultCode, data);
751		if (resultCode == RESULT_OK) {
752			if (requestCode == REQUEST_DECRYPT_PGP) {
753				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
754						.findFragmentByTag("conversation");
755				if (selectedFragment != null) {
756					selectedFragment.hideSnackbar();
757				}
758			} else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) {
759				pendingImageUri = data.getData();
760				if (xmppConnectionServiceBound) {
761					attachImageToConversation(getSelectedConversation(),
762							pendingImageUri);
763					pendingImageUri = null;
764				}
765			} else if (requestCode == REQUEST_SEND_PGP_IMAGE) {
766
767			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
768				attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
769			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
770				attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
771			} else if (requestCode == REQUEST_ANNOUNCE_PGP) {
772				announcePgp(getSelectedConversation().getAccount(),
773						getSelectedConversation());
774			} else if (requestCode == REQUEST_ENCRYPT_MESSAGE) {
775				// encryptTextMessage();
776			} else if (requestCode == REQUEST_IMAGE_CAPTURE) {
777				if (xmppConnectionServiceBound) {
778					attachImageToConversation(getSelectedConversation(),
779							pendingImageUri);
780					pendingImageUri = null;
781				}
782				Intent intent = new Intent(
783						Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
784				intent.setData(pendingImageUri);
785				sendBroadcast(intent);
786			} else if (requestCode == REQUEST_RECORD_AUDIO) {
787				attachAudioToConversation(getSelectedConversation(),
788						data.getData());
789			}
790		}
791	}
792
793	private void attachAudioToConversation(Conversation conversation, Uri uri) {
794
795	}
796
797	private void attachImageToConversation(Conversation conversation, Uri uri) {
798		prepareImageToast = Toast.makeText(getApplicationContext(),
799				getText(R.string.preparing_image), Toast.LENGTH_LONG);
800		prepareImageToast.show();
801		xmppConnectionService.attachImageToConversation(conversation, uri,
802				new UiCallback<Message>() {
803
804					@Override
805					public void userInputRequried(PendingIntent pi,
806							Message object) {
807						hidePrepareImageToast();
808						ConversationActivity.this.runIntent(pi,
809								ConversationActivity.REQUEST_SEND_PGP_IMAGE);
810					}
811
812					@Override
813					public void success(Message message) {
814						xmppConnectionService.sendMessage(message);
815					}
816
817					@Override
818					public void error(int error, Message message) {
819						hidePrepareImageToast();
820						displayErrorDialog(error);
821					}
822				});
823	}
824
825	private void hidePrepareImageToast() {
826		if (prepareImageToast != null) {
827			runOnUiThread(new Runnable() {
828
829				@Override
830				public void run() {
831					prepareImageToast.cancel();
832				}
833			});
834		}
835	}
836
837	public void updateConversationList() {
838		xmppConnectionService
839				.populateWithOrderedConversations(conversationList);
840		listAdapter.notifyDataSetChanged();
841	}
842
843	public void runIntent(PendingIntent pi, int requestCode) {
844		try {
845			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
846					null, 0, 0, 0);
847		} catch (SendIntentException e1) {
848		}
849	}
850
851	public void encryptTextMessage(Message message) {
852		xmppConnectionService.getPgpEngine().encrypt(message,
853				new UiCallback<Message>() {
854
855					@Override
856					public void userInputRequried(PendingIntent pi,
857							Message message) {
858						activity.runIntent(pi,
859								ConversationActivity.REQUEST_SEND_MESSAGE);
860					}
861
862					@Override
863					public void success(Message message) {
864						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
865						xmppConnectionService.sendMessage(message);
866					}
867
868					@Override
869					public void error(int error, Message message) {
870
871					}
872				});
873	}
874
875	public boolean forceEncryption() {
876		return getPreferences().getBoolean("force_encryption", false);
877	}
878
879	public boolean useSendButtonToIndicateStatus() {
880		return getPreferences().getBoolean("send_button_status", false);
881	}
882
883	public boolean indicateReceived() {
884		return getPreferences().getBoolean("indicate_received", false);
885	}
886
887	@Override
888	public void onAccountUpdate() {
889		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
890				.findFragmentByTag("conversation");
891		if (fragment != null) {
892			runOnUiThread(new Runnable() {
893
894				@Override
895				public void run() {
896					fragment.updateMessages();
897				}
898			});
899		}
900	}
901
902	@Override
903	public void onConversationUpdate() {
904		runOnUiThread(new Runnable() {
905
906			@Override
907			public void run() {
908				updateConversationList();
909				if (paneShouldBeOpen) {
910					if (conversationList.size() >= 1) {
911						swapConversationFragment();
912					} else {
913						startActivity(new Intent(getApplicationContext(),
914								StartConversationActivity.class));
915						finish();
916					}
917				}
918				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
919						.findFragmentByTag("conversation");
920				if (selectedFragment != null) {
921					selectedFragment.updateMessages();
922				}
923			}
924		});
925	}
926
927	@Override
928	public void onRosterUpdate() {
929		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
930				.findFragmentByTag("conversation");
931		if (fragment != null) {
932			runOnUiThread(new Runnable() {
933
934				@Override
935				public void run() {
936					fragment.updateMessages();
937				}
938			});
939		}
940	}
941}