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