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