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