ConversationActivity.java

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