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