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