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.ContextMenu;
 19import android.view.KeyEvent;
 20import android.view.Menu;
 21import android.view.MenuItem;
 22import android.view.View;
 23import android.widget.AdapterView;
 24import android.widget.AdapterView.OnItemClickListener;
 25import android.widget.ArrayAdapter;
 26import android.widget.CheckBox;
 27import android.widget.ListView;
 28import android.widget.PopupMenu;
 29import android.widget.PopupMenu.OnMenuItemClickListener;
 30import android.widget.Toast;
 31
 32import java.util.ArrayList;
 33import java.util.List;
 34
 35import eu.siacs.conversations.R;
 36import eu.siacs.conversations.entities.Contact;
 37import eu.siacs.conversations.entities.Conversation;
 38import eu.siacs.conversations.entities.Message;
 39import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
 40import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
 41import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
 42import eu.siacs.conversations.ui.adapter.ConversationAdapter;
 43import eu.siacs.conversations.utils.ExceptionHelper;
 44
 45public class ConversationActivity extends XmppActivity implements
 46		OnAccountUpdate, OnConversationUpdate, OnRosterUpdate {
 47
 48	public static final String VIEW_CONVERSATION = "viewConversation";
 49	public static final String CONVERSATION = "conversationUuid";
 50	public static final String TEXT = "text";
 51	public static final String NICK = "nick";
 52	public static final String PRESENCE = "eu.siacs.conversations.presence";
 53
 54	public static final int REQUEST_SEND_MESSAGE = 0x0201;
 55	public static final int REQUEST_DECRYPT_PGP = 0x0202;
 56	public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
 57	private static final int REQUEST_ATTACH_IMAGE_DIALOG = 0x0203;
 58	private static final int REQUEST_IMAGE_CAPTURE = 0x0204;
 59	private static final int REQUEST_RECORD_AUDIO = 0x0205;
 60	private static final int REQUEST_SEND_PGP_IMAGE = 0x0206;
 61	private static final int REQUEST_ATTACH_FILE_DIALOG = 0x0208;
 62	private static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
 63	private static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
 64	private static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
 65	private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
 66	private static final String STATE_PANEL_OPEN = "state_panel_open";
 67	private static final String STATE_PENDING_URI = "state_pending_uri";
 68
 69	private String mOpenConverstaion = null;
 70	private boolean mPanelOpen = true;
 71	private Uri mPendingImageUri = null;
 72	private Uri mPendingFileUri = null;
 73
 74	private View mContentView;
 75
 76	private List<Conversation> conversationList = new ArrayList<>();
 77	private Conversation mSelectedConversation = null;
 78	private ListView listView;
 79	private ConversationFragment mConversationFragment;
 80
 81	private ArrayAdapter<Conversation> listAdapter;
 82
 83	private Toast prepareFileToast;
 84
 85
 86	public List<Conversation> getConversationList() {
 87		return this.conversationList;
 88	}
 89
 90	public Conversation getSelectedConversation() {
 91		return this.mSelectedConversation;
 92	}
 93
 94	public void setSelectedConversation(Conversation conversation) {
 95		this.mSelectedConversation = conversation;
 96	}
 97
 98	public ListView getConversationListView() {
 99		return this.listView;
100	}
101
102	public void showConversationsOverview() {
103		if (mContentView instanceof SlidingPaneLayout) {
104			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
105			mSlidingPaneLayout.openPane();
106		}
107	}
108
109	@Override
110	protected String getShareableUri() {
111		Conversation conversation = getSelectedConversation();
112		if (conversation != null) {
113			return conversation.getAccount().getShareableUri();
114		} else {
115			return "";
116		}
117	}
118
119	public void hideConversationsOverview() {
120		if (mContentView instanceof SlidingPaneLayout) {
121			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
122			mSlidingPaneLayout.closePane();
123		}
124	}
125
126	public boolean isConversationsOverviewHideable() {
127		if (mContentView instanceof SlidingPaneLayout) {
128			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
129			return mSlidingPaneLayout.isSlideable();
130		} else {
131			return false;
132		}
133	}
134
135	public boolean isConversationsOverviewVisable() {
136		if (mContentView instanceof SlidingPaneLayout) {
137			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
138			return mSlidingPaneLayout.isOpen();
139		} else {
140			return true;
141		}
142	}
143
144	@Override
145	protected void onCreate(Bundle savedInstanceState) {
146		super.onCreate(savedInstanceState);
147		if (savedInstanceState != null) {mOpenConverstaion = savedInstanceState.getString(
148					STATE_OPEN_CONVERSATION, null);
149			mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
150			String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
151			if (pending != null) {
152				mPendingImageUri = Uri.parse(pending);
153			}
154		}
155
156		setContentView(R.layout.fragment_conversations_overview);
157
158		this.mConversationFragment = new ConversationFragment();
159		FragmentTransaction transaction = getFragmentManager().beginTransaction();
160		transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
161		transaction.commit();
162
163		listView = (ListView) findViewById(R.id.list);
164		this.listAdapter = new ConversationAdapter(this, conversationList);
165		listView.setAdapter(this.listAdapter);
166
167		if (getActionBar() != null) {
168			getActionBar().setDisplayHomeAsUpEnabled(false);
169			getActionBar().setHomeButtonEnabled(false);
170		}
171
172		listView.setOnItemClickListener(new OnItemClickListener() {
173
174			@Override
175			public void onItemClick(AdapterView<?> arg0, View clickedView,
176									int position, long arg3) {
177				if (getSelectedConversation() != conversationList.get(position)) {
178					setSelectedConversation(conversationList.get(position));
179					ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
180				}
181				hideConversationsOverview();
182			}
183		});
184		mContentView = findViewById(R.id.content_view_spl);
185		if (mContentView == null) {
186			mContentView = findViewById(R.id.content_view_ll);
187		}
188		if (mContentView instanceof SlidingPaneLayout) {
189			SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
190			mSlidingPaneLayout.setParallaxDistance(150);
191			mSlidingPaneLayout
192					.setShadowResource(R.drawable.es_slidingpane_shadow);
193			mSlidingPaneLayout.setSliderFadeColor(0);
194			mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
195
196				@Override
197				public void onPanelOpened(View arg0) {
198					updateActionBarTitle();
199					invalidateOptionsMenu();
200					hideKeyboard();
201					if (xmppConnectionServiceBound) {
202						xmppConnectionService.getNotificationService()
203								.setOpenConversation(null);
204					}
205					closeContextMenu();
206				}
207
208				@Override
209				public void onPanelClosed(View arg0) {
210					openConversation();
211				}
212
213				@Override
214				public void onPanelSlide(View arg0, float arg1) {
215					// TODO Auto-generated method stub
216
217				}
218			});
219		}
220	}
221
222	@Override
223	public void switchToConversation(Conversation conversation) {
224		setSelectedConversation(conversation);
225		runOnUiThread(new Runnable() {
226			@Override
227			public void run() {
228				ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
229				openConversation();
230			}
231		});
232	}
233
234	private void updateActionBarTitle() {
235		updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
236	}
237
238	private void updateActionBarTitle(boolean titleShouldBeName) {
239		ActionBar ab = getActionBar();
240		if (ab != null) {
241			if (titleShouldBeName) {
242				ab.setDisplayHomeAsUpEnabled(true);
243				ab.setHomeButtonEnabled(true);
244				if (getSelectedConversation().getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
245					ab.setTitle(getSelectedConversation().getName());
246				} else {
247					ab.setTitle(getSelectedConversation().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
495								.clearConversationHistory(conversation);
496						if (endConversationCheckBox.isChecked()) {
497							endConversation(conversation);
498						}
499					}
500				});
501		builder.create().show();
502	}
503
504	protected void attachFileDialog() {
505		View menuAttachFile = findViewById(R.id.action_attach_file);
506		if (menuAttachFile == null) {
507			return;
508		}
509		PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
510		attachFilePopup.inflate(R.menu.attachment_choices);
511		attachFilePopup
512				.setOnMenuItemClickListener(new OnMenuItemClickListener() {
513
514					@Override
515					public boolean onMenuItemClick(MenuItem item) {
516						switch (item.getItemId()) {
517							case R.id.attach_choose_picture:
518								attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
519								break;
520							case R.id.attach_take_picture:
521								attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
522								break;
523							case R.id.attach_record_voice:
524								attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
525								break;
526						}
527						return false;
528					}
529				});
530		attachFilePopup.show();
531	}
532
533	protected void selectEncryptionDialog(final Conversation conversation) {
534		View menuItemView = findViewById(R.id.action_security);
535		if (menuItemView == null) {
536			return;
537		}
538		PopupMenu popup = new PopupMenu(this, menuItemView);
539		final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
540				.findFragmentByTag("conversation");
541		if (fragment != null) {
542			popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
543
544				@Override
545				public boolean onMenuItemClick(MenuItem item) {
546					switch (item.getItemId()) {
547						case R.id.encryption_choice_none:
548							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
549							item.setChecked(true);
550							break;
551						case R.id.encryption_choice_otr:
552							conversation.setNextEncryption(Message.ENCRYPTION_OTR);
553							item.setChecked(true);
554							break;
555						case R.id.encryption_choice_pgp:
556							if (hasPgp()) {
557								if (conversation.getAccount().getKeys()
558										.has("pgp_signature")) {
559									conversation
560											.setNextEncryption(Message.ENCRYPTION_PGP);
561									item.setChecked(true);
562								} else {
563									announcePgp(conversation.getAccount(),
564											conversation);
565								}
566							} else {
567								showInstallPgpDialog();
568							}
569							break;
570						default:
571							conversation.setNextEncryption(Message.ENCRYPTION_NONE);
572							break;
573					}
574					xmppConnectionService.databaseBackend
575							.updateConversation(conversation);
576					fragment.updateChatMsgHint();
577					return true;
578				}
579			});
580			popup.inflate(R.menu.encryption_choices);
581			MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
582			MenuItem none = popup.getMenu().findItem(
583					R.id.encryption_choice_none);
584			if (conversation.getMode() == Conversation.MODE_MULTI) {
585				otr.setEnabled(false);
586			} else {
587				if (forceEncryption()) {
588					none.setVisible(false);
589				}
590			}
591			switch (conversation.getNextEncryption(forceEncryption())) {
592				case Message.ENCRYPTION_NONE:
593					none.setChecked(true);
594					break;
595				case Message.ENCRYPTION_OTR:
596					otr.setChecked(true);
597					break;
598				case Message.ENCRYPTION_PGP:
599					popup.getMenu().findItem(R.id.encryption_choice_pgp)
600							.setChecked(true);
601					break;
602				default:
603					popup.getMenu().findItem(R.id.encryption_choice_none)
604							.setChecked(true);
605					break;
606			}
607			popup.show();
608		}
609	}
610
611	protected void muteConversationDialog(final Conversation conversation) {
612		AlertDialog.Builder builder = new AlertDialog.Builder(this);
613		builder.setTitle(R.string.disable_notifications);
614		final int[] durations = getResources().getIntArray(
615				R.array.mute_options_durations);
616		builder.setItems(R.array.mute_options_descriptions,
617				new OnClickListener() {
618
619					@Override
620					public void onClick(DialogInterface dialog, int which) {
621						long till;
622						if (durations[which] == -1) {
623							till = Long.MAX_VALUE;
624						} else {
625							till = SystemClock.elapsedRealtime()
626									+ (durations[which] * 1000);
627						}
628						conversation.setMutedTill(till);
629						ConversationActivity.this.xmppConnectionService.databaseBackend
630								.updateConversation(conversation);
631						updateConversationList();
632						ConversationActivity.this.mConversationFragment.updateMessages();
633						invalidateOptionsMenu();
634					}
635				});
636		builder.create().show();
637	}
638
639	public void unmuteConversation(final Conversation conversation) {
640		conversation.setMutedTill(0);
641		this.xmppConnectionService.databaseBackend.updateConversation(conversation);
642		updateConversationList();
643		ConversationActivity.this.mConversationFragment.updateMessages();
644		invalidateOptionsMenu();
645	}
646
647	@Override
648	public void onBackPressed() {
649		if (!isConversationsOverviewVisable()) {
650			showConversationsOverview();
651		} else {
652			moveTaskToBack(true);
653		}
654	}
655
656	@Override
657	protected void onNewIntent(final Intent intent) {
658		if (xmppConnectionServiceBound) {
659			if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
660				handleViewConversationIntent(intent);
661			}
662		} else {
663			setIntent(intent);
664		}
665	}
666
667	@Override
668	public void onStart() {
669		super.onStart();
670		if (this.xmppConnectionServiceBound) {
671			this.onBackendConnected();
672		}
673		if (conversationList.size() >= 1) {
674			this.onConversationUpdate();
675		}
676	}
677
678	@Override
679	public void onSaveInstanceState(final Bundle savedInstanceState) {
680		Conversation conversation = getSelectedConversation();
681		if (conversation != null) {
682			savedInstanceState.putString(STATE_OPEN_CONVERSATION,
683					conversation.getUuid());
684		}
685		savedInstanceState.putBoolean(STATE_PANEL_OPEN,
686				isConversationsOverviewVisable());
687		if (this.mPendingImageUri != null) {
688			savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUri.toString());
689		}
690		super.onSaveInstanceState(savedInstanceState);
691	}
692
693	@Override
694	void onBackendConnected() {
695		updateConversationList();
696		if (xmppConnectionService.getAccounts().size() == 0) {
697			startActivity(new Intent(this, EditAccountActivity.class));
698		} else if (conversationList.size() <= 0) {
699			startActivity(new Intent(this, StartConversationActivity.class));
700			finish();
701		} else if (getIntent() != null
702				&& VIEW_CONVERSATION.equals(getIntent().getType())) {
703			handleViewConversationIntent(getIntent());
704		} else if (mOpenConverstaion != null) {
705			selectConversationByUuid(mOpenConverstaion);
706			if (mPanelOpen) {
707				showConversationsOverview();
708			} else {
709				if (isConversationsOverviewHideable()) {
710					openConversation();
711				}
712			}
713			this.mConversationFragment.reInit(getSelectedConversation());
714			mOpenConverstaion = null;
715		} else if (getSelectedConversation() != null) {
716			this.mConversationFragment.updateMessages();
717		} else {
718			showConversationsOverview();
719			mPendingImageUri = null;
720			mPendingFileUri = null;
721			setSelectedConversation(conversationList.get(0));
722			this.mConversationFragment.reInit(getSelectedConversation());
723		}
724
725		if (mPendingImageUri != null) {
726			attachImageToConversation(getSelectedConversation(),mPendingImageUri);
727			mPendingImageUri = null;
728		} else if (mPendingFileUri != null) {
729			attachFileToConversation(getSelectedConversation(),mPendingFileUri);
730			mPendingFileUri = null;
731		}
732		ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
733		setIntent(new Intent());
734	}
735
736	private void handleViewConversationIntent(Intent intent) {
737		String uuid = (String) intent.getExtras().get(CONVERSATION);
738		String text = intent.getExtras().getString(TEXT, "");
739		String nick = intent.getExtras().getString(NICK,null);
740		selectConversationByUuid(uuid);
741		this.mConversationFragment.reInit(getSelectedConversation());
742		if (nick!=null) {
743			this.mConversationFragment.highlightInConference(nick);
744		} else {
745			this.mConversationFragment.appendText(text);
746		}
747		hideConversationsOverview();
748		openConversation();
749		if (mContentView instanceof SlidingPaneLayout) {
750			updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
751		}
752	}
753
754	private void selectConversationByUuid(String uuid) {
755        for (Conversation aConversationList : conversationList) {
756            if (aConversationList.getUuid().equals(uuid)) {
757                setSelectedConversation(aConversationList);
758            }
759        }
760	}
761
762	@Override
763	protected void unregisterListeners() {
764		super.unregisterListeners();
765		xmppConnectionService.getNotificationService().setOpenConversation(null);
766	}
767
768	@Override
769	protected void onActivityResult(int requestCode, int resultCode,
770									final Intent data) {
771		super.onActivityResult(requestCode, resultCode, data);
772		if (resultCode == RESULT_OK) {
773			if (requestCode == REQUEST_DECRYPT_PGP) {
774				ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
775						.findFragmentByTag("conversation");
776				if (selectedFragment != null) {
777					selectedFragment.hideSnackbar();
778					selectedFragment.updateMessages();
779				}
780			} else if (requestCode == REQUEST_ATTACH_IMAGE_DIALOG) {
781				mPendingImageUri = data.getData();
782				if (xmppConnectionServiceBound) {
783					attachImageToConversation(getSelectedConversation(),
784							mPendingImageUri);
785					mPendingImageUri = null;
786				}
787			} else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) {
788				mPendingFileUri = data.getData();
789				if (xmppConnectionServiceBound) {
790					attachFileToConversation(getSelectedConversation(),
791							mPendingFileUri);
792					mPendingFileUri = null;
793				}
794			} else if (requestCode == REQUEST_SEND_PGP_IMAGE) {
795
796			} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
797				attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
798			} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
799				attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
800			} else if (requestCode == REQUEST_ANNOUNCE_PGP) {
801				announcePgp(getSelectedConversation().getAccount(),
802						getSelectedConversation());
803			} else if (requestCode == REQUEST_ENCRYPT_MESSAGE) {
804				// encryptTextMessage();
805			} else if (requestCode == REQUEST_IMAGE_CAPTURE && mPendingImageUri != null) {
806				if (xmppConnectionServiceBound) {
807					attachImageToConversation(getSelectedConversation(),
808							mPendingImageUri);
809					mPendingImageUri = null;
810				}
811				Intent intent = new Intent(
812						Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
813				intent.setData(mPendingImageUri);
814				sendBroadcast(intent);
815			}
816		} else {
817			if (requestCode == REQUEST_IMAGE_CAPTURE) {
818				mPendingImageUri = null;
819			}
820		}
821	}
822
823	private void attachFileToConversation(Conversation conversation, Uri uri) {
824		prepareFileToast = Toast.makeText(getApplicationContext(),
825				getText(R.string.preparing_file), Toast.LENGTH_LONG);
826		prepareFileToast.show();
827		xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
828			@Override
829			public void success(Message message) {
830				hidePrepareFileToast();
831				xmppConnectionService.sendMessage(message);
832			}
833
834			@Override
835			public void error(int errorCode, Message message) {
836				displayErrorDialog(errorCode);
837			}
838
839			@Override
840			public void userInputRequried(PendingIntent pi, Message message) {
841
842			}
843		});
844	}
845
846	private void attachImageToConversation(Conversation conversation, Uri uri) {
847		prepareFileToast = Toast.makeText(getApplicationContext(),
848				getText(R.string.preparing_image), Toast.LENGTH_LONG);
849		prepareFileToast.show();
850		xmppConnectionService.attachImageToConversation(conversation, uri,
851				new UiCallback<Message>() {
852
853					@Override
854					public void userInputRequried(PendingIntent pi,
855												  Message object) {
856						hidePrepareFileToast();
857						ConversationActivity.this.runIntent(pi,
858								ConversationActivity.REQUEST_SEND_PGP_IMAGE);
859					}
860
861					@Override
862					public void success(Message message) {
863						xmppConnectionService.sendMessage(message);
864					}
865
866					@Override
867					public void error(int error, Message message) {
868						hidePrepareFileToast();
869						displayErrorDialog(error);
870					}
871				});
872	}
873
874	private void hidePrepareFileToast() {
875		if (prepareFileToast != null) {
876			runOnUiThread(new Runnable() {
877
878				@Override
879				public void run() {
880					prepareFileToast.cancel();
881				}
882			});
883		}
884	}
885
886	public void updateConversationList() {
887		xmppConnectionService
888				.populateWithOrderedConversations(conversationList);
889		listAdapter.notifyDataSetChanged();
890	}
891
892	public void runIntent(PendingIntent pi, int requestCode) {
893		try {
894			this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
895					null, 0, 0, 0);
896		} catch (final SendIntentException ignored) {
897		}
898	}
899
900	public void encryptTextMessage(Message message) {
901		xmppConnectionService.getPgpEngine().encrypt(message,
902				new UiCallback<Message>() {
903
904					@Override
905					public void userInputRequried(PendingIntent pi,
906												  Message message) {
907						ConversationActivity.this.runIntent(pi,
908								ConversationActivity.REQUEST_SEND_MESSAGE);
909					}
910
911					@Override
912					public void success(Message message) {
913						message.setEncryption(Message.ENCRYPTION_DECRYPTED);
914						xmppConnectionService.sendMessage(message);
915					}
916
917					@Override
918					public void error(int error, Message message) {
919
920					}
921				});
922	}
923
924	public boolean forceEncryption() {
925		return getPreferences().getBoolean("force_encryption", false);
926	}
927
928	public boolean useSendButtonToIndicateStatus() {
929		return getPreferences().getBoolean("send_button_status", false);
930	}
931
932	public boolean indicateReceived() {
933		return getPreferences().getBoolean("indicate_received", false);
934	}
935
936	@Override
937	public void onAccountUpdate() {
938		runOnUiThread(new Runnable() {
939
940			@Override
941			public void run() {
942				updateConversationList();
943				ConversationActivity.this.mConversationFragment.updateMessages();
944				updateActionBarTitle();
945			}
946		});
947	}
948
949	@Override
950	public void onConversationUpdate() {
951		runOnUiThread(new Runnable() {
952
953			@Override
954			public void run() {
955				updateConversationList();
956				if (conversationList.size() == 0) {
957					startActivity(new Intent(getApplicationContext(),
958							StartConversationActivity.class));
959					finish();
960				}
961				ConversationActivity.this.mConversationFragment.updateMessages();
962				updateActionBarTitle();
963			}
964		});
965	}
966
967	@Override
968	public void onRosterUpdate() {
969		runOnUiThread(new Runnable() {
970
971				@Override
972				public void run() {
973					updateConversationList();
974					ConversationActivity.this.mConversationFragment.updateMessages();
975					updateActionBarTitle();
976				}
977			});
978	}
979}