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