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 }
195
196 @Override
197 public void onPanelClosed(View arg0) {
198 paneShouldBeOpen = false;
199 if ((conversationList.size() > 0)
200 && (getSelectedConversation() != null)) {
201 openConversation(getSelectedConversation());
202 if (!getSelectedConversation().isRead()) {
203 xmppConnectionService.markRead(
204 getSelectedConversation(), true);
205 listView.invalidateViews();
206 }
207 }
208 }
209
210 @Override
211 public void onPanelSlide(View arg0, float arg1) {
212 // TODO Auto-generated method stub
213
214 }
215 });
216 }
217 }
218
219 public void openConversation(Conversation conversation) {
220 ActionBar ab = getActionBar();
221 if (ab != null) {
222 ab.setDisplayHomeAsUpEnabled(true);
223 ab.setHomeButtonEnabled(true);
224 if (getSelectedConversation().getMode() == Conversation.MODE_SINGLE
225 || ConversationActivity.this.useSubjectToIdentifyConference()) {
226 ab.setTitle(getSelectedConversation().getName());
227 } else {
228 ab.setTitle(getSelectedConversation().getContactJid()
229 .split("/")[0]);
230 }
231 }
232 invalidateOptionsMenu();
233 if (xmppConnectionServiceBound) {
234 xmppConnectionService.getNotificationService().setOpenConversation(
235 conversation);
236 }
237 }
238
239 @Override
240 public boolean onCreateOptionsMenu(Menu menu) {
241 getMenuInflater().inflate(R.menu.conversations, menu);
242 MenuItem menuSecure = (MenuItem) menu.findItem(R.id.action_security);
243 MenuItem menuArchive = (MenuItem) menu.findItem(R.id.action_archive);
244 MenuItem menuMucDetails = (MenuItem) menu
245 .findItem(R.id.action_muc_details);
246 MenuItem menuContactDetails = (MenuItem) menu
247 .findItem(R.id.action_contact_details);
248 MenuItem menuAttach = (MenuItem) menu.findItem(R.id.action_attach_file);
249 MenuItem menuClearHistory = (MenuItem) menu
250 .findItem(R.id.action_clear_history);
251 MenuItem menuAdd = (MenuItem) menu.findItem(R.id.action_add);
252 MenuItem menuInviteContact = (MenuItem) menu
253 .findItem(R.id.action_invite);
254 MenuItem menuMute = (MenuItem) menu.findItem(R.id.action_mute);
255
256 if (isConversationsOverviewVisable()
257 && isConversationsOverviewHideable()) {
258 menuArchive.setVisible(false);
259 menuMucDetails.setVisible(false);
260 menuContactDetails.setVisible(false);
261 menuSecure.setVisible(false);
262 menuInviteContact.setVisible(false);
263 menuAttach.setVisible(false);
264 menuClearHistory.setVisible(false);
265 menuMute.setVisible(false);
266 } else {
267 menuAdd.setVisible(!isConversationsOverviewHideable());
268 if (this.getSelectedConversation() != null) {
269 if (this.getSelectedConversation().getLatestMessage()
270 .getEncryption() != Message.ENCRYPTION_NONE) {
271 menuSecure.setIcon(R.drawable.ic_action_secure);
272 }
273 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
274 menuContactDetails.setVisible(false);
275 menuAttach.setVisible(false);
276 } else {
277 menuMucDetails.setVisible(false);
278 menuInviteContact.setVisible(false);
279 }
280 }
281 }
282 return true;
283 }
284
285 private void selectPresenceToAttachFile(final int attachmentChoice) {
286 selectPresence(getSelectedConversation(), new OnPresenceSelected() {
287
288 @Override
289 public void onPresenceSelected() {
290 if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_PHOTO) {
291 pendingImageUri = xmppConnectionService.getFileBackend()
292 .getTakePhotoUri();
293 Intent takePictureIntent = new Intent(
294 MediaStore.ACTION_IMAGE_CAPTURE);
295 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
296 pendingImageUri);
297 if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
298 startActivityForResult(takePictureIntent,
299 REQUEST_IMAGE_CAPTURE);
300 }
301 } else if (attachmentChoice == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
302 Intent attachFileIntent = new Intent();
303 attachFileIntent.setType("image/*");
304 attachFileIntent.setAction(Intent.ACTION_GET_CONTENT);
305 Intent chooser = Intent.createChooser(attachFileIntent,
306 getString(R.string.attach_file));
307 startActivityForResult(chooser, REQUEST_ATTACH_FILE_DIALOG);
308 } else if (attachmentChoice == ATTACHMENT_CHOICE_RECORD_VOICE) {
309 Intent intent = new Intent(
310 MediaStore.Audio.Media.RECORD_SOUND_ACTION);
311 startActivityForResult(intent, REQUEST_RECORD_AUDIO);
312 }
313 }
314 });
315 }
316
317 private void attachFile(final int attachmentChoice) {
318 final Conversation conversation = getSelectedConversation();
319 if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
320 if (hasPgp()) {
321 if (conversation.getContact().getPgpKeyId() != 0) {
322 xmppConnectionService.getPgpEngine().hasKey(
323 conversation.getContact(),
324 new UiCallback<Contact>() {
325
326 @Override
327 public void userInputRequried(PendingIntent pi,
328 Contact contact) {
329 ConversationActivity.this.runIntent(pi,
330 attachmentChoice);
331 }
332
333 @Override
334 public void success(Contact contact) {
335 selectPresenceToAttachFile(attachmentChoice);
336 }
337
338 @Override
339 public void error(int error, Contact contact) {
340 displayErrorDialog(error);
341 }
342 });
343 } else {
344 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
345 .findFragmentByTag("conversation");
346 if (fragment != null) {
347 fragment.showNoPGPKeyDialog(false,
348 new OnClickListener() {
349
350 @Override
351 public void onClick(DialogInterface dialog,
352 int which) {
353 conversation
354 .setNextEncryption(Message.ENCRYPTION_NONE);
355 xmppConnectionService.databaseBackend
356 .updateConversation(conversation);
357 selectPresenceToAttachFile(attachmentChoice);
358 }
359 });
360 }
361 }
362 } else {
363 showInstallPgpDialog();
364 }
365 } else if (getSelectedConversation().getNextEncryption(
366 forceEncryption()) == Message.ENCRYPTION_NONE) {
367 selectPresenceToAttachFile(attachmentChoice);
368 } else {
369 selectPresenceToAttachFile(attachmentChoice);
370 }
371 }
372
373 @Override
374 public boolean onOptionsItemSelected(MenuItem item) {
375 if (item.getItemId() == android.R.id.home) {
376 showConversationsOverview();
377 return true;
378 } else if (item.getItemId() == R.id.action_add) {
379 startActivity(new Intent(this, StartConversationActivity.class));
380 return true;
381 } else if (getSelectedConversation() != null) {
382 switch (item.getItemId()) {
383 case R.id.action_attach_file:
384 attachFileDialog();
385 break;
386 case R.id.action_archive:
387 this.endConversation(getSelectedConversation());
388 break;
389 case R.id.action_contact_details:
390 Contact contact = this.getSelectedConversation().getContact();
391 if (contact.showInRoster()) {
392 switchToContactDetails(contact);
393 } else {
394 showAddToRosterDialog(getSelectedConversation());
395 }
396 break;
397 case R.id.action_muc_details:
398 Intent intent = new Intent(this,
399 ConferenceDetailsActivity.class);
400 intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
401 intent.putExtra("uuid", getSelectedConversation().getUuid());
402 startActivity(intent);
403 break;
404 case R.id.action_invite:
405 inviteToConversation(getSelectedConversation());
406 break;
407 case R.id.action_security:
408 selectEncryptionDialog(getSelectedConversation());
409 break;
410 case R.id.action_clear_history:
411 clearHistoryDialog(getSelectedConversation());
412 break;
413 case R.id.action_mute:
414 muteConversationDialog(getSelectedConversation());
415 break;
416 default:
417 break;
418 }
419 return super.onOptionsItemSelected(item);
420 } else {
421 return super.onOptionsItemSelected(item);
422 }
423 }
424
425 public void endConversation(Conversation conversation) {
426 conversation.setStatus(Conversation.STATUS_ARCHIVED);
427 paneShouldBeOpen = true;
428 showConversationsOverview();
429 xmppConnectionService.archiveConversation(conversation);
430 if (conversationList.size() > 0) {
431 setSelectedConversation(conversationList.get(0));
432 } else {
433 setSelectedConversation(null);
434 }
435 }
436
437 @SuppressLint("InflateParams")
438 protected void clearHistoryDialog(final Conversation conversation) {
439 AlertDialog.Builder builder = new AlertDialog.Builder(this);
440 builder.setTitle(getString(R.string.clear_conversation_history));
441 View dialogView = getLayoutInflater().inflate(
442 R.layout.dialog_clear_history, null);
443 final CheckBox endConversationCheckBox = (CheckBox) dialogView
444 .findViewById(R.id.end_conversation_checkbox);
445 builder.setView(dialogView);
446 builder.setNegativeButton(getString(R.string.cancel), null);
447 builder.setPositiveButton(getString(R.string.delete_messages),
448 new OnClickListener() {
449
450 @Override
451 public void onClick(DialogInterface dialog, int which) {
452 ConversationActivity.this.xmppConnectionService
453 .clearConversationHistory(conversation);
454 if (endConversationCheckBox.isChecked()) {
455 endConversation(conversation);
456 }
457 }
458 });
459 builder.create().show();
460 }
461
462 protected void attachFileDialog() {
463 View menuAttachFile = findViewById(R.id.action_attach_file);
464 if (menuAttachFile == null) {
465 return;
466 }
467 PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
468 attachFilePopup.inflate(R.menu.attachment_choices);
469 attachFilePopup
470 .setOnMenuItemClickListener(new OnMenuItemClickListener() {
471
472 @Override
473 public boolean onMenuItemClick(MenuItem item) {
474 switch (item.getItemId()) {
475 case R.id.attach_choose_picture:
476 attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
477 break;
478 case R.id.attach_take_picture:
479 attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
480 break;
481 case R.id.attach_record_voice:
482 attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
483 break;
484 }
485 return false;
486 }
487 });
488 attachFilePopup.show();
489 }
490
491 protected void selectEncryptionDialog(final Conversation conversation) {
492 View menuItemView = findViewById(R.id.action_security);
493 if (menuItemView == null) {
494 return;
495 }
496 PopupMenu popup = new PopupMenu(this, menuItemView);
497 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
498 .findFragmentByTag("conversation");
499 if (fragment != null) {
500 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
501
502 @Override
503 public boolean onMenuItemClick(MenuItem item) {
504 switch (item.getItemId()) {
505 case R.id.encryption_choice_none:
506 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
507 item.setChecked(true);
508 break;
509 case R.id.encryption_choice_otr:
510 conversation.setNextEncryption(Message.ENCRYPTION_OTR);
511 item.setChecked(true);
512 break;
513 case R.id.encryption_choice_pgp:
514 if (hasPgp()) {
515 if (conversation.getAccount().getKeys()
516 .has("pgp_signature")) {
517 conversation
518 .setNextEncryption(Message.ENCRYPTION_PGP);
519 item.setChecked(true);
520 } else {
521 announcePgp(conversation.getAccount(),
522 conversation);
523 }
524 } else {
525 showInstallPgpDialog();
526 }
527 break;
528 default:
529 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
530 break;
531 }
532 xmppConnectionService.databaseBackend
533 .updateConversation(conversation);
534 fragment.updateChatMsgHint();
535 return true;
536 }
537 });
538 popup.inflate(R.menu.encryption_choices);
539 MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
540 MenuItem none = popup.getMenu().findItem(
541 R.id.encryption_choice_none);
542 if (conversation.getMode() == Conversation.MODE_MULTI) {
543 otr.setEnabled(false);
544 } else {
545 if (forceEncryption()) {
546 none.setVisible(false);
547 }
548 }
549 switch (conversation.getNextEncryption(forceEncryption())) {
550 case Message.ENCRYPTION_NONE:
551 none.setChecked(true);
552 break;
553 case Message.ENCRYPTION_OTR:
554 otr.setChecked(true);
555 break;
556 case Message.ENCRYPTION_PGP:
557 popup.getMenu().findItem(R.id.encryption_choice_pgp)
558 .setChecked(true);
559 break;
560 default:
561 popup.getMenu().findItem(R.id.encryption_choice_none)
562 .setChecked(true);
563 break;
564 }
565 popup.show();
566 }
567 }
568
569 protected void muteConversationDialog(final Conversation conversation) {
570 AlertDialog.Builder builder = new AlertDialog.Builder(this);
571 builder.setTitle(R.string.disable_notifications_for_this_conversation);
572 final int[] durations = getResources().getIntArray(
573 R.array.mute_options_durations);
574 builder.setItems(R.array.mute_options_descriptions,
575 new OnClickListener() {
576
577 @Override
578 public void onClick(DialogInterface dialog, int which) {
579 long till;
580 if (durations[which] == -1) {
581 till = Long.MAX_VALUE;
582 } else {
583 till = SystemClock.elapsedRealtime()
584 + (durations[which] * 1000);
585 }
586 conversation.setMutedTill(till);
587 ConversationActivity.this.xmppConnectionService.databaseBackend
588 .updateConversation(conversation);
589 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
590 .findFragmentByTag("conversation");
591 if (selectedFragment != null) {
592 selectedFragment.updateMessages();
593 }
594 }
595 });
596 builder.create().show();
597 }
598
599 protected ConversationFragment swapConversationFragment() {
600 ConversationFragment selectedFragment = new ConversationFragment();
601 if (!isFinishing()) {
602
603 FragmentTransaction transaction = getFragmentManager()
604 .beginTransaction();
605 transaction.replace(R.id.selected_conversation, selectedFragment,
606 "conversation");
607
608 transaction.commitAllowingStateLoss();
609 }
610 return selectedFragment;
611 }
612
613 @Override
614 public boolean onKeyDown(int keyCode, KeyEvent event) {
615 if (keyCode == KeyEvent.KEYCODE_BACK) {
616 if (!isConversationsOverviewVisable()) {
617 showConversationsOverview();
618 return false;
619 }
620 }
621 return super.onKeyDown(keyCode, event);
622 }
623
624 @Override
625 protected void onNewIntent(Intent intent) {
626 if (xmppConnectionServiceBound) {
627 if ((Intent.ACTION_VIEW.equals(intent.getAction()) && (VIEW_CONVERSATION
628 .equals(intent.getType())))) {
629 String convToView = (String) intent.getExtras().get(
630 CONVERSATION);
631 updateConversationList();
632 for (int i = 0; i < conversationList.size(); ++i) {
633 if (conversationList.get(i).getUuid().equals(convToView)) {
634 setSelectedConversation(conversationList.get(i));
635 break;
636 }
637 }
638 paneShouldBeOpen = false;
639 String text = intent.getExtras().getString(TEXT, null);
640 swapConversationFragment().setText(text);
641 }
642 } else {
643 handledViewIntent = false;
644 setIntent(intent);
645 }
646 }
647
648 @Override
649 public void onStart() {
650 super.onStart();
651 if (this.xmppConnectionServiceBound) {
652 this.onBackendConnected();
653 }
654 if (conversationList.size() >= 1) {
655 this.onConversationUpdate();
656 }
657 }
658
659 @Override
660 protected void onStop() {
661 if (xmppConnectionServiceBound) {
662 xmppConnectionService.removeOnConversationListChangedListener();
663 xmppConnectionService.removeOnAccountListChangedListener();
664 xmppConnectionService.removeOnRosterUpdateListener();
665 xmppConnectionService.getNotificationService().setOpenConversation(
666 null);
667 }
668 super.onStop();
669 }
670
671 @Override
672 public void onSaveInstanceState(Bundle savedInstanceState) {
673 Conversation conversation = getSelectedConversation();
674 if (conversation != null) {
675 savedInstanceState.putString(STATE_OPEN_CONVERSATION,
676 conversation.getUuid());
677 }
678 savedInstanceState.putBoolean(STATE_PANEL_OPEN,
679 isConversationsOverviewVisable());
680 super.onSaveInstanceState(savedInstanceState);
681 }
682
683 @Override
684 void onBackendConnected() {
685 this.registerListener();
686 updateConversationList();
687
688 if (xmppConnectionService.getAccounts().size() == 0) {
689 startActivity(new Intent(this, EditAccountActivity.class));
690 } else if (conversationList.size() <= 0) {
691 startActivity(new Intent(this, StartConversationActivity.class));
692 finish();
693 } else if (mOpenConverstaion != null) {
694 selectConversationByUuid(mOpenConverstaion);
695 paneShouldBeOpen = mPanelOpen;
696 if (paneShouldBeOpen) {
697 showConversationsOverview();
698 }
699 swapConversationFragment();
700 mOpenConverstaion = null;
701 } else if (getIntent() != null
702 && VIEW_CONVERSATION.equals(getIntent().getType())) {
703 String uuid = (String) getIntent().getExtras().get(CONVERSATION);
704 String text = getIntent().getExtras().getString(TEXT, null);
705 selectConversationByUuid(uuid);
706 paneShouldBeOpen = false;
707 swapConversationFragment().setText(text);
708 setIntent(null);
709 } else {
710 showConversationsOverview();
711 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
712 .findFragmentByTag("conversation");
713 if (selectedFragment != null) {
714 selectedFragment.onBackendConnected();
715 } else {
716 pendingImageUri = null;
717 setSelectedConversation(conversationList.get(0));
718 swapConversationFragment();
719 }
720 }
721
722 if (pendingImageUri != null) {
723 attachImageToConversation(getSelectedConversation(),
724 pendingImageUri);
725 pendingImageUri = null;
726 }
727 ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
728 }
729
730 private void selectConversationByUuid(String uuid) {
731 for (int i = 0; i < conversationList.size(); ++i) {
732 if (conversationList.get(i).getUuid().equals(uuid)) {
733 setSelectedConversation(conversationList.get(i));
734 }
735 }
736 }
737
738 public void registerListener() {
739 xmppConnectionService.setOnConversationListChangedListener(this);
740 xmppConnectionService.setOnAccountListChangedListener(this);
741 xmppConnectionService.setOnRosterUpdateListener(this);
742 }
743
744 @Override
745 protected void onActivityResult(int requestCode, int resultCode,
746 final Intent data) {
747 super.onActivityResult(requestCode, resultCode, data);
748 if (resultCode == RESULT_OK) {
749 if (requestCode == REQUEST_DECRYPT_PGP) {
750 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
751 .findFragmentByTag("conversation");
752 if (selectedFragment != null) {
753 selectedFragment.hideSnackbar();
754 }
755 } else if (requestCode == REQUEST_ATTACH_FILE_DIALOG) {
756 pendingImageUri = data.getData();
757 if (xmppConnectionServiceBound) {
758 attachImageToConversation(getSelectedConversation(),
759 pendingImageUri);
760 pendingImageUri = null;
761 }
762 } else if (requestCode == REQUEST_SEND_PGP_IMAGE) {
763
764 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
765 attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
766 } else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
767 attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
768 } else if (requestCode == REQUEST_ANNOUNCE_PGP) {
769 announcePgp(getSelectedConversation().getAccount(),
770 getSelectedConversation());
771 } else if (requestCode == REQUEST_ENCRYPT_MESSAGE) {
772 // encryptTextMessage();
773 } else if (requestCode == REQUEST_IMAGE_CAPTURE) {
774 if (xmppConnectionServiceBound) {
775 attachImageToConversation(getSelectedConversation(),
776 pendingImageUri);
777 pendingImageUri = null;
778 }
779 Intent intent = new Intent(
780 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
781 intent.setData(pendingImageUri);
782 sendBroadcast(intent);
783 } else if (requestCode == REQUEST_RECORD_AUDIO) {
784 attachAudioToConversation(getSelectedConversation(),
785 data.getData());
786 }
787 }
788 }
789
790 private void attachAudioToConversation(Conversation conversation, Uri uri) {
791
792 }
793
794 private void attachImageToConversation(Conversation conversation, Uri uri) {
795 prepareImageToast = Toast.makeText(getApplicationContext(),
796 getText(R.string.preparing_image), Toast.LENGTH_LONG);
797 prepareImageToast.show();
798 xmppConnectionService.attachImageToConversation(conversation, uri,
799 new UiCallback<Message>() {
800
801 @Override
802 public void userInputRequried(PendingIntent pi,
803 Message object) {
804 hidePrepareImageToast();
805 ConversationActivity.this.runIntent(pi,
806 ConversationActivity.REQUEST_SEND_PGP_IMAGE);
807 }
808
809 @Override
810 public void success(Message message) {
811 xmppConnectionService.sendMessage(message);
812 }
813
814 @Override
815 public void error(int error, Message message) {
816 hidePrepareImageToast();
817 displayErrorDialog(error);
818 }
819 });
820 }
821
822 private void hidePrepareImageToast() {
823 if (prepareImageToast != null) {
824 runOnUiThread(new Runnable() {
825
826 @Override
827 public void run() {
828 prepareImageToast.cancel();
829 }
830 });
831 }
832 }
833
834 public void updateConversationList() {
835 xmppConnectionService
836 .populateWithOrderedConversations(conversationList);
837 listAdapter.notifyDataSetChanged();
838 }
839
840 public void runIntent(PendingIntent pi, int requestCode) {
841 try {
842 this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
843 null, 0, 0, 0);
844 } catch (SendIntentException e1) {
845 }
846 }
847
848 public void encryptTextMessage(Message message) {
849 xmppConnectionService.getPgpEngine().encrypt(message,
850 new UiCallback<Message>() {
851
852 @Override
853 public void userInputRequried(PendingIntent pi,
854 Message message) {
855 ConversationActivity.this.runIntent(pi,
856 ConversationActivity.REQUEST_SEND_MESSAGE);
857 }
858
859 @Override
860 public void success(Message message) {
861 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
862 xmppConnectionService.sendMessage(message);
863 }
864
865 @Override
866 public void error(int error, Message message) {
867
868 }
869 });
870 }
871
872 public boolean forceEncryption() {
873 return getPreferences().getBoolean("force_encryption", false);
874 }
875
876 public boolean useSendButtonToIndicateStatus() {
877 return getPreferences().getBoolean("send_button_status", false);
878 }
879
880 public boolean indicateReceived() {
881 return getPreferences().getBoolean("indicate_received", false);
882 }
883
884 @Override
885 public void onAccountUpdate() {
886 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
887 .findFragmentByTag("conversation");
888 if (fragment != null) {
889 runOnUiThread(new Runnable() {
890
891 @Override
892 public void run() {
893 fragment.updateMessages();
894 }
895 });
896 }
897 }
898
899 @Override
900 public void onConversationUpdate() {
901 runOnUiThread(new Runnable() {
902
903 @Override
904 public void run() {
905 updateConversationList();
906 if (paneShouldBeOpen) {
907 if (conversationList.size() >= 1) {
908 swapConversationFragment();
909 } else {
910 startActivity(new Intent(getApplicationContext(),
911 StartConversationActivity.class));
912 finish();
913 }
914 }
915 ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager()
916 .findFragmentByTag("conversation");
917 if (selectedFragment != null) {
918 selectedFragment.updateMessages();
919 }
920 }
921 });
922 }
923
924 @Override
925 public void onRosterUpdate() {
926 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
927 .findFragmentByTag("conversation");
928 if (fragment != null) {
929 runOnUiThread(new Runnable() {
930
931 @Override
932 public void run() {
933 fragment.updateMessages();
934 }
935 });
936 }
937 }
938}