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