ConversationActivity.java

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