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