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