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