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