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