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