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