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.entities.Message;
15import eu.siacs.conversations.utils.UIHelper;
16import android.app.PendingIntent;
17import android.content.Intent;
18import android.content.SharedPreferences;
19import android.graphics.Bitmap;
20import android.net.Uri;
21import android.os.Bundle;
22import android.preference.PreferenceManager;
23import android.view.View;
24import android.view.View.OnClickListener;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27import android.widget.TextView;
28import android.widget.Toast;
29
30public class ShareWithActivity extends XmppActivity {
31
32 private LinearLayout conversations;
33 private LinearLayout contacts;
34 private boolean isImage = false;
35
36 private UiCallback<Message> attachImageCallback = new UiCallback<Message>() {
37
38 @Override
39 public void userInputRequried(PendingIntent pi, Message object) {
40 // TODO Auto-generated method stub
41
42 }
43
44 @Override
45 public void success(Message message) {
46 xmppConnectionService.sendMessage(message);
47 }
48
49 @Override
50 public void error(int errorCode, Message object) {
51 // TODO Auto-generated method stub
52
53 }
54 };
55
56 @Override
57 protected void onCreate(Bundle savedInstanceState) {
58
59 super.onCreate(savedInstanceState);
60
61 setContentView(R.layout.share_with);
62 setTitle(getString(R.string.title_activity_sharewith));
63
64 contacts = (LinearLayout) findViewById(R.id.contacts);
65 conversations = (LinearLayout) findViewById(R.id.conversations);
66
67 }
68
69 public View createContactView(String name, String msgTxt, Bitmap bm) {
70 View view = (View) getLayoutInflater().inflate(R.layout.contact, null);
71 view.setBackgroundResource(R.drawable.greybackground);
72 TextView contactName = (TextView) view
73 .findViewById(R.id.contact_display_name);
74 contactName.setText(name);
75 TextView msg = (TextView) view.findViewById(R.id.contact_jid);
76 msg.setText(msgTxt);
77 ImageView imageView = (ImageView) view.findViewById(R.id.contact_photo);
78 imageView.setImageBitmap(bm);
79 return view;
80 }
81
82 @Override
83 void onBackendConnected() {
84 this.isImage = (getIntent().getType() != null && getIntent().getType()
85 .startsWith("image/"));
86 SharedPreferences preferences = PreferenceManager
87 .getDefaultSharedPreferences(this);
88 boolean useSubject = preferences.getBoolean("use_subject_in_muc", true);
89
90 Set<Contact> displayedContacts = new HashSet<Contact>();
91 conversations.removeAllViews();
92 List<Conversation> convList = xmppConnectionService.getConversations();
93 Collections.sort(convList, new Comparator<Conversation>() {
94 @Override
95 public int compare(Conversation lhs, Conversation rhs) {
96 return (int) (rhs.getLatestMessage().getTimeSent() - lhs
97 .getLatestMessage().getTimeSent());
98 }
99 });
100 for (final Conversation conversation : convList) {
101 if (!isImage || conversation.getMode() == Conversation.MODE_SINGLE) {
102 View view = createContactView(
103 conversation.getName(useSubject),
104 conversation.getLatestMessage().getBody().trim(),
105 UIHelper.getContactPicture(conversation, 48,
106 this.getApplicationContext(), false));
107 view.setOnClickListener(new OnClickListener() {
108
109 @Override
110 public void onClick(View v) {
111 share(conversation);
112 }
113 });
114 conversations.addView(view);
115 displayedContacts.add(conversation.getContact());
116 }
117 }
118 contacts.removeAllViews();
119 List<Contact> contactsList = new ArrayList<Contact>();
120 for (Account account : xmppConnectionService.getAccounts()) {
121 for (Contact contact : account.getRoster().getContacts()) {
122 if (!displayedContacts.contains(contact)
123 && (contact.showInRoster())) {
124 contactsList.add(contact);
125 }
126 }
127 }
128
129 Collections.sort(contactsList, new Comparator<Contact>() {
130 @Override
131 public int compare(Contact lhs, Contact rhs) {
132 return lhs.getDisplayName().compareToIgnoreCase(
133 rhs.getDisplayName());
134 }
135 });
136
137 for (int i = 0; i < contactsList.size(); ++i) {
138 final Contact con = contactsList.get(i);
139 View view = createContactView(
140 con.getDisplayName(),
141 con.getJid(),
142 UIHelper.getContactPicture(con, 48,
143 this.getApplicationContext(), false));
144 view.setOnClickListener(new OnClickListener() {
145
146 @Override
147 public void onClick(View v) {
148 Conversation conversation = xmppConnectionService
149 .findOrCreateConversation(con.getAccount(),
150 con.getJid(), false);
151 share(conversation);
152 }
153 });
154 contacts.addView(view);
155 }
156 }
157
158 private void share(final Conversation conversation) {
159 String sharedText = null;
160 if (isImage) {
161 final Uri uri = (Uri) getIntent().getParcelableExtra(
162 Intent.EXTRA_STREAM);
163 selectPresence(conversation, new OnPresenceSelected() {
164 @Override
165 public void onPresenceSelected() {
166 Toast.makeText(getApplicationContext(),
167 getText(R.string.preparing_image),
168 Toast.LENGTH_LONG).show();
169 ShareWithActivity.this.xmppConnectionService
170 .attachImageToConversation(conversation, uri,
171 attachImageCallback);
172 switchToConversation(conversation, null, true);
173 finish();
174 }
175 });
176
177 } else {
178 sharedText = getIntent().getStringExtra(Intent.EXTRA_TEXT);
179 switchToConversation(conversation, sharedText, true);
180 finish();
181 }
182
183 }
184
185}