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