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