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