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), conversation.getLatestMessage().getBody().trim(), UIHelper.getContactPicture(conversation.getContact(),conversation.getName(useSubject), 90,this.getApplicationContext()));
85 view.setOnClickListener(new OnClickListener() {
86
87 @Override
88 public void onClick(View v) {
89 String sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
90 switchToConversation(conversation, sharedText);
91 finish();
92 }
93 });
94 conversations.addView(view);
95 if (conversation.getContact() != null) {
96 displayedContacts.add(conversation.getContact().getUuid());
97 }
98 }
99 contacts.removeAllViews();
100 final List<Contact> contactsList = new ArrayList<Contact>();
101 for(Account account : xmppConnectionService.getAccounts()) {
102 for(final Contact contact : xmppConnectionService.getRoster(account)) {
103 if (!displayedContacts.contains(contact.getUuid())) {
104 contactsList.add(contact);
105 }
106 }
107 }
108
109 Collections.sort(contactsList, new Comparator<Contact>() {
110 @Override
111 public int compare(Contact lhs, Contact rhs) {
112 return lhs.getDisplayName().compareToIgnoreCase(rhs.getDisplayName());
113 }
114 });
115
116 for(int i = 0; i < contactsList.size(); ++i) {
117 final Contact con = contactsList.get(i);
118 View view = createContactView(con.getDisplayName(), con.getJid(), UIHelper.getContactPicture(con,null, 90,this.getApplicationContext()));
119 view.setOnClickListener(new OnClickListener() {
120
121 @Override
122 public void onClick(View v) {
123 String sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
124 Conversation conversation = xmppConnectionService.findOrCreateConversation(con.getAccount(), con.getJid(), false);
125 switchToConversation(conversation, sharedText);
126 finish();
127 }
128 });
129 contacts.addView(view);
130 }
131 }
132
133}