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