1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.Collections;
5import java.util.Comparator;
6import java.util.HashSet;
7import java.util.List;
8import java.util.Set;
9
10import eu.siacs.conversations.R;
11import eu.siacs.conversations.entities.Account;
12import eu.siacs.conversations.entities.Contact;
13import eu.siacs.conversations.entities.Conversation;
14import eu.siacs.conversations.utils.UIHelper;
15import android.content.Intent;
16import android.content.SharedPreferences;
17import android.graphics.Bitmap;
18import android.os.Bundle;
19import android.preference.PreferenceManager;
20import android.util.Log;
21import android.view.View;
22import android.view.View.OnClickListener;
23import android.widget.ImageView;
24import android.widget.LinearLayout;
25import android.widget.TextView;
26
27public class ShareWithActivity extends XmppActivity {
28
29 private LinearLayout conversations;
30 private LinearLayout contacts;
31
32 private OnClickListener click = new OnClickListener() {
33
34 @Override
35 public void onClick(View v) {
36 // TODO Auto-generated method stub
37
38 }
39 };
40
41 @Override
42 protected void onCreate(Bundle savedInstanceState) {
43
44 super.onCreate(savedInstanceState);
45
46 setContentView(R.layout.share_with);
47 setTitle("Share with Conversation");
48
49 contacts = (LinearLayout) findViewById(R.id.contacts);
50 conversations = (LinearLayout) findViewById(R.id.conversations);
51
52 }
53
54
55 public View createContactView(String name, String msgTxt, Bitmap bm) {
56 View view = (View) getLayoutInflater().inflate(R.layout.contact, null);
57 view.setBackgroundResource(R.drawable.greybackground);
58 TextView contactName =(TextView) view.findViewById(R.id.contact_display_name);
59 contactName.setText(name);
60 TextView msg = (TextView) view.findViewById(R.id.contact_jid);
61 msg.setText(msgTxt);
62 ImageView imageView = (ImageView) view.findViewById(R.id.contact_photo);
63 imageView.setImageBitmap(bm);
64 return view;
65 }
66
67
68
69 @Override
70 void onBackendConnected() {
71 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
72 boolean useSubject = preferences.getBoolean("use_subject_in_muc", true);
73
74 Set<String> displayedContacts = new HashSet<String>();
75 conversations.removeAllViews();
76 List<Conversation> convList = xmppConnectionService.getConversations();
77 Collections.sort(convList, new Comparator<Conversation>() {
78 @Override
79 public int compare(Conversation lhs, Conversation rhs) {
80 return (int) (rhs.getLatestMessage().getTimeSent() - lhs.getLatestMessage().getTimeSent());
81 }
82 });
83 for(final Conversation conversation : convList) {
84 View view = createContactView(conversation.getName(useSubject),
85 conversation.getLatestMessage().getBody().trim(),
86 UIHelper.getContactPicture(conversation, 48,
87 this.getApplicationContext(), false));
88 view.setOnClickListener(new OnClickListener() {
89
90 @Override
91 public void onClick(View v) {
92 String sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
93 switchToConversation(conversation, sharedText);
94 finish();
95 }
96 });
97 conversations.addView(view);
98 if (conversation.getContact() != null) {
99 displayedContacts.add(conversation.getContact().getUuid());
100 }
101 }
102 contacts.removeAllViews();
103 final List<Contact> contactsList = new ArrayList<Contact>();
104 for(Account account : xmppConnectionService.getAccounts()) {
105 for(final Contact contact : account.getRoster().getContacts()) {
106 if (!displayedContacts.contains(contact.getUuid())) {
107 contactsList.add(contact);
108 }
109 }
110 }
111
112 Collections.sort(contactsList, new Comparator<Contact>() {
113 @Override
114 public int compare(Contact lhs, Contact rhs) {
115 return lhs.getDisplayName().compareToIgnoreCase(rhs.getDisplayName());
116 }
117 });
118
119 for(int i = 0; i < contactsList.size(); ++i) {
120 final Contact con = contactsList.get(i);
121 View view = createContactView(con.getDisplayName(), con.getJid(),
122 UIHelper.getContactPicture(con, 48, this.getApplicationContext(), false));
123 view.setOnClickListener(new OnClickListener() {
124
125 @Override
126 public void onClick(View v) {
127 String sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
128 Conversation conversation = xmppConnectionService.findOrCreateConversation(con.getAccount(), con.getJid(), false);
129 switchToConversation(conversation, sharedText);
130 finish();
131 }
132 });
133 contacts.addView(view);
134 }
135 }
136
137}