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