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